Subversion Repositories general

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
904 dev 1
package ak.hostcaptain.core.action;
899 dev 2
 
3
import java.util.List;
4
import java.util.Collections;
5
import java.util.ArrayList;
6
 
7
import javax.servlet.http.HttpServletRequest;
8
import javax.servlet.http.HttpServletResponse;
9
 
10
import org.apache.struts.action.Action;
11
import org.apache.struts.action.ActionMapping;
12
import org.apache.struts.action.ActionForm;
13
import org.apache.struts.action.DynaActionForm;
14
import org.apache.struts.action.ActionForward;
15
import org.apache.struts.action.ActionMessages;
16
import org.apache.struts.action.ActionErrors;
17
import org.apache.struts.action.ActionError;
18
 
19
import ak.strutsx.RequestUtilsX;
911 dev 20
import ak.strutsx.ErrorHandlerX;
899 dev 21
import ak.backpath.BackPath;
22
 
904 dev 23
import ak.hostcaptain.util.StringConverter;
24
import ak.hostcaptain.util.UserException;
25
import ak.hostcaptain.core.CoreResources;
26
import ak.hostcaptain.core.model.User;
27
import ak.hostcaptain.core.model.UserManager;
899 dev 28
 
29
public final class UserAction
30
	extends Action
911 dev 31
	implements ErrorHandlerX
899 dev 32
{
911 dev 33
	public void handleErrors(ActionMapping mapping, ActionForm form,
34
			 HttpServletRequest request, HttpServletResponse response)
35
		throws Exception
36
	{
37
		if("submit".equals(mapping.getParameter())) {
915 dev 38
			User user = (User)request.getSession().getAttribute("user");
39
			initUserList(request, user);
911 dev 40
		}
41
	}
42
 
899 dev 43
	public ActionForward execute(ActionMapping mapping, ActionForm form,
44
			HttpServletRequest request, HttpServletResponse response)
45
		throws Exception
46
	{
47
		User user = (User)request.getSession().getAttribute("user");
48
 
49
		if("list".equals(mapping.getParameter())) {
915 dev 50
			List list = new ArrayList(UserManager.getInstance().listUsers(user));
899 dev 51
			Collections.sort(list, UserManager.LOGIN_COMPARATOR);
52
			request.setAttribute("users", list);
53
 
54
			return mapping.findForward("default");
55
		}
56
		else if("edit".equals(mapping.getParameter())) {
57
			DynaActionForm theForm  = (DynaActionForm)form;
58
			Long           userId   = StringConverter.parseLong(theForm.get("id"));
59
			DynaActionForm showForm = (DynaActionForm)RequestUtilsX.populateActionForm(
904 dev 60
				this, request, "ak.hostcaptain.core.form.UserEditForm");
899 dev 61
 
62
			if(userId == null) {
913 dev 63
				showForm.set("enabled", new Boolean(true));
899 dev 64
			}
65
			else {
915 dev 66
				User u = UserManager.getInstance().get(user, userId);
911 dev 67
				showForm.set("login",     u.getLogin());
68
				if(u.getBoss() != null)
69
					showForm.set("boss", StringConverter.toString(u.getBoss().getId()));
70
				showForm.set("superuser", u.getSuperuser());
913 dev 71
				showForm.set("enabled",   u.getEnabled());
72
				showForm.set("comment",   u.getComment());
899 dev 73
			}
74
 
915 dev 75
			initUserList(request, user);
899 dev 76
			return mapping.findForward("default");
77
		}
78
		else if("delete".equals(mapping.getParameter())) {
79
			DynaActionForm theForm  = (DynaActionForm)form;
80
			Long           userId   = StringConverter.parseLong(theForm.get("id"));
915 dev 81
			User           u        = UserManager.getInstance().get(user, userId);
899 dev 82
 
83
			if(u.equals(user))
84
				throw new UserException(CoreResources.DELETE_ME_SELF);
85
 
86
			// FIXME: invalidate session of deleted user if it is logged in
87
            // FIXME: if two admins delete each other at the same time
88
 
915 dev 89
			UserManager.getInstance().delete(user, u);
899 dev 90
			response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl());
91
			return null;
92
		}
93
		else if("submit".equals(mapping.getParameter())) {
94
			DynaActionForm theForm  = (DynaActionForm)form;
95
			Long           userId   = StringConverter.parseLong(theForm.get("id"));
96
			User           u;
97
            String         password = (String)theForm.get("password");
98
 
99
			if(userId == null) {
100
	            if(password == null || password.equals(""))
101
	            	throw new UserException(CoreResources.PASSWORD_REQUIRED);
102
 
915 dev 103
				u = UserManager.getInstance().create(user);
899 dev 104
			}
105
			else {
915 dev 106
				u = UserManager.getInstance().get(user, userId);
899 dev 107
			}
108
 
911 dev 109
			u.setLogin((String)theForm.get("login"));
110
 
111
			Long bossId = StringConverter.parseLong(theForm.get("boss"));
112
			if(bossId == null)
913 dev 113
				u.setBoss(null);
114
			else
915 dev 115
				u.setBoss(UserManager.getInstance().get(user, bossId));
911 dev 116
 
117
			if(!user.equals(u)) // do not allow user to change own superuser status
118
				u.setSuperuser((Boolean)theForm.get("superuser"));
899 dev 119
 
120
            if(password != null && !password.equals(""))
121
            	u.setNewPassword(password);
122
 
913 dev 123
			u.setEnabled((Boolean)theForm.get("enabled"));
124
			u.setComment((String)theForm.get("comment"));
125
 
915 dev 126
			UserManager.getInstance().save(user, u);
899 dev 127
			response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl());
128
			return null;
129
		}
130
		else {
131
			throw new Exception("unknown mapping parameter");
132
		}
133
	}
911 dev 134
 
915 dev 135
	private void initUserList(HttpServletRequest request, User user)
911 dev 136
		throws Exception
137
	{
915 dev 138
		List list = new ArrayList(UserManager.getInstance().listUsers(user));
911 dev 139
		Collections.sort(list, UserManager.LOGIN_COMPARATOR);
140
		request.setAttribute("users", list);
141
	}
899 dev 142
}