KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nextime > ion > backoffice > action > security > EditUserAction


1 package org.nextime.ion.backoffice.action.security;
2
3 import java.io.IOException JavaDoc;
4 import java.util.Vector JavaDoc;
5
6 import javax.servlet.ServletException JavaDoc;
7 import javax.servlet.http.HttpServletRequest JavaDoc;
8 import javax.servlet.http.HttpServletResponse JavaDoc;
9
10 import org.apache.struts.action.ActionErrors;
11 import org.apache.struts.action.ActionForm;
12 import org.apache.struts.action.ActionForward;
13 import org.apache.struts.action.ActionMapping;
14 import org.nextime.ion.backoffice.action.BaseAction;
15 import org.nextime.ion.backoffice.form.EditUserForm;
16 import org.nextime.ion.backoffice.exception.BackofficeSecurityException;
17 import org.nextime.ion.backoffice.security.SecurityManagerImpl;
18
19 import org.nextime.ion.framework.business.Group;
20 import org.nextime.ion.framework.business.User;
21 import org.nextime.ion.framework.mapping.Mapping;
22
23 public class EditUserAction extends BaseAction {
24
25     public ActionForward perform(
26         ActionMapping mapping,
27         ActionForm form,
28         HttpServletRequest JavaDoc request,
29         HttpServletResponse JavaDoc response)
30         throws IOException JavaDoc, ServletException JavaDoc {
31
32         // check if user is correctly logged
33
checkUser(request);
34         
35
36         // check if the user is authorized to perform this action
37
try {
38             Mapping.begin();
39             if (!new SecurityManagerImpl().canAdminSecurity(User.getInstance(request.getSession().getAttribute("userLogin")+"")) ) {
40                 throw new Exception JavaDoc();
41             }
42         } catch (Exception JavaDoc e) {
43             throw new BackofficeSecurityException();
44         } finally {
45             Mapping.rollback();
46         }
47
48
49         // get the form
50
EditUserForm sform = (EditUserForm) form;
51         ActionErrors errors = sform.myValidate(request);
52
53         // user need cancel
54
if (request.getParameter("cancel") != null) {
55             // Forward to the next page
56
return (mapping.findForward("cancel"));
57         }
58
59         // retrieve id
60
String JavaDoc id =
61             (request.getAttribute("id") != null)
62                 ? request.getAttribute("id").toString()
63                 : request.getParameter("id").toString();
64
65         // fill data | first time
66
if (sform.getName() == null) {
67             try {
68                 Mapping.begin();
69                 User user = User.getInstance(id);
70                 Vector JavaDoc groups = Group.listAll();
71                 Mapping.rollback();
72                 
73                 sform.setEmail((String JavaDoc)user.getMetaData("email"));
74                 sform.setName((String JavaDoc)user.getMetaData("name"));
75                 sform.setGroups(user.getGroupsIds());
76                 sform.setPassword(user.getPassword());
77                 request.setAttribute("groups", groups);
78                 
79             } catch (Exception JavaDoc e) {
80                 Mapping.rollback();
81                 throw new ServletException JavaDoc(e);
82             }
83
84             // Forward to the view page
85
return (mapping.findForward("view"));
86         }
87
88         // fill data | errors
89
if (errors.size() > 0) {
90             try {
91                 Mapping.begin();
92                 Vector JavaDoc groups = Group.listAll();
93                 Mapping.rollback();
94                 
95                 request.setAttribute("groups", groups);
96                 request.setAttribute(ERROR_KEY, errors);
97                                 
98             } catch (Exception JavaDoc e) {
99                 Mapping.rollback();
100                 throw new ServletException JavaDoc(e);
101             }
102
103             // Forward to the view page
104
return (mapping.findForward("view"));
105         }
106
107         // all it's ok : update user
108
try {
109             Mapping.begin();
110             User user = User.getInstance(id);
111             user.setMetaData("name", sform.getName());
112             user.setMetaData("email", sform.getEmail());
113             user.setPassword(sform.getPassword());
114             user.resetGroups();
115             for( int i=0; i<sform.getGroups().length; i++ ) {
116                 Group group = Group.getInstance(sform.getGroups()[i]);
117                 user.addGroup(group);
118             }
119             Mapping.commit();
120             
121         } catch (Exception JavaDoc e) {
122             Mapping.rollback();
123             throw new ServletException JavaDoc(e);
124         }
125
126         // Forward to the next page
127
return (mapping.findForward("ok"));
128     }
129
130 }
131
Popular Tags