-
Notifications
You must be signed in to change notification settings - Fork 1
/
UserPrincipal.cs
73 lines (64 loc) · 2.38 KB
/
UserPrincipal.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#region Related components
using System.Security.Principal;
using System.Security.Claims;
#endregion
namespace net.vieapps.Components.Security
{
/// <summary>
/// Presents a principal of an user
/// </summary>
public class UserPrincipal : ClaimsPrincipal
{
/// <summary>
/// Initializes the new instance of an user principal from the specified identity
/// </summary>
public UserPrincipal()
: this(new UserIdentity()) { }
/// <summary>
/// Initializes the new instance of an user principal from the specified identity
/// </summary>
/// <param name="user">The identity from which to initialize the new principal</param>
public UserPrincipal(IUser user)
: this(new UserIdentity(user)) { }
/// <summary>
/// Initializes the new instance of an user principal from the specified identity
/// </summary>
/// <param name="identity">The identity from which to initialize the new principal</param>
public UserPrincipal(UserIdentity identity)
: base(identity)
=> this.Identity = identity ?? new UserIdentity();
/// <summary>
/// Initializes the new instance of an user principal
/// </summary>
/// <param name="principal">The principal from which to initialize the new principal</param>
public UserPrincipal(ClaimsPrincipal principal)
: base(principal)
=> this.Identity = new UserIdentity(principal);
/// <summary>
/// Gets the current principal
/// </summary>
public static new UserPrincipal Current
=> new UserPrincipal(ClaimsPrincipal.Current);
/// <summary>
/// Gets the identity that associated with this principal
/// </summary>
public override IIdentity Identity { get; }
/// <summary>
/// Gets a value that indicates whether the user (of the current identity) has been authenticated
/// </summary>
public bool IsAuthenticated
=> this.Identity != null && (this.Identity as UserIdentity).IsAuthenticated;
/// <summary>
/// Determines whether the current principal is system administrator or not
/// </summary>
public bool IsSystemAdministrator
=> this.Identity != null && (this.Identity as UserIdentity).IsSystemAdministrator;
/// <summary>
/// Determines whether the current principal belongs to the specified role
/// </summary>
/// <param name="role"></param>
/// <returns></returns>
public override bool IsInRole(string role)
=> this.Identity != null && (this.Identity as UserIdentity).IsInRole(role);
}
}