using System; using System.Collections; using System.Collections.Generic; namespace ln.identities { public class AssignedRoles : IEnumerable { IIdentityProvider identityProvider; public IIdentityProvider IdentityProvider => identityProvider; Dictionary assignedRoles = new Dictionary(); 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 GetEnumerator() { return assignedRoles.Values.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } }