use of de.tum.in.www1.artemis.exception.VersionControlException in project Artemis by ls1intum.
the class BitbucketUserManagementService method createVcsUser.
/**
* Creates a new user in the VCS based on a local Artemis user. Should be called if Artemis handles user creation
* and management
*
* @param user The local Artemis user, which will be available in the VCS after invoking this method
*/
@Override
public void createVcsUser(User user) throws VersionControlException {
if (!user.isInternal()) {
return;
}
if (bitbucketService.userExists(user.getLogin())) {
log.debug("Bitbucket user {} already exists", user.getLogin());
return;
}
log.debug("Bitbucket user {} does not exist yet", user.getLogin());
String displayName = user.getName().trim();
bitbucketService.createUser(user.getLogin(), passwordService.decryptPasswordByLogin(user.getLogin()).get(), user.getEmail(), displayName);
try {
// NOTE: we need to fetch the user here again to make sure that the groups are not lazy loaded.
User repoUser = userRepository.getUserWithGroupsAndAuthorities(user.getLogin());
bitbucketService.addUserToGroups(repoUser.getLogin(), repoUser.getGroups());
} catch (BitbucketException e) {
/*
* This might throw exceptions, for example if the group does not exist on Bitbucket. We can safely ignore them.
*/
}
}
use of de.tum.in.www1.artemis.exception.VersionControlException in project ArTEMiS by ls1intum.
the class BitbucketUserManagementService method createVcsUser.
/**
* Creates a new user in the VCS based on a local Artemis user. Should be called if Artemis handles user creation
* and management
*
* @param user The local Artemis user, which will be available in the VCS after invoking this method
* @param password the password of the user to be set
*/
@Override
public void createVcsUser(User user, String password) throws VersionControlException {
if (!user.isInternal()) {
return;
}
if (bitbucketService.userExists(user.getLogin())) {
log.debug("Bitbucket user {} already exists", user.getLogin());
return;
}
log.debug("Bitbucket user {} does not exist yet", user.getLogin());
String displayName = user.getName() != null ? user.getName().trim() : null;
bitbucketService.createUser(user.getLogin(), password, user.getEmail(), displayName);
try {
// NOTE: we need to fetch the user here again to make sure that the groups are not lazy loaded.
User repoUser = userRepository.getUserWithGroupsAndAuthorities(user.getLogin());
bitbucketService.addUserToGroups(repoUser.getLogin(), repoUser.getGroups());
} catch (BitbucketException e) {
/*
* This might throw exceptions, for example if the group does not exist on Bitbucket. We can safely ignore them.
*/
}
}
use of de.tum.in.www1.artemis.exception.VersionControlException in project Artemis by ls1intum.
the class BitbucketUserManagementService method createVcsUser.
/**
* Creates a new user in the VCS based on a local Artemis user. Should be called if Artemis handles user creation
* and management
*
* @param user The local Artemis user, which will be available in the VCS after invoking this method
* @param password the password of the user to be set
*/
@Override
public void createVcsUser(User user, String password) throws VersionControlException {
if (!user.isInternal()) {
return;
}
if (bitbucketService.userExists(user.getLogin())) {
log.debug("Bitbucket user {} already exists", user.getLogin());
return;
}
log.debug("Bitbucket user {} does not exist yet", user.getLogin());
String displayName = user.getName() != null ? user.getName().trim() : null;
bitbucketService.createUser(user.getLogin(), password, user.getEmail(), displayName);
try {
// NOTE: we need to fetch the user here again to make sure that the groups are not lazy loaded.
User repoUser = userRepository.getUserWithGroupsAndAuthorities(user.getLogin());
bitbucketService.addUserToGroups(repoUser.getLogin(), repoUser.getGroups());
} catch (BitbucketException e) {
/*
* This might throw exceptions, for example if the group does not exist on Bitbucket. We can safely ignore them.
*/
}
}
use of de.tum.in.www1.artemis.exception.VersionControlException in project ArTEMiS by ls1intum.
the class UserService method registerUser.
/**
* Register user and create it only in the internal Artemis database. This is a pure service method without any logic with respect to external systems.
*
* @param userDTO user data transfer object
* @param password string
* @return newly registered user or throw registration exception
*/
public User registerUser(UserDTO userDTO, String password) {
// Prepare the new user object.
final var newUser = new User();
String passwordHash = passwordService.hashPassword(password);
newUser.setLogin(userDTO.getLogin().toLowerCase());
// new user gets initially a generated password
newUser.setPassword(passwordHash);
newUser.setFirstName(userDTO.getFirstName());
newUser.setLastName(userDTO.getLastName());
newUser.setEmail(userDTO.getEmail().toLowerCase());
newUser.setImageUrl(userDTO.getImageUrl());
newUser.setLangKey(userDTO.getLangKey());
// new user is not active
newUser.setActivated(false);
// registered users are always internal
newUser.setInternal(true);
// new user gets registration key
newUser.setActivationKey(RandomUtil.generateActivationKey());
Set<Authority> authorities = new HashSet<>();
authorityRepository.findById(STUDENT.getAuthority()).ifPresent(authorities::add);
newUser.setAuthorities(authorities);
// Find user that has the same login
Optional<User> optionalExistingUser = userRepository.findOneWithGroupsByLogin(userDTO.getLogin().toLowerCase());
if (optionalExistingUser.isPresent()) {
User existingUser = optionalExistingUser.get();
return handleRegisterUserWithSameLoginAsExistingUser(newUser, existingUser, password);
}
// Find user that has the same email
optionalExistingUser = userRepository.findOneWithGroupsByEmailIgnoreCase(userDTO.getEmail());
if (optionalExistingUser.isPresent()) {
User existingUser = optionalExistingUser.get();
// An account with the same login is already activated.
if (existingUser.getActivated()) {
throw new EmailAlreadyUsedException();
}
// account with a different email. Block this.
throw new AccountRegistrationBlockedException(newUser.getEmail());
}
// we need to save first so that the user can be found in the database in the subsequent method
User savedNonActivatedUser = saveUser(newUser);
// Create an account on the VCS. If it fails, abort registration.
optionalVcsUserManagementService.ifPresent(vcsUserManagementService -> {
try {
vcsUserManagementService.createVcsUser(savedNonActivatedUser, password);
vcsUserManagementService.deactivateUser(savedNonActivatedUser.getLogin());
} catch (VersionControlException e) {
log.error("An error occurred while registering GitLab user " + savedNonActivatedUser.getLogin() + ":", e);
deleteUser(savedNonActivatedUser);
throw e;
}
});
// Automatically remove the user if it wasn't activated after a certain amount of time.
instanceMessageSendService.sendRemoveNonActivatedUserSchedule(savedNonActivatedUser.getId());
log.debug("Created Information for User: {}", newUser);
return newUser;
}
use of de.tum.in.www1.artemis.exception.VersionControlException in project Artemis by ls1intum.
the class UserService method registerUser.
/**
* Register user and create it only in the internal Artemis database. This is a pure service method without any logic with respect to external systems.
*
* @param userDTO user data transfer object
* @param password string
* @return newly registered user or throw registration exception
*/
public User registerUser(UserDTO userDTO, String password) {
// Prepare the new user object.
final var newUser = new User();
String passwordHash = passwordService.hashPassword(password);
newUser.setLogin(userDTO.getLogin().toLowerCase());
// new user gets initially a generated password
newUser.setPassword(passwordHash);
newUser.setFirstName(userDTO.getFirstName());
newUser.setLastName(userDTO.getLastName());
newUser.setEmail(userDTO.getEmail().toLowerCase());
newUser.setImageUrl(userDTO.getImageUrl());
newUser.setLangKey(userDTO.getLangKey());
// new user is not active
newUser.setActivated(false);
// registered users are always internal
newUser.setInternal(true);
// new user gets registration key
newUser.setActivationKey(RandomUtil.generateActivationKey());
Set<Authority> authorities = new HashSet<>();
authorityRepository.findById(STUDENT.getAuthority()).ifPresent(authorities::add);
newUser.setAuthorities(authorities);
// Find user that has the same login
Optional<User> optionalExistingUser = userRepository.findOneWithGroupsByLogin(userDTO.getLogin().toLowerCase());
if (optionalExistingUser.isPresent()) {
User existingUser = optionalExistingUser.get();
return handleRegisterUserWithSameLoginAsExistingUser(newUser, existingUser, password);
}
// Find user that has the same email
optionalExistingUser = userRepository.findOneWithGroupsByEmailIgnoreCase(userDTO.getEmail());
if (optionalExistingUser.isPresent()) {
User existingUser = optionalExistingUser.get();
// An account with the same login is already activated.
if (existingUser.getActivated()) {
throw new EmailAlreadyUsedException();
}
// account with a different email. Block this.
throw new AccountRegistrationBlockedException(newUser.getEmail());
}
// we need to save first so that the user can be found in the database in the subsequent method
User savedNonActivatedUser = saveUser(newUser);
// Create an account on the VCS. If it fails, abort registration.
optionalVcsUserManagementService.ifPresent(vcsUserManagementService -> {
try {
vcsUserManagementService.createVcsUser(savedNonActivatedUser, password);
vcsUserManagementService.deactivateUser(savedNonActivatedUser.getLogin());
} catch (VersionControlException e) {
log.error("An error occurred while registering GitLab user " + savedNonActivatedUser.getLogin() + ":", e);
deleteUser(savedNonActivatedUser);
throw e;
}
});
// Automatically remove the user if it wasn't activated after a certain amount of time.
instanceMessageSendService.sendRemoveNonActivatedUserSchedule(savedNonActivatedUser.getId());
log.debug("Created Information for User: {}", newUser);
return newUser;
}
Aggregations