KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.nextime.ion.backoffice.action.security;
2
3 import java.io.IOException JavaDoc;
4 import java.util.Collections JavaDoc;
5 import java.util.Vector JavaDoc;
6
7 import javax.servlet.ServletException JavaDoc;
8 import javax.servlet.http.HttpServletRequest JavaDoc;
9 import javax.servlet.http.HttpServletResponse JavaDoc;
10
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.exception.BackofficeSecurityException;
16 import org.nextime.ion.backoffice.security.SecurityManagerImpl;
17
18 import org.nextime.ion.framework.business.PublicationVersion;
19 import org.nextime.ion.framework.business.User;
20 import org.nextime.ion.framework.mapping.Mapping;
21
22 public class DeleteUserAction extends BaseAction {
23
24     public ActionForward perform(
25         ActionMapping mapping,
26         ActionForm form,
27         HttpServletRequest JavaDoc request,
28         HttpServletResponse JavaDoc response)
29         throws IOException JavaDoc, ServletException JavaDoc {
30
31         // check if user is correctly logged
32
checkUser(request);
33
34         // check if the user is authorized to perform this action
35
try {
36             Mapping.begin();
37             if (!new SecurityManagerImpl().canAdminSecurity(User.getInstance(request.getSession().getAttribute("userLogin")+"")) ) {
38                 throw new Exception JavaDoc();
39             }
40         } catch (Exception JavaDoc e) {
41             throw new BackofficeSecurityException();
42         } finally {
43             Mapping.rollback();
44         }
45         
46         // user need cancel
47
if (request.getParameter("cancel") != null) {
48             // Forward to the next page
49
return (mapping.findForward("cancel"));
50         }
51
52
53         // retrieve id
54
String JavaDoc id =
55             (request.getAttribute("id") != null)
56                 ? request.getAttribute("id").toString()
57                 : request.getParameter("id").toString();
58                 
59         // fill data | first time
60
if (request.getParameter("newUser") == null) {
61             try {
62                 Mapping.begin();
63                 Vector JavaDoc users = User.listAll();
64                 Collections.sort(users);
65                 Collections.reverse(users);
66                 request.setAttribute("users",users);
67                 Mapping.rollback();
68             } catch (Exception JavaDoc e) {
69                 Mapping.rollback();
70                 throw new ServletException JavaDoc(e);
71             }
72
73             // Forward to the view page
74
return (mapping.findForward("view"));
75         }
76         
77         // all it's ok : delete user
78
try {
79             Mapping.begin();
80             User user = User.getInstance(id);
81             User newUser = User.getInstance(request.getParameter("newUser"));
82             
83             Vector JavaDoc publis = user.listPublications();
84             for( int i=0; i<publis.size(); i++ ) {
85                 PublicationVersion pv = (PublicationVersion)publis.get(i);
86                 pv.setAuthor(newUser);
87                 pv.getWorkflow().getVariables().put("author", newUser.getLogin());
88             }
89             
90             user.remove();
91             Mapping.commit();
92             
93             if( id.equals( request.getSession().getAttribute("userLogin")) ){
94                 request.getSession().removeAttribute("userLogin");
95             }
96             
97         } catch (Exception JavaDoc e) {
98             Mapping.rollback();
99             throw new ServletException JavaDoc(e);
100         }
101
102         // Forward to the next page
103
return (mapping.findForward("ok"));
104     }
105
106 }
107
Popular Tags