Ejemplo n.º 1
0
        public bool TryRemoveAbility(CLToolContext context, string abilityName)
        {
            //Find
            //Incase include includes all UserAbility so we check Username in UserAbility again
            var userAbility = UserAbility.FirstOrDefault(a => a.Ability == abilityName);

            //Remove if having
            if (userAbility == null)
            {
                //User doesnt have ability
                logger.Info($"Username: {Username} doesnt have {abilityName} => skip");
                return(false);
            }
            //EF only allow remove on root DbSet
            //UserAbility.Remove(userAbility);
            context.UserAbility.Remove(userAbility);
            return(true);
        }
Ejemplo n.º 2
0
        public bool TryAddAbility(CLToolContext context, string abilityName)
        {
            var ability = context.Ability.FirstOrDefault(a => a.Name == abilityName);

            if (ability == null)
            {
                throw new InvalidOperationException($"Cant find Ablity: {abilityName}");
            }

            //Find if user currently have ability
            //Incase include includes all UserAbility so we check Username in UserAbility again
            if (UserAbility.Any(a => a.Ability == abilityName))
            {
                //Already have -> ignore
                logger.Info($"Username: {Username} already have {abilityName} => skip");
                return(false);
            }
            UserAbility.Add(new UserAbility()
            {
                AbilityNavigation = ability
            });
            return(true);
        }