use of com.epam.reportportal.auth.store.entity.ldap.SynchronizationAttributes in project service-authorization by reportportal.
the class LdapUserReplicator method replicateUser.
/**
* Replicates LDAP user to internal database (if does NOT exist). Creates personal project for that user
*
* @param name Username
* @param ctx LDAP context
* @param attributes Synchronization Attributes
* @return Internal User representation
*/
public User replicateUser(String name, DirContextOperations ctx, SynchronizationAttributes attributes) {
String email = (String) ctx.getObjectAttribute(attributes.getEmail());
if (isNullOrEmpty(email)) {
throw new UserSynchronizationException("Email not provided");
}
email = normalizeId(email);
String login = normalizeId(name);
User user = userRepository.findOne(login);
if (null == user) {
User newUser = new User();
newUser.setLogin(login);
ofNullable(attributes.getFullName()).flatMap(it -> ofNullable(ctx.getStringAttribute(it))).ifPresent(newUser::setFullName);
ofNullable(attributes.getPhoto()).flatMap(it -> ofNullable(ctx.getObjectAttribute(it))).filter(photo -> photo instanceof byte[]).map(photo -> (byte[]) photo).ifPresent(photo -> newUser.setPhotoId(uploadPhoto(login, photo)));
checkEmail(email);
newUser.setEmail(email);
newUser.setMetaInfo(defaultMetaInfo());
newUser.setType(UserType.LDAP);
newUser.setRole(UserRole.USER);
newUser.setIsExpired(false);
newUser.setDefaultProject(generatePersonalProject(newUser));
userRepository.save(newUser);
user = newUser;
} else if (!UserType.LDAP.equals(user.getType())) {
// if user with such login exists, but it's not GitHub user than throw an exception
throw new UserSynchronizationException("User with login '" + user.getId() + "' already exists");
}
return user;
}
Aggregations