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);
        }
Example #2
0
        void AssertCurrentUserHasCapability <TEntity, TCapability>(object entityProviderValue, TCapability requiredCapability, string actionName)
            where TEntity : IEntity
            where TCapability : struct, Enum
        {
            var targetEntity = targetEntityProvider.GetTargetEntityIdentity <TEntity>(entityProviderValue);

            if (targetEntity == null)
            {
                return;
            }

            var tester = testerFactory.GetCapabilityTester <TEntity, TCapability>();

            try
            {
                tester.AssertUserHasCapability(userProvider.GetCurrentCapabilityUser(),
                                               targetEntity,
                                               requiredCapability);
            }
            catch (UserMustHaveCapabilityException e)
            {
                // The reason for the catch-and-rethrow is to add the 'action name' information to the exception.
                var exception = new UserMustHaveCapabilityException($"{e.Message}\nAction: {actionName}",
                                                                    e,
                                                                    e.UserIdentity,
                                                                    e.EntityIdentity,
                                                                    e.RequiredCapabilities,
                                                                    e.ActualCapabilities,
                                                                    actionName);
                if (logger.IsInfoEnabled)
                {
                    logger.Info($@"{e.UserIdentity} does not have capability for action {actionName}
       Entity identity: {e.EntityIdentity}
 Required capabilities: {e.RequiredCapabilities}
   Actual capabilities: {e.ActualCapabilities}");
                }

                throw exception;
            }
        }