public ActionResult Put(int id, [FromBody] CreateRoleDto dto)
        {
            dto.Id = id;
            try
            {
                _editRole.Execute(dto);
                return(StatusCode(204));
            }
            catch (EntityAlreadyExistsException e)
            {
                return(UnprocessableEntity(e.Message));
            }
            catch (EntityNotFoundException e) {
                return(NotFound(e.Message));
            }

            catch (Exception e)
            {
                return(StatusCode(500, e.Message));
            }
        }
예제 #2
0
        public async Task <IHttpActionResult> Create(CreateRoleDto createRoleDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var role = new IdentityRole {
                Name = createRoleDto.Name
            };
            var result = await NdRoleManager.CreateAsync(role);

            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }

            Uri locationHeader = new Uri(Url.Link("GetRoleById", new { id = role.Id }));

            return(Created(locationHeader, _factory.Create(role)));
        }
예제 #3
0
        public async Task <IActionResult> Create(CreateRoleDto dto)
        {
            try
            {
                var roleId = await _service.CreateAsync(dto);

                return(RedirectToAction(nameof(Get), new { id = roleId }));
            }
            catch (ValidationException e)
            {
                Logger.LogDebug("A validation error occured while creating a role: {0}", e.Message);

                return(BadRequest(e.Message));
            }
            catch (Exception e)
            {
                Logger.LogError(e, "An error occured while creating a role.");

                return(InternalServerError(e.Message));
            }
        }
        public async Task UpdateRoleInTenant_WrongTenantId_Test()
        {
            CreateTenantDto createTenantDto = new CreateTenantDto()
            {
                TenancyName      = "TestTenant",
                Name             = "TestTenant",
                AdminPhoneNumber = "13851400000",
                IsActive         = true
            };
            var tenantDto = await _tenantAppService.CreateAsync(createTenantDto);

            var createRoleDto = new CreateRoleDto()
            {
                Name               = "RoleTest",
                DisplayName        = "Test role",
                Description        = "Role for test",
                GrantedPermissions = new List <string>()
                {
                    PermissionNames.Pages_Roles
                }
            };

            var roleDto = await _omRoleAppService.CreateRoleInTenantAsync(tenantDto.Id, createRoleDto);

            roleDto.DisplayName        = "**Test role**";
            roleDto.Description        = "**Role for test**";
            roleDto.GrantedPermissions = new List <string>()
            {
                PermissionNames.Pages_Users
            };

            try
            {
                var updateRoleDto = await _omRoleAppService.UpdateRoleInTenantAsync(1, roleDto);
            }
            catch (Exception exception)
            {
                exception.Message.ShouldBe("There is no role with id: 4");
            }
        }
        public async Task GetAllRolesInTenant_Test()
        {
            CreateTenantDto createTenantDto = new CreateTenantDto()
            {
                TenancyName      = "TestTenant",
                Name             = "TestTenant",
                AdminPhoneNumber = "13851400000",
                IsActive         = true
            };
            var tenantDto = await _tenantAppService.CreateAsync(createTenantDto);

            int roleCount = 50;

            for (int i = 0; i < roleCount; i++)
            {
                var createRoleDto = new CreateRoleDto()
                {
                    Name               = $"Role{i,2}",
                    DisplayName        = $"Test role{i,2}",
                    Description        = $"Role{i,2} for test",
                    GrantedPermissions = new List <string>()
                    {
                        PermissionNames.Pages_Roles, PermissionNames.Pages_Users
                    }
                };
                var roleDto = await _omRoleAppService.CreateRoleInTenantAsync(tenantDto.Id, createRoleDto);
            }

            PagedRoleResultRequestDto pagedRoleDto = new PagedRoleResultRequestDto()
            {
                Keyword        = string.Empty,
                SkipCount      = 13,
                MaxResultCount = 6
            };

            var roles = await _omRoleAppService.GetAllRolesInTenantAsync(tenantDto.Id, pagedRoleDto);

            roles.Items.Count.ShouldBe(6);
            roles.Items[0].Name.ShouldBe("Role12");
        }
예제 #6
0
        public async Task GetRole_Test()
        {
            var createRoleDto = new CreateRoleDto()
            {
                Name               = "RoleTest",
                DisplayName        = "Test role",
                Description        = "Role for test",
                GrantedPermissions = new List <string>()
                {
                    PermissionNames.Pages_Roles
                }
            };

            var roleDto = await _roleAppService.CreateAsync(createRoleDto);

            var getRoleDto = await _roleAppService.GetAsync(new EntityDto <int>(roleDto.Id));

            getRoleDto.Name.ShouldBe(createRoleDto.Name);
            getRoleDto.DisplayName.ShouldBe(createRoleDto.DisplayName);
            getRoleDto.Description.ShouldBe(createRoleDto.Description);
            getRoleDto.GrantedPermissions.Count.ShouldBe(1);
        }
예제 #7
0
        public async Task Create_RoleDTO_ReturnCreated(CreateRoleDto dto,
                                                       [Frozen] Mock <RoleManager <IdentityRole> > roleManagerMock,
                                                       [Frozen] Mock <IMapper> mapper)
        {
            //Arrange
            roleManagerMock
            .Setup(c => c.CreateAsync(It.IsAny <IdentityRole>()))
            .ReturnsAsync(IdentityResult.Success);

            var sut = new RolesController(roleManagerMock.Object, mapper.Object);

            //Act
            var result = await sut.PostAsync(dto);

            var contentResult = result as ObjectResult;

            //Assert
            Assert.NotNull(result);
            Assert.IsType <CreatedResult>(result);
            Assert.IsAssignableFrom <Response <CreateRoleDto> >(contentResult.Value);
            Assert.Equal(dto.Name, ((Response <CreateRoleDto>)contentResult.Value).Data.Name);
        }
예제 #8
0
        public async Task Update_IdDto_ReturnNoContent(IdentityRole role, CreateRoleDto dto,
                                                       [Frozen] Mock <RoleManager <IdentityRole> > roleManagerMock,
                                                       [Frozen] Mock <IMapper> mapper)
        {
            //Arrange
            roleManagerMock.Setup(c => c.FindByIdAsync(It.IsAny <string>()))
            .ReturnsAsync(role);

            roleManagerMock.Setup(c => c.UpdateAsync(It.IsAny <IdentityRole>()))
            .ReturnsAsync(IdentityResult.Success);

            mapper.Setup(m => m.Map <CreateRoleDto, IdentityRole>(It.IsAny <CreateRoleDto>())).Returns(role);

            var sut = new RolesController(roleManagerMock.Object, mapper.Object);

            //Act
            var result = await sut.PutAsync(It.IsAny <string>(), dto);

            //Assign
            Assert.NotNull(result);
            Assert.IsType <NoContentResult>(result);
        }
        public async Task UpdateRoleInTenant_CorrectTenantId_Test()
        {
            CreateTenantDto createTenantDto = new CreateTenantDto()
            {
                TenancyName      = "TestTenant",
                Name             = "TestTenant",
                AdminPhoneNumber = "13851400000",
                IsActive         = true
            };
            var tenantDto = await _tenantAppService.CreateAsync(createTenantDto);

            var createRoleDto = new CreateRoleDto()
            {
                Name               = "RoleTest",
                DisplayName        = "Test role",
                Description        = "Role for test",
                GrantedPermissions = new List <string>()
                {
                    PermissionNames.Pages_Roles
                }
            };

            var roleDto = await _omRoleAppService.CreateRoleInTenantAsync(tenantDto.Id, createRoleDto);

            roleDto.DisplayName        = "**Test role**";
            roleDto.Description        = "**Role for test**";
            roleDto.GrantedPermissions = new List <string>()
            {
                PermissionNames.Pages_Users
            };

            var updateRoleDto = await _omRoleAppService.UpdateRoleInTenantAsync(tenantDto.Id, roleDto);

            updateRoleDto.DisplayName.ShouldBe(roleDto.DisplayName);
            updateRoleDto.Description.ShouldBe(roleDto.Description);
            updateRoleDto.GrantedPermissions.Count.ShouldBe(1);
            updateRoleDto.GrantedPermissions[0].ShouldBe(PermissionNames.Pages_Users);
        }
        public async Task DeleteRoleInTenant_Exist_Test()
        {
            CreateTenantDto createTenantDto = new CreateTenantDto()
            {
                TenancyName      = "TestTenant",
                Name             = "TestTenant",
                AdminPhoneNumber = "13851400000",
                IsActive         = true
            };
            var tenantDto = await _tenantAppService.CreateAsync(createTenantDto);

            var createRoleDto = new CreateRoleDto()
            {
                Name               = "RoleTest",
                DisplayName        = "Test role",
                Description        = "Role for test",
                GrantedPermissions = new List <string>()
                {
                    PermissionNames.Pages_Roles
                }
            };

            var roleDto = await _omRoleAppService.CreateRoleInTenantAsync(tenantDto.Id, createRoleDto);

            await UsingDbContextAsync(async context =>
            {
                var testTenant = await context.Roles.FirstOrDefaultAsync(r => r.Id == roleDto.Id);
                testTenant.ShouldNotBeNull();
            });

            await _omRoleAppService.DeleteRoleInTenantAsync(tenantDto.Id, new EntityDto <int>(roleDto.Id));

            await UsingDbContextAsync(async context =>
            {
                var testTenant = await context.Roles.FirstOrDefaultAsync(r => r.Id == roleDto.Id);
                testTenant.IsDeleted.ShouldBeTrue();
            });
        }
예제 #11
0
        public async Task CreateRole_Test()
        {
            var createRoleDto = new CreateRoleDto()
            {
                Name               = "RoleTest",
                DisplayName        = "Test role",
                Description        = "Role for test",
                GrantedPermissions = new List <string>()
                {
                    PermissionNames.Pages_Roles
                }
            };
            var roleDto = await _roleAppService.CreateAsync(createRoleDto);

            roleDto.Id.ShouldNotBe(0);
            roleDto.NormalizedName.ShouldBe("ROLETEST");

            await UsingDbContextAsync(async context =>
            {
                var testTenant = await context.Roles.FirstOrDefaultAsync(r => r.Id == roleDto.Id);
                testTenant.TenantId.ShouldBe(1);
            });
        }
예제 #12
0
        /// <summary>
        /// 创建角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public async Task <DataResult <string> > CreateRoleAsync(CreateRoleDto role)
        {
            if (role is null)
            {
                throw new ArgumentNullException(nameof(role));
            }

            ApplicationRole applicationRole = new ApplicationRole
            {
                Id   = GuidEx.NewGuid().ToString(),
                Name = role.Name,
                Desc = role.Desc
            };

            var identityResult = await roleManager.CreateAsync(applicationRole).ConfigureAwait(false);

            if (identityResult.Succeeded)
            {
                return(OkDataResult(applicationRole.Id));
            }

            return(FailedDataResult <string>(identityResult.Errors.FirstOrDefault().Description));
        }
예제 #13
0
        public async Task AddRoleToOrgUnit_Test()
        {
            var createRoleDto = new CreateRoleDto()
            {
                Name               = "RoleTest",
                DisplayName        = "Test role",
                Description        = "Role for test",
                GrantedPermissions = new List <string>()
                {
                    PermissionNames.Pages_Roles
                }
            };
            var roleDto = await _roleAppService.CreateAsync(createRoleDto);

            CreateOrgUnitDto createOrgUnitDto = new CreateOrgUnitDto()
            {
                ParentId    = null,
                DisplayName = "Ou Test"
            };
            var orgUnitDto = await _orgUnitAppService.CreateAsync(createOrgUnitDto);

            SetOrgUnitRoleDto setOrgUnitRoleDto = new SetOrgUnitRoleDto()
            {
                OrgUnitId = orgUnitDto.Id,
                RoleId    = roleDto.Id
            };
            await _orgUnitAppService.AddRoleToOrgUnitAsync(setOrgUnitRoleDto);

            await UsingDbContextAsync(async context =>
            {
                var testOus = await context.OrganizationUnitRoles
                              .Where(our => our.OrganizationUnitId == orgUnitDto.Id).ToListAsync();
                testOus.Count.ShouldBe(1);
                testOus[0].RoleId.ShouldBe(roleDto.Id);
            });
        }
예제 #14
0
        public async Task <IActionResult> Post([FromBody] CreateRoleDto createRole)
        {
            var role = new Role {
                Name = createRole.Name
            };

            await _unitOfWork.RoleService.AddAsync(role);

            foreach (var permissionId in createRole.PermissionId)
            {
                if (!await _unitOfWork.PermissionService.HasPermissionExist(permissionId))
                {
                    return(NotFound(new ApiMessage()));
                }

                role.RolePermissions.Add(new RolePermission {
                    RoleId = role.Id, PermissionId = permissionId
                });
            }

            await _unitOfWork.SaveChangesAsync();

            return(Created(Url.Link("GetPermission", new { id = role.Id }), _mapper.Map <RoleDto>(role)));
        }
예제 #15
0
        public async Task <IActionResult> CreateRole([FromBody] CreateRoleDto createRoleDto)
        {
            if (createRoleDto == null)
            {
                return(BadRequest("No Role Name Present"));
            }
            if (ModelState.IsValid)
            {
                var add = await _role.CreateRole(createRoleDto.Name);

                if (add)
                {
                    var role = await _role.GetRoleByName(createRoleDto.Name);

                    return(CreatedAtRoute("GetRole", new { id = role.Id }, role));
                }
                //
                return(StatusCode(500, "Server Error, Something went wrong with our server"));
            }
            else
            {
                return(BadRequest(ModelState));
            }
        }
예제 #16
0
        private async Task CreateComplexRoleAndOrgUnit()
        {
            // Role 1
            var createRole1Dto = new CreateRoleDto()
            {
                Name               = "Role1",
                DisplayName        = "Role1",
                Description        = "Role1 for test",
                GrantedPermissions = new List <string>()
                {
                    PermissionNames.Pages_Roles
                }
            };
            var role1Dto = await _roleAppService.CreateAsync(createRole1Dto);

            // Role 2
            var createRole2Dto = new CreateRoleDto()
            {
                Name               = "Role2",
                DisplayName        = "Role2",
                Description        = "Role2 for test",
                GrantedPermissions = new List <string>()
                {
                    PermissionNames.Pages_Users, PermissionNames.Pages_Roles
                }
            };
            var role2Dto = await _roleAppService.CreateAsync(createRole2Dto);

            // Role 2
            var createRole3Dto = new CreateRoleDto()
            {
                Name               = "Role3",
                DisplayName        = "Role3",
                Description        = "Role3 for test",
                GrantedPermissions = new List <string>()
                {
                    PermissionNames.Pages_Roles
                }
            };
            var role3Dto = await _roleAppService.CreateAsync(createRole3Dto);

            // OrgUnit with Role1 and Role2
            CreateOrgUnitDto createOrgUnitDto = new CreateOrgUnitDto()
            {
                ParentId    = null,
                DisplayName = "Ou Test"
            };
            var orgUnitDto = await _orgUnitAppService.CreateAsync(createOrgUnitDto);

            SetOrgUnitRoleDto setOrgUnitRole1Dto = new SetOrgUnitRoleDto()
            {
                OrgUnitId = orgUnitDto.Id,
                RoleId    = role1Dto.Id
            };
            await _orgUnitAppService.AddRoleToOrgUnitAsync(setOrgUnitRole1Dto);

            SetOrgUnitRoleDto setOrgUnitRole2Dto = new SetOrgUnitRoleDto()
            {
                OrgUnitId = orgUnitDto.Id,
                RoleId    = role2Dto.Id
            };
            await _orgUnitAppService.AddRoleToOrgUnitAsync(setOrgUnitRole2Dto);
        }
 public RoleCannotBeCreatedWithDuplicateNameAssociatedToApplicationBusinessRule(IQueryable <Role> roles, CreateRoleDto createDto)
 {
     _roles     = roles;
     _createDto = createDto;
 }
 public Task <RoleDto> Create(CreateRoleDto input)
 {
     throw new NotImplementedException();
 }
예제 #19
0
 public void When(CreateRoleDto c)
 {
     WhenAsync(c).GetAwaiter().GetResult();
 }
예제 #20
0
        public async Task <JsonResult> Create([FromBody] CreateRoleDto input)
        {
            var role = await _roleAppService.Create(input);

            return(Json(role));
        }
예제 #21
0
 public async Task <IActionResult> Put(string id, [FromBody] CreateRoleDto createRoleDto) => Ok(await AdministrationServices.Put(id, createRoleDto));
예제 #22
0
 public async Task <IActionResult> CreateRole([FromBody] CreateRoleDto createRoleDto) => Ok(await AdministrationServices.CreateRole(createRoleDto));
 public CreateRoleCommand(CreateRoleDto createDto)
 {
     CreateRoleDto = createDto;
 }
예제 #24
0
        public async Task <ActionResult> AddRole(CreateRoleDto role)
        {
            RoleDto result = await _roleAppService.Create(role);

            return(AbpJson(result));
        }
        public async Task <ActionResult <RoleInfo> > CreateRoleInfo(CreateRoleDto createRoleDto)
        {
            var createrole = await _roleInfoRepository.CreateRoleInfo(createRoleDto);

            return(createrole);
        }
예제 #26
0
        public async Task <IActionResult> Create(CreateRoleDto createRoleInfo)
        {
            var role = await this.Repository.Create(this.Mapper.Map <Role>(createRoleInfo));

            return(this.Created(new Uri($"{Request.GetDisplayUrl()}/{role.ID}"), this.Mapper.Map <RoleDto>(role)));
        }
예제 #27
0
 public Result Create([FromBody] CreateRoleDto dto) => base.Create(dto);
예제 #28
0
        public async Task <IActionResult> Create(CreateRoleDto createRoleDto)
        {
            await _roleService.Create(createRoleDto);

            return(Ok());
        }
예제 #29
0
        public async Task <IActionResult> CreateAsync([FromBody] CreateRoleDto data)
        {
            var result = await _roleService.CreateAsync(data);

            return(Ok(result));
        }
예제 #30
0
 public IActionResult Post([FromBody] CreateRoleDto model) =>
 _mapper.Map <RoleInsertModel>(model)
 .Map(_commandRepository.Insert)
 .Map(x => AllOk(new { id = x }))
 .Reduce(_ => BadRequest(), error => error is ArgumentNotSet)
 .Reduce(_ => InternalServerError(), x => _logger.LogError(x.ToString()));