Пример #1
0
        public async Task <IdentityResult> EditRoleAsync(CreateOrUpdateRoleCommand command)
        {
            var role = await _roleManager.FindByIdAsync(command.Role.Id.ToString()).ConfigureAwait(false);

            if (role.Name == command.Role.Name && role.Id != command.Role.Id)
            {
                return(IdentityResult.Failed(new IdentityError
                {
                    Code = "RoleNameAlreadyExist",
                    Description = "This role name is already exists!"
                }));
            }

            role.Name = command.Role.Name;
            role.RolePermissions.Clear();

            var updateRoleResult = await _roleManager.UpdateAsync(role).ConfigureAwait(false);

            if (updateRoleResult.Succeeded)
            {
                GrantPermissionsToRole(command.GrantedPermissionIds, role);
            }

            return(updateRoleResult);
        }
Пример #2
0
        public async Task <ActionResult> PostRoles([FromBody] CreateOrUpdateRoleCommand command)
        {
            var identityResult = await _mediator.Send(command)
                                 .ConfigureAwait(false);

            if (identityResult.Succeeded)
            {
                return(Created(Url.Action("PostRoles") ?? string.Empty, identityResult));
            }

            return(BadRequest(identityResult.Errors.Select(e => new NameValueDto(e.Code, e.Description))));
        }
Пример #3
0
        public async Task <IdentityResult> AddRoleAsync(CreateOrUpdateRoleCommand command)
        {
            var role = new Role
            {
                Id   = command.Role.Id,
                Name = command.Role.Name
            };

            var createRoleResult = await _roleManager.CreateAsync(role).ConfigureAwait(false);

            if (createRoleResult.Succeeded)
            {
                GrantPermissionsToRole(command.GrantedPermissionIds, role);
            }

            return(createRoleResult);
        }