Verwaltung von LDAP Accounts und Gruppen
Peter Hormanns
2020-03-06 0901802b61f059f35239f9488d2fa0aa869a1c5b
add external email address
17 files modified
164 ■■■■ changed files
src/main/java/de/jalin/ldapadmin/beans/User.java 39 ●●●● patch | view | raw | blame | history
src/main/java/de/jalin/ldapadmin/ldap/LDAPConfig.java 7 ●●●●● patch | view | raw | blame | history
src/main/java/de/jalin/ldapadmin/ldap/UsersDAO.java 63 ●●●● patch | view | raw | blame | history
src/main/java/de/jalin/ldapadmin/web/ResetPasswordServlet.java 6 ●●●● patch | view | raw | blame | history
src/main/java/de/jalin/ldapadmin/web/UserServlet.java 14 ●●●●● patch | view | raw | blame | history
src/main/resources/config.properties 1 ●●●● patch | view | raw | blame | history
src/main/resources/de/jalin/ldapadmin/users.properties 1 ●●●● patch | view | raw | blame | history
src/main/resources/de/jalin/ldapadmin/users_de.properties 1 ●●●● patch | view | raw | blame | history
src/main/resources/de/jalin/ldapadmin/users_en.properties 1 ●●●● patch | view | raw | blame | history
src/main/webapp/user.jsp 10 ●●●●● patch | view | raw | blame | history
src/test/java/de/jalin/ldapadmin/ldap/TestCreateGroup.java 2 ●●● patch | view | raw | blame | history
src/test/java/de/jalin/ldapadmin/ldap/TestCreateUser.java 5 ●●●● patch | view | raw | blame | history
src/test/java/de/jalin/ldapadmin/ldap/TestDeleteUser.java 2 ●●● patch | view | raw | blame | history
src/test/java/de/jalin/ldapadmin/ldap/TestReadUser.java 4 ●●●● patch | view | raw | blame | history
src/test/java/de/jalin/ldapadmin/ldap/TestUpdateAsBindUser.java 2 ●●● patch | view | raw | blame | history
src/test/java/de/jalin/ldapadmin/ldap/TestUpdateAsSimpleUser.java 4 ●●●● patch | view | raw | blame | history
src/test/java/de/jalin/ldapadmin/ldap/TestUpdateUser.java 2 ●●● patch | view | raw | blame | history
src/main/java/de/jalin/ldapadmin/beans/User.java
@@ -4,12 +4,14 @@
import java.util.ArrayList;
import java.util.List;
import de.jalin.ldapadmin.ldap.LDAPConfig;
import de.jalin.ldapadmin.ldap.PasswordValidator;
import de.jalin.ldapadmin.ldap.SimplePasswordException;
public class User implements Serializable, LDAPBean {
    private static final long serialVersionUID = 1L;
    private static final String INTERNAL_EMAILDOMAINS = LDAPConfig.getConfig().getSmtpInternalDomains();
    private String dn;
    private String login;
@@ -17,7 +19,8 @@
    private String firstname;
    private String lastname;
    private String displayname;
    private String email;
    private String emailInternal;
    private String emailExternal;
    private String phone;
    private String mobile;
    private List<String> groups;
@@ -77,12 +80,36 @@
        this.displayname = displayname;
    }
    public String getEmail() {
        return email;
    public String getEmailInternal() {
        return emailInternal;
    }
    public void setEmail(String email) {
        this.email = email;
    public void setEmailInternal(String email) {
        this.emailInternal = email;
    }
    public String getEmailExternal() {
        return emailExternal;
    }
    public void setEmailAddresses(List<String> emails) {
        if (emails != null) {
            for (final String email : emails) {
                final String[] parts = email.split("@");
                if (parts.length == 2) {
                    final String emailDomain = parts[1];
                    if (INTERNAL_EMAILDOMAINS.contains(emailDomain)) {
                        setEmailInternal(email);
                    } else {
                        setEmailExternal(email);
                    }
                }
            }
        }
    }
    public void setEmailExternal(String email) {
        this.emailExternal = email;
    }
    public String getPhone() {
@@ -111,7 +138,7 @@
    @Override
    public String toString() {
        return getFirstname() + " " + getLastname() + " (" + getLogin() + ", " + getEmail() + ")";
        return getFirstname() + " " + getLastname() + " (" + getLogin() + ", " + getEmailExternal() + ")";
    }
    @Override
src/main/java/de/jalin/ldapadmin/ldap/LDAPConfig.java
@@ -22,6 +22,7 @@
    private String smtpFromAddress;
    private String smtpHost;
    private String smtpPort;
    private String smtpInternalDomains;
    private String tempDir;
    private LDAPConfig() {
@@ -36,6 +37,7 @@
        smtpHost = "localhost";
        smtpPort = "25";
        smtpFromAddress = "nobody@example.com";
        smtpInternalDomains = "example.com,example.org";
        tempDir = System.getProperty("java.io.tmpdir");
        try {
            final Properties props = loadConfig();
@@ -46,6 +48,7 @@
            smtpHost = props.getProperty("smtp.host", smtpHost);
            smtpPort = props.getProperty("smtp.port", smtpPort);
            smtpFromAddress = props.getProperty("smtp.from", smtpFromAddress);
            smtpInternalDomains = props.getProperty("smtp.internal", smtpInternalDomains);
            tempDir = props.getProperty("temp.dir", tempDir);
            final LDAPUriParser uriParser = new LDAPUriParser(ldapProviderUrl);
            ldapDistinguishedName = uriParser.getDn();
@@ -108,6 +111,10 @@
        return smtpFromAddress;
    }
    public String getSmtpInternalDomains() {
        return smtpInternalDomains;
    }
    public String getTempDir() {
        return tempDir;
    }
src/main/java/de/jalin/ldapadmin/ldap/UsersDAO.java
@@ -30,7 +30,7 @@
            final User usr = new User();
            usr.setFirstname(session.getStringValue(attribs, "givenName"));
            usr.setLastname(session.getStringValue(attribs, "sn"));
            usr.setEmail(session.getStringValue(attribs, "mail"));
            usr.setEmailAddresses(session.getListOfValues(attribs, "mail"));
            usr.setLogin(session.getStringValue(attribs, "uid"));
            usr.setPhone(session.getStringValue(attribs, "telephoneNumber"));
            usr.setMobile(session.getStringValue(attribs, "mobile"));
@@ -56,19 +56,25 @@
        objClass.add("organizationalPerson");
        objClass.add("inetOrgPerson");
        attributes.put(objClass);
        final String firstName = usr.getFirstname();
        if (hasValue(firstName)) {
            attributes.put("givenName", firstName);
        final String firstname = usr.getFirstname();
        if (hasValue(firstname)) {
            attributes.put("givenName", firstname);
        }
        final String lastname = usr.getLastname();
        if (!hasValue(lastname)) {
            throw new RequiredAttributeException("lastname");
        }
        attributes.put("sn", lastname);
        final String email = usr.getEmail();
        if (hasValue(email)) {
            attributes.put("mail", email);
        final BasicAttribute mail = new BasicAttribute("mail");
        final String emailInternal = usr.getEmailInternal();
        if (hasValue(emailInternal)) {
            mail.add(emailInternal);
        }
        final String emailExternal = usr.getEmailExternal();
        if (hasValue(emailExternal)) {
            mail.add(emailExternal);
        }
        attributes.put(mail);
        attributes.put("uid", uid);
        attributes.put("cn", uid);
        final String telephone = usr.getPhone();
@@ -83,7 +89,7 @@
        if (hasValue(comment)) {
            attributes.put("displayName", comment);
        } else {
            attributes.put("displayName", firstName == null ? lastname : lastname + ", " + firstName);
            attributes.put("displayName", firstname == null ? lastname : firstname + " " + lastname);
        }
        final String password = usr.getPassword();
        if (!hasValue(password)) {
@@ -99,7 +105,7 @@
        final User usr = new User();
        usr.setFirstname(session.getStringValue(attribs, "givenName"));
        usr.setLastname(session.getStringValue(attribs, "sn"));
        usr.setEmail(session.getStringValue(attribs, "mail"));
        usr.setEmailAddresses(session.getListOfValues(attribs, "mail"));
        usr.setLogin(session.getStringValue(attribs, "uid"));
        usr.setPhone(session.getStringValue(attribs, "telephoneNumber"));
        usr.setMobile(session.getStringValue(attribs, "mobile"));
@@ -117,7 +123,16 @@
            "uid=${uid},ou=users".replace("${uid}", uid));
        final List<ModificationItem> updates = new ArrayList<ModificationItem>();
        addStringAttrUpdate(updates, attribs, "displayName", usr.getDisplayname());
        addStringAttrUpdate(updates, attribs, "mail", usr.getEmail());
        final ArrayList<String> emailAdressList = new ArrayList<String>();
        final String emailInternal = usr.getEmailInternal();
        if (hasValue(emailInternal)) {
            emailAdressList.add(emailInternal);
        }
        final String emailExternal = usr.getEmailExternal();
        if (hasValue(emailExternal)) {
            emailAdressList.add(emailExternal);
        }
        addMultiValueAttrUpdate(updates, attribs, "mail", emailAdressList);
        addStringAttrUpdate(updates, attribs, "givenName", usr.getFirstname());
        addStringAttrUpdate(updates, attribs, "sn", usr.getLastname());
        addStringAttrUpdate(updates, attribs, "mobile", usr.getMobile());
@@ -131,8 +146,12 @@
        session.unbind("uid=${id},ou=users", user.getLogin());
    }
    private boolean hasValue(final String email) {
        return email != null && !email.isEmpty();
    private boolean hasValue(final String param) {
        return param != null && !param.isEmpty();
    }
    private boolean hasValue(final List<String> param) {
        return param != null && !param.isEmpty();
    }
    private void addStringAttrUpdate(final List<ModificationItem> updates,
@@ -156,6 +175,26 @@
        }
    }
    private void addMultiValueAttrUpdate(final List<ModificationItem> updates,
            final Attributes attribs, final String attributeName, final List<String> newValues) throws LDAPSessionException {
        final List<String> oldValues = session.getListOfValues(attribs, attributeName);
        final BasicAttribute basicAttribute = new BasicAttribute(attributeName);
        if (hasValue(newValues)) {
            for (final String val : newValues) {
                basicAttribute.add(val);
            }
            int ldapOp = DirContext.REPLACE_ATTRIBUTE;
            if (!hasValue(oldValues)) {
                ldapOp = DirContext.ADD_ATTRIBUTE;
            }
            updates.add(new ModificationItem(ldapOp, basicAttribute));
        } else {
            if (hasValue(oldValues)) {
                updates.add(new ModificationItem(DirContext.REMOVE_ATTRIBUTE, basicAttribute));
            }
        }
    }
    private void addPasswordUpdate(final List<ModificationItem> updates,
            final String attributeName, final String newValue) throws LDAPSessionException {
        if (hasValue(newValue)) {
src/main/java/de/jalin/ldapadmin/web/ResetPasswordServlet.java
@@ -126,9 +126,9 @@
                    final SortedMap<String, User> usersMap = usrDAO.loadUsers();
                    final Collection<User> allUsers = usersMap.values();
                    for (User usr : allUsers) {
                        if (usr.getEmail() != null && usr.getEmail().equalsIgnoreCase(loginOrEMail)) {
                        if (usr.getEmailInternal() != null && usr.getEmailInternal().equalsIgnoreCase(loginOrEMail)) {
                            login = usr.getLogin();
                            email = usr.getEmail();
                            email = usr.getEmailInternal();
                            salutation = usr.getFirstname() + " " + usr.getLastname();
                        }
                    }
@@ -136,7 +136,7 @@
                    final User usr = usrDAO.read("uid=" + loginOrEMail + ",ou=users,");
                    if (usr != null) {
                        login = usr.getLogin();
                        email = usr.getEmail();
                        email = usr.getEmailInternal();
                        salutation = usr.getFirstname() + " " + usr.getLastname();
                    }
                }
src/main/java/de/jalin/ldapadmin/web/UserServlet.java
@@ -65,7 +65,7 @@
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
    req.setCharacterEncoding("UTF-8");
        req.setCharacterEncoding("UTF-8");
        final Messages messages = new Messages(req.getLocale());
        final HttpSession httpSession = req.getSession();
        cleanSession(httpSession);
@@ -79,10 +79,12 @@
            operation = "none";
        }
        final UsersDAO usersDAO = new UsersDAO(ldapSession);
        final String login = req.getParameter("login");
        final String parLogin = req.getParameter("login");
        final String login = parLogin == null ? null : parLogin.toLowerCase();
        final String firstname = req.getParameter("firstname");
        final String lastname = req.getParameter("lastname");
        final String email = req.getParameter("email");
        final String emailExt = req.getParameter("extemail");
        final String phone = req.getParameter("phone");
        final String mobile = req.getParameter("mobile");
        final String password = req.getParameter("password");
@@ -94,12 +96,12 @@
        usr.setLogin(login);
        usr.setFirstname(firstname);
        usr.setLastname(lastname);
        usr.setDisplayname(lastname + ", " + firstname);
        usr.setEmail(email);
        usr.setDisplayname(firstname + " " + lastname);
        usr.setEmailInternal(email);
        usr.setEmailExternal(emailExt);
        usr.setPhone(phone);
        usr.setMobile(mobile);
        final List<String> memberships;
        memberships = new ArrayList<>();
        final List<String> memberships = new ArrayList<>();
        @SuppressWarnings("unchecked")
        final SortedMap<String, Group> groupsHash = (SortedMap<String, Group>) httpSession.getAttribute("groups");
        final Iterator<String> groupDNIterator = groupsHash.keySet().iterator();
src/main/resources/config.properties
@@ -3,3 +3,4 @@
security.password=streng-geheim
smtp.host=localhost
smtp.from=nobody@example.com
smtp.internal=example.com,example.org
src/main/resources/de/jalin/ldapadmin/users.properties
@@ -9,6 +9,7 @@
users.label.firstname=First name
users.label.lastname=Last name
users.label.email=EMail address
users.label.extemail=external EMail address
users.label.phone=Phone
users.label.mobile=Mobile
users.label.password=Password
src/main/resources/de/jalin/ldapadmin/users_de.properties
@@ -9,6 +9,7 @@
users.label.firstname=Vorname
users.label.lastname=Nachname
users.label.email=E-Mail Adresse
users.label.extemail=E-Mail Adresse (extern)
users.label.phone=Telefon
users.label.mobile=Handy
users.label.password=Passwort
src/main/resources/de/jalin/ldapadmin/users_en.properties
@@ -9,6 +9,7 @@
users.label.firstname=First name
users.label.lastname=Last name
users.label.email=EMail address
users.label.extemail=external EMail address
users.label.phone=Phone
users.label.mobile=Mobile
users.label.password=Password
src/main/webapp/user.jsp
@@ -84,7 +84,15 @@
                            <div class="col-sm-9">
                                <input
                                    type="email" class="form-control" id="email" name="email"
                                    value="${user.email}" placeholder="Enter email">
                                    value="${user.emailInternal}" placeholder="Enter email">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="extemail" class="col-sm-3 control-label"><fmt:message key="users.label.extemail"/></label>
                            <div class="col-sm-9">
                                <input
                                    type="email" class="form-control" id="extemail" name="extemail"
                                    value="${user.emailExternal}" placeholder="Enter email">
                            </div>
                        </div>
                        <div class="form-group">
src/test/java/de/jalin/ldapadmin/ldap/TestCreateGroup.java
@@ -61,7 +61,7 @@
        final User newUser = new User();
        newUser.setDn(USERS_DN.replace("${uid}", uid));
        newUser.setDisplayname(fn + " " + ln);
        newUser.setEmail(fn.toLowerCase() + "." + ln.toLowerCase() + "@example.com");
        newUser.setEmailInternal(fn.toLowerCase() + "." + ln.toLowerCase() + "@example.com");
        newUser.setFirstname(fn);
        newUser.setLastname(ln);
        newUser.setLogin(uid);
src/test/java/de/jalin/ldapadmin/ldap/TestCreateUser.java
@@ -40,7 +40,8 @@
            final User newUser = new User();
            newUser.setDn(USERS_DN);
            newUser.setDisplayname("Peter Petersen");
            newUser.setEmail("peter.petersen@example.com");
            newUser.setEmailInternal("peter.petersen@example.com");
            newUser.setEmailExternal("peter.petersen@external.com");
            newUser.setFirstname("Peter");
            newUser.setLastname("Petersen");
            newUser.setLogin("pet");
@@ -50,6 +51,8 @@
            dao.create(newUser);
            final User createdUser = dao.loadUsers().get(USERS_DN);
            assertNotNull("should exist", createdUser);
            assertTrue("peter.petersen@example.com".equals(createdUser.getEmailInternal()));
            assertTrue("peter.petersen@external.com".equals(createdUser.getEmailExternal()));
        } catch (LDAPSessionException | RequiredAttributeException | AlreadyBoundException e) {
            fail(e.getMessage());
        }
src/test/java/de/jalin/ldapadmin/ldap/TestDeleteUser.java
@@ -40,7 +40,7 @@
            final User newUser = new User();
            newUser.setDn(USERS_DN);
            newUser.setDisplayname("Hein Hanssen");
            newUser.setEmail("hein.hanssen@example.com");
            newUser.setEmailInternal("hein.hanssen@example.com");
            newUser.setFirstname("Hein");
            newUser.setLastname("Hanssen");
            newUser.setLogin("hei");
src/test/java/de/jalin/ldapadmin/ldap/TestReadUser.java
@@ -40,7 +40,7 @@
            final User newUser = new User();
            newUser.setDn(USERS_DN);
            newUser.setDisplayname("Chris Christansen");
            newUser.setEmail("chris.christansen@example.com");
            newUser.setEmailInternal("chris.christansen@example.com");
            newUser.setFirstname("Chris");
            newUser.setLastname("Christansen");
            newUser.setLogin("chr");
@@ -52,7 +52,7 @@
            assertNotNull("should exist", createdUser);
            final User readUser = dao.read(USERS_DN);
            assertNotNull(readUser);
            assertTrue("chris.christansen@example.com".equals(readUser.getEmail()));
            assertTrue("chris.christansen@example.com".equals(readUser.getEmailInternal()));
        } catch (LDAPSessionException | RequiredAttributeException | AlreadyBoundException e) {
            fail(e.getMessage());
        }
src/test/java/de/jalin/ldapadmin/ldap/TestUpdateAsBindUser.java
@@ -48,7 +48,7 @@
            final User newUser = new User();
            newUser.setDn(USERS_DN);
            newUser.setDisplayname("Paul Paulsen");
            newUser.setEmail("paul.paulsen@example.com");
            newUser.setEmailInternal("paul.paulsen@example.com");
            newUser.setFirstname("Paul");
            newUser.setLastname("Pausen");
            newUser.setLogin("pau");
src/test/java/de/jalin/ldapadmin/ldap/TestUpdateAsSimpleUser.java
@@ -31,7 +31,7 @@
        final User newUser = new User();
        newUser.setDn(USERS_DN);
        newUser.setDisplayname("Paul Petersen");
        newUser.setEmail("paul.petersen@example.com");
        newUser.setEmailInternal("paul.petersen@example.com");
        newUser.setFirstname("Paul");
        newUser.setLastname("Petersen");
        newUser.setLogin("plp");
@@ -61,7 +61,7 @@
            final User newUser = new User();
            newUser.setDn(USERS_DN);
            newUser.setDisplayname("Micha Michaelsen");
            newUser.setEmail("micha.michaelsen@example.com");
            newUser.setEmailInternal("micha.michaelsen@example.com");
            newUser.setFirstname("Michael");
            newUser.setLastname("Michaelsen");
            newUser.setLogin("mic");
src/test/java/de/jalin/ldapadmin/ldap/TestUpdateUser.java
@@ -43,7 +43,7 @@
            final User newUser = new User();
            newUser.setDn(USERS_DN);
            newUser.setDisplayname("Klaas Clahsen");
            newUser.setEmail("klaas.clahsen@example.com");
            newUser.setEmailInternal("klaas.clahsen@example.com");
            newUser.setFirstname("Klaas");
            newUser.setLastname("Klahsen");
            newUser.setLogin("kla");