コード例 #1
0
        public async Task <IActionResult> DeletePermissionRole([FromRoute] Guid id, [FromRoute] Guid permissionId)
        {
            var command = new RemovePermissionToRoleCommand {
                RoleId = id, PermissionId = permissionId
            };
            await _mediator.Send(command);

            return(Ok());
        }
コード例 #2
0
        public async Task <IActionResult> DeletePermissionRole(RemovePermissionToRoleCommand command)
        {
            try
            {
                await _mediator.Send(command);

                return(Ok());
            }
            catch (KeyNotFoundException ex)
            {
                return(NotFound());
            }
            catch (ArgumentException argumentException)
            {
                return(BadRequest(argumentException.Message));
            }
        }
コード例 #3
0
        public async Task <bool> Handle(RemovePermissionToRoleCommand message, CancellationToken cancellationToken)
        {
            var role = await _roleManager.FindByIdAsync(message.RoleId.ToString()) ?? throw new KeyNotFoundException();

            var permission = await _permissionRepository.GetById(message.PermissionId) ?? throw new KeyNotFoundException();

            if (role.Permissions.Any(pname => pname == permission.Name))
            {
                role.Permissions.Remove(permission.Name);
            }
            else
            {
                throw new KeyNotFoundException("Permission not found in this role");
            }


            await _roleManager.UpdateAsync(role);

            return(true);
        }