Rev 1051 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package ak.hostadmiral.core.action;
import java.util.List;
import java.util.Collection;
import java.util.Collections;
import java.util.ArrayList;
import java.util.HashSet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;
import ak.strutsx.RequestUtilsX;
import ak.strutsx.ErrorHandlerX;
import ak.backpath.BackPath;
import ak.hostadmiral.util.StringConverter;
import ak.hostadmiral.util.UserException;
import ak.hostadmiral.util.CollectionInfo;
import ak.hostadmiral.core.resources.CoreResources;
import ak.hostadmiral.core.model.User;
import ak.hostadmiral.core.model.UserManager;
import ak.hostadmiral.core.servlet.SessionKeys;
public final class UserAction
extends Action
implements ErrorHandlerX
{
public static final int PAGE_SIZE = 20;
public void handleErrors(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
ActionUtils.prepare(request, response);
if("submit".equals(mapping.getParameter()) || "partsubmit".equals(mapping.getParameter())) {
User user = (User)request.getSession().getAttribute(SessionKeys.USER);
initUserList(request, user);
DynaActionForm theForm = (DynaActionForm)form;
Long userId;
User u;
try {
userId = StringConverter.parseLong(theForm.get("id"));
}
catch(NumberFormatException ex) {
userId = null;
}
if(userId == null)
u = UserManager.getInstance().create(user);
else
u = UserManager.getInstance().get(user, userId);
request.setAttribute("u", u);
}
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
ActionUtils.prepare(request, response);
User user = (User)request.getSession().getAttribute(SessionKeys.USER);
if("list".equals(mapping.getParameter())) {
DynaActionForm theForm = (DynaActionForm)form;
Long page = StringConverter.parseLong(theForm.get("pg"));
CollectionInfo listInfo = new CollectionInfo();
Collection list = UserManager.getInstance().listUsers(
listInfo, PAGE_SIZE, (page == null) ? 0 : page.intValue(),
new Integer[] { UserManager.SORT_LOGIN }, user);
request.setAttribute("users", list);
request.setAttribute("listInfo", listInfo);
request.setAttribute("allowedToCreate",
Boolean.valueOf(UserManager.getInstance().allowedToCreate(user)));
request.setAttribute("mayViewAllLogins", Boolean.valueOf(user.mayViewAllLogins()));
return mapping.findForward("default");
}
else if("edit".equals(mapping.getParameter())) {
DynaActionForm theForm = (DynaActionForm)form;
Long userId = StringConverter.parseLong(theForm.get("id"));
User u;
DynaActionForm showForm = (DynaActionForm)RequestUtilsX.populateActionForm(
this, request, "UserEditForm");
if(userId == null) {
u = UserManager.getInstance().create(user);
showForm.set("enabled", Boolean.TRUE);
}
else {
u = UserManager.getInstance().get(user, userId);
showForm.set("login", u.getLogin());
if(u.getBoss() != null)
showForm.set("boss", StringConverter.toString(u.getBoss().getId()));
showForm.set("superuser", u.getSuperuser());
showForm.set("locale", u.getLocale().toString());
showForm.set("enabled", u.getEnabled());
showForm.set("comment", u.getComment());
}
initUserList(request, user);
request.setAttribute("u", u);
if(u.editableBy(user))
return mapping.findForward("default");
else
return mapping.findForward("view");
}
else if("partedit".equals(mapping.getParameter())) {
DynaActionForm theForm = (DynaActionForm)form;
Long userId = StringConverter.parseLong(theForm.get("id"));
User u = UserManager.getInstance().get(user, userId);
DynaActionForm showForm = (DynaActionForm)RequestUtilsX.populateActionForm(
this, request, "UserPartEditForm");
showForm.set("locale", u.getLocale().toString());
initUserList(request, user);
request.setAttribute("u", u);
return mapping.findForward("default");
}
else if("deleting".equals(mapping.getParameter())) {
DynaActionForm theForm = (DynaActionForm)form;
Long userId = StringConverter.parseLong(theForm.get("id"));
User u = UserManager.getInstance().get(user, userId);
request.setAttribute("action", "/user/delete.do");
request.setAttribute("object", u);
request.setAttribute("cascade",
UserManager.getInstance().beforeDelete(user, u, new HashSet()));
return mapping.findForward("default");
}
else if("delete".equals(mapping.getParameter())) {
DynaActionForm theForm = (DynaActionForm)form;
Long userId = StringConverter.parseLong(theForm.get("id"));
User u = UserManager.getInstance().get(user, userId);
request.setAttribute("u", u);
if(u.equals(user)) {
handleErrors(mapping, form, request, response);
throw new UserException(CoreResources.DELETE_ME_SELF);
}
// FIXME: invalidate session of deleted user if it is logged in
// FIXME: if two admins delete each other at the same time
UserManager.getInstance().delete(user, u);
response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl());
return null;
}
else if("submit".equals(mapping.getParameter())) {
DynaActionForm theForm = (DynaActionForm)form;
Long userId = StringConverter.parseLong(theForm.get("id"));
User u;
String password = (String)theForm.get("password");
if(userId == null) {
if(password == null || password.equals("")) {
handleErrors(mapping, form, request, response);
throw new UserException(CoreResources.PASSWORD_REQUIRED);
}
u = UserManager.getInstance().create(user);
}
else {
u = UserManager.getInstance().get(user, userId);
}
request.setAttribute("u", u);
String login = (String)theForm.get("login");
if(UserManager.getInstance().loginExists(user, u, login)) {
handleErrors(mapping, form, request, response);
throw new UserException(CoreResources.NONUNIQUE_USER_LOGIN);
}
u.setLogin(user, login);
if(u.mayChangeBoss(user)) {
Long bossId = StringConverter.parseLong(theForm.get("boss"));
if(bossId == null)
u.setBoss(user, null);
else
u.setBoss(user, UserManager.getInstance().get(user, bossId));
}
if(u.editableBy(user)) {
u.setLocaleName(user, (String)theForm.get("locale"));
u.setEnabled(user, (Boolean)theForm.get("enabled"));
u.setComment(user, (String)theForm.get("comment"));
}
if(u.mayChangeSuperuser(user))
u.setSuperuser(user, (Boolean)theForm.get("superuser"));
if(password != null && !password.equals("")
&& u.editableBy(user) // more strong condition, because normal
&& u.partEditableBy(user)) // user have to enter first the old password
{
u.setPassword(user, password);
}
UserManager.getInstance().save(user, u);
response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl());
return null;
}
else if("partsubmit".equals(mapping.getParameter())) {
DynaActionForm theForm = (DynaActionForm)form;
Long userId = StringConverter.parseLong(theForm.get("id"));
User u = UserManager.getInstance().get(user, userId);
u.setLocaleName(user, (String)theForm.get("locale"));
UserManager.getInstance().save(user, u);
response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl());
return null;
}
else {
throw new Exception("unknown mapping parameter");
}
}
private void initUserList(HttpServletRequest request, User user)
throws Exception
{
List list = new ArrayList(UserManager.getInstance().listUsers(user));
Collections.sort(list, UserManager.LOGIN_COMPARATOR);
request.setAttribute("users", list);
}
}