add external email address

This commit is contained in:
Peter Hormanns 2020-03-06 17:49:26 +01:00
parent 03e5a86167
commit 0901802b61
17 changed files with 127 additions and 37 deletions

View File

@ -4,12 +4,14 @@ import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import de.jalin.ldapadmin.ldap.LDAPConfig;
import de.jalin.ldapadmin.ldap.PasswordValidator; import de.jalin.ldapadmin.ldap.PasswordValidator;
import de.jalin.ldapadmin.ldap.SimplePasswordException; import de.jalin.ldapadmin.ldap.SimplePasswordException;
public class User implements Serializable, LDAPBean { public class User implements Serializable, LDAPBean {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private static final String INTERNAL_EMAILDOMAINS = LDAPConfig.getConfig().getSmtpInternalDomains();
private String dn; private String dn;
private String login; private String login;
@ -17,7 +19,8 @@ public class User implements Serializable, LDAPBean {
private String firstname; private String firstname;
private String lastname; private String lastname;
private String displayname; private String displayname;
private String email; private String emailInternal;
private String emailExternal;
private String phone; private String phone;
private String mobile; private String mobile;
private List<String> groups; private List<String> groups;
@ -77,12 +80,36 @@ public class User implements Serializable, LDAPBean {
this.displayname = displayname; this.displayname = displayname;
} }
public String getEmail() { public String getEmailInternal() {
return email; return emailInternal;
} }
public void setEmail(String email) { public void setEmailInternal(String email) {
this.email = 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() { public String getPhone() {
@ -111,7 +138,7 @@ public class User implements Serializable, LDAPBean {
@Override @Override
public String toString() { public String toString() {
return getFirstname() + " " + getLastname() + " (" + getLogin() + ", " + getEmail() + ")"; return getFirstname() + " " + getLastname() + " (" + getLogin() + ", " + getEmailExternal() + ")";
} }
@Override @Override

View File

@ -22,6 +22,7 @@ public class LDAPConfig {
private String smtpFromAddress; private String smtpFromAddress;
private String smtpHost; private String smtpHost;
private String smtpPort; private String smtpPort;
private String smtpInternalDomains;
private String tempDir; private String tempDir;
private LDAPConfig() { private LDAPConfig() {
@ -36,6 +37,7 @@ public class LDAPConfig {
smtpHost = "localhost"; smtpHost = "localhost";
smtpPort = "25"; smtpPort = "25";
smtpFromAddress = "nobody@example.com"; smtpFromAddress = "nobody@example.com";
smtpInternalDomains = "example.com,example.org";
tempDir = System.getProperty("java.io.tmpdir"); tempDir = System.getProperty("java.io.tmpdir");
try { try {
final Properties props = loadConfig(); final Properties props = loadConfig();
@ -46,6 +48,7 @@ public class LDAPConfig {
smtpHost = props.getProperty("smtp.host", smtpHost); smtpHost = props.getProperty("smtp.host", smtpHost);
smtpPort = props.getProperty("smtp.port", smtpPort); smtpPort = props.getProperty("smtp.port", smtpPort);
smtpFromAddress = props.getProperty("smtp.from", smtpFromAddress); smtpFromAddress = props.getProperty("smtp.from", smtpFromAddress);
smtpInternalDomains = props.getProperty("smtp.internal", smtpInternalDomains);
tempDir = props.getProperty("temp.dir", tempDir); tempDir = props.getProperty("temp.dir", tempDir);
final LDAPUriParser uriParser = new LDAPUriParser(ldapProviderUrl); final LDAPUriParser uriParser = new LDAPUriParser(ldapProviderUrl);
ldapDistinguishedName = uriParser.getDn(); ldapDistinguishedName = uriParser.getDn();
@ -108,6 +111,10 @@ public class LDAPConfig {
return smtpFromAddress; return smtpFromAddress;
} }
public String getSmtpInternalDomains() {
return smtpInternalDomains;
}
public String getTempDir() { public String getTempDir() {
return tempDir; return tempDir;
} }

View File

@ -30,7 +30,7 @@ public class UsersDAO {
final User usr = new User(); final User usr = new User();
usr.setFirstname(session.getStringValue(attribs, "givenName")); usr.setFirstname(session.getStringValue(attribs, "givenName"));
usr.setLastname(session.getStringValue(attribs, "sn")); 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.setLogin(session.getStringValue(attribs, "uid"));
usr.setPhone(session.getStringValue(attribs, "telephoneNumber")); usr.setPhone(session.getStringValue(attribs, "telephoneNumber"));
usr.setMobile(session.getStringValue(attribs, "mobile")); usr.setMobile(session.getStringValue(attribs, "mobile"));
@ -56,19 +56,25 @@ public class UsersDAO {
objClass.add("organizationalPerson"); objClass.add("organizationalPerson");
objClass.add("inetOrgPerson"); objClass.add("inetOrgPerson");
attributes.put(objClass); attributes.put(objClass);
final String firstName = usr.getFirstname(); final String firstname = usr.getFirstname();
if (hasValue(firstName)) { if (hasValue(firstname)) {
attributes.put("givenName", firstName); attributes.put("givenName", firstname);
} }
final String lastname = usr.getLastname(); final String lastname = usr.getLastname();
if (!hasValue(lastname)) { if (!hasValue(lastname)) {
throw new RequiredAttributeException("lastname"); throw new RequiredAttributeException("lastname");
} }
attributes.put("sn", lastname); attributes.put("sn", lastname);
final String email = usr.getEmail(); final BasicAttribute mail = new BasicAttribute("mail");
if (hasValue(email)) { final String emailInternal = usr.getEmailInternal();
attributes.put("mail", email); 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("uid", uid);
attributes.put("cn", uid); attributes.put("cn", uid);
final String telephone = usr.getPhone(); final String telephone = usr.getPhone();
@ -83,7 +89,7 @@ public class UsersDAO {
if (hasValue(comment)) { if (hasValue(comment)) {
attributes.put("displayName", comment); attributes.put("displayName", comment);
} else { } else {
attributes.put("displayName", firstName == null ? lastname : lastname + ", " + firstName); attributes.put("displayName", firstname == null ? lastname : firstname + " " + lastname);
} }
final String password = usr.getPassword(); final String password = usr.getPassword();
if (!hasValue(password)) { if (!hasValue(password)) {
@ -99,7 +105,7 @@ public class UsersDAO {
final User usr = new User(); final User usr = new User();
usr.setFirstname(session.getStringValue(attribs, "givenName")); usr.setFirstname(session.getStringValue(attribs, "givenName"));
usr.setLastname(session.getStringValue(attribs, "sn")); 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.setLogin(session.getStringValue(attribs, "uid"));
usr.setPhone(session.getStringValue(attribs, "telephoneNumber")); usr.setPhone(session.getStringValue(attribs, "telephoneNumber"));
usr.setMobile(session.getStringValue(attribs, "mobile")); usr.setMobile(session.getStringValue(attribs, "mobile"));
@ -117,7 +123,16 @@ public class UsersDAO {
"uid=${uid},ou=users".replace("${uid}", uid)); "uid=${uid},ou=users".replace("${uid}", uid));
final List<ModificationItem> updates = new ArrayList<ModificationItem>(); final List<ModificationItem> updates = new ArrayList<ModificationItem>();
addStringAttrUpdate(updates, attribs, "displayName", usr.getDisplayname()); 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, "givenName", usr.getFirstname());
addStringAttrUpdate(updates, attribs, "sn", usr.getLastname()); addStringAttrUpdate(updates, attribs, "sn", usr.getLastname());
addStringAttrUpdate(updates, attribs, "mobile", usr.getMobile()); addStringAttrUpdate(updates, attribs, "mobile", usr.getMobile());
@ -131,8 +146,12 @@ public class UsersDAO {
session.unbind("uid=${id},ou=users", user.getLogin()); session.unbind("uid=${id},ou=users", user.getLogin());
} }
private boolean hasValue(final String email) { private boolean hasValue(final String param) {
return email != null && !email.isEmpty(); return param != null && !param.isEmpty();
}
private boolean hasValue(final List<String> param) {
return param != null && !param.isEmpty();
} }
private void addStringAttrUpdate(final List<ModificationItem> updates, private void addStringAttrUpdate(final List<ModificationItem> updates,
@ -156,6 +175,26 @@ public class UsersDAO {
} }
} }
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, private void addPasswordUpdate(final List<ModificationItem> updates,
final String attributeName, final String newValue) throws LDAPSessionException { final String attributeName, final String newValue) throws LDAPSessionException {
if (hasValue(newValue)) { if (hasValue(newValue)) {

View File

@ -126,9 +126,9 @@ public class ResetPasswordServlet extends AbstractLDAPServlet {
final SortedMap<String, User> usersMap = usrDAO.loadUsers(); final SortedMap<String, User> usersMap = usrDAO.loadUsers();
final Collection<User> allUsers = usersMap.values(); final Collection<User> allUsers = usersMap.values();
for (User usr : allUsers) { for (User usr : allUsers) {
if (usr.getEmail() != null && usr.getEmail().equalsIgnoreCase(loginOrEMail)) { if (usr.getEmailInternal() != null && usr.getEmailInternal().equalsIgnoreCase(loginOrEMail)) {
login = usr.getLogin(); login = usr.getLogin();
email = usr.getEmail(); email = usr.getEmailInternal();
salutation = usr.getFirstname() + " " + usr.getLastname(); salutation = usr.getFirstname() + " " + usr.getLastname();
} }
} }
@ -136,7 +136,7 @@ public class ResetPasswordServlet extends AbstractLDAPServlet {
final User usr = usrDAO.read("uid=" + loginOrEMail + ",ou=users,"); final User usr = usrDAO.read("uid=" + loginOrEMail + ",ou=users,");
if (usr != null) { if (usr != null) {
login = usr.getLogin(); login = usr.getLogin();
email = usr.getEmail(); email = usr.getEmailInternal();
salutation = usr.getFirstname() + " " + usr.getLastname(); salutation = usr.getFirstname() + " " + usr.getLastname();
} }
} }

View File

@ -79,10 +79,12 @@ public class UserServlet extends AbstractLDAPServlet {
operation = "none"; operation = "none";
} }
final UsersDAO usersDAO = new UsersDAO(ldapSession); 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 firstname = req.getParameter("firstname");
final String lastname = req.getParameter("lastname"); final String lastname = req.getParameter("lastname");
final String email = req.getParameter("email"); final String email = req.getParameter("email");
final String emailExt = req.getParameter("extemail");
final String phone = req.getParameter("phone"); final String phone = req.getParameter("phone");
final String mobile = req.getParameter("mobile"); final String mobile = req.getParameter("mobile");
final String password = req.getParameter("password"); final String password = req.getParameter("password");
@ -94,12 +96,12 @@ public class UserServlet extends AbstractLDAPServlet {
usr.setLogin(login); usr.setLogin(login);
usr.setFirstname(firstname); usr.setFirstname(firstname);
usr.setLastname(lastname); usr.setLastname(lastname);
usr.setDisplayname(lastname + ", " + firstname); usr.setDisplayname(firstname + " " + lastname);
usr.setEmail(email); usr.setEmailInternal(email);
usr.setEmailExternal(emailExt);
usr.setPhone(phone); usr.setPhone(phone);
usr.setMobile(mobile); usr.setMobile(mobile);
final List<String> memberships; final List<String> memberships = new ArrayList<>();
memberships = new ArrayList<>();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final SortedMap<String, Group> groupsHash = (SortedMap<String, Group>) httpSession.getAttribute("groups"); final SortedMap<String, Group> groupsHash = (SortedMap<String, Group>) httpSession.getAttribute("groups");
final Iterator<String> groupDNIterator = groupsHash.keySet().iterator(); final Iterator<String> groupDNIterator = groupsHash.keySet().iterator();

View File

@ -3,3 +3,4 @@ security.principal=uid=admin,ou=system
security.password=streng-geheim security.password=streng-geheim
smtp.host=localhost smtp.host=localhost
smtp.from=nobody@example.com smtp.from=nobody@example.com
smtp.internal=example.com,example.org

View File

@ -9,6 +9,7 @@ users.label.login=Login name
users.label.firstname=First name users.label.firstname=First name
users.label.lastname=Last name users.label.lastname=Last name
users.label.email=EMail address users.label.email=EMail address
users.label.extemail=external EMail address
users.label.phone=Phone users.label.phone=Phone
users.label.mobile=Mobile users.label.mobile=Mobile
users.label.password=Password users.label.password=Password

View File

@ -9,6 +9,7 @@ users.label.login=Anmelde-Kennung
users.label.firstname=Vorname users.label.firstname=Vorname
users.label.lastname=Nachname users.label.lastname=Nachname
users.label.email=E-Mail Adresse users.label.email=E-Mail Adresse
users.label.extemail=E-Mail Adresse (extern)
users.label.phone=Telefon users.label.phone=Telefon
users.label.mobile=Handy users.label.mobile=Handy
users.label.password=Passwort users.label.password=Passwort

View File

@ -9,6 +9,7 @@ users.label.login=Login name
users.label.firstname=First name users.label.firstname=First name
users.label.lastname=Last name users.label.lastname=Last name
users.label.email=EMail address users.label.email=EMail address
users.label.extemail=external EMail address
users.label.phone=Phone users.label.phone=Phone
users.label.mobile=Mobile users.label.mobile=Mobile
users.label.password=Password users.label.password=Password

View File

@ -84,7 +84,15 @@
<div class="col-sm-9"> <div class="col-sm-9">
<input <input
type="email" class="form-control" id="email" name="email" 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> </div>
<div class="form-group"> <div class="form-group">

View File

@ -61,7 +61,7 @@ public class TestCreateGroup {
final User newUser = new User(); final User newUser = new User();
newUser.setDn(USERS_DN.replace("${uid}", uid)); newUser.setDn(USERS_DN.replace("${uid}", uid));
newUser.setDisplayname(fn + " " + ln); newUser.setDisplayname(fn + " " + ln);
newUser.setEmail(fn.toLowerCase() + "." + ln.toLowerCase() + "@example.com"); newUser.setEmailInternal(fn.toLowerCase() + "." + ln.toLowerCase() + "@example.com");
newUser.setFirstname(fn); newUser.setFirstname(fn);
newUser.setLastname(ln); newUser.setLastname(ln);
newUser.setLogin(uid); newUser.setLogin(uid);

View File

@ -40,7 +40,8 @@ public class TestCreateUser {
final User newUser = new User(); final User newUser = new User();
newUser.setDn(USERS_DN); newUser.setDn(USERS_DN);
newUser.setDisplayname("Peter Petersen"); 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.setFirstname("Peter");
newUser.setLastname("Petersen"); newUser.setLastname("Petersen");
newUser.setLogin("pet"); newUser.setLogin("pet");
@ -50,6 +51,8 @@ public class TestCreateUser {
dao.create(newUser); dao.create(newUser);
final User createdUser = dao.loadUsers().get(USERS_DN); final User createdUser = dao.loadUsers().get(USERS_DN);
assertNotNull("should exist", createdUser); 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) { } catch (LDAPSessionException | RequiredAttributeException | AlreadyBoundException e) {
fail(e.getMessage()); fail(e.getMessage());
} }

View File

@ -40,7 +40,7 @@ public class TestDeleteUser {
final User newUser = new User(); final User newUser = new User();
newUser.setDn(USERS_DN); newUser.setDn(USERS_DN);
newUser.setDisplayname("Hein Hanssen"); newUser.setDisplayname("Hein Hanssen");
newUser.setEmail("hein.hanssen@example.com"); newUser.setEmailInternal("hein.hanssen@example.com");
newUser.setFirstname("Hein"); newUser.setFirstname("Hein");
newUser.setLastname("Hanssen"); newUser.setLastname("Hanssen");
newUser.setLogin("hei"); newUser.setLogin("hei");

View File

@ -40,7 +40,7 @@ public class TestReadUser {
final User newUser = new User(); final User newUser = new User();
newUser.setDn(USERS_DN); newUser.setDn(USERS_DN);
newUser.setDisplayname("Chris Christansen"); newUser.setDisplayname("Chris Christansen");
newUser.setEmail("chris.christansen@example.com"); newUser.setEmailInternal("chris.christansen@example.com");
newUser.setFirstname("Chris"); newUser.setFirstname("Chris");
newUser.setLastname("Christansen"); newUser.setLastname("Christansen");
newUser.setLogin("chr"); newUser.setLogin("chr");
@ -52,7 +52,7 @@ public class TestReadUser {
assertNotNull("should exist", createdUser); assertNotNull("should exist", createdUser);
final User readUser = dao.read(USERS_DN); final User readUser = dao.read(USERS_DN);
assertNotNull(readUser); assertNotNull(readUser);
assertTrue("chris.christansen@example.com".equals(readUser.getEmail())); assertTrue("chris.christansen@example.com".equals(readUser.getEmailInternal()));
} catch (LDAPSessionException | RequiredAttributeException | AlreadyBoundException e) { } catch (LDAPSessionException | RequiredAttributeException | AlreadyBoundException e) {
fail(e.getMessage()); fail(e.getMessage());
} }

View File

@ -48,7 +48,7 @@ public class TestUpdateAsBindUser {
final User newUser = new User(); final User newUser = new User();
newUser.setDn(USERS_DN); newUser.setDn(USERS_DN);
newUser.setDisplayname("Paul Paulsen"); newUser.setDisplayname("Paul Paulsen");
newUser.setEmail("paul.paulsen@example.com"); newUser.setEmailInternal("paul.paulsen@example.com");
newUser.setFirstname("Paul"); newUser.setFirstname("Paul");
newUser.setLastname("Pausen"); newUser.setLastname("Pausen");
newUser.setLogin("pau"); newUser.setLogin("pau");

View File

@ -31,7 +31,7 @@ public class TestUpdateAsSimpleUser {
final User newUser = new User(); final User newUser = new User();
newUser.setDn(USERS_DN); newUser.setDn(USERS_DN);
newUser.setDisplayname("Paul Petersen"); newUser.setDisplayname("Paul Petersen");
newUser.setEmail("paul.petersen@example.com"); newUser.setEmailInternal("paul.petersen@example.com");
newUser.setFirstname("Paul"); newUser.setFirstname("Paul");
newUser.setLastname("Petersen"); newUser.setLastname("Petersen");
newUser.setLogin("plp"); newUser.setLogin("plp");
@ -61,7 +61,7 @@ public class TestUpdateAsSimpleUser {
final User newUser = new User(); final User newUser = new User();
newUser.setDn(USERS_DN); newUser.setDn(USERS_DN);
newUser.setDisplayname("Micha Michaelsen"); newUser.setDisplayname("Micha Michaelsen");
newUser.setEmail("micha.michaelsen@example.com"); newUser.setEmailInternal("micha.michaelsen@example.com");
newUser.setFirstname("Michael"); newUser.setFirstname("Michael");
newUser.setLastname("Michaelsen"); newUser.setLastname("Michaelsen");
newUser.setLogin("mic"); newUser.setLogin("mic");

View File

@ -43,7 +43,7 @@ public class TestUpdateUser {
final User newUser = new User(); final User newUser = new User();
newUser.setDn(USERS_DN); newUser.setDn(USERS_DN);
newUser.setDisplayname("Klaas Clahsen"); newUser.setDisplayname("Klaas Clahsen");
newUser.setEmail("klaas.clahsen@example.com"); newUser.setEmailInternal("klaas.clahsen@example.com");
newUser.setFirstname("Klaas"); newUser.setFirstname("Klaas");
newUser.setLastname("Klahsen"); newUser.setLastname("Klahsen");
newUser.setLogin("kla"); newUser.setLogin("kla");