Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
899 | dev | 1 | package ak.webcontrol.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; |
||
20 | import ak.backpath.BackPath; |
||
21 | |||
22 | import ak.webcontrol.util.StringConverter; |
||
23 | import ak.webcontrol.util.UserException; |
||
24 | import ak.webcontrol.core.CoreResources; |
||
25 | import ak.webcontrol.core.model.User; |
||
26 | import ak.webcontrol.core.model.UserManager; |
||
27 | |||
28 | public final class UserAction |
||
29 | extends Action |
||
30 | { |
||
31 | public ActionForward execute(ActionMapping mapping, ActionForm form, |
||
32 | HttpServletRequest request, HttpServletResponse response) |
||
33 | throws Exception |
||
34 | { |
||
35 | User user = (User)request.getSession().getAttribute("user"); |
||
36 | |||
37 | if("list".equals(mapping.getParameter())) { |
||
38 | List list = new ArrayList(UserManager.getInstance().listUsers()); |
||
39 | Collections.sort(list, UserManager.LOGIN_COMPARATOR); |
||
40 | request.setAttribute("users", list); |
||
41 | |||
42 | return mapping.findForward("default"); |
||
43 | } |
||
44 | else if("edit".equals(mapping.getParameter())) { |
||
45 | DynaActionForm theForm = (DynaActionForm)form; |
||
46 | Long userId = StringConverter.parseLong(theForm.get("id")); |
||
47 | DynaActionForm showForm = (DynaActionForm)RequestUtilsX.populateActionForm( |
||
48 | this, request, "ak.webcontrol.core.form.UserEditForm"); |
||
49 | |||
50 | if(userId == null) { |
||
51 | |||
52 | } |
||
53 | else { |
||
54 | User u = UserManager.getInstance().get(userId); |
||
55 | showForm.set("login", u.getLogin()); |
||
56 | } |
||
57 | |||
58 | return mapping.findForward("default"); |
||
59 | } |
||
60 | else if("delete".equals(mapping.getParameter())) { |
||
61 | DynaActionForm theForm = (DynaActionForm)form; |
||
62 | Long userId = StringConverter.parseLong(theForm.get("id")); |
||
63 | User u = UserManager.getInstance().get(userId); |
||
64 | |||
65 | if(u.equals(user)) |
||
66 | throw new UserException(CoreResources.DELETE_ME_SELF); |
||
67 | |||
68 | // FIXME: invalidate session of deleted user if it is logged in |
||
69 | // FIXME: if two admins delete each other at the same time |
||
70 | |||
71 | UserManager.getInstance().delete(u); |
||
72 | response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl()); |
||
73 | return null; |
||
74 | } |
||
75 | else if("submit".equals(mapping.getParameter())) { |
||
76 | DynaActionForm theForm = (DynaActionForm)form; |
||
77 | Long userId = StringConverter.parseLong(theForm.get("id")); |
||
78 | User u; |
||
79 | String password = (String)theForm.get("password"); |
||
80 | |||
81 | if(userId == null) { |
||
82 | if(password == null || password.equals("")) |
||
83 | throw new UserException(CoreResources.PASSWORD_REQUIRED); |
||
84 | |||
85 | u = UserManager.getInstance().create(); |
||
86 | } |
||
87 | else { |
||
88 | u = UserManager.getInstance().get(userId); |
||
89 | } |
||
90 | |||
91 | u.setLogin((String)theForm.get("login")); |
||
92 | |||
93 | if(password != null && !password.equals("")) |
||
94 | u.setNewPassword(password); |
||
95 | |||
96 | UserManager.getInstance().save(u); |
||
97 | response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl()); |
||
98 | return null; |
||
99 | } |
||
100 | else { |
||
101 | throw new Exception("unknown mapping parameter"); |
||
102 | } |
||
103 | } |
||
104 | } |