Subversion Repositories general

Rev

Rev 913 | Blame | Last modification | View Log | RSS feed

package ak.hostcaptain.core.action;

import java.util.List;
import java.util.Collections;
import java.util.ArrayList;

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.hostcaptain.util.StringConverter;
import ak.hostcaptain.util.UserException;
import ak.hostcaptain.core.CoreResources;
import ak.hostcaptain.core.model.User;
import ak.hostcaptain.core.model.UserManager;

public final class UserAction
        extends Action
        implements ErrorHandlerX
{
        public void handleErrors(ActionMapping mapping, ActionForm form,
                         HttpServletRequest request, HttpServletResponse response)
                throws Exception
        {
                if("submit".equals(mapping.getParameter())) {
                        User user = (User)request.getSession().getAttribute("user");
                        initUserList(request, user);
                }
        }

        public ActionForward execute(ActionMapping mapping, ActionForm form,
                        HttpServletRequest request, HttpServletResponse response)
                throws Exception
        {
                User user = (User)request.getSession().getAttribute("user");

                if("list".equals(mapping.getParameter())) {
                        List list = new ArrayList(UserManager.getInstance().listUsers(user));
                        Collections.sort(list, UserManager.LOGIN_COMPARATOR);
                        request.setAttribute("users", list);

                        return mapping.findForward("default");
                }
                else if("edit".equals(mapping.getParameter())) {
                        DynaActionForm theForm  = (DynaActionForm)form;
                        Long           userId   = StringConverter.parseLong(theForm.get("id"));
                        DynaActionForm showForm = (DynaActionForm)RequestUtilsX.populateActionForm(
                                this, request, "ak.hostcaptain.core.form.UserEditForm");

                        if(userId == null) {
                                showForm.set("enabled", new Boolean(true));
                        }
                        else {
                                User 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("enabled",   u.getEnabled());
                                showForm.set("comment",   u.getComment());
                        }

                        initUserList(request, user);
                        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);

                        if(u.equals(user))
                                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(""))
                        throw new UserException(CoreResources.PASSWORD_REQUIRED);

                                u = UserManager.getInstance().create(user);
                        }
                        else {
                                u = UserManager.getInstance().get(user, userId);
                        }

                        u.setLogin((String)theForm.get("login"));

                        Long bossId = StringConverter.parseLong(theForm.get("boss"));
                        if(bossId == null)
                                u.setBoss(null);
                        else
                                u.setBoss(UserManager.getInstance().get(user, bossId));

                        if(!user.equals(u)) // do not allow user to change own superuser status
                                u.setSuperuser((Boolean)theForm.get("superuser"));

            if(password != null && !password.equals(""))
                u.setNewPassword(password);

                        u.setEnabled((Boolean)theForm.get("enabled"));
                        u.setComment((String)theForm.get("comment"));

                        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);
        }
}