add external email address
This commit is contained in:
parent
03e5a86167
commit
0901802b61
@ -4,12 +4,14 @@ import java.io.Serializable;
|
||||
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 @@ public class User implements Serializable, LDAPBean {
|
||||
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 @@ public class User implements Serializable, LDAPBean {
|
||||
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 @@ public class User implements Serializable, LDAPBean {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getFirstname() + " " + getLastname() + " (" + getLogin() + ", " + getEmail() + ")";
|
||||
return getFirstname() + " " + getLastname() + " (" + getLogin() + ", " + getEmailExternal() + ")";
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -22,6 +22,7 @@ public class LDAPConfig {
|
||||
private String smtpFromAddress;
|
||||
private String smtpHost;
|
||||
private String smtpPort;
|
||||
private String smtpInternalDomains;
|
||||
private String tempDir;
|
||||
|
||||
private LDAPConfig() {
|
||||
@ -36,6 +37,7 @@ public class LDAPConfig {
|
||||
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 @@ public class LDAPConfig {
|
||||
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 @@ public class LDAPConfig {
|
||||
return smtpFromAddress;
|
||||
}
|
||||
|
||||
public String getSmtpInternalDomains() {
|
||||
return smtpInternalDomains;
|
||||
}
|
||||
|
||||
public String getTempDir() {
|
||||
return tempDir;
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ public class UsersDAO {
|
||||
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 @@ public class UsersDAO {
|
||||
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 @@ public class UsersDAO {
|
||||
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 @@ public class UsersDAO {
|
||||
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 @@ public class UsersDAO {
|
||||
"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 @@ public class UsersDAO {
|
||||
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 @@ 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,
|
||||
final String attributeName, final String newValue) throws LDAPSessionException {
|
||||
if (hasValue(newValue)) {
|
||||
|
@ -126,9 +126,9 @@ public class ResetPasswordServlet extends AbstractLDAPServlet {
|
||||
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 @@ public class ResetPasswordServlet extends AbstractLDAPServlet {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -65,7 +65,7 @@ public class UserServlet extends AbstractLDAPServlet {
|
||||
@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 @@ public class UserServlet extends AbstractLDAPServlet {
|
||||
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 @@ public class UserServlet extends AbstractLDAPServlet {
|
||||
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();
|
||||
|
@ -3,3 +3,4 @@ security.principal=uid=admin,ou=system
|
||||
security.password=streng-geheim
|
||||
smtp.host=localhost
|
||||
smtp.from=nobody@example.com
|
||||
smtp.internal=example.com,example.org
|
@ -9,6 +9,7 @@ users.label.login=Login name
|
||||
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
|
||||
|
@ -9,6 +9,7 @@ users.label.login=Anmelde-Kennung
|
||||
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
|
||||
|
@ -9,6 +9,7 @@ users.label.login=Login name
|
||||
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
|
||||
|
@ -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">
|
||||
|
@ -61,7 +61,7 @@ public class TestCreateGroup {
|
||||
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);
|
||||
|
@ -40,7 +40,8 @@ public class TestCreateUser {
|
||||
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 @@ public class TestCreateUser {
|
||||
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());
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ public class TestDeleteUser {
|
||||
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");
|
||||
|
@ -40,7 +40,7 @@ public class TestReadUser {
|
||||
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 @@ public class TestReadUser {
|
||||
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());
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ public class TestUpdateAsBindUser {
|
||||
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");
|
||||
|
@ -31,7 +31,7 @@ public class TestUpdateAsSimpleUser {
|
||||
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 @@ public class TestUpdateAsSimpleUser {
|
||||
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");
|
||||
|
@ -43,7 +43,7 @@ public class TestUpdateUser {
|
||||
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");
|
||||
|
Loading…
Reference in New Issue
Block a user