reset or change mailbox password if mailbox exists

This commit is contained in:
Peter Hormanns 2020-03-26 19:50:18 +01:00
parent 9ea2f82a7b
commit 63201377f2
5 changed files with 52 additions and 2 deletions

View File

@ -57,7 +57,24 @@ public class MailboxDAO {
final Map<String, Object> userAsMap = (Map<String, Object>) rpcResult[0];
return (String) userAsMap.get("name");
}
throw new MailboxNotFound();
throw new MailboxNotFound("mailbox for " + localpart + " not found");
}
public void changePassword(String pac, String localpart, String newPassword) throws IOException, XmlRpcException, MailboxNotFound {
final String mailbox = getMailbox(pac, localpart);
final List<Serializable> xmlRpcParamsList = new ArrayList<Serializable>();
xmlRpcParamsList.add(pac);
xmlRpcParamsList.add(ticketBox.getTicket());
final HashMap<String, Serializable> setParamsMap = new HashMap<String, Serializable>();
xmlRpcParamsList.add(setParamsMap);
setParamsMap.put("password", newPassword);
final HashMap<String, Serializable> whereParamsMap = new HashMap<String, Serializable>();
xmlRpcParamsList.add(whereParamsMap);
whereParamsMap.put("name", mailbox);
final Object[] rpcResult = (Object[])rpcClient.execute("user.update", xmlRpcParamsList);
if (rpcResult.length != 1) {
throw new MailboxNotFound("mailbox for " + localpart + " not found");
}
}
private void createMailbox(String pac, String localpart, String comment) throws IOException, XmlRpcException, MailboxNotFound {

View File

@ -4,4 +4,8 @@ public class MailboxNotFound extends Exception {
private static final long serialVersionUID = 1L;
public MailboxNotFound(String message) {
super(message);
}
}

View File

@ -66,7 +66,7 @@ public class DirectoryServiceRunner {
directoryService.setShutdownHookEnabled(true);
directoryService.getChangeLog().setEnabled(false);
directoryService.setAllowAnonymousAccess(false);
directoryService.setAccessControlEnabled(true);
directoryService.setAccessControlEnabled(false);
directoryService.setPasswordHidden(false);
return directoryService;
}

View File

@ -11,8 +11,12 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.xmlrpc.XmlRpcException;
import de.jalin.ldapadmin.beans.User;
import de.jalin.ldapadmin.beans.ValidationException;
import de.jalin.ldapadmin.hsadmin.MailboxDAO;
import de.jalin.ldapadmin.hsadmin.MailboxNotFound;
import de.jalin.ldapadmin.ldap.GroupsDAO;
import de.jalin.ldapadmin.ldap.LDAPSessionException;
import de.jalin.ldapadmin.ldap.SimplePasswordException;
@ -22,7 +26,15 @@ import de.jalin.ldapadmin.ldap.UsersDAO;
public class ProfileServlet extends AbstractLDAPServlet {
private static final long serialVersionUID = 1L;
private String pacAdmin;
@Override
public void init() throws ServletException {
super.init();
pacAdmin = config.getPacAdmin();
}
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
throws ServletException, IOException {
@ -80,6 +92,12 @@ public class ProfileServlet extends AbstractLDAPServlet {
throw new ValidationException("password2", messages.getString("ProfileServlet.passwords_donot_match"));
} else {
usr.setAndValidatePassword(password);
final MailboxDAO hsadminMailboxDAO = new MailboxDAO(getTicketProvider());
try {
hsadminMailboxDAO.changePassword(pacAdmin, usr.getLogin(), password);
} catch (XmlRpcException | MailboxNotFound e) {
System.out.println(e.getLocalizedMessage());
}
}
}
} catch (SimplePasswordException e) {

View File

@ -20,8 +20,11 @@ import javax.servlet.http.HttpSession;
import org.apache.commons.net.smtp.SMTPClient;
import org.apache.commons.net.smtp.SMTPReply;
import org.apache.commons.net.smtp.SimpleSMTPHeader;
import org.apache.xmlrpc.XmlRpcException;
import de.jalin.ldapadmin.beans.User;
import de.jalin.ldapadmin.hsadmin.MailboxDAO;
import de.jalin.ldapadmin.hsadmin.MailboxNotFound;
import de.jalin.ldapadmin.ldap.LDAPSessionException;
import de.jalin.ldapadmin.ldap.SimplePasswordException;
import de.jalin.ldapadmin.ldap.UsersDAO;
@ -35,6 +38,7 @@ public class ResetPasswordServlet extends AbstractLDAPServlet {
private String smtpHost;
private String smtpPort;
private String smtpFrom;
private String hsadminPacAdmin;
private String tempDir;
@Override
@ -43,6 +47,7 @@ public class ResetPasswordServlet extends AbstractLDAPServlet {
smtpHost = config.getSmtpHost();
smtpPort = config.getSmtpPort();
smtpFrom = config.getSmtpFromAddress();
hsadminPacAdmin = config.getPacAdmin();
tempDir = config.getTempDir();
}
@ -82,6 +87,7 @@ public class ResetPasswordServlet extends AbstractLDAPServlet {
final UsersDAO usrDAO = new UsersDAO(ldapSession);
final String loginParam = req.getParameter("login");
final User sessUsr = (User) httpSession.getAttribute("user");
final MailboxDAO hsMailboxDAO = new MailboxDAO(getTicketProvider());
if (loginParam != null && sessUsr != null && loginParam.equals(sessUsr.getLogin())) {
final String password1 = req.getParameter("password");
final String password2 = req.getParameter("password2");
@ -94,6 +100,11 @@ public class ResetPasswordServlet extends AbstractLDAPServlet {
try {
sessUsr.setAndValidatePassword(password1);
usrDAO.update(sessUsr);
try {
hsMailboxDAO.changePassword(hsadminPacAdmin, sessUsr.getLogin(), password1);
} catch (XmlRpcException | MailboxNotFound e) {
System.out.println(e.getLocalizedMessage());
}
httpSession.setAttribute("successmessage", messages.getString("ResetPasswordServlet.password_changed"));
req.getRequestDispatcher("/new-password.jsp").forward(req, resp);
return;