Subversion Repositories general

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
919 dev 1
package ak.hostcaptain.core.action;
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;
919 dev 21
import ak.backpath.BackPath;
22
 
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;
28
 
29
public final class UserAction
30
	extends Action
911 dev 31
	implements ErrorHandlerX
919 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())) {
919 dev 38
			User user = (User)request.getSession().getAttribute("user");
915 dev 39
			initUserList(request, user);
919 dev 40
 
41
			DynaActionForm theForm = (DynaActionForm)form;
42
			Long           userId;
43
			User           u;
44
 
45
			try {
46
				userId = StringConverter.parseLong(theForm.get("id"));
47
			}
48
			catch(NumberFormatException ex) {
49
				userId = null;
50
			}
51
 
52
			if(userId == null)
53
				u = UserManager.getInstance().create(user);
54
			else
55
				u = UserManager.getInstance().get(user, userId);
56
 
57
			request.setAttribute("u", u);
911 dev 58
		}
59
	}
60
 
919 dev 61
	public ActionForward execute(ActionMapping mapping, ActionForm form,
62
			HttpServletRequest request, HttpServletResponse response)
63
		throws Exception
64
	{
65
		User user = (User)request.getSession().getAttribute("user");
66
 
67
		if("list".equals(mapping.getParameter())) {
68
			List list = new ArrayList(UserManager.getInstance().listUsers(user));
69
			Collections.sort(list, UserManager.LOGIN_COMPARATOR);
70
			request.setAttribute("users", list);
71
 
72
			return mapping.findForward("default");
73
		}
74
		else if("edit".equals(mapping.getParameter())) {
75
			DynaActionForm theForm  = (DynaActionForm)form;
76
			Long           userId   = StringConverter.parseLong(theForm.get("id"));
77
			User           u;
78
			DynaActionForm showForm = (DynaActionForm)RequestUtilsX.populateActionForm(
79
				this, request, "ak.hostcaptain.core.form.UserEditForm");
80
 
81
			if(userId == null) {
82
				u = UserManager.getInstance().create(user);
83
				showForm.set("enabled", new Boolean(true));
84
			}
85
			else {
86
				u = UserManager.getInstance().get(user, userId);
87
				showForm.set("login",     u.getLogin());
911 dev 88
				if(u.getBoss() != null)
89
					showForm.set("boss", StringConverter.toString(u.getBoss().getId()));
919 dev 90
				showForm.set("superuser", u.getSuperuser());
91
				showForm.set("enabled",   u.getEnabled());
92
				showForm.set("comment",   u.getComment());
93
			}
94
 
915 dev 95
			initUserList(request, user);
919 dev 96
			request.setAttribute("u", u);
923 dev 97
			if(u.editableBy(user))
98
				return mapping.findForward("default");
99
			else
100
				return mapping.findForward("view");
919 dev 101
		}
102
		else if("delete".equals(mapping.getParameter())) {
103
			DynaActionForm theForm  = (DynaActionForm)form;
104
			Long           userId   = StringConverter.parseLong(theForm.get("id"));
105
			User           u        = UserManager.getInstance().get(user, userId);
106
			request.setAttribute("u", u);
107
 
923 dev 108
			if(u.equals(user)) {
109
				handleErrors(mapping, form, request, response);
919 dev 110
				throw new UserException(CoreResources.DELETE_ME_SELF);
923 dev 111
            }
919 dev 112
 
113
			// FIXME: invalidate session of deleted user if it is logged in
114
            // FIXME: if two admins delete each other at the same time
115
 
116
			UserManager.getInstance().delete(user, u);
117
			response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl());
118
			return null;
119
		}
120
		else if("submit".equals(mapping.getParameter())) {
121
			DynaActionForm theForm  = (DynaActionForm)form;
122
			Long           userId   = StringConverter.parseLong(theForm.get("id"));
123
			User           u;
124
            String         password = (String)theForm.get("password");
125
 
126
			if(userId == null) {
923 dev 127
	            if(password == null || password.equals("")) {
128
					handleErrors(mapping, form, request, response);
919 dev 129
	            	throw new UserException(CoreResources.PASSWORD_REQUIRED);
923 dev 130
    			}
919 dev 131
 
132
				u = UserManager.getInstance().create(user);
133
			}
134
			else {
135
				u = UserManager.getInstance().get(user, userId);
136
			}
137
			request.setAttribute("u", u);
138
 
923 dev 139
			String login = (String)theForm.get("login");
140
            if(UserManager.getInstance().loginExists(user, u, login)) {
141
				handleErrors(mapping, form, request, response);
142
            	throw new UserException(CoreResources.NONUNIQUE_USER_LOGIN);
143
            }
144
			u.setLogin(user, login);
919 dev 145
 
918 dev 146
			if(u.editableBy(user)) {
147
				Long bossId = StringConverter.parseLong(theForm.get("boss"));
148
				if(bossId == null)
919 dev 149
					u.setBoss(user, null);
918 dev 150
				else
151
					u.setBoss(user, UserManager.getInstance().get(user, bossId));
911 dev 152
 
919 dev 153
				u.setEnabled(user, (Boolean)theForm.get("enabled"));
154
				u.setComment(user, (String)theForm.get("comment"));
155
			}
156
 
918 dev 157
			if(u.mayChangeSuperuser(user))
919 dev 158
				u.setSuperuser(user, (Boolean)theForm.get("superuser"));
911 dev 159
 
919 dev 160
            if(password != null && !password.equals("")
161
            	&& u.editableBy(user)          // more strong condition, because normal
162
            	&& u.mayChangePassword(user))  // user have to enter first the old password
163
            {
164
            	u.setPassword(user, password);
165
            }
166
 
167
			UserManager.getInstance().save(user, u);
168
			response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl());
169
			return null;
170
		}
171
		else {
172
			throw new Exception("unknown mapping parameter");
173
		}
174
	}
175
 
915 dev 176
	private void initUserList(HttpServletRequest request, User user)
911 dev 177
		throws Exception
178
	{
915 dev 179
		List list = new ArrayList(UserManager.getInstance().listUsers(user));
911 dev 180
		Collections.sort(list, UserManager.LOGIN_COMPARATOR);
181
		request.setAttribute("users", list);
182
	}
919 dev 183
}