clean comments

This commit is contained in:
Peter Hormanns 2019-07-24 12:44:51 +02:00
parent 8c1f3d7a40
commit 565913903a
15 changed files with 275 additions and 271 deletions

View File

@ -22,6 +22,6 @@ public class MembershipCheck {
}
public String getChecked() {
return user != null && group != null && user.getGroups().contains(group.getDn()) ? "checked" : ""; //$NON-NLS-1$ //$NON-NLS-2$
return user != null && group != null && user.getGroups().contains(group.getDn()) ? "checked" : "";
}
}

View File

@ -111,7 +111,7 @@ public class User implements Serializable, LDAPBean {
@Override
public String toString() {
return getFirstname() + " " + getLastname() + " (" + getLogin() + ", " + getEmail() + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
return getFirstname() + " " + getLastname() + " (" + getLogin() + ", " + getEmail() + ")";
}
@Override

View File

@ -76,7 +76,11 @@ public class DirectoryServiceRunner {
final CacheService cacheService = service.getCacheService();
final Cache cache = cacheService.getCache("dnCache");
final DefaultDnFactory defaultDnFactory = new DefaultDnFactory(schemaManager, cache);
final Partition partition = partitionFactory.createPartition(schemaManager, defaultDnFactory, partitionId, dnString, 400, new File("ldap-data." + Double.valueOf(Math.random()).hashCode()));
final Partition partition =
partitionFactory.createPartition(
schemaManager, defaultDnFactory, partitionId, dnString, 400,
new File("ldap-data." + Double.valueOf(Math.random()).hashCode())
);
service.addPartition(partition);
}

View File

@ -25,13 +25,13 @@ public class GroupsDAO {
public SortedMap<String, Group> loadGroups(final SortedMap<String, User> users) throws LDAPSessionException {
final SortedMap<String, Group> list;
list = new TreeMap<>();
final List<SearchResult> searchResult = session.search("ou=groups"); //$NON-NLS-1$
final List<SearchResult> searchResult = session.search("ou=groups");
for (final SearchResult result : searchResult) {
final Attributes attribs = result.getAttributes();
final Group grp = new Group();
grp.setName(session.getStringValue(attribs, "cn")); //$NON-NLS-1$
grp.setName(session.getStringValue(attribs, "cn"));
grp.setDn(result.getNameInNamespace());
final List<String> listOfMembers = session.getListOfValues(attribs, "uniqueMember"); //$NON-NLS-1$
final List<String> listOfMembers = session.getListOfValues(attribs, "uniqueMember");
final String dn = grp.getDn();
listOfMembers.stream().map((userDN) -> users.get(userDN)).map((user) -> user.getGroups()).forEachOrdered((groups) -> {
groups.add(dn);
@ -47,29 +47,29 @@ public class GroupsDAO {
final String name = grp.getName();
assert name != null;
final BasicAttributes attributes = new BasicAttributes();
final BasicAttribute objClass = new BasicAttribute("objectClass"); //$NON-NLS-1$
objClass.add("top"); //$NON-NLS-1$
objClass.add("groupOfUniqueNames"); //$NON-NLS-1$
final BasicAttribute objClass = new BasicAttribute("objectClass");
objClass.add("top");
objClass.add("groupOfUniqueNames");
attributes.put(objClass);
attributes.put("cn", name); //$NON-NLS-1$
attributes.put("cn", name);
final List<String> uniqueMembers = grp.getMembers();
final BasicAttribute uniqMembers = new BasicAttribute("uniqueMember"); //$NON-NLS-1$
final BasicAttribute uniqMembers = new BasicAttribute("uniqueMember");
if (uniqueMembers != null && uniqueMembers.size() > 0) {
uniqueMembers.forEach((dn) -> {
uniqMembers.add(dn);
});
}
attributes.put(uniqMembers);
final String dn = session.createSubcontext("cn=${cn},ou=groups".replace("${cn}", name), attributes); //$NON-NLS-1$ //$NON-NLS-2$
final String dn = session.createSubcontext("cn=${cn},ou=groups".replace("${cn}", name), attributes);
grp.setDn(dn);
}
public Group readGroup(final String dn, final SortedMap<String, User> users) throws LDAPSessionException {
final Attributes attribs = session.getAttributes(dn.substring(0, dn.indexOf("ou=groups") + 9)); //$NON-NLS-1$
final Attributes attribs = session.getAttributes(dn.substring(0, dn.indexOf("ou=groups") + 9));
final Group grp = new Group();
grp.setDn(dn);
grp.setName(session.getStringValue(attribs, "cn")); //$NON-NLS-1$
final List<String> listOfMembers = session.getListOfValues(attribs, "uniqueMember"); //$NON-NLS-1$
grp.setName(session.getStringValue(attribs, "cn"));
final List<String> listOfMembers = session.getListOfValues(attribs, "uniqueMember");
listOfMembers.stream().map((userDN) -> users.get(userDN)).map((user) -> user.getGroups()).forEachOrdered((groups) -> {
groups.add(dn);
});
@ -84,22 +84,22 @@ public class GroupsDAO {
if (grp.getMembers().isEmpty()) {
throw new NoGroupMembersException(name);
}
final BasicAttribute membersOfAttrib = new BasicAttribute("uniqueMember"); //$NON-NLS-1$
final BasicAttribute membersOfAttrib = new BasicAttribute("uniqueMember");
grp.getMembers().forEach((memberDN) -> {
membersOfAttrib.add(memberDN);
});
final ModificationItem modificationItem = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, membersOfAttrib);
session.modifyAttributes("cn=${cn},ou=groups".replace("${cn}", name), new ModificationItem[]{modificationItem}); //$NON-NLS-1$ //$NON-NLS-2$
session.modifyAttributes("cn=${cn},ou=groups".replace("${cn}", name), new ModificationItem[]{modificationItem});
}
public void updateMemberships(final User usr) throws LDAPSessionException, NoGroupMembersException {
final List<SearchResult> searchResult = session.search("ou=groups"); //$NON-NLS-1$
final List<SearchResult> searchResult = session.search("ou=groups");
for (final SearchResult result : searchResult) {
final Attributes attribs = result.getAttributes();
final Group grp = new Group();
grp.setName(session.getStringValue(attribs, "cn")); //$NON-NLS-1$
grp.setName(session.getStringValue(attribs, "cn"));
grp.setDn(result.getNameInNamespace());
final List<String> listOfGroupMembers = session.getListOfValues(attribs, "uniqueMember"); //$NON-NLS-1$
final List<String> listOfGroupMembers = session.getListOfValues(attribs, "uniqueMember");
grp.setMembers(listOfGroupMembers);
final List<String> listOfUserMemberships = usr.getGroups();
if (listOfGroupMembers.contains(usr.getDn()) && !listOfUserMemberships.contains(grp.getDn())) {
@ -115,7 +115,7 @@ public class GroupsDAO {
public void delete(final Group grp) throws LDAPSessionException {
assert grp != null;
session.unbind("cn=${id},ou=groups", grp.getName()); //$NON-NLS-1$
session.unbind("cn=${id},ou=groups", grp.getName());
}
}

View File

@ -24,10 +24,10 @@ public class LDAPSession {
public LDAPSession(final String providerURL, final String principal, final String password) throws LDAPSessionException {
final Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); //$NON-NLS-1$
env.put("com.sun.jndi.ldap.connect.pool", "true"); //$NON-NLS-1$ //$NON-NLS-2$
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put("com.sun.jndi.ldap.connect.pool", "true");
env.put(Context.PROVIDER_URL, providerURL);
env.put(Context.SECURITY_AUTHENTICATION, "simple"); //$NON-NLS-1$
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, principal);
env.put(Context.SECURITY_CREDENTIALS, password);
try {
@ -140,7 +140,7 @@ public class LDAPSession {
public void unbind(final String searchPattern, final String id) throws LDAPSessionException {
try {
ctx.unbind(searchPattern.replace("${id}", id)); //$NON-NLS-1$
ctx.unbind(searchPattern.replace("${id}", id));
} catch (NamingException e) {
throw new LDAPSessionException(e);
}
@ -156,12 +156,12 @@ public class LDAPSession {
private void createOrgUnitNodesIfNotExist() throws LDAPSessionException {
try {
final BasicAttributes usersAttributes = new BasicAttributes();
final BasicAttribute usersObjClass = new BasicAttribute("objectClass"); //$NON-NLS-1$
usersObjClass.add("top"); //$NON-NLS-1$
usersObjClass.add("organizationalUnit"); //$NON-NLS-1$
final BasicAttribute usersObjClass = new BasicAttribute("objectClass");
usersObjClass.add("top");
usersObjClass.add("organizationalUnit");
usersAttributes.put(usersObjClass);
usersAttributes.put("ou", "users"); //$NON-NLS-1$ //$NON-NLS-2$
ctx.createSubcontext("ou=users", usersAttributes); //$NON-NLS-1$
usersAttributes.put("ou", "users");
ctx.createSubcontext("ou=users", usersAttributes);
} catch (NamingException e) {
if (!((e instanceof NameAlreadyBoundException) || (e instanceof NoPermissionException))) {
throw new LDAPSessionException(e);
@ -169,12 +169,12 @@ public class LDAPSession {
}
try {
final BasicAttributes groupsAttributes = new BasicAttributes();
final BasicAttribute groupsObjClass = new BasicAttribute("objectClass"); //$NON-NLS-1$
groupsObjClass.add("top"); //$NON-NLS-1$
groupsObjClass.add("organizationalUnit"); //$NON-NLS-1$
final BasicAttribute groupsObjClass = new BasicAttribute("objectClass");
groupsObjClass.add("top");
groupsObjClass.add("organizationalUnit");
groupsAttributes.put(groupsObjClass);
groupsAttributes.put("ou", "groups"); //$NON-NLS-1$ //$NON-NLS-2$
ctx.createSubcontext("ou=groups", groupsAttributes); //$NON-NLS-1$
groupsAttributes.put("ou", "groups");
ctx.createSubcontext("ou=groups", groupsAttributes);
} catch (NamingException e) {
if (!((e instanceof NameAlreadyBoundException) || (e instanceof NoPermissionException))) {
throw new LDAPSessionException(e);

View File

@ -24,18 +24,18 @@ public class UsersDAO {
public SortedMap<String, User> loadUsers() throws LDAPSessionException {
final SortedMap<String, User> usersHash = new TreeMap<String, User>();
final List<SearchResult> enumeration = session.search("ou=users"); //$NON-NLS-1$
final List<SearchResult> enumeration = session.search("ou=users");
for (SearchResult result : enumeration) {
final Attributes attribs = result.getAttributes();
final User usr = new User();
usr.setFirstname(session.getStringValue(attribs, "givenName")); //$NON-NLS-1$
usr.setLastname(session.getStringValue(attribs, "sn")); //$NON-NLS-1$
usr.setEmail(session.getStringValue(attribs, "mail")); //$NON-NLS-1$
usr.setLogin(session.getStringValue(attribs, "uid")); //$NON-NLS-1$
usr.setPhone(session.getStringValue(attribs, "telephoneNumber")); //$NON-NLS-1$
usr.setMobile(session.getStringValue(attribs, "mobile")); //$NON-NLS-1$
usr.setDisplayname(session.getStringValue(attribs, "displayName")); //$NON-NLS-1$
usr.setLogin(session.getStringValue(attribs, "uid")); //$NON-NLS-1$
usr.setFirstname(session.getStringValue(attribs, "givenName"));
usr.setLastname(session.getStringValue(attribs, "sn"));
usr.setEmail(session.getStringValue(attribs, "mail"));
usr.setLogin(session.getStringValue(attribs, "uid"));
usr.setPhone(session.getStringValue(attribs, "telephoneNumber"));
usr.setMobile(session.getStringValue(attribs, "mobile"));
usr.setDisplayname(session.getStringValue(attribs, "displayName"));
usr.setLogin(session.getStringValue(attribs, "uid"));
usr.setDn(result.getNameInNamespace());
usersHash.put(usr.getDn(), usr);
}
@ -46,65 +46,65 @@ public class UsersDAO {
assert usr != null;
final String uid = usr.getLogin();
if (!hasValue(uid)) {
throw new RequiredAttributeException("uid"); //$NON-NLS-1$
throw new RequiredAttributeException("uid");
}
assert uid != null;
final BasicAttributes attributes = new BasicAttributes();
final BasicAttribute objClass = new BasicAttribute("objectClass"); //$NON-NLS-1$
objClass.add("top"); //$NON-NLS-1$
objClass.add("person"); //$NON-NLS-1$
objClass.add("organizationalPerson"); //$NON-NLS-1$
objClass.add("inetOrgPerson"); //$NON-NLS-1$
final BasicAttribute objClass = new BasicAttribute("objectClass");
objClass.add("top");
objClass.add("person");
objClass.add("organizationalPerson");
objClass.add("inetOrgPerson");
attributes.put(objClass);
final String firstName = usr.getFirstname();
if (hasValue(firstName)) {
attributes.put("givenName", firstName); //$NON-NLS-1$
attributes.put("givenName", firstName);
}
final String lastname = usr.getLastname();
if (!hasValue(lastname)) {
throw new RequiredAttributeException("lastname"); //$NON-NLS-1$
throw new RequiredAttributeException("lastname");
}
attributes.put("sn", lastname); //$NON-NLS-1$
attributes.put("sn", lastname);
final String email = usr.getEmail();
if (hasValue(email)) {
attributes.put("mail", email); //$NON-NLS-1$
attributes.put("mail", email);
}
attributes.put("uid", uid); //$NON-NLS-1$
attributes.put("cn", uid); //$NON-NLS-1$
attributes.put("uid", uid);
attributes.put("cn", uid);
final String telephone = usr.getPhone();
if (hasValue(telephone)) {
attributes.put("telephoneNumber", telephone); //$NON-NLS-1$
attributes.put("telephoneNumber", telephone);
}
final String mobile = usr.getMobile();
if (hasValue(mobile)) {
attributes.put("mobile", mobile); //$NON-NLS-1$
attributes.put("mobile", mobile);
}
final String comment = usr.getDisplayname();
if (hasValue(comment)) {
attributes.put("displayName", comment); //$NON-NLS-1$
attributes.put("displayName", comment);
} else {
attributes.put("displayName", firstName == null ? lastname : lastname + ", " + firstName); //$NON-NLS-1$ //$NON-NLS-2$
attributes.put("displayName", firstName == null ? lastname : lastname + ", " + firstName);
}
final String password = usr.getPassword();
if (!hasValue(password)) {
throw new RequiredAttributeException("password"); //$NON-NLS-1$
throw new RequiredAttributeException("password");
}
attributes.put("userPassword", password); //$NON-NLS-1$
final String dn = session.createSubcontext("uid=${uid},ou=users".replace("${uid}", uid), attributes); //$NON-NLS-1$ //$NON-NLS-2$
attributes.put("userPassword", password);
final String dn = session.createSubcontext("uid=${uid},ou=users".replace("${uid}", uid), attributes);
usr.setDn(dn);
}
public User read(final String dn) throws LDAPSessionException {
final Attributes attribs = session.getAttributes(dn.substring(0, dn.indexOf("ou=users") + 8)); //$NON-NLS-1$
final Attributes attribs = session.getAttributes(dn.substring(0, dn.indexOf("ou=users") + 8));
final User usr = new User();
usr.setFirstname(session.getStringValue(attribs, "givenName")); //$NON-NLS-1$
usr.setLastname(session.getStringValue(attribs, "sn")); //$NON-NLS-1$
usr.setEmail(session.getStringValue(attribs, "mail")); //$NON-NLS-1$
usr.setLogin(session.getStringValue(attribs, "uid")); //$NON-NLS-1$
usr.setPhone(session.getStringValue(attribs, "telephoneNumber")); //$NON-NLS-1$
usr.setMobile(session.getStringValue(attribs, "mobile")); //$NON-NLS-1$
usr.setDisplayname(session.getStringValue(attribs, "displayName")); //$NON-NLS-1$
usr.setLogin(session.getStringValue(attribs, "uid")); //$NON-NLS-1$
usr.setFirstname(session.getStringValue(attribs, "givenName"));
usr.setLastname(session.getStringValue(attribs, "sn"));
usr.setEmail(session.getStringValue(attribs, "mail"));
usr.setLogin(session.getStringValue(attribs, "uid"));
usr.setPhone(session.getStringValue(attribs, "telephoneNumber"));
usr.setMobile(session.getStringValue(attribs, "mobile"));
usr.setDisplayname(session.getStringValue(attribs, "displayName"));
usr.setLogin(session.getStringValue(attribs, "uid"));
usr.setDn(dn);
return usr;
}
@ -114,21 +114,21 @@ public class UsersDAO {
final String uid = usr.getLogin();
assert uid != null;
final Attributes attribs = session.getAttributes(
"uid=${uid},ou=users".replace("${uid}", uid)); //$NON-NLS-1$ //$NON-NLS-2$
"uid=${uid},ou=users".replace("${uid}", uid));
final List<ModificationItem> updates = new ArrayList<ModificationItem>();
addStringAttrUpdate(updates, attribs, "displayName", usr.getDisplayname()); //$NON-NLS-1$
addStringAttrUpdate(updates, attribs, "mail", usr.getEmail()); //$NON-NLS-1$
addStringAttrUpdate(updates, attribs, "givenName", usr.getFirstname()); //$NON-NLS-1$
addStringAttrUpdate(updates, attribs, "sn", usr.getLastname()); //$NON-NLS-1$
addStringAttrUpdate(updates, attribs, "mobile", usr.getMobile()); //$NON-NLS-1$
addStringAttrUpdate(updates, attribs, "telephoneNumber", usr.getPhone()); //$NON-NLS-1$
addPasswordUpdate(updates, "userPassword", usr.getPassword()); //$NON-NLS-1$
session.modifyAttributes("uid=${uid},ou=users".replace("${uid}", uid), updates.toArray(new ModificationItem[]{})); //$NON-NLS-1$ //$NON-NLS-2$
addStringAttrUpdate(updates, attribs, "displayName", usr.getDisplayname());
addStringAttrUpdate(updates, attribs, "mail", usr.getEmail());
addStringAttrUpdate(updates, attribs, "givenName", usr.getFirstname());
addStringAttrUpdate(updates, attribs, "sn", usr.getLastname());
addStringAttrUpdate(updates, attribs, "mobile", usr.getMobile());
addStringAttrUpdate(updates, attribs, "telephoneNumber", usr.getPhone());
addPasswordUpdate(updates, "userPassword", usr.getPassword());
session.modifyAttributes("uid=${uid},ou=users".replace("${uid}", uid), updates.toArray(new ModificationItem[]{}));
}
public void delete(final User user) throws LDAPSessionException {
assert user != null;
session.unbind("uid=${id},ou=users", user.getLogin()); //$NON-NLS-1$
session.unbind("uid=${id},ou=users", user.getLogin());
}
private boolean hasValue(final String email) {

View File

@ -45,11 +45,11 @@ public class AbstractLDAPServlet extends HttpServlet {
@Override
public void init() throws ServletException {
super.init();
final InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config.properties"); //$NON-NLS-1$
final InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config.properties");
config = new Properties();
try {
config.load(inputStream);
ldapSession = new LDAPSession(config.getProperty("provider.url"), config.getProperty("security.principal"), config.getProperty("security.password")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
ldapSession = new LDAPSession(config.getProperty("provider.url"), config.getProperty("security.principal"), config.getProperty("security.password"));
} catch (IOException | LDAPSessionException e) {
LOG.severe(e.getMessage());
throw new ServletException(e);
@ -67,15 +67,15 @@ public class AbstractLDAPServlet extends HttpServlet {
}
protected void throwServletException(final HttpSession session, final Exception e) throws ServletException {
session.setAttribute("servletexception", e); //$NON-NLS-1$
session.setAttribute("servletexception", e);
LOG.severe(e.getMessage());
throw new ServletException(e);
}
protected void cleanSession(final HttpSession httpSession) {
httpSession.removeAttribute("errormessage"); //$NON-NLS-1$
httpSession.removeAttribute("successmessage"); //$NON-NLS-1$
httpSession.removeAttribute("servletexception"); //$NON-NLS-1$
httpSession.removeAttribute("errormessage");
httpSession.removeAttribute("successmessage");
httpSession.removeAttribute("servletexception");
}
}

View File

@ -29,34 +29,34 @@ public class GroupServlet extends AbstractLDAPServlet {
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
throws ServletException, IOException {
final String pathInfo = req.getPathInfo();
String groupDN = ""; //$NON-NLS-1$
String groupDN = "";
if (pathInfo != null && pathInfo.length() > 1 && pathInfo.charAt(0) == '/') {
groupDN = pathInfo.substring(1);
}
final HttpSession httpSession = req.getSession();
cleanSession(httpSession);
String operation = req.getParameter("op"); //$NON-NLS-1$
String operation = req.getParameter("op");
if (operation == null || operation.isEmpty()) {
operation = "view"; //$NON-NLS-1$
operation = "view";
}
httpSession.setAttribute("operation", operation); //$NON-NLS-1$
httpSession.setAttribute("formdisabled", "view".equals(operation) || "delete".equals(operation) ? "disabled" : ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
httpSession.setAttribute("iddisabled", "create".equals(operation) ? "" : "disabled"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
httpSession.setAttribute("operation", operation);
httpSession.setAttribute("formdisabled", "view".equals(operation) || "delete".equals(operation) ? "disabled" : "");
httpSession.setAttribute("iddisabled", "create".equals(operation) ? "" : "disabled");
final GroupsDAO groupsDAO = new GroupsDAO(ldapSession);
final UsersDAO usersDAO = new UsersDAO(ldapSession);
try {
final SortedMap<String, User> users = usersDAO.loadUsers();
httpSession.setAttribute("users", users); //$NON-NLS-1$
httpSession.setAttribute("users", users);
if (groupDN != null && groupDN.length() > 9) {
httpSession.setAttribute("group", groupsDAO.readGroup(groupDN, users)); //$NON-NLS-1$
httpSession.setAttribute("group", groupsDAO.readGroup(groupDN, users));
} else {
httpSession.setAttribute("group", new Group()); //$NON-NLS-1$
httpSession.setAttribute("group", new Group());
}
} catch (LDAPSessionException e) {
throwServletException(httpSession, e);
}
req.getRequestDispatcher("/group.jsp").forward(req, resp); //$NON-NLS-1$
req.getRequestDispatcher("/group.jsp").forward(req, resp);
}
@Override
@ -64,13 +64,13 @@ public class GroupServlet extends AbstractLDAPServlet {
throws ServletException, IOException {
final Messages messages = new Messages(req.getLocale());
final String pathInfo = req.getPathInfo();
String dn = ""; //$NON-NLS-1$
String dn = "";
if (pathInfo != null && pathInfo.length() > 1 && pathInfo.charAt(0) == '/') {
dn = pathInfo.substring(1);
}
String operation = req.getParameter("op"); //$NON-NLS-1$
String operation = req.getParameter("op");
if (operation == null || operation.isEmpty()) {
operation = "none"; //$NON-NLS-1$
operation = "none";
}
final GroupsDAO groupsDAO = new GroupsDAO(ldapSession);
final Group grp = new Group();
@ -79,64 +79,64 @@ public class GroupServlet extends AbstractLDAPServlet {
final HttpSession httpSession = req.getSession();
cleanSession(httpSession);
@SuppressWarnings("unchecked")
final SortedMap<String, User> usersHash = (SortedMap<String, User>) httpSession.getAttribute("users"); //$NON-NLS-1$
final SortedMap<String, User> usersHash = (SortedMap<String, User>) httpSession.getAttribute("users");
final Iterator<String> userDNIterator = usersHash.keySet().iterator();
while (userDNIterator.hasNext()) {
final String userDN = userDNIterator.next();
final User usr = usersHash.get(userDN);
final String isChecked = req.getParameter("check_user_" + usr.getLogin()); //$NON-NLS-1$
final String isChecked = req.getParameter("check_user_" + usr.getLogin());
if (isChecked != null && !isChecked.isEmpty()) {
members.add(usr.getDn());
}
}
grp.setMembers(members);
try {
if ("edit".equals(operation) && !dn.isEmpty()) { //$NON-NLS-1$
if ("edit".equals(operation) && !dn.isEmpty()) {
final Group oldValue = groupsDAO.readGroup(dn, usersHash);
grp.setDn(dn);
grp.setName(oldValue.getName());
if (grp.getMembers().isEmpty()) {
httpSession.setAttribute("group", grp); //$NON-NLS-1$
httpSession.setAttribute("errormessage", messages.getString("GroupServlet.no_empty_group")); //$NON-NLS-1$ //$NON-NLS-2$
req.getRequestDispatcher("/group.jsp").forward(req, resp); //$NON-NLS-1$
httpSession.setAttribute("group", grp);
httpSession.setAttribute("errormessage", messages.getString("GroupServlet.no_empty_group"));
req.getRequestDispatcher("/group.jsp").forward(req, resp);
return;
}
try {
groupsDAO.update(grp);
} catch (NoGroupMembersException e) {
httpSession.setAttribute("group", grp); //$NON-NLS-1$
httpSession.setAttribute("errormessage", messages.getString("GroupServlet.no_empty_group")); //$NON-NLS-1$ //$NON-NLS-2$
req.getRequestDispatcher("/group.jsp").forward(req, resp); //$NON-NLS-1$
httpSession.setAttribute("group", grp);
httpSession.setAttribute("errormessage", messages.getString("GroupServlet.no_empty_group"));
req.getRequestDispatcher("/group.jsp").forward(req, resp);
return;
}
}
if ("delete".equals(operation) && !dn.isEmpty()) { //$NON-NLS-1$
if ("delete".equals(operation) && !dn.isEmpty()) {
final Group oldValue = groupsDAO.readGroup(dn, usersHash);
grp.setDn(dn);
grp.setName(oldValue.getName());
groupsDAO.delete(grp);
}
if ("create".equals(operation)) { //$NON-NLS-1$
final String grpName = req.getParameter("name"); //$NON-NLS-1$
if ("create".equals(operation)) {
final String grpName = req.getParameter("name");
grp.setName(grpName);
if (grp.getMembers().isEmpty()) {
httpSession.setAttribute("group", grp); //$NON-NLS-1$
httpSession.setAttribute("errormessage", messages.getString("GroupServlet.no_empty_group")); //$NON-NLS-1$ //$NON-NLS-2$
req.getRequestDispatcher("/group.jsp").forward(req, resp); //$NON-NLS-1$
httpSession.setAttribute("group", grp);
httpSession.setAttribute("errormessage", messages.getString("GroupServlet.no_empty_group"));
req.getRequestDispatcher("/group.jsp").forward(req, resp);
return;
}
try {
groupsDAO.create(grp);
} catch (AlreadyBoundException e) {
httpSession.setAttribute("group", grp); //$NON-NLS-1$
httpSession.setAttribute("errormessage", messages.getString("GroupServlet.group_exists")); //$NON-NLS-1$ //$NON-NLS-2$
req.getRequestDispatcher("/group.jsp").forward(req, resp); //$NON-NLS-1$
httpSession.setAttribute("group", grp);
httpSession.setAttribute("errormessage", messages.getString("GroupServlet.group_exists"));
req.getRequestDispatcher("/group.jsp").forward(req, resp);
return;
}
resp.sendRedirect(req.getContextPath() + req.getServletPath() + "/" + grp.getDn()); //$NON-NLS-1$
resp.sendRedirect(req.getContextPath() + req.getServletPath() + "/" + grp.getDn());
} else {
if ("delete".equals(operation)) { //$NON-NLS-1$
resp.sendRedirect(req.getContextPath() + "/groups"); //$NON-NLS-1$
if ("delete".equals(operation)) {
resp.sendRedirect(req.getContextPath() + "/groups");
} else {
resp.sendRedirect(req.getContextPath() + req.getServletPath() + pathInfo);
}

View File

@ -28,12 +28,12 @@ public class GroupsServlet extends AbstractLDAPServlet {
final GroupsDAO groupsDAO = new GroupsDAO(ldapSession);
try {
final SortedMap<String, User> loadedUsers = usersDAO.loadUsers();
httpSession.setAttribute("users", loadedUsers); //$NON-NLS-1$
httpSession.setAttribute("groups", groupsDAO.loadGroups(loadedUsers)); //$NON-NLS-1$
httpSession.setAttribute("users", loadedUsers);
httpSession.setAttribute("groups", groupsDAO.loadGroups(loadedUsers));
} catch (LDAPSessionException e) {
throwServletException(httpSession, e);
}
req.getRequestDispatcher("/groups.jsp").forward(req, resp); //$NON-NLS-1$
req.getRequestDispatcher("/groups.jsp").forward(req, resp);
}
}

View File

@ -19,7 +19,7 @@ public class LogoutServlet extends AbstractLDAPServlet {
final HttpSession httpSession = req.getSession();
cleanSession(httpSession);
httpSession.invalidate();
resp.sendRedirect(httpSession.getServletContext().getContextPath() + "/"); //$NON-NLS-1$
resp.sendRedirect(httpSession.getServletContext().getContextPath() + "/");
}
}

View File

@ -6,7 +6,7 @@ import java.util.ResourceBundle;
public class Messages {
private static final String BUNDLE_NAME = "de.jalin.ldapadmin.web.messages"; //$NON-NLS-1$
private static final String BUNDLE_NAME = "de.jalin.ldapadmin.web.messages";
private final ResourceBundle resourceBundle;

View File

@ -27,32 +27,32 @@ public class ProfileServlet extends AbstractLDAPServlet {
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
throws ServletException, IOException {
final HttpSession httpSession = req.getSession();
String userDN = ""; //$NON-NLS-1$
String userDN = "";
try {
final String remoteUID = req.getRemoteUser();
final List<SearchResult> list = ldapSession.search("ou=users", "uid", remoteUID); //$NON-NLS-1$ //$NON-NLS-2$
final List<SearchResult> list = ldapSession.search("ou=users", "uid", remoteUID);
if (list != null && list.size() > 0) {
final SearchResult first = list.get(0);
userDN = first.getNameInNamespace();
}
cleanSession(httpSession);
String operation = req.getParameter("op"); //$NON-NLS-1$
String operation = req.getParameter("op");
if (operation == null || operation.isEmpty()) {
operation = "profile"; //$NON-NLS-1$
operation = "profile";
}
httpSession.setAttribute("operation", operation); //$NON-NLS-1$
httpSession.setAttribute("formdisabled", "profile".equals(operation) ? "disabled" : ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
httpSession.setAttribute("iddisabled", "disabled"); //$NON-NLS-1$ //$NON-NLS-2$
httpSession.setAttribute("operation", operation);
httpSession.setAttribute("formdisabled", "profile".equals(operation) ? "disabled" : "");
httpSession.setAttribute("iddisabled", "disabled");
final UsersDAO usersDAO = new UsersDAO(ldapSession);
final GroupsDAO groupsDAO = new GroupsDAO(ldapSession);
SortedMap<String, User> users = usersDAO.loadUsers();
httpSession.setAttribute("users", users); //$NON-NLS-1$
httpSession.setAttribute("groups", groupsDAO.loadGroups(users)); //$NON-NLS-1$
httpSession.setAttribute("user", users.get(userDN)); //$NON-NLS-1$
httpSession.setAttribute("users", users);
httpSession.setAttribute("groups", groupsDAO.loadGroups(users));
httpSession.setAttribute("user", users.get(userDN));
} catch (LDAPSessionException e) {
throwServletException(httpSession, e);
}
req.getRequestDispatcher("/user.jsp").forward(req, resp); //$NON-NLS-1$
req.getRequestDispatcher("/user.jsp").forward(req, resp);
}
@Override
@ -62,54 +62,54 @@ public class ProfileServlet extends AbstractLDAPServlet {
final HttpSession httpSession = req.getSession();
cleanSession(httpSession);
final String pathInfo = req.getPathInfo();
String dn = ""; //$NON-NLS-1$
String dn = "";
if (pathInfo != null && pathInfo.length() > 1 && pathInfo.charAt(0) == '/') {
dn = pathInfo.substring(1);
}
String operation = req.getParameter("op"); //$NON-NLS-1$
String operation = req.getParameter("op");
if (operation == null || operation.isEmpty()) {
operation = "none"; //$NON-NLS-1$
operation = "none";
}
final UsersDAO usersDAO = new UsersDAO(ldapSession);
final String password = req.getParameter("password"); //$NON-NLS-1$
final String password2 = req.getParameter("password2"); //$NON-NLS-1$
final User usr = (User) httpSession.getAttribute("user"); //$NON-NLS-1$
final String password = req.getParameter("password");
final String password2 = req.getParameter("password2");
final User usr = (User) httpSession.getAttribute("user");
try {
if (password != null && !password.isEmpty()) {
if (password2 == null || !password2.equals(password)) {
throw new ValidationException("password2", messages.getString("ProfileServlet.passwords_donot_match")); //$NON-NLS-1$ //$NON-NLS-2$
throw new ValidationException("password2", messages.getString("ProfileServlet.passwords_donot_match"));
} else {
usr.setAndValidatePassword(password);
}
}
} catch (SimplePasswordException e) {
httpSession.setAttribute("user", usr); //$NON-NLS-1$
httpSession.setAttribute("errormessage", messages.getString("ResetPasswordServlet.simple_password")); //$NON-NLS-1$ //$NON-NLS-2$
req.getRequestDispatcher("/user.jsp").forward(req, resp); //$NON-NLS-1$
httpSession.setAttribute("user", usr);
httpSession.setAttribute("errormessage", messages.getString("ResetPasswordServlet.simple_password"));
req.getRequestDispatcher("/user.jsp").forward(req, resp);
return;
} catch (ValidationException e) {
httpSession.setAttribute("user", usr); //$NON-NLS-1$
httpSession.setAttribute("errormessage", messages.getString("ProfileServlet.inputfield") + e.getFieldname() + " " + e.getCondition()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
req.getRequestDispatcher("/user.jsp").forward(req, resp); //$NON-NLS-1$
httpSession.setAttribute("user", usr);
httpSession.setAttribute("errormessage", messages.getString("ProfileServlet.inputfield") + e.getFieldname() + " " + e.getCondition());
req.getRequestDispatcher("/user.jsp").forward(req, resp);
return;
}
try {
if ("password".equals(operation)) { //$NON-NLS-1$
if ("password".equals(operation)) {
final User oldValue = usersDAO.read(dn);
usr.setLogin(oldValue.getLogin());
usr.setDn(dn);
usersDAO.update(usr);
httpSession.setAttribute("successmessage", messages.getString("ProfileServlet.password_changed")); //$NON-NLS-1$ //$NON-NLS-2$
httpSession.setAttribute("operation", "profile"); //$NON-NLS-1$ //$NON-NLS-2$
httpSession.setAttribute("user", usr); //$NON-NLS-1$
httpSession.setAttribute("successmessage", messages.getString("ProfileServlet.password_changed"));
httpSession.setAttribute("operation", "profile");
httpSession.setAttribute("user", usr);
req.getRequestDispatcher("/user.jsp").forward(req, resp);
}
} catch (LDAPSessionException e) {
final String excMessage = e.getMessage();
if (excMessage != null && excMessage.contains("invalid reuse of password")) { //$NON-NLS-1$
httpSession.setAttribute("user", usr); //$NON-NLS-1$
httpSession.setAttribute("errormessage", messages.getString("ProfileServlet.invalid_reuse")); //$NON-NLS-1$ //$NON-NLS-2$
req.getRequestDispatcher("/user.jsp").forward(req, resp); //$NON-NLS-1$
if (excMessage != null && excMessage.contains("invalid reuse of password")) {
httpSession.setAttribute("user", usr);
httpSession.setAttribute("errormessage", messages.getString("ProfileServlet.invalid_reuse"));
req.getRequestDispatcher("/user.jsp").forward(req, resp);
return;
}
throwServletException(httpSession, e);

View File

@ -38,9 +38,9 @@ public class ResetPasswordServlet extends AbstractLDAPServlet {
@Override
public void init() throws ServletException {
super.init();
smtpHost = config.getProperty("smtp.host", "localhost"); //$NON-NLS-1$ //$NON-NLS-2$
smtpPort = config.getProperty("smtp.port", "25"); //$NON-NLS-1$ //$NON-NLS-2$
smtpFrom = config.getProperty("smtp.from", "nobody@localhost"); //$NON-NLS-1$ //$NON-NLS-2$
smtpHost = config.getProperty("smtp.host", "localhost");
smtpPort = config.getProperty("smtp.port", "25");
smtpFrom = config.getProperty("smtp.from", "nobody@localhost");
}
@Override
@ -48,26 +48,26 @@ public class ResetPasswordServlet extends AbstractLDAPServlet {
throws ServletException, IOException {
final HttpSession httpSession = req.getSession();
cleanSession(httpSession);
final String token = req.getParameter("token"); //$NON-NLS-1$
final String token = req.getParameter("token");
if (token != null && !token.isEmpty()) {
final File passwdResetFile = new File("/tmp/passwd" + token + ".tmp"); //$NON-NLS-1$ //$NON-NLS-2$
final File passwdResetFile = new File("/tmp/passwd" + token + ".tmp");
if (passwdResetFile.exists() && passwdResetFile.canRead()) {
try (final BufferedReader reader = new BufferedReader(new FileReader(passwdResetFile))) {
final String[] uidAndEMail = reader.readLine().split(":"); //$NON-NLS-1$
final String[] uidAndEMail = reader.readLine().split(":");
final UsersDAO usrDAO = new UsersDAO(ldapSession);
final User usr = usrDAO.read("uid=" + uidAndEMail[0] + ",ou=users,"); //$NON-NLS-1$ //$NON-NLS-2$
httpSession.setAttribute("user", usr); //$NON-NLS-1$
req.getRequestDispatcher("/new-password.jsp").forward(req, resp); //$NON-NLS-1$
final User usr = usrDAO.read("uid=" + uidAndEMail[0] + ",ou=users,");
httpSession.setAttribute("user", usr);
req.getRequestDispatcher("/new-password.jsp").forward(req, resp);
return;
} catch (LDAPSessionException e) {
LOG.warning("no valid password reset request");
httpSession.setAttribute("errormessage", new Messages(req.getLocale()).getString("ResetPasswordServlet.no_valid_passwordreset_request")); //$NON-NLS-1$ //$NON-NLS-2$
req.getRequestDispatcher("/reset-password.jsp").forward(req, resp); //$NON-NLS-1$
httpSession.setAttribute("errormessage", new Messages(req.getLocale()).getString("ResetPasswordServlet.no_valid_passwordreset_request"));
req.getRequestDispatcher("/reset-password.jsp").forward(req, resp);
return;
}
}
}
req.getRequestDispatcher("/reset-password.jsp").forward(req, resp); //$NON-NLS-1$
req.getRequestDispatcher("/reset-password.jsp").forward(req, resp);
}
@Override
@ -77,34 +77,34 @@ public class ResetPasswordServlet extends AbstractLDAPServlet {
final HttpSession httpSession = req.getSession();
cleanSession(httpSession);
final UsersDAO usrDAO = new UsersDAO(ldapSession);
final String loginParam = req.getParameter("login"); //$NON-NLS-1$
final User sessUsr = (User) httpSession.getAttribute("user"); //$NON-NLS-1$
final String loginParam = req.getParameter("login");
final User sessUsr = (User) httpSession.getAttribute("user");
if (loginParam != null && sessUsr != null && loginParam.equals(sessUsr.getLogin())) {
final String password1 = req.getParameter("password"); //$NON-NLS-1$
final String password2 = req.getParameter("password2"); //$NON-NLS-1$
final String password1 = req.getParameter("password");
final String password2 = req.getParameter("password2");
if (password1 != null && !password1.isEmpty()) {
if (password2 == null || !password2.equals(password1)) {
httpSession.setAttribute("errormessage", messages.getString("ResetPasswordServlet.passwords_donot_match")); //$NON-NLS-1$ //$NON-NLS-2$
req.getRequestDispatcher("/new-password.jsp").forward(req, resp); //$NON-NLS-1$
httpSession.setAttribute("errormessage", messages.getString("ResetPasswordServlet.passwords_donot_match"));
req.getRequestDispatcher("/new-password.jsp").forward(req, resp);
return;
} else {
try {
sessUsr.setAndValidatePassword(password1);
usrDAO.update(sessUsr);
httpSession.setAttribute("successmessage", messages.getString("ResetPasswordServlet.password_changed")); //$NON-NLS-1$ //$NON-NLS-2$
req.getRequestDispatcher("/new-password.jsp").forward(req, resp); //$NON-NLS-1$
httpSession.setAttribute("successmessage", messages.getString("ResetPasswordServlet.password_changed"));
req.getRequestDispatcher("/new-password.jsp").forward(req, resp);
return;
} catch (SimplePasswordException e) {
httpSession.setAttribute("user", sessUsr); //$NON-NLS-1$
httpSession.setAttribute("errormessage", messages.getString("ResetPasswordServlet.simple_password")); //$NON-NLS-1$ //$NON-NLS-2$
req.getRequestDispatcher("/new-password.jsp").forward(req, resp); //$NON-NLS-1$
httpSession.setAttribute("user", sessUsr);
httpSession.setAttribute("errormessage", messages.getString("ResetPasswordServlet.simple_password"));
req.getRequestDispatcher("/new-password.jsp").forward(req, resp);
return;
} catch (LDAPSessionException e) {
final String excMessage = e.getMessage();
if (excMessage != null && excMessage.contains("invalid reuse of password")) { //$NON-NLS-1$
httpSession.setAttribute("user", sessUsr); //$NON-NLS-1$
httpSession.setAttribute("errormessage", messages.getString("ResetPasswordServlet.invalid_password_reuse")); //$NON-NLS-1$ //$NON-NLS-2$
req.getRequestDispatcher("/new-password.jsp").forward(req, resp); //$NON-NLS-1$
if (excMessage != null && excMessage.contains("invalid reuse of password")) {
httpSession.setAttribute("user", sessUsr);
httpSession.setAttribute("errormessage", messages.getString("ResetPasswordServlet.invalid_password_reuse"));
req.getRequestDispatcher("/new-password.jsp").forward(req, resp);
return;
}
throwServletException(httpSession, e);
@ -112,55 +112,55 @@ public class ResetPasswordServlet extends AbstractLDAPServlet {
}
}
}
final String loginOrEMail = req.getParameter("loginoremail"); //$NON-NLS-1$
final File tempFile = File.createTempFile("passwd", ".tmp", new File("/tmp")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
final String loginOrEMail = req.getParameter("loginoremail");
final File tempFile = File.createTempFile("passwd", ".tmp", new File("/tmp"));
try (final PrintStream printStream = new PrintStream(tempFile)) {
String email = ""; //$NON-NLS-1$
String login = ""; //$NON-NLS-1$
String salutation = ""; //$NON-NLS-1$
String email = "";
String login = "";
String salutation = "";
if (loginOrEMail != null) {
if (loginOrEMail.contains("@")) { //$NON-NLS-1$
if (loginOrEMail.contains("@")) {
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)) {
login = usr.getLogin();
email = usr.getEmail();
salutation = usr.getFirstname() + " " + usr.getLastname(); //$NON-NLS-1$
salutation = usr.getFirstname() + " " + usr.getLastname();
}
}
} else {
final User usr = usrDAO.read("uid=" + loginOrEMail + ",ou=users,"); //$NON-NLS-1$ //$NON-NLS-2$
final User usr = usrDAO.read("uid=" + loginOrEMail + ",ou=users,");
if (usr != null) {
login = usr.getLogin();
email = usr.getEmail();
salutation = usr.getFirstname() + " " + usr.getLastname(); //$NON-NLS-1$
salutation = usr.getFirstname() + " " + usr.getLastname();
}
}
if (login.isEmpty() || email.isEmpty()) {
httpSession.setAttribute("errormessage", messages.getString("ResetPasswordServlet.error_sending_password_reset")); //$NON-NLS-1$ //$NON-NLS-2$
req.getRequestDispatcher("/reset-password.jsp").forward(req, resp); //$NON-NLS-1$
httpSession.setAttribute("errormessage", messages.getString("ResetPasswordServlet.error_sending_password_reset"));
req.getRequestDispatcher("/reset-password.jsp").forward(req, resp);
return;
}
printStream.println(login + ":" + email); //$NON-NLS-1$
printStream.println(login + ":" + email);
}
final StringBuffer messageText = new StringBuffer(messages.getString("ResetPasswordServlet.email_greeting")); //$NON-NLS-1$
final StringBuffer messageText = new StringBuffer(messages.getString("ResetPasswordServlet.email_greeting"));
messageText.append(salutation);
messageText.append(messages.getString("ResetPasswordServlet.email_content")); //$NON-NLS-1$
messageText.append(messages.getString("ResetPasswordServlet.email_content"));
messageText.append(req.getRequestURL().toString().replaceAll("^http\\:", "https://"));
messageText.append("?token="); //$NON-NLS-1$
messageText.append("?token=");
final String filename = tempFile.getName();
messageText.append(filename.substring(6, filename.length() - 4));
messageText.append(messages.getString("ResetPasswordServlet.email_signature")); //$NON-NLS-1$
smtpSend(smtpHost, smtpPort, messages, smtpFrom, email, messages.getString("ResetPasswordServlet.email_subject"), messageText.toString()); //$NON-NLS-1$ //$NON-NLS-2$
messageText.append(messages.getString("ResetPasswordServlet.email_signature"));
smtpSend(smtpHost, smtpPort, messages, smtpFrom, email, messages.getString("ResetPasswordServlet.email_subject"), messageText.toString());
} catch (LDAPSessionException | IOException e) {
LOG.severe("smtp problem");
httpSession.setAttribute("errormessage", messages.getString("ResetPasswordServlet.error_sending_password_reset")); //$NON-NLS-1$ //$NON-NLS-2$
req.getRequestDispatcher("/reset-password.jsp").forward(req, resp); //$NON-NLS-1$
httpSession.setAttribute("errormessage", messages.getString("ResetPasswordServlet.error_sending_password_reset"));
req.getRequestDispatcher("/reset-password.jsp").forward(req, resp);
return;
}
httpSession.invalidate();
resp.sendRedirect(httpSession.getServletContext().getContextPath() + "/"); //$NON-NLS-1$
resp.sendRedirect(httpSession.getServletContext().getContextPath() + "/");
}
private static void smtpSend(final String smtpHost, final String smtpPort, final Messages messages, final String fromAddress, final String toAddress, final String subject, final String text) throws IOException {
@ -169,14 +169,14 @@ public class ResetPasswordServlet extends AbstractLDAPServlet {
client.connect(smtpHost, Integer.parseInt(smtpPort));
int reply = client.getReplyCode();
if (!SMTPReply.isPositiveCompletion(reply)) {
throw new IOException(messages.getString("ResetPasswordServlet.error_sending_email_server")); //$NON-NLS-1$
throw new IOException(messages.getString("ResetPasswordServlet.error_sending_email_server"));
}
client.login(canonicalHostName);
client.setSender(fromAddress.trim());
client.addRecipient(toAddress.trim());
final Writer sendMessageData = client.sendMessageData();
if (sendMessageData == null) {
throw new IOException(messages.getString("ResetPasswordServlet.error_sending_email_relay")); //$NON-NLS-1$
throw new IOException(messages.getString("ResetPasswordServlet.error_sending_email_relay"));
}
try (PrintWriter wr = new PrintWriter(sendMessageData)) {
final SimpleSMTPHeader header = new SimpleSMTPHeader(fromAddress, toAddress, subject);
@ -186,7 +186,7 @@ public class ResetPasswordServlet extends AbstractLDAPServlet {
wr.write(text);
}
if (!client.completePendingCommand()) {
throw new IOException(messages.getString("ResetPasswordServlet.error_sending_email")); //$NON-NLS-1$
throw new IOException(messages.getString("ResetPasswordServlet.error_sending_email"));
}
client.logout();
client.disconnect();

View File

@ -32,34 +32,34 @@ public class UserServlet extends AbstractLDAPServlet {
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
throws ServletException, IOException {
final String pathInfo = req.getPathInfo();
String userDN = ""; //$NON-NLS-1$
String userDN = "";
if (pathInfo != null && pathInfo.length() > 1 && pathInfo.charAt(0) == '/') {
userDN = pathInfo.substring(1);
}
final HttpSession httpSession = req.getSession();
cleanSession(httpSession);
String operation = req.getParameter("op"); //$NON-NLS-1$
String operation = req.getParameter("op");
if (operation == null || operation.isEmpty()) {
operation = "view"; //$NON-NLS-1$
operation = "view";
}
httpSession.setAttribute("operation", operation); //$NON-NLS-1$
httpSession.setAttribute("formdisabled", "view".equals(operation) || "delete".equals(operation) ? "disabled" : ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
httpSession.setAttribute("iddisabled", "create".equals(operation) ? "" : "disabled"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
httpSession.setAttribute("operation", operation);
httpSession.setAttribute("formdisabled", "view".equals(operation) || "delete".equals(operation) ? "disabled" : "");
httpSession.setAttribute("iddisabled", "create".equals(operation) ? "" : "disabled");
final UsersDAO usersDAO = new UsersDAO(ldapSession);
final GroupsDAO groupsDAO = new GroupsDAO(ldapSession);
try {
SortedMap<String, User> users = usersDAO.loadUsers();
httpSession.setAttribute("users", users); //$NON-NLS-1$
httpSession.setAttribute("groups", groupsDAO.loadGroups(users)); //$NON-NLS-1$
if ("create".equals(operation)) { //$NON-NLS-1$
httpSession.setAttribute("user", new User()); //$NON-NLS-1$
httpSession.setAttribute("users", users);
httpSession.setAttribute("groups", groupsDAO.loadGroups(users));
if ("create".equals(operation)) {
httpSession.setAttribute("user", new User());
} else {
httpSession.setAttribute("user", users.get(userDN)); //$NON-NLS-1$
httpSession.setAttribute("user", users.get(userDN));
}
} catch (LDAPSessionException e) {
throwServletException(httpSession, e);
}
req.getRequestDispatcher("/user.jsp").forward(req, resp); //$NON-NLS-1$
req.getRequestDispatcher("/user.jsp").forward(req, resp);
}
@Override
@ -69,23 +69,23 @@ public class UserServlet extends AbstractLDAPServlet {
final HttpSession httpSession = req.getSession();
cleanSession(httpSession);
final String pathInfo = req.getPathInfo();
String dn = ""; //$NON-NLS-1$
String dn = "";
if (pathInfo != null && pathInfo.length() > 1 && pathInfo.charAt(0) == '/') {
dn = pathInfo.substring(1);
}
String operation = req.getParameter("op"); //$NON-NLS-1$
String operation = req.getParameter("op");
if (operation == null || operation.isEmpty()) {
operation = "none"; //$NON-NLS-1$
operation = "none";
}
final UsersDAO usersDAO = new UsersDAO(ldapSession);
final String login = req.getParameter("login"); //$NON-NLS-1$
final String firstname = req.getParameter("firstname"); //$NON-NLS-1$
final String lastname = req.getParameter("lastname"); //$NON-NLS-1$
final String email = req.getParameter("email"); //$NON-NLS-1$
final String phone = req.getParameter("phone"); //$NON-NLS-1$
final String mobile = req.getParameter("mobile"); //$NON-NLS-1$
final String password = req.getParameter("password"); //$NON-NLS-1$
final String password2 = req.getParameter("password2"); //$NON-NLS-1$
final String login = req.getParameter("login");
final String firstname = req.getParameter("firstname");
final String lastname = req.getParameter("lastname");
final String email = req.getParameter("email");
final String phone = req.getParameter("phone");
final String mobile = req.getParameter("mobile");
final String password = req.getParameter("password");
final String password2 = req.getParameter("password2");
final User usr = new User();
if (!dn.isEmpty()) {
usr.setDn(dn);
@ -93,57 +93,57 @@ public class UserServlet extends AbstractLDAPServlet {
usr.setLogin(login);
usr.setFirstname(firstname);
usr.setLastname(lastname);
usr.setDisplayname(lastname + ", " + firstname); //$NON-NLS-1$
usr.setDisplayname(lastname + ", " + firstname);
usr.setEmail(email);
usr.setPhone(phone);
usr.setMobile(mobile);
final List<String> memberships;
memberships = new ArrayList<>();
@SuppressWarnings("unchecked")
final SortedMap<String, Group> groupsHash = (SortedMap<String, Group>) httpSession.getAttribute("groups"); //$NON-NLS-1$
final SortedMap<String, Group> groupsHash = (SortedMap<String, Group>) httpSession.getAttribute("groups");
final Iterator<String> groupDNIterator = groupsHash.keySet().iterator();
while (groupDNIterator.hasNext()) {
final String groupDN = groupDNIterator.next();
final Group grp = groupsHash.get(groupDN);
final String isChecked = req.getParameter("check_group_" + grp.getName()); //$NON-NLS-1$
final String isChecked = req.getParameter("check_group_" + grp.getName());
if (isChecked != null && !isChecked.isEmpty()) {
memberships.add(grp.getDn());
}
}
usr.setGroups(memberships);
try {
validatePhone(messages, "phone", phone); //$NON-NLS-1$
validatePhone(messages, "mobile", mobile); //$NON-NLS-1$
validatePhone(messages, "phone", phone);
validatePhone(messages, "mobile", mobile);
validateEMail(messages, email);
validateLastName(messages, lastname);
if (password != null && !password.isEmpty()) {
if (password2 == null || !password2.equals(password)) {
throw new ValidationException("password2", messages.getString("UserServlet.passwords_donot_match")); //$NON-NLS-1$ //$NON-NLS-2$
throw new ValidationException("password2", messages.getString("UserServlet.passwords_donot_match"));
} else {
usr.setAndValidatePassword(password);
}
}
} catch (SimplePasswordException e) {
httpSession.setAttribute("user", usr); //$NON-NLS-1$
httpSession.setAttribute("errormessage", messages.getString("ResetPasswordServlet.simple_password")); //$NON-NLS-1$ //$NON-NLS-2$
req.getRequestDispatcher("/user.jsp").forward(req, resp); //$NON-NLS-1$
httpSession.setAttribute("user", usr);
httpSession.setAttribute("errormessage", messages.getString("ResetPasswordServlet.simple_password"));
req.getRequestDispatcher("/user.jsp").forward(req, resp);
return;
} catch (ValidationException e) {
httpSession.setAttribute("user", usr); //$NON-NLS-1$
httpSession.setAttribute("errormessage", messages.getString("UserServlet.input_field") + " \"" + e.getFieldname() + "\" " + e.getCondition()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
req.getRequestDispatcher("/user.jsp").forward(req, resp); //$NON-NLS-1$
httpSession.setAttribute("user", usr);
httpSession.setAttribute("errormessage", messages.getString("UserServlet.input_field") + " \"" + e.getFieldname() + "\" " + e.getCondition());
req.getRequestDispatcher("/user.jsp").forward(req, resp);
return;
}
final GroupsDAO groupsDAO = new GroupsDAO(ldapSession);
try {
if ("edit".equals(operation)) { //$NON-NLS-1$
if ("edit".equals(operation)) {
final User oldValue = usersDAO.read(dn);
usr.setLogin(oldValue.getLogin());
usr.setDn(dn);
usersDAO.update(usr);
groupsDAO.updateMemberships(usr);
}
if ("delete".equals(operation)) { //$NON-NLS-1$
if ("delete".equals(operation)) {
final User oldValue = usersDAO.read(dn);
usr.setLogin(oldValue.getLogin());
usr.setDn(dn);
@ -151,39 +151,39 @@ public class UserServlet extends AbstractLDAPServlet {
groupsDAO.updateMemberships(usr);
usersDAO.delete(usr);
}
if ("create".equals(operation)) { //$NON-NLS-1$
if ("create".equals(operation)) {
usr.setLogin(login);
usersDAO.create(usr);
groupsDAO.updateMemberships(usr);
resp.sendRedirect(req.getContextPath() + req.getServletPath() + "/" + usr.getDn()); //$NON-NLS-1$
resp.sendRedirect(req.getContextPath() + req.getServletPath() + "/" + usr.getDn());
} else {
if ("delete".equals(operation)) { //$NON-NLS-1$
resp.sendRedirect(req.getContextPath() + "/users"); //$NON-NLS-1$
if ("delete".equals(operation)) {
resp.sendRedirect(req.getContextPath() + "/users");
} else {
resp.sendRedirect(req.getContextPath() + req.getServletPath() + pathInfo);
}
}
} catch (LDAPSessionException e) {
final String excMessage = e.getMessage();
if (excMessage != null && excMessage.contains("invalid reuse of password")) { //$NON-NLS-1$
httpSession.setAttribute("user", usr); //$NON-NLS-1$
httpSession.setAttribute("errormessage", messages.getString("UserServlet.invalid_password_reuse")); //$NON-NLS-1$ //$NON-NLS-2$
req.getRequestDispatcher("/user.jsp").forward(req, resp); //$NON-NLS-1$
if (excMessage != null && excMessage.contains("invalid reuse of password")) {
httpSession.setAttribute("user", usr);
httpSession.setAttribute("errormessage", messages.getString("UserServlet.invalid_password_reuse"));
req.getRequestDispatcher("/user.jsp").forward(req, resp);
return;
}
throwServletException(httpSession, e);
} catch (NoGroupMembersException e) {
httpSession.setAttribute("user", usr); //$NON-NLS-1$
httpSession.setAttribute("errormessage", messages.getString("UserServlet.group_last_member")); //$NON-NLS-1$ //$NON-NLS-2$
req.getRequestDispatcher("/user.jsp").forward(req, resp); //$NON-NLS-1$
httpSession.setAttribute("user", usr);
httpSession.setAttribute("errormessage", messages.getString("UserServlet.group_last_member"));
req.getRequestDispatcher("/user.jsp").forward(req, resp);
} catch (RequiredAttributeException e) {
httpSession.setAttribute("user", usr); //$NON-NLS-1$
httpSession.setAttribute("errormessage", messages.getString("UserServlet.the_input_field") + " " + e.getFieldname() + " " + messages.getString("UserServlet.is_required")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
req.getRequestDispatcher("/user.jsp").forward(req, resp); //$NON-NLS-1$
httpSession.setAttribute("user", usr);
httpSession.setAttribute("errormessage", messages.getString("UserServlet.the_input_field") + " " + e.getFieldname() + " " + messages.getString("UserServlet.is_required"));
req.getRequestDispatcher("/user.jsp").forward(req, resp);
} catch (AlreadyBoundException e) {
httpSession.setAttribute("user", usr); //$NON-NLS-1$
httpSession.setAttribute("errormessage", messages.getString("UserServlet.user_exists")); //$NON-NLS-1$ //$NON-NLS-2$
req.getRequestDispatcher("/user.jsp").forward(req, resp); //$NON-NLS-1$
httpSession.setAttribute("user", usr);
httpSession.setAttribute("errormessage", messages.getString("UserServlet.user_exists"));
req.getRequestDispatcher("/user.jsp").forward(req, resp);
}
}
@ -192,8 +192,8 @@ public class UserServlet extends AbstractLDAPServlet {
return;
}
final String valid = phone.trim();
if (!valid.matches("[0-9\\-\\ \\(\\)]*")) { //$NON-NLS-1$
throw new ValidationException(field, messages.getString("UserServlet.phone_not_valid")); //$NON-NLS-1$
if (!valid.matches("[0-9\\-\\ \\(\\)]*")) {
throw new ValidationException(field, messages.getString("UserServlet.phone_not_valid"));
}
}
@ -202,14 +202,14 @@ public class UserServlet extends AbstractLDAPServlet {
return;
}
final String valid = email.trim();
if (!valid.matches("[A-Za-z0-9_+\\.\\-]*@[a-z0-9\\.\\-üöäß]*")) { //$NON-NLS-1$
throw new ValidationException("email", messages.getString("UserServlet.email_not_valid")); //$NON-NLS-1$ //$NON-NLS-2$
if (!valid.matches("[A-Za-z0-9_+\\.\\-]*@[a-z0-9\\.\\-üöäß]*")) {
throw new ValidationException("email", messages.getString("UserServlet.email_not_valid"));
}
}
private void validateLastName(final Messages messages, final String lastname) throws ValidationException {
if (lastname == null || lastname.isEmpty()) {
throw new ValidationException("lastname", messages.getString("UserServlet.is_required")); //$NON-NLS-1$ //$NON-NLS-2$
throw new ValidationException("lastname", messages.getString("UserServlet.is_required"));
}
}

View File

@ -30,12 +30,12 @@ public class UsersServlet extends AbstractLDAPServlet {
try {
final SortedMap<String, User> users = usersDAO.loadUsers();
final SortedMap<String, Group> groups = groupsDAO.loadGroups(users);
httpSession.setAttribute("users", users); //$NON-NLS-1$
httpSession.setAttribute("groups", groups); //$NON-NLS-1$
httpSession.setAttribute("users", users);
httpSession.setAttribute("groups", groups);
} catch (LDAPSessionException e) {
throwServletException(httpSession, e);
}
req.getRequestDispatcher("/users.jsp").forward(req, resp); //$NON-NLS-1$
req.getRequestDispatcher("/users.jsp").forward(req, resp);
}
}