use of io.gravitee.rest.api.portal.rest.model.User in project gravitee-management-rest-api by gravitee-io.
the class UserMapperTest method testConvertSearchableUser.
@Test
public void testConvertSearchableUser() {
// init
SearchableUser searchableUser = Mockito.mock(SearchableUser.class);
Mockito.when(searchableUser.getDisplayName()).thenReturn(SEARCHABLE_USER_DISPLAY_NAME);
Mockito.when(searchableUser.getEmail()).thenReturn(USER_EMAIL);
Mockito.when(searchableUser.getFirstname()).thenReturn(USER_FIRSTNAME);
Mockito.when(searchableUser.getId()).thenReturn(USER_ID);
Mockito.when(searchableUser.getLastname()).thenReturn(USER_LASTNAME);
Mockito.when(searchableUser.getReference()).thenReturn(SEARCHABLE_USER_REFERENCE);
// Test
User responseUser = userMapper.convert(searchableUser);
assertNotNull(responseUser);
assertEquals(USER_ID, responseUser.getId());
assertEquals(USER_EMAIL, responseUser.getEmail());
assertEquals(USER_FIRSTNAME, responseUser.getFirstName());
assertEquals(USER_LASTNAME, responseUser.getLastName());
assertEquals(SEARCHABLE_USER_DISPLAY_NAME, responseUser.getDisplayName());
assertEquals(SEARCHABLE_USER_REFERENCE, responseUser.getReference());
}
use of io.gravitee.rest.api.portal.rest.model.User in project gravitee-management-rest-api by gravitee-io.
the class UserResource method updateCurrentUser.
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response updateCurrentUser(@Valid @NotNull(message = "Input must not be null.") UserInput user) {
if (!getAuthenticatedUser().equals(user.getId())) {
throw new UnauthorizedAccessException();
}
UserEntity existingUser = userService.findById(getAuthenticatedUser());
UpdateUserEntity updateUserEntity = new UpdateUserEntity();
// the avatar picture
if (user.getAvatar() != null && !user.getAvatar().startsWith("http")) {
updateUserEntity.setPicture(checkAndScaleImage(user.getAvatar()));
} else {
updateUserEntity.setPicture(existingUser.getPicture());
}
if (user.getEmail() != null) {
updateUserEntity.setEmail(user.getEmail());
}
if (user.getFirstName() != null) {
updateUserEntity.setFirstname(user.getFirstName());
}
if (user.getLastName() != null) {
updateUserEntity.setLastname(user.getLastName());
}
updateUserEntity.setCustomFields(user.getCustomFields());
UserEntity updatedUser = userService.update(user.getId(), updateUserEntity);
final User currentUser = userMapper.convert(updatedUser);
currentUser.setLinks(userMapper.computeUserLinks(userURL(uriInfo.getBaseUriBuilder()), updatedUser.getUpdatedAt()));
return Response.ok(currentUser).build();
}
use of io.gravitee.rest.api.portal.rest.model.User in project gravitee-management-rest-api by gravitee-io.
the class UserResource method getCurrentUser.
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getCurrentUser() {
final String authenticatedUser = getAuthenticatedUser();
try {
UserEntity userEntity = userService.findByIdWithRoles(authenticatedUser);
User currentUser = userMapper.convert(userEntity);
boolean withManagement = (authenticatedUser != null && permissionService.hasManagementRights(authenticatedUser));
if (withManagement) {
Management managementConfig = this.configService.getConsoleSettings().getManagement();
if (managementConfig != null && managementConfig.getUrl() != null) {
UserConfig userConfig = new UserConfig();
userConfig.setManagementUrl(managementConfig.getUrl());
currentUser.setConfig(userConfig);
}
}
currentUser.setLinks(userMapper.computeUserLinks(userURL(uriInfo.getBaseUriBuilder()), userEntity.getUpdatedAt()));
return Response.ok(currentUser).build();
} catch (final UserNotFoundException unfe) {
response.addCookie(cookieGenerator.generate(null));
return status(Response.Status.UNAUTHORIZED).build();
}
}
use of io.gravitee.rest.api.portal.rest.model.User in project gravitee-management-rest-api by gravitee-io.
the class UserResourceTest method init.
@Before
public void init() throws IOException, URISyntaxException {
resetAllMocks();
doReturn(new User()).when(userMapper).convert(nullable(UserEntity.class));
doReturn(new UserLinks()).when(userMapper).computeUserLinks(any(), any());
InlinePictureEntity mockImage = new InlinePictureEntity();
byte[] apiLogoContent = Files.readAllBytes(Paths.get(this.getClass().getClassLoader().getResource("media/logo.svg").toURI()));
mockImage.setContent(apiLogoContent);
mockImage.setType("image/svg");
doReturn(mockImage).when(userService).getPicture(any());
}
use of io.gravitee.rest.api.portal.rest.model.User in project gravitee-management-rest-api by gravitee-io.
the class UserResourceTest method shouldGetCurrentUserWithoutConfig.
@Test
public void shouldGetCurrentUserWithoutConfig() {
when(userService.findByIdWithRoles(USER_NAME)).thenReturn(new UserEntity());
when(permissionService.hasManagementRights(USER_NAME)).thenReturn(Boolean.FALSE);
final Response response = target().request().get();
assertEquals(HttpStatusCode.OK_200, response.getStatus());
ArgumentCaptor<String> userId = ArgumentCaptor.forClass(String.class);
Mockito.verify(userService).findByIdWithRoles(userId.capture());
assertEquals(USER_NAME, userId.getValue());
User user = response.readEntity(User.class);
assertNotNull(user);
assertNull(user.getConfig());
assertNotNull(user.getLinks());
}
Aggregations