use of com.epam.reportportal.auth.oauth.UserSynchronizationException in project service-authorization by reportportal.
the class GitHubUserReplicator method replicateUser.
/**
* Replicates GitHub user to internal database (if does NOT exist). Creates personal project for that user
*
* @param userInfo GitHub user to be replicated
* @param gitHubClient Configured github client
* @return Internal User representation
*/
public User replicateUser(UserResource userInfo, GitHubClient gitHubClient) {
String login = normalizeId(userInfo.login);
User user = userRepository.findOne(login);
if (null == user) {
user = new User();
user.setLogin(login);
String email = userInfo.email;
if (Strings.isNullOrEmpty(email)) {
email = gitHubClient.getUserEmails().stream().filter(EmailResource::isVerified).filter(EmailResource::isPrimary).findAny().get().getEmail();
}
email = normalizeId(email);
checkEmail(email);
user.setEmail(email);
if (!Strings.isNullOrEmpty(userInfo.name)) {
user.setFullName(userInfo.name);
}
user.setMetaInfo(defaultMetaInfo());
user.setType(UserType.GITHUB);
user.setRole(UserRole.USER);
Object avatarUrl = userInfo.avatarUrl;
user.setPhotoId(uploadAvatar(gitHubClient, login, avatarUrl));
user.setIsExpired(false);
user.setDefaultProject(generatePersonalProject(user));
userRepository.save(user);
} else if (!UserType.GITHUB.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;
}
use of com.epam.reportportal.auth.oauth.UserSynchronizationException 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