コード例 #1
0
        public static async Task <IStatusGeneric <RoleToPermissions> > CreateRoleWithPermissionsAsync(
            string roleName,
            string description,
            IEnumerable <Permission> permissionInRole,
            IAuthorizationRepository repository)
        {
            if (roleName == null)
            {
                throw new ArgumentNullException(nameof(roleName));
            }
            if (repository == null)
            {
                throw new ArgumentNullException(nameof(repository));
            }

            var status = new StatusGenericHandler <RoleToPermissions>();
            RoleToPermissions roleToPermissions = await repository.GetRoleToPermissionAsync(roleName);

            if (roleToPermissions != null)
            {
                status.AddError("That role already exists");
                return(status);
            }

            return(status.SetResult(new RoleToPermissions(roleName, description, permissionInRole)));
        }
コード例 #2
0
        /// <summary>
        /// This will update a role
        /// </summary>
        /// <param name="roleName"></param>
        /// <param name="description"></param>
        /// <param name="permissions"></param>
        public async Task UpdateRoleAsync(
            string roleName,
            string description,
            ICollection <Permission> permissions)
        {
            RoleToPermissions existingRole = await _repository.GetRoleToPermissionAsync(roleName);

            if (existingRole == null)
            {
                throw new KeyNotFoundException($"Could not find the role {roleName} to update.");
            }
            existingRole.Update(description, permissions);
            await _repository.UpdateAsync(existingRole);
        }
コード例 #3
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);
        }
コード例 #4
0
ファイル: UserToRole.cs プロジェクト: Bengbenz/Specta.Compta
        public static async Task <IStatusGeneric <UserToRole> > AddRoleToUserAsync(
            string userId,
            string roleName,
            IAuthorizationRepository repository)
        {
            if (userId == null)
            {
                throw new ArgumentNullException(nameof(userId));
            }
            if (roleName == null)
            {
                throw new ArgumentNullException(nameof(roleName));
            }
            if (repository == null)
            {
                throw new ArgumentNullException(nameof(repository));
            }

            var        status     = new StatusGenericHandler <UserToRole>();
            UserToRole userToRole = await repository.GetUserToRoleAsync(userId, roleName);

            if (userToRole != null)
            {
                status.AddError($"The user already has the Role '{roleName}'.");
                return(status);
            }

            RoleToPermissions roleToAdd = await repository.GetRoleToPermissionAsync(roleName);

            if (roleToAdd == null)
            {
                status.AddError($"Could not find the Role '{roleName}'.");
                return(status);
            }

            return(status.SetResult(new UserToRole(userId, roleToAdd)));
        }