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;
}
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());
}
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);
}
}
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);
}
}
}
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());
}
Aggregations