private void createRaveUserFromLdapInfo(DirContextOperations ctx, String username) { User newUser = new UserImpl(); newUser.setUsername(username); if (!ctx.attributeExists(mailAttributeName) || StringUtils.isBlank(ctx.getStringAttribute(mailAttributeName))) { throw new RuntimeException("Missing LDAP attribute for email for user " + username); } newUser.setEmail(ctx.getStringAttribute(mailAttributeName)); if (ctx.attributeExists(displayNameAttributeName)) { newUser.setDisplayName(ctx.getStringAttribute(displayNameAttributeName)); } newUser.setPassword(RandomStringUtils.random(16)); newUser.setDefaultPageLayoutCode(pageLayoutCode); try { newAccountService.createNewAccount(newUser); } catch (Exception e) { throw new RuntimeException("Could not bind LDAP username '{" + username + "}' to a user", e); } }
@Override public UserOrg doMapFromContext(final DirContextOperations context) { final UserOrg user = new UserOrg(); user.setDn(context.getDn().toString()); user.setLastName(context.getStringAttribute(SN_ATTRIBUTE)); user.setFirstName(context.getStringAttribute(GIVEN_NAME_ATTRIBUTE)); user.setSecured(context.getObjectAttribute(PASSWORD_ATTRIBUTE) != null); user.setId(Normalizer.normalize(context.getStringAttribute(uidAttribute))); // Special and also optional attributes Optional.ofNullable(departmentAttribute).ifPresent(a -> user.setDepartment(context.getStringAttribute(a))); Optional.ofNullable(localIdAttribute).ifPresent(a -> user.setLocalId(context.getStringAttribute(a))); Optional.ofNullable(lockedAttribute).ifPresent(a -> fillLockedData(user, context.getStringAttribute(a))); // Save the normalized CN of the company user.setCompany(toCompany(user.getDn())); if (context.attributeExists(PWD_ACCOUNT_LOCKED_ATTRIBUTE)) { user.setLockedBy(PPOLICY_NAME); user.setLocked(parseLdapDate(context.getStringAttribute(PWD_ACCOUNT_LOCKED_ATTRIBUTE))); } // Save the mails user.setMails( new ArrayList<>(CollectionUtils.emptyIfNull(context.getAttributeSortedStringSet(MAIL_ATTRIBUTE)))); return user; }