Exemple #1
0
        public IStatusGeneric DeleteRole(string roleName, bool removeFromUsers,
                                         ExtraAuthorizeDbContext context)
        {
            var status = new StatusGenericHandler {
                Message = "Deleted role successfully."
            };
            var roleToUpdate = context.Find <RoleToPermissions>(roleName);

            if (roleToUpdate == null)
            {
                return(status.AddError("That role doesn't exists"));
            }

            var usersWithRoles = context.UserToRoles.Where(x => x.RoleName == roleName).ToList();

            if (usersWithRoles.Any())
            {
                if (!removeFromUsers)
                {
                    return(status.AddError($"That role is used by {usersWithRoles.Count} and you didn't ask for them to be updated."));
                }

                context.RemoveRange(usersWithRoles);
                status.Message = $"Removed role from {usersWithRoles.Count} user and then deleted role successfully.";
            }

            context.Remove(roleToUpdate);
            return(status);
        }