use of org.olat.user.UserImpl in project openolat by klemens.
the class SetupModule method createUser.
/**
* Method to create a user with the given configuration
*
* @return Identity or null
*/
protected Identity createUser(DefaultUser user) {
Identity identity;
identity = securityManager.findIdentityByName(user.getUserName());
if (identity == null) {
// Create new user and subject
UserImpl newUser = new UserImpl();
newUser.setFirstName(user.getFirstName());
newUser.setLastName(user.getLastName());
newUser.setEmail(user.getEmail());
newUser.getPreferences().setLanguage(user.getLanguage());
newUser.getPreferences().setInformSessionTimeout(true);
if (!StringUtils.hasText(authenticationProviderConstant)) {
throw new OLATRuntimeException(this.getClass(), "Auth token not set! Please fix! " + authenticationProviderConstant, null);
}
// Now finally create that user thing on the database with all
// credentials, person etc. in one transation context!
identity = securityManager.createAndPersistIdentityAndUser(user.getUserName(), null, newUser, authenticationProviderConstant, user.getUserName(), user.getPassword());
if (identity == null) {
throw new OLATRuntimeException(this.getClass(), "Error, could not create user and subject with name " + user.getUserName(), null);
} else {
if (user.isGuest()) {
SecurityGroup anonymousGroup = securityManager.findSecurityGroupByName(Constants.GROUP_ANONYMOUS);
securityManager.addIdentityToSecurityGroup(identity, anonymousGroup);
log.info("Created anonymous user " + user.getUserName());
} else {
SecurityGroup olatuserGroup = securityManager.findSecurityGroupByName(Constants.GROUP_OLATUSERS);
if (user.isAdmin()) {
SecurityGroup adminGroup = securityManager.findSecurityGroupByName(Constants.GROUP_ADMIN);
securityManager.addIdentityToSecurityGroup(identity, adminGroup);
securityManager.addIdentityToSecurityGroup(identity, olatuserGroup);
log.info("Created admin user " + user.getUserName());
} else if (user.isAuthor()) {
SecurityGroup authorGroup = securityManager.findSecurityGroupByName(Constants.GROUP_AUTHORS);
securityManager.addIdentityToSecurityGroup(identity, authorGroup);
securityManager.addIdentityToSecurityGroup(identity, olatuserGroup);
log.info("Created author user " + user.getUserName());
} else if (user.isUserManager()) {
SecurityGroup usermanagerGroup = securityManager.findSecurityGroupByName(Constants.GROUP_USERMANAGERS);
securityManager.addIdentityToSecurityGroup(identity, usermanagerGroup);
securityManager.addIdentityToSecurityGroup(identity, olatuserGroup);
log.info("Created userManager user " + user.getUserName());
} else if (user.isGroupManager()) {
SecurityGroup groupmanagerGroup = securityManager.findSecurityGroupByName(Constants.GROUP_GROUPMANAGERS);
securityManager.addIdentityToSecurityGroup(identity, groupmanagerGroup);
securityManager.addIdentityToSecurityGroup(identity, olatuserGroup);
log.info("Created groupManager user " + user.getUserName());
} else {
securityManager.addIdentityToSecurityGroup(identity, olatuserGroup);
log.info("Created user " + user.getUserName());
}
}
}
}
return identity;
}
Aggregations