String[] reqAttrs = LDAPHelper.checkReqAttr(userAttributes);
if (reqAttrs != null) {
log.warn("Can not create and persist user, the following attributes are missing::" + ArrayUtils.toString(reqAttrs));
return;
}
Manager securityManager = ManagerFactory.getManager();
String uid = LDAPHelper.getAttributeValue(userAttributes.get(LDAPHelper
.mapOlatPropertyToLdapAttribute(LDAPConstants.LDAP_USER_IDENTIFYER)));
String email = LDAPHelper.getAttributeValue(userAttributes.get(LDAPHelper.mapOlatPropertyToLdapAttribute(UserConstants.EMAIL)));
// Lookup user
if (securityManager.findIdentityByName(uid) != null) {
log.error("Can't create user with username='" + uid + "', does already exist in OLAT database");
return;
}
// Create User (first and lastname is added in next step)
User user = UserManager.getInstance().createUser(null, null, email);
// Set User Property's (Iterates over Attributes and gets OLAT Property out
// of olatexconfig.xml)
NamingEnumeration<Attribute> neAttr = (NamingEnumeration<Attribute>) userAttributes.getAll();
try {
while (neAttr.hasMore()) {
Attribute attr = neAttr.next();
String olatProperty = LDAPHelper.mapLdapAttributeToOlatProperty(attr.getID());
if (attr.get() != uid) {
String ldapValue = LDAPHelper.getAttributeValue(attr);
if (olatProperty == null || ldapValue == null) continue;
user.setProperty(olatProperty, ldapValue);
}
}
// Add static user properties from the configuration
Map<String, String> staticProperties = LDAPLoginModule.getStaticUserProperties();
if (staticProperties != null && staticProperties.size() > 0) {
for (Entry<String, String> staticProperty : staticProperties.entrySet()) {
user.setProperty(staticProperty.getKey(), staticProperty.getValue());
}
}
} catch (NamingException e) {
log.error("NamingException when trying to create and persist LDAP user with username::" + uid, e);
return;
} catch (Exception e) {
// catch any exception here to properly log error
log.error("Unknown exception when trying to create and persist LDAP user with username::" + uid, e);
return;
}
// Create Identity
Identity identity = securityManager.createAndPersistIdentityAndUser(uid, user, LDAPAuthenticationController.PROVIDER_LDAP, uid, null);
// Add to SecurityGroup LDAP
SecurityGroup secGroup = securityManager.findSecurityGroupByName(LDAPConstants.SECURITY_GROUP_LDAP);
securityManager.addIdentityToSecurityGroup(identity, secGroup);
// Add to SecurityGroup OLATUSERS
secGroup = securityManager.findSecurityGroupByName(Constants.GROUP_OLATUSERS);
securityManager.addIdentityToSecurityGroup(identity, secGroup);
log.info("Created LDAP user username::" + uid);
}