Repository navigation
Expand file tree
/
Copy pathUser.cs
More file actions
78 lines (68 loc) · 2.49 KB
/
Copy pathUser.cs
File metadata and controls
78 lines (68 loc) · 2.49 KB
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
74
75
76
77
78
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Security.Claims;
using Opserver.Security;
namespace Opserver.Models
{
public class User
{
public string AccountName { get; }
public bool IsAnonymous { get; }
public bool IsGlobalAdmin { get; }
private SecurityProvider Provider { get; }
/// <summary>
/// Returns this user's role on the current site.
/// </summary>
public ImmutableHashSet<string> Roles { get; }
/// <summary>
/// Gets the <see cref="ClaimsPrincipal"/> representing the logged-in user.
/// </summary>
public ClaimsPrincipal Principal { get; }
public User(SecurityProvider provider, ClaimsPrincipal principal, List<string> baseRoles, IEnumerable<StatusModule> modules)
{
Provider = provider;
Principal = principal;
var identity = principal?.Identity;
if (identity == null)
{
IsAnonymous = true;
return;
}
IsAnonymous = !identity.IsAuthenticated;
if (identity.IsAuthenticated)
AccountName = identity.Name;
var roles = baseRoles;
if (IsAnonymous)
{
roles.Add(Models.Roles.Anonymous);
}
else
{
roles.Add(Models.Roles.Authenticated);
// Global admins are unique and can see a few more things about Opserver itself (not per-module)
if (provider.IsGlobalAdmin(this))
{
roles.Add(Models.Roles.GlobalAdmin);
IsGlobalAdmin = true;
}
}
// Add per-module roles
foreach (var module in modules)
{
if (IsAdmin(module))
{
roles.Add(module.SecuritySettings.AdminRole);
roles.Add(module.SecuritySettings.ViewRole);
}
else if (HasAccess(module))
{
roles.Add(module.SecuritySettings.ViewRole);
}
}
Roles = roles.ToImmutableHashSet();
}
public bool Is(string role) => Roles.Contains(role) || IsGlobalAdmin;
public bool HasAccess(StatusModule module) => Provider.InReadGroups(this, module);
public bool IsAdmin(StatusModule module) => Provider.InAdminGroups(this, module);
}
}