Search in sources :

Example 1 with RoleRequest

use of org.entando.entando.web.role.model.RoleRequest in project entando-core by entando.

the class RoleService method createRole.

protected Role createRole(RoleRequest roleRequest) {
    Role role = new Role();
    role.setName(roleRequest.getCode());
    role.setDescription(roleRequest.getName());
    if (null != roleRequest.getPermissions()) {
        roleRequest.getPermissions().entrySet().stream().filter(entry -> null != entry.getValue() && entry.getValue().booleanValue()).forEach(i -> role.addPermission(i.getKey()));
    }
    return role;
}
Also used : Role(com.agiletec.aps.system.services.role.Role) UserDto(org.entando.entando.aps.system.services.user.model.UserDto) LoggerFactory(org.slf4j.LoggerFactory) RestRourceNotFoundException(org.entando.entando.aps.system.exception.RestRourceNotFoundException) Role(com.agiletec.aps.system.services.role.Role) RoleValidator(org.entando.entando.web.role.validator.RoleValidator) FieldSearchFilter(com.agiletec.aps.system.common.FieldSearchFilter) RestServerError(org.entando.entando.aps.system.exception.RestServerError) IRoleManager(com.agiletec.aps.system.services.role.IRoleManager) IAuthorizationService(com.agiletec.aps.system.services.authorization.IAuthorizationService) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) RoleRequest(org.entando.entando.web.role.model.RoleRequest) Permission(com.agiletec.aps.system.services.role.Permission) Logger(org.slf4j.Logger) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) DtoBuilder(org.entando.entando.aps.system.services.DtoBuilder) Collectors(java.util.stream.Collectors) List(java.util.List) RestListRequest(org.entando.entando.web.common.model.RestListRequest) PermissionDto(org.entando.entando.aps.system.services.role.model.PermissionDto) RoleDto(org.entando.entando.aps.system.services.role.model.RoleDto) Filter(org.entando.entando.web.common.model.Filter) SearcherDaoPaginatedResult(com.agiletec.aps.system.common.model.dao.SearcherDaoPaginatedResult) PagedMetadata(org.entando.entando.web.common.model.PagedMetadata) PostConstruct(javax.annotation.PostConstruct) ValidationConflictException(org.entando.entando.web.common.exceptions.ValidationConflictException) Comparator(java.util.Comparator) IDtoBuilder(org.entando.entando.aps.system.services.IDtoBuilder)

Example 2 with RoleRequest

use of org.entando.entando.web.role.model.RoleRequest in project entando-core by entando.

the class RoleControllerUnitTest method testAddWithInvalidCodeAndName.

@Test
public void testAddWithInvalidCodeAndName() throws Exception {
    UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
    String accessToken = mockOAuthInterceptor(user);
    ObjectMapper mapper = new ObjectMapper();
    RoleRequest request = new RoleRequest();
    request.setCode(StringUtils.repeat("a", 100));
    request.setName(StringUtils.repeat("b", 100));
    // request.setCode(code);
    String payload = mapper.writeValueAsString(request);
    ResultActions result = mockMvc.perform(post("/roles").contentType(MediaType.APPLICATION_JSON_VALUE).content(payload).header("Authorization", "Bearer " + accessToken));
    String x = result.andReturn().getResponse().getContentAsString();
    System.out.println(x);
    result.andExpect(status().isBadRequest());
}
Also used : UserDetails(com.agiletec.aps.system.services.user.UserDetails) ResultActions(org.springframework.test.web.servlet.ResultActions) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RoleRequest(org.entando.entando.web.role.model.RoleRequest) AbstractControllerTest(org.entando.entando.web.AbstractControllerTest) Test(org.junit.Test)

Example 3 with RoleRequest

use of org.entando.entando.web.role.model.RoleRequest in project entando-core by entando.

the class RoleService method updateRole.

@Override
public RoleDto updateRole(RoleRequest roleRequest) {
    try {
        Role role = this.getRoleManager().getRole(roleRequest.getCode());
        if (null == role) {
            logger.warn("no role found with code {}", roleRequest.getCode());
            throw new RestRourceNotFoundException(RoleValidator.ERRCODE_ROLE_NOT_FOUND, "role", roleRequest.getCode());
        }
        role.setDescription(roleRequest.getName());
        role.getPermissions().clear();
        if (null != roleRequest.getPermissions()) {
            roleRequest.getPermissions().entrySet().stream().filter(entry -> null != entry.getValue() && entry.getValue().booleanValue()).forEach(i -> role.addPermission(i.getKey()));
        }
        BeanPropertyBindingResult validationResult = this.validateRoleForUpdate(role);
        if (validationResult.hasErrors()) {
            throw new ValidationConflictException(validationResult);
        }
        this.getRoleManager().updateRole(role);
        RoleDto dto = this.getDtoBuilder().toDto(role, this.getRoleManager().getPermissionsCodes());
        return dto;
    } catch (ApsSystemException e) {
        logger.error("Error updating a role", e);
        throw new RestServerError("error in update role", e);
    }
}
Also used : Role(com.agiletec.aps.system.services.role.Role) RestRourceNotFoundException(org.entando.entando.aps.system.exception.RestRourceNotFoundException) UserDto(org.entando.entando.aps.system.services.user.model.UserDto) LoggerFactory(org.slf4j.LoggerFactory) RestRourceNotFoundException(org.entando.entando.aps.system.exception.RestRourceNotFoundException) Role(com.agiletec.aps.system.services.role.Role) RoleValidator(org.entando.entando.web.role.validator.RoleValidator) FieldSearchFilter(com.agiletec.aps.system.common.FieldSearchFilter) RestServerError(org.entando.entando.aps.system.exception.RestServerError) IRoleManager(com.agiletec.aps.system.services.role.IRoleManager) IAuthorizationService(com.agiletec.aps.system.services.authorization.IAuthorizationService) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) RoleRequest(org.entando.entando.web.role.model.RoleRequest) Permission(com.agiletec.aps.system.services.role.Permission) Logger(org.slf4j.Logger) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) DtoBuilder(org.entando.entando.aps.system.services.DtoBuilder) Collectors(java.util.stream.Collectors) List(java.util.List) RestListRequest(org.entando.entando.web.common.model.RestListRequest) PermissionDto(org.entando.entando.aps.system.services.role.model.PermissionDto) RoleDto(org.entando.entando.aps.system.services.role.model.RoleDto) Filter(org.entando.entando.web.common.model.Filter) SearcherDaoPaginatedResult(com.agiletec.aps.system.common.model.dao.SearcherDaoPaginatedResult) PagedMetadata(org.entando.entando.web.common.model.PagedMetadata) PostConstruct(javax.annotation.PostConstruct) ValidationConflictException(org.entando.entando.web.common.exceptions.ValidationConflictException) Comparator(java.util.Comparator) IDtoBuilder(org.entando.entando.aps.system.services.IDtoBuilder) RoleDto(org.entando.entando.aps.system.services.role.model.RoleDto) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) RestServerError(org.entando.entando.aps.system.exception.RestServerError) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) ValidationConflictException(org.entando.entando.web.common.exceptions.ValidationConflictException)

Example 4 with RoleRequest

use of org.entando.entando.web.role.model.RoleRequest in project entando-core by entando.

the class RoleControllerIntegrationTest method testCrudRole.

/**
 * Add a role without permissions
 * </p>
 *
 * Update the role with 1 valid permissions
 * </p>
 *
 * Update the role by adding 1 invalid permissions
 * </p>
 *
 * Update the role by adding 1 valid permissions
 * </p>
 *
 * delete the role
 * </p>
 *
 * @throws Exception
 */
@Test
public void testCrudRole() throws Exception {
    String code = "test";
    try {
        UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
        String accessToken = mockOAuthInterceptor(user);
        ObjectMapper mapper = new ObjectMapper();
        RoleRequest request = new RoleRequest();
        request.setCode(code);
        request.setName(code);
        String payload = mapper.writeValueAsString(request);
        ResultActions result = mockMvc.perform(post("/roles").contentType(MediaType.APPLICATION_JSON_VALUE).content(payload).header("Authorization", "Bearer " + accessToken));
        result.andExpect(status().isOk());
        System.out.println("ADD");
        // {"payload":{"code":"test","name":"test","permissions":{"enterBackend":false,"managePages":false,"editContents":false,"validateContents":false,"manageResources":false,"superuser":false,"manageCategories":false}},"errors":[],"metaData":{}}
        System.out.println(result.andReturn().getResponse().getContentAsString());
        // --------------
        request = new RoleRequest();
        request.setCode(code);
        request.setName(code.toUpperCase());
        request.getPermissions().put("editContents", true);
        payload = mapper.writeValueAsString(request);
        result = mockMvc.perform(put("/roles/{code}", code).contentType(MediaType.APPLICATION_JSON_VALUE).content(payload).header("Authorization", "Bearer " + accessToken));
        result.andExpect(status().isOk());
        System.out.println("UPDATE");
        System.out.println(result.andReturn().getResponse().getContentAsString());
        // {"payload":{"code":"test","name":"TEST","permissions":{"enterBackend":false,"managePages":false,"editContents":true,"validateContents":false,"manageResources":false,"superuser":false,"manageCategories":false}},"errors":[],"metaData":{}}
        // --------------
        request = new RoleRequest();
        request.setCode(code);
        request.setName(code.toUpperCase());
        request.getPermissions().put("editContents", true);
        request.getPermissions().put("WRONG", true);
        payload = mapper.writeValueAsString(request);
        result = mockMvc.perform(put("/roles/{code}", code).contentType(MediaType.APPLICATION_JSON_VALUE).content(payload).header("Authorization", "Bearer " + accessToken));
        result.andExpect(status().isConflict());
        System.out.println("UPDATE-WRONG");
        System.out.println(result.andReturn().getResponse().getContentAsString());
        // {"payload":[],"errors":[{"code":"4","message":"role.permission.notFound"}],"metaData":{}}
        // --------------
        request = new RoleRequest();
        request.setCode(code);
        request.setName(code.toUpperCase());
        request.getPermissions().put("editContents", true);
        request.getPermissions().put("manageResources", true);
        request.getPermissions().put("manageCategories", false);
        payload = mapper.writeValueAsString(request);
        result = mockMvc.perform(put("/roles/{code}", code).contentType(MediaType.APPLICATION_JSON_VALUE).content(payload).header("Authorization", "Bearer " + accessToken));
        result.andExpect(status().isOk());
        System.out.println("UPDATE-OK");
        System.out.println(result.andReturn().getResponse().getContentAsString());
        // --------------
        result = mockMvc.perform(delete("/roles/{code}", code).contentType(MediaType.APPLICATION_JSON_VALUE).header("Authorization", "Bearer " + accessToken));
        result.andExpect(status().isOk());
        System.out.println("UPDATE-OK");
        System.out.println(result.andReturn().getResponse().getContentAsString());
    } finally {
        Role role = this.roleManager.getRole(code);
        if (null != role) {
            this.roleManager.removeRole(role);
        }
    }
}
Also used : Role(com.agiletec.aps.system.services.role.Role) UserDetails(com.agiletec.aps.system.services.user.UserDetails) ResultActions(org.springframework.test.web.servlet.ResultActions) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RoleRequest(org.entando.entando.web.role.model.RoleRequest) Test(org.junit.Test) AbstractControllerIntegrationTest(org.entando.entando.web.AbstractControllerIntegrationTest)

Example 5 with RoleRequest

use of org.entando.entando.web.role.model.RoleRequest in project entando-core by entando.

the class RoleControllerUnitTest method testAddWithEmptyPayload.

@Test
public void testAddWithEmptyPayload() throws Exception {
    UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
    String accessToken = mockOAuthInterceptor(user);
    ObjectMapper mapper = new ObjectMapper();
    RoleRequest request = new RoleRequest();
    request.setCode("");
    // request.setCode(code);
    String payload = mapper.writeValueAsString(request);
    ResultActions result = mockMvc.perform(post("/roles").contentType(MediaType.APPLICATION_JSON_VALUE).content(payload).header("Authorization", "Bearer " + accessToken));
    String x = result.andReturn().getResponse().getContentAsString();
    System.out.println(x);
    result.andExpect(status().isBadRequest());
}
Also used : UserDetails(com.agiletec.aps.system.services.user.UserDetails) ResultActions(org.springframework.test.web.servlet.ResultActions) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RoleRequest(org.entando.entando.web.role.model.RoleRequest) AbstractControllerTest(org.entando.entando.web.AbstractControllerTest) Test(org.junit.Test)

Aggregations

RoleRequest (org.entando.entando.web.role.model.RoleRequest)5 Role (com.agiletec.aps.system.services.role.Role)3 UserDetails (com.agiletec.aps.system.services.user.UserDetails)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Test (org.junit.Test)3 ResultActions (org.springframework.test.web.servlet.ResultActions)3 FieldSearchFilter (com.agiletec.aps.system.common.FieldSearchFilter)2 SearcherDaoPaginatedResult (com.agiletec.aps.system.common.model.dao.SearcherDaoPaginatedResult)2 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)2 IAuthorizationService (com.agiletec.aps.system.services.authorization.IAuthorizationService)2 IRoleManager (com.agiletec.aps.system.services.role.IRoleManager)2 Permission (com.agiletec.aps.system.services.role.Permission)2 Comparator (java.util.Comparator)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 PostConstruct (javax.annotation.PostConstruct)2 RestRourceNotFoundException (org.entando.entando.aps.system.exception.RestRourceNotFoundException)2 RestServerError (org.entando.entando.aps.system.exception.RestServerError)2 DtoBuilder (org.entando.entando.aps.system.services.DtoBuilder)2 IDtoBuilder (org.entando.entando.aps.system.services.IDtoBuilder)2