56 lines
1.9 KiB
C#
56 lines
1.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
namespace ln.identities
|
|
{
|
|
public class AssignedRoles : IEnumerable<AssignedRole>
|
|
{
|
|
IIdentityProvider identityProvider;
|
|
public IIdentityProvider IdentityProvider => identityProvider;
|
|
|
|
Dictionary<Guid, AssignedRole> assignedRoles = new Dictionary<Guid, AssignedRole>();
|
|
|
|
public AssignedRoles(IIdentityProvider identityProvider)
|
|
{
|
|
this.identityProvider = identityProvider;
|
|
}
|
|
|
|
public AssignedRole this[Identity identity]
|
|
{
|
|
get
|
|
{
|
|
if (!assignedRoles.ContainsKey(identity.UniqueID))
|
|
{
|
|
assignedRoles.Add(identity.UniqueID, new AssignedRole(identityProvider, identity));
|
|
}
|
|
return assignedRoles[identity.UniqueID];
|
|
}
|
|
}
|
|
public AssignedRole this[Guid identityUniqueID]
|
|
{
|
|
get
|
|
{
|
|
if (!assignedRoles.ContainsKey(identityUniqueID))
|
|
{
|
|
if (identityUniqueID.Equals(Guid.Empty))
|
|
assignedRoles.Add(identityUniqueID, new AssignedRole(identityProvider, Identity.Anonymous(identityProvider)));
|
|
else
|
|
throw new KeyNotFoundException();
|
|
}
|
|
return assignedRoles[identityUniqueID];
|
|
}
|
|
}
|
|
|
|
public bool HasRole(Role role) => HasRole(null, role);
|
|
public bool HasRole(Identity identity, Role role) => HasRole(identity?.UniqueID ?? Guid.Empty, role);
|
|
public bool HasRole(Guid identityUniqueID, Role role)=> (this[identityUniqueID].Role & role) == role;
|
|
|
|
public IEnumerator<AssignedRole> GetEnumerator()
|
|
{
|
|
return assignedRoles.Values.GetEnumerator();
|
|
}
|
|
|
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
|
}
|
|
}
|