@Override public String getId() { return userEntity.getId(); }
@Override public String getId() { return userEntity.getId(); }
public PrimaryOwnerEntity(UserEntity user) { this.id = user.getId(); this.email = user.getEmail(); this.displayName = user.getDisplayName(); }
@PUT @ApiOperation(value = "Update user") public Response updateCurrentUser(@Valid @NotNull final UpdateUserEntity user) { UserEntity userEntity = userService.findById(getAuthenticatedUser()); // TODO: how to ensure that we can update the user profile? /* if (!userEntity.get.equals(userService.findById(getAuthenticatedUser()).getUsername())) { throw new ForbiddenAccessException(); } */ checkImageSize(user.getPicture()); return ok(userService.update(userEntity.getId(), user)).build(); }
@PUT @ApiOperation(value = "Update user") public Response updateCurrentUser(@Valid @NotNull final UpdateUserEntity user) { UserEntity userEntity = userService.findById(getAuthenticatedUser()); // TODO: how to ensure that we can update the user profile? /* if (!userEntity.get.equals(userService.findById(getAuthenticatedUser()).getUsername())) { throw new ForbiddenAccessException(); } */ checkImageSize(user.getPicture()); return ok(userService.update(userEntity.getId(), user)).build(); }
.forEach(userEntity -> userIdToUserEntity.put(userEntity.getId(), userEntity));
.forEach(userEntity -> userIdToUserEntity.put(userEntity.getId(), userEntity));
.forEach(userEntity -> userIdToUserEntity.put(userEntity.getId(), userEntity));
@GET @Path("avatar") @ApiOperation(value = "Get user's avatar") public Response getCurrentUserPicture(@Context Request request) { String userId = userService.findById(getAuthenticatedUser()).getId(); PictureEntity picture = userService.getPicture(userId); if (picture == null) { throw new NotFoundException(); } if (picture instanceof UrlPictureEntity) { return Response.temporaryRedirect(URI.create(((UrlPictureEntity) picture).getUrl())).build(); } InlinePictureEntity image = (InlinePictureEntity) picture; EntityTag etag = new EntityTag(Integer.toString(new String(image.getContent()).hashCode())); Response.ResponseBuilder builder = request.evaluatePreconditions(etag); if (builder != null) { return builder.build(); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write(image.getContent(), 0, image.getContent().length); return ok() .entity(baos) .tag(etag) .type(image.getType()) .build(); }
@GET @Path("avatar") @ApiOperation(value = "Get user's avatar") public Response getCurrentUserPicture(@Context Request request) { String userId = userService.findById(getAuthenticatedUser()).getId(); PictureEntity picture = userService.getPicture(userId); if (picture == null) { throw new NotFoundException(); } if (picture instanceof UrlPictureEntity) { return Response.temporaryRedirect(URI.create(((UrlPictureEntity) picture).getUrl())).build(); } InlinePictureEntity image = (InlinePictureEntity) picture; EntityTag etag = new EntityTag(Integer.toString(new String(image.getContent()).hashCode())); Response.ResponseBuilder builder = request.evaluatePreconditions(etag); if (builder != null) { return builder.build(); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write(image.getContent(), 0, image.getContent().length); return ok() .entity(baos) .tag(etag) .type(image.getType()) .build(); }
private Map<String, Object> getTokenRegistrationParams(final UserEntity userEntity, final String portalUri) { // generate a JWT to store user's information and for security purpose final Map<String, Object> claims = new HashMap<>(); claims.put(Claims.ISSUER, environment.getProperty("jwt.issuer", DEFAULT_JWT_ISSUER)); claims.put(Claims.SUBJECT, userEntity.getId()); claims.put(Claims.EMAIL, userEntity.getEmail()); claims.put(Claims.FIRSTNAME, userEntity.getFirstname()); claims.put(Claims.LASTNAME, userEntity.getLastname()); final JWTSigner.Options options = new JWTSigner.Options(); options.setExpirySeconds(environment.getProperty("user.creation.token.expire-after", Integer.class, DEFAULT_JWT_EMAIL_REGISTRATION_EXPIRE_AFTER)); options.setIssuedAt(true); options.setJwtId(true); // send a confirm email with the token final String jwtSecret = environment.getProperty("jwt.secret"); if (jwtSecret == null || jwtSecret.isEmpty()) { throw new IllegalStateException("JWT secret is mandatory"); } final String token = new JWTSigner(jwtSecret).sign(claims, options); String portalUrl = environment.getProperty("portalURL"); if (portalUrl!= null && portalUrl.endsWith("/")) { portalUrl = portalUrl.substring(0, portalUrl.length() - 1); } String registrationUrl = portalUrl + portalUri + token; return new NotificationParamsBuilder() .user(userEntity) .token(token) .registrationUrl(registrationUrl) .build(); }
@Override public Document transform(Indexable indexable) { Document doc = new Document(); UserEntity user = (UserEntity) indexable; doc.add(new StringField(FIELD_ID, user.getId(), Field.Store.YES)); doc.add(new StringField(FIELD_TYPE, FIELD_TYPE_VALUE, Field.Store.YES)); doc.add(new StringField(FIELD_SOURCE, user.getSource(), Field.Store.NO)); doc.add(new StringField(FIELD_REFERENCE, user.getSourceId(), Field.Store.NO)); if (user.getDisplayName() != null) { doc.add(new StringField(FIELD_DISPLAYNAME, user.getDisplayName(), Field.Store.NO)); doc.add(new TextField(FIELD_DISPLAYNAME_SPLIT, user.getDisplayName(), Field.Store.NO)); } if (user.getFirstname() != null) { doc.add(new StringField(FIELD_FIRSTNAME, user.getFirstname(), Field.Store.NO)); } if (user.getLastname() != null) { doc.add(new StringField(FIELD_LASTNAME, user.getLastname(), Field.Store.NO)); } if (user.getEmail() != null) { doc.add(new StringField(FIELD_EMAIL, user.getEmail(), Field.Store.NO)); } return doc; }
@Override public Document transform(Indexable indexable) { Document doc = new Document(); UserEntity user = (UserEntity) indexable; doc.add(new StringField(FIELD_ID, user.getId(), Field.Store.YES)); doc.add(new StringField(FIELD_TYPE, FIELD_TYPE_VALUE, Field.Store.YES)); doc.add(new StringField(FIELD_SOURCE, user.getSource(), Field.Store.NO)); doc.add(new StringField(FIELD_REFERENCE, user.getSourceId(), Field.Store.NO)); if (user.getDisplayName() != null) { doc.add(new StringField(FIELD_DISPLAYNAME, user.getDisplayName(), Field.Store.NO)); doc.add(new TextField(FIELD_DISPLAYNAME_SPLIT, user.getDisplayName(), Field.Store.NO)); } if (user.getFirstname() != null) { doc.add(new StringField(FIELD_FIRSTNAME, user.getFirstname(), Field.Store.NO)); } if (user.getLastname() != null) { doc.add(new StringField(FIELD_LASTNAME, user.getLastname(), Field.Store.NO)); } if (user.getEmail() != null) { doc.add(new StringField(FIELD_EMAIL, user.getEmail(), Field.Store.NO)); } return doc; }
private Map<String, Object> getTokenRegistrationParams(final UserEntity userEntity, final String portalUri) { // generate a JWT to store user's information and for security purpose final Map<String, Object> claims = new HashMap<>(); claims.put(Claims.ISSUER, environment.getProperty("jwt.issuer", DEFAULT_JWT_ISSUER)); claims.put(Claims.SUBJECT, userEntity.getId()); claims.put(Claims.EMAIL, userEntity.getEmail()); claims.put(Claims.FIRSTNAME, userEntity.getFirstname()); claims.put(Claims.LASTNAME, userEntity.getLastname()); final JWTSigner.Options options = new JWTSigner.Options(); options.setExpirySeconds(environment.getProperty("user.creation.token.expire-after", Integer.class, DEFAULT_JWT_EMAIL_REGISTRATION_EXPIRE_AFTER)); options.setIssuedAt(true); options.setJwtId(true); // send a confirm email with the token final String jwtSecret = environment.getProperty("jwt.secret"); if (jwtSecret == null || jwtSecret.isEmpty()) { throw new IllegalStateException("JWT secret is mandatory"); } final String token = new JWTSigner(jwtSecret).sign(claims, options); String portalUrl = environment.getProperty("portalURL"); if (portalUrl!= null && portalUrl.endsWith("/")) { portalUrl = portalUrl.substring(0, portalUrl.length() - 1); } String registrationUrl = portalUrl + portalUri + token; return new NotificationParamsBuilder() .user(userEntity) .token(token) .registrationUrl(registrationUrl) .build(); }
@Override public void onApplicationEvent(AuthenticationSuccessEvent event) { final UserDetails details = (UserDetails) event.getAuthentication().getPrincipal(); try { UserEntity registeredUser = userService.findBySource(details.getSource(), details.getSourceId(), false); updateRegisteredUser(registeredUser, details); // Principal username is the technical identifier of the user // Dirty hack because spring security is requiring a username... details.setUsername(registeredUser.getId()); } catch (UserNotFoundException unfe) { final NewExternalUserEntity newUser = new NewExternalUserEntity(); newUser.setSource(details.getSource()); newUser.setSourceId(details.getSourceId()); newUser.setFirstname(details.getFirstname()); newUser.setLastname(details.getLastname()); newUser.setEmail(details.getEmail()); boolean addDefaultRole = false; if (event.getAuthentication().getAuthorities() == null || event.getAuthentication().getAuthorities().isEmpty()) { addDefaultRole = true; } UserEntity createdUser = userService.create(newUser, addDefaultRole); // Principal username is the technical identifier of the user details.setUsername(createdUser.getId()); if (!addDefaultRole) { addRole(RoleScope.MANAGEMENT, createdUser.getId(), event.getAuthentication().getAuthorities()); addRole(RoleScope.PORTAL, createdUser.getId(), event.getAuthentication().getAuthorities()); } } userService.connect(details.getUsername()); }
@Override public void onApplicationEvent(AuthenticationSuccessEvent event) { final UserDetails details = (UserDetails) event.getAuthentication().getPrincipal(); try { UserEntity registeredUser = userService.findBySource(details.getSource(), details.getSourceId(), false); updateRegisteredUser(registeredUser, details); // Principal username is the technical identifier of the user // Dirty hack because spring security is requiring a username... details.setUsername(registeredUser.getId()); } catch (UserNotFoundException unfe) { final NewExternalUserEntity newUser = new NewExternalUserEntity(); newUser.setSource(details.getSource()); newUser.setSourceId(details.getSourceId()); newUser.setFirstname(details.getFirstname()); newUser.setLastname(details.getLastname()); newUser.setEmail(details.getEmail()); boolean addDefaultRole = false; if (event.getAuthentication().getAuthorities() == null || event.getAuthentication().getAuthorities().isEmpty()) { addDefaultRole = true; } UserEntity createdUser = userService.create(newUser, addDefaultRole); // Principal username is the technical identifier of the user details.setUsername(createdUser.getId()); if (!addDefaultRole) { addRole(RoleScope.MANAGEMENT, createdUser.getId(), event.getAuthentication().getAuthorities()); addRole(RoleScope.PORTAL, createdUser.getId(), event.getAuthentication().getAuthorities()); } } userService.connect(details.getUsername()); }
private UserDetails mapUserEntityToUserDetails(UserEntity userEntity) { List<GrantedAuthority> authorities = AuthorityUtils.NO_AUTHORITIES; if (userEntity.getRoles() != null && userEntity.getRoles().size() > 0) { authorities = AuthorityUtils.commaSeparatedStringToAuthorityList( userEntity.getRoles().stream().map(r -> r.getScope().name()+':'+r.getName()).collect(Collectors.joining(",")) ); } io.gravitee.management.idp.api.authentication.UserDetails userDetails = new io.gravitee.management.idp.api.authentication.UserDetails( userEntity.getId(), userEntity.getPassword(), authorities); userDetails.setFirstname(userEntity.getFirstname()); userDetails.setLastname(userEntity.getLastname()); userDetails.setEmail(userEntity.getEmail()); userDetails.setSource(RepositoryIdentityProvider.PROVIDER_TYPE); userDetails.setSourceId(userEntity.getSourceId()); return userDetails; }
private void updateRegisteredUser(UserEntity registeredUser, UserDetails details) { if ((registeredUser.getFirstname() != null && !registeredUser.getFirstname().equals(details.getFirstname())) || (registeredUser.getLastname() != null && !registeredUser.getLastname().equals(details.getLastname())) || (registeredUser.getEmail() != null && !registeredUser.getEmail().equals(details.getEmail()))) { UpdateUserEntity updateUserEntity = new UpdateUserEntity(registeredUser); updateUserEntity.setFirstname(details.getFirstname()); updateUserEntity.setLastname(details.getLastname()); updateUserEntity.setEmail(details.getEmail()); userService.update(registeredUser.getId(), updateUserEntity); } }
private UserDetails mapUserEntityToUserDetails(UserEntity userEntity) { List<GrantedAuthority> authorities = AuthorityUtils.NO_AUTHORITIES; if (userEntity.getRoles() != null && userEntity.getRoles().size() > 0) { authorities = AuthorityUtils.commaSeparatedStringToAuthorityList( userEntity.getRoles().stream().map(r -> r.getScope().name()+':'+r.getName()).collect(Collectors.joining(",")) ); } io.gravitee.management.idp.api.authentication.UserDetails userDetails = new io.gravitee.management.idp.api.authentication.UserDetails( userEntity.getId(), userEntity.getPassword(), authorities); userDetails.setFirstname(userEntity.getFirstname()); userDetails.setLastname(userEntity.getLastname()); userDetails.setEmail(userEntity.getEmail()); userDetails.setSource(RepositoryIdentityProvider.PROVIDER_TYPE); userDetails.setSourceId(userEntity.getSourceId()); return userDetails; }
private void updateRegisteredUser(UserEntity registeredUser, UserDetails details) { if ((registeredUser.getFirstname() != null && !registeredUser.getFirstname().equals(details.getFirstname())) || (registeredUser.getLastname() != null && !registeredUser.getLastname().equals(details.getLastname())) || (registeredUser.getEmail() != null && !registeredUser.getEmail().equals(details.getEmail()))) { UpdateUserEntity updateUserEntity = new UpdateUserEntity(registeredUser); updateUserEntity.setFirstname(details.getFirstname()); updateUserEntity.setLastname(details.getLastname()); updateUserEntity.setEmail(details.getEmail()); userService.update(registeredUser.getId(), updateUserEntity); } }