예제 #1
0
        public async Task <IStatusGeneric> DeleteRoleAsync(
            string roleName,
            bool isRemoveFromUsers,
            IAuthorizationRepository repository)
        {
            if (repository == null)
            {
                throw new ArgumentNullException(nameof(repository));
            }

            var status = new StatusGenericHandler {
                Message = "Deleted role successfully."
            };
            RoleToPermissions roleToUpdate = await repository.GetRoleToPermissionAsync(roleName);

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

            ICollection <UserToRole> usersWithRoles = await repository.GetUsersToRoleByNameAsync(roleName);

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

                await repository.DeleteAsync(usersWithRoles);

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

            await repository.DeleteAsync(roleToUpdate);

            return(status);
        }