KickJava   Java API By Example, From Geeks To Geeks.

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


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.CreateUserForm;
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 CreateUserAction 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         // check if this action is allowed
36
try {
37             Mapping.begin();
38             if (!new SecurityManagerImpl().canAdminSecurity(User.getInstance(request.getSession().getAttribute("userLogin")+"")) ) {
39                 throw new Exception JavaDoc();
40             }
41         } catch (Exception JavaDoc e) {
42             throw new BackofficeSecurityException();
43         } finally {
44             Mapping.rollback();
45         }
46
47         // get the form
48
CreateUserForm sform = (CreateUserForm) form;
49         ActionErrors errors = sform.myValidate(request);
50
51         // user need cancel
52
if (request.getParameter("cancel") != null) {
53             // Forward to the next page
54
return (mapping.findForward("cancel"));
55         }
56
57         // fill data | first time
58
if (sform.getName() == null) {
59             try {
60                 Mapping.begin();
61                 Vector JavaDoc groups = Group.listAll();
62                 Mapping.rollback();
63
64                 request.setAttribute("groups", groups);
65
66             } catch (Exception JavaDoc e) {
67                 Mapping.rollback();
68                 throw new ServletException JavaDoc(e);
69             }
70
71             // Forward to the view page
72
return (mapping.findForward("view"));
73         }
74
75         // fill data | errors
76
if (errors.size() > 0) {
77             try {
78                 Mapping.begin();
79                 Vector JavaDoc groups = Group.listAll();
80                 Mapping.rollback();
81
82                 request.setAttribute("groups", groups);
83                 request.setAttribute(ERROR_KEY, errors);
84
85             } catch (Exception JavaDoc e) {
86                 Mapping.rollback();
87                 throw new ServletException JavaDoc(e);
88             }
89
90             // Forward to the view page
91
return (mapping.findForward("view"));
92         }
93
94         // all it's ok : update user
95
try {
96             Mapping.begin();
97             User user = User.create(sform.getLogin());
98             user.setMetaData("name", sform.getName());
99             user.setMetaData("email", sform.getEmail());
100             user.setPassword(sform.getPassword());
101             user.resetGroups();
102             if (sform.getGroups() != null) {
103                 for (int i = 0; i < sform.getGroups().length; i++) {
104                     Group group = Group.getInstance(sform.getGroups()[i]);
105                     user.addGroup(group);
106                 }
107             }
108             Mapping.commit();
109
110         } catch (Exception JavaDoc e) {
111             Mapping.rollback();
112             throw new ServletException JavaDoc(e);
113         }
114
115         // Forward to the next page
116
return (mapping.findForward("ok"));
117     }
118
119 }
120
Popular Tags