format code
This commit is contained in:
parent
904f4c330f
commit
22dd341de7
@ -13,7 +13,7 @@ public class Group implements Serializable, LDAPBean {
|
||||
private List<String> members;
|
||||
|
||||
public Group() {
|
||||
members = new ArrayList<String>();
|
||||
members = new ArrayList<>();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
|
@ -22,9 +22,9 @@ public class GroupsDAO {
|
||||
this.session = session;
|
||||
}
|
||||
|
||||
public SortedMap<String, Group> loadGroups(final SortedMap<String, User> users) throws LDAPSessionException
|
||||
{
|
||||
final SortedMap<String, Group> list = new TreeMap<String, Group>();
|
||||
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$
|
||||
for (final SearchResult result : searchResult) {
|
||||
final Attributes attribs = result.getAttributes();
|
||||
@ -33,11 +33,9 @@ public class GroupsDAO {
|
||||
grp.setDn(result.getNameInNamespace());
|
||||
final List<String> listOfMembers = session.getListOfValues(attribs, "uniqueMember"); //$NON-NLS-1$
|
||||
final String dn = grp.getDn();
|
||||
for (String userDN : listOfMembers) {
|
||||
final User user = users.get(userDN);
|
||||
final List<String> groups = user.getGroups();
|
||||
listOfMembers.stream().map((userDN) -> users.get(userDN)).map((user) -> user.getGroups()).forEachOrdered((groups) -> {
|
||||
groups.add(dn);
|
||||
}
|
||||
});
|
||||
grp.setMembers(listOfMembers);
|
||||
list.put(dn, grp);
|
||||
}
|
||||
@ -57,9 +55,9 @@ public class GroupsDAO {
|
||||
final List<String> uniqueMembers = grp.getMembers();
|
||||
final BasicAttribute uniqMembers = new BasicAttribute("uniqueMember"); //$NON-NLS-1$
|
||||
if (uniqueMembers != null && uniqueMembers.size() > 0) {
|
||||
for (String dn : uniqueMembers) {
|
||||
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$
|
||||
@ -72,11 +70,9 @@ public class GroupsDAO {
|
||||
grp.setDn(dn);
|
||||
grp.setName(session.getStringValue(attribs, "cn")); //$NON-NLS-1$
|
||||
final List<String> listOfMembers = session.getListOfValues(attribs, "uniqueMember"); //$NON-NLS-1$
|
||||
for (String userDN : listOfMembers) {
|
||||
final User user = users.get(userDN);
|
||||
final List<String> groups = user.getGroups();
|
||||
listOfMembers.stream().map((userDN) -> users.get(userDN)).map((user) -> user.getGroups()).forEachOrdered((groups) -> {
|
||||
groups.add(dn);
|
||||
}
|
||||
});
|
||||
grp.setMembers(listOfMembers);
|
||||
return grp;
|
||||
}
|
||||
@ -85,15 +81,15 @@ public class GroupsDAO {
|
||||
assert grp != null;
|
||||
final String name = grp.getName();
|
||||
assert name != null;
|
||||
if (grp.getMembers().size() == 0) {
|
||||
if (grp.getMembers().isEmpty()) {
|
||||
throw new NoGroupMembersException(name);
|
||||
}
|
||||
final BasicAttribute membersOfAttrib = new BasicAttribute("uniqueMember"); //$NON-NLS-1$
|
||||
for (final String memberDN : grp.getMembers()) {
|
||||
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}); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
}
|
||||
|
||||
public void updateMemberships(final User usr) throws LDAPSessionException, NoGroupMembersException {
|
||||
|
@ -20,7 +20,7 @@ import javax.naming.directory.SearchResult;
|
||||
|
||||
public class LDAPSession {
|
||||
|
||||
private InitialDirContext ctx;
|
||||
private InitialDirContext ctx;
|
||||
|
||||
public LDAPSession(final String providerURL, final String principal, final String password) throws LDAPSessionException {
|
||||
final Properties env = new Properties();
|
||||
@ -69,7 +69,8 @@ private InitialDirContext ctx;
|
||||
}
|
||||
try {
|
||||
final int size = attribute.size();
|
||||
List<String> listOfValues = new ArrayList<String>();
|
||||
List<String> listOfValues;
|
||||
listOfValues = new ArrayList<>();
|
||||
for (int idx = 0; idx < size; idx++) {
|
||||
listOfValues.add((String) attribute.get(idx));
|
||||
}
|
||||
@ -91,7 +92,8 @@ private InitialDirContext ctx;
|
||||
}
|
||||
|
||||
public List<SearchResult> search(final String name, final String attribName, final String attribValue) throws LDAPSessionException {
|
||||
final List<SearchResult> searchResult = new ArrayList<SearchResult>();
|
||||
final List<SearchResult> searchResult;
|
||||
searchResult = new ArrayList<>();
|
||||
try {
|
||||
Attributes matchingAttributes = new BasicAttributes();
|
||||
matchingAttributes.put(attribName, attribValue);
|
||||
@ -106,7 +108,8 @@ private InitialDirContext ctx;
|
||||
}
|
||||
|
||||
public List<SearchResult> search(final String name) throws LDAPSessionException {
|
||||
final List<SearchResult> searchResult = new ArrayList<SearchResult>();
|
||||
final List<SearchResult> searchResult;
|
||||
searchResult = new ArrayList<>();
|
||||
try {
|
||||
final NamingEnumeration<SearchResult> searchEnum = ctx.search(name, null);
|
||||
while (searchEnum.hasMore()) {
|
||||
|
@ -1,6 +1,5 @@
|
||||
package de.jalin.ldapadmin.ldap;
|
||||
|
||||
|
||||
public class PasswordValidator {
|
||||
|
||||
private static final int MIN_PASSWORD_LEN = 6;
|
||||
@ -24,13 +23,15 @@ public class PasswordValidator {
|
||||
} else {
|
||||
if (type == Character.LOWERCASE_LETTER) {
|
||||
hasLowerCaseChar = 1;
|
||||
} else
|
||||
} else {
|
||||
if (type == Character.UPPERCASE_LETTER) {
|
||||
hasUpperCaseChar = 1;
|
||||
} else
|
||||
} else {
|
||||
hasSpecialChar = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (hasDigits + hasLowerCaseChar + hasUpperCaseChar + hasSpecialChar < 3) {
|
||||
throw new SimplePasswordException("a password requires 3 out of 4 "
|
||||
+ "different character types: lowercase, uppercase, digits and special characters");
|
||||
|
@ -22,8 +22,7 @@ public class UsersDAO {
|
||||
this.session = session;
|
||||
}
|
||||
|
||||
public SortedMap<String, User> loadUsers() throws LDAPSessionException
|
||||
{
|
||||
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$
|
||||
for (SearchResult result : enumeration) {
|
||||
@ -124,7 +123,7 @@ public class UsersDAO {
|
||||
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$
|
||||
session.modifyAttributes("uid=${uid},ou=users".replace("${uid}", uid), updates.toArray(new ModificationItem[]{})); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
}
|
||||
|
||||
public void delete(final User user) throws LDAPSessionException {
|
||||
|
@ -66,8 +66,7 @@ public class AbstractLDAPServlet extends HttpServlet {
|
||||
}
|
||||
}
|
||||
|
||||
protected void throwServletException(final HttpSession session, final Exception e) throws ServletException
|
||||
{
|
||||
protected void throwServletException(final HttpSession session, final Exception e) throws ServletException {
|
||||
session.setAttribute("servletexception", e); //$NON-NLS-1$
|
||||
LOG.severe(e.getMessage());
|
||||
throw new ServletException(e);
|
||||
|
@ -20,7 +20,7 @@ import de.jalin.ldapadmin.ldap.LDAPSessionException;
|
||||
import de.jalin.ldapadmin.ldap.NoGroupMembersException;
|
||||
import de.jalin.ldapadmin.ldap.UsersDAO;
|
||||
|
||||
@WebServlet(name="LdapGroup",urlPatterns={"/group/*"})
|
||||
@WebServlet(name = "LdapGroup", urlPatterns = {"/group/*"})
|
||||
public class GroupServlet extends AbstractLDAPServlet {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
@ -43,7 +43,6 @@ public class GroupServlet extends AbstractLDAPServlet {
|
||||
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$
|
||||
|
||||
|
||||
final GroupsDAO groupsDAO = new GroupsDAO(ldapSession);
|
||||
final UsersDAO usersDAO = new UsersDAO(ldapSession);
|
||||
try {
|
||||
@ -79,7 +78,8 @@ public class GroupServlet extends AbstractLDAPServlet {
|
||||
members = new ArrayList<>();
|
||||
final HttpSession httpSession = req.getSession();
|
||||
cleanSession(httpSession);
|
||||
@SuppressWarnings("unchecked") final SortedMap<String, User> usersHash = (SortedMap<String, User>) httpSession.getAttribute("users"); //$NON-NLS-1$
|
||||
@SuppressWarnings("unchecked")
|
||||
final SortedMap<String, User> usersHash = (SortedMap<String, User>) httpSession.getAttribute("users"); //$NON-NLS-1$
|
||||
final Iterator<String> userDNIterator = usersHash.keySet().iterator();
|
||||
while (userDNIterator.hasNext()) {
|
||||
final String userDN = userDNIterator.next();
|
||||
|
@ -14,15 +14,14 @@ import de.jalin.ldapadmin.ldap.GroupsDAO;
|
||||
import de.jalin.ldapadmin.ldap.LDAPSessionException;
|
||||
import de.jalin.ldapadmin.ldap.UsersDAO;
|
||||
|
||||
@WebServlet(name="LdapGroups",urlPatterns={"/groups"})
|
||||
@WebServlet(name = "LdapGroups", urlPatterns = {"/groups"})
|
||||
public class GroupsServlet extends AbstractLDAPServlet {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
|
||||
throws ServletException, IOException
|
||||
{
|
||||
throws ServletException, IOException {
|
||||
final HttpSession httpSession = req.getSession();
|
||||
cleanSession(httpSession);
|
||||
final UsersDAO usersDAO = new UsersDAO(ldapSession);
|
||||
|
@ -8,7 +8,7 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
@WebServlet(name="Logout",urlPatterns={"/logout"})
|
||||
@WebServlet(name = "Logout", urlPatterns = {"/logout"})
|
||||
public class LogoutServlet extends AbstractLDAPServlet {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
@ -19,7 +19,7 @@ public class NaiveTrustManager implements X509TrustManager {
|
||||
static {
|
||||
try {
|
||||
SSL_CONTEXT = SSLContext.getInstance("TLSv1.2");
|
||||
SSL_CONTEXT.init(null, new TrustManager[] { new NaiveTrustManager() }, null);
|
||||
SSL_CONTEXT.init(null, new TrustManager[]{new NaiveTrustManager()}, null);
|
||||
SSLContext.setDefault(SSL_CONTEXT);
|
||||
} catch (NoSuchAlgorithmException | KeyManagementException e) {
|
||||
throw new RuntimeException("Unable to initialise SSL context", e);
|
||||
|
@ -18,7 +18,7 @@ import de.jalin.ldapadmin.ldap.LDAPSessionException;
|
||||
import de.jalin.ldapadmin.ldap.SimplePasswordException;
|
||||
import de.jalin.ldapadmin.ldap.UsersDAO;
|
||||
|
||||
@WebServlet(name="LdapProfile",urlPatterns={"/profile", "/profile/*"})
|
||||
@WebServlet(name = "LdapProfile", urlPatterns = {"/profile", "/profile/*"})
|
||||
public class ProfileServlet extends AbstractLDAPServlet {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
@ -26,10 +26,9 @@ import de.jalin.ldapadmin.ldap.LDAPSessionException;
|
||||
import de.jalin.ldapadmin.ldap.SimplePasswordException;
|
||||
import de.jalin.ldapadmin.ldap.UsersDAO;
|
||||
|
||||
@WebServlet(name="ResetPassword",urlPatterns={"/passwordreset"})
|
||||
@WebServlet(name = "ResetPassword", urlPatterns = {"/passwordreset"})
|
||||
public class ResetPasswordServlet extends AbstractLDAPServlet {
|
||||
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private String smtpHost;
|
||||
@ -121,7 +120,7 @@ public class ResetPasswordServlet extends AbstractLDAPServlet {
|
||||
String salutation = ""; //$NON-NLS-1$
|
||||
if (loginOrEMail != null) {
|
||||
if (loginOrEMail.contains("@")) { //$NON-NLS-1$
|
||||
final SortedMap<String,User> usersMap = usrDAO.loadUsers();
|
||||
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)) {
|
||||
@ -151,7 +150,7 @@ public class ResetPasswordServlet extends AbstractLDAPServlet {
|
||||
messageText.append(req.getRequestURL().toString().replaceAll("^http\\:", "https://"));
|
||||
messageText.append("?token="); //$NON-NLS-1$
|
||||
final String filename = tempFile.getName();
|
||||
messageText.append(filename.substring(6,filename.length()-4));
|
||||
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$
|
||||
} catch (LDAPSessionException | IOException e) {
|
||||
|
@ -23,7 +23,7 @@ import de.jalin.ldapadmin.ldap.RequiredAttributeException;
|
||||
import de.jalin.ldapadmin.ldap.SimplePasswordException;
|
||||
import de.jalin.ldapadmin.ldap.UsersDAO;
|
||||
|
||||
@WebServlet(name="LdapUser",urlPatterns={"/user/*"})
|
||||
@WebServlet(name = "LdapUser", urlPatterns = {"/user/*"})
|
||||
public class UserServlet extends AbstractLDAPServlet {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
@ -99,7 +99,8 @@ public class UserServlet extends AbstractLDAPServlet {
|
||||
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$
|
||||
@SuppressWarnings("unchecked")
|
||||
final SortedMap<String, Group> groupsHash = (SortedMap<String, Group>) httpSession.getAttribute("groups"); //$NON-NLS-1$
|
||||
final Iterator<String> groupDNIterator = groupsHash.keySet().iterator();
|
||||
while (groupDNIterator.hasNext()) {
|
||||
final String groupDN = groupDNIterator.next();
|
||||
|
@ -15,7 +15,7 @@ import de.jalin.ldapadmin.ldap.GroupsDAO;
|
||||
import de.jalin.ldapadmin.ldap.LDAPSessionException;
|
||||
import de.jalin.ldapadmin.ldap.UsersDAO;
|
||||
|
||||
@WebServlet(name="LdapUsers",urlPatterns={"/users"})
|
||||
@WebServlet(name = "LdapUsers", urlPatterns = {"/users"})
|
||||
public class UsersServlet extends AbstractLDAPServlet {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
@ -29,7 +29,7 @@ public class UsersServlet extends AbstractLDAPServlet {
|
||||
final GroupsDAO groupsDAO = new GroupsDAO(ldapSession);
|
||||
try {
|
||||
final SortedMap<String, User> users = usersDAO.loadUsers();
|
||||
final SortedMap<String,Group> groups = groupsDAO.loadGroups(users);
|
||||
final SortedMap<String, Group> groups = groupsDAO.loadGroups(users);
|
||||
httpSession.setAttribute("users", users); //$NON-NLS-1$
|
||||
httpSession.setAttribute("groups", groups); //$NON-NLS-1$
|
||||
} catch (LDAPSessionException e) {
|
||||
|
@ -45,11 +45,11 @@ public class TestCreateGroup {
|
||||
udao.create(newUser2);
|
||||
Group login = new Group();
|
||||
login.setName("login");
|
||||
login.setMembers(Arrays.asList(new String[] { newUser1.getDn(), newUser2.getDn() }));
|
||||
login.setMembers(Arrays.asList(new String[]{newUser1.getDn(), newUser2.getDn()}));
|
||||
gdao.create(login);
|
||||
Group admins = new Group();
|
||||
admins.setName("admins");
|
||||
admins.setMembers(Arrays.asList(new String[] { newUser1.getDn(), newUser2.getDn() }));
|
||||
admins.setMembers(Arrays.asList(new String[]{newUser1.getDn(), newUser2.getDn()}));
|
||||
gdao.create(admins);
|
||||
} catch (LDAPSessionException | RequiredAttributeException | AlreadyBoundException e) {
|
||||
fail(e.getMessage());
|
||||
|
Loading…
Reference in New Issue
Block a user