ln.http/ln.http/HttpPrincipal.cs

42 lines
1.3 KiB
C#
Raw Permalink Normal View History

2022-02-07 09:29:30 +01:00
using System.Collections.Generic;
namespace ln.http
{
public class HttpPrincipal
{
public string UniqueId { get; set; }
public string Username { get; set; }
2022-05-28 19:25:25 +02:00
/**
* If this principal is a delegated one, authenticated by another principal
*/
public HttpPrincipal AuthenticatedPrincipal { get; }
2022-02-07 09:29:30 +01:00
2022-05-28 19:25:25 +02:00
private HashSet<string> _roles = new HashSet<string>();
public IReadOnlySet<string> Roles => _roles;
2022-02-07 09:29:30 +01:00
2022-05-28 19:25:25 +02:00
public bool HasRole(string role) => _roles.Contains(role);
public HttpPrincipal(string uniquedId, string username, string[] roles)
2022-02-07 09:29:30 +01:00
{
2022-05-28 19:25:25 +02:00
UniqueId = uniquedId;
Username = username;
foreach (var role in roles)
_roles.Add(role);
2022-02-07 09:29:30 +01:00
}
2022-05-28 19:25:25 +02:00
public HttpPrincipal(string uniquedId, string username, string[] roles, HttpPrincipal authenticatedPrincipal) :
this(uniquedId, username, roles)
2022-02-07 09:29:30 +01:00
{
2022-05-28 19:25:25 +02:00
AuthenticatedPrincipal = authenticatedPrincipal;
2022-02-07 09:29:30 +01:00
}
2022-05-28 19:25:25 +02:00
2022-02-07 09:29:30 +01:00
public override string ToString()
{
if (AuthenticatedPrincipal is null)
return string.Format("{0}[{1}]", Username, UniqueId);
else
return string.Format("{2}=>{0}[{1}]", Username, UniqueId, AuthenticatedPrincipal.ToString());
}
}
}