private void createUserIfNeeded(LDAPResource ldap, String cn) throws LdapException { Entry ldapUser = ldap.getUser(cn); String email = ldap.getAttribute(ldapUser, emailAttr); User user = users.findByEmail(email); if (user == null) { String fullName = ldap.getAttribute(ldapUser, nameAttr); if (isNotEmpty(surnameAttr)) { fullName += " " + ldap.getAttribute(ldapUser, surnameAttr); } user = new User(fromTrustedText(fullName.trim()), email); LoginMethod brutalLogin = LoginMethod.brutalLogin(user, email, PLACHOLDER_PASSWORD); user.add(brutalLogin); users.save(user); loginMethods.save(brutalLogin); } // update moderator status // if (isNotEmpty(moderatorGroup) && ldap.getGroups(ldapUser).contains(moderatorGroup)) { // user = user.asModerator(); // } else { // user.removeModerator(); // } // updateAvatarImage(ldap, ldapUser, user); users.save(user); }
/** * Find the email address for a given username * * @param username * @return */ public String getEmail(String username) { try (LDAPResource ldap = new LDAPResource()) { Entry ldapUser = ldap.getUser(userCn(username)); return ldap.getAttribute(ldapUser, emailAttr); } catch (LdapException | IOException e) { logger.debug("LDAP connection error", e); throw new AuthenticationException(LDAP_AUTH, "LDAP connection error", e); } }