public bool DoesUserHaveCapability <TCapability, TEntity>(TCapability capability, IIdentity <TEntity> identity) where TEntity : IEntity where TCapability : struct, Enum { var user = userReader.GetCurrentCapabilityUser(); var actualCapabilities = capabilitiesProvider.GetCapabilities <TEntity, TCapability>(user, identity); var requiredList = capability.GetBase2FlagValues().ToList(); var actualList = actualCapabilities.GetBase2FlagValues().ToList(); var result = requiredList.Intersect(actualList).Count() == requiredList.Count(); return(result); }
public void AssertUserHasCapability(IIdentity <User> user, IIdentity <TEntity> targetEntity, TCapability requiredCapability) { // Once https://github.com/csf-dev/CSF.Utils/issues/127 is resolved, convert this to an extension method call. EnumFlagsExtensions.AssertIsFlagsEnum(typeof(TCapability)); var actualCapability = capabilitiesProvider.GetCapabilities(user, targetEntity); var requiredList = requiredCapability.GetBase2FlagValues().ToList(); var actualList = actualCapability.GetBase2FlagValues().ToList(); if (requiredList.Intersect(actualList).Count() == requiredList.Count()) { return; } var message = $@"The user {user} must have the capability {requiredCapability} for {targetEntity}, but their actual capabilities were {(Equals(actualCapability,default(TCapability))? "<none>" : actualCapability.ToString())}."; throw new UserMustHaveCapabilityException(message, user, targetEntity, requiredCapability.ToString(), actualCapability.ToString()); }