KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > security > actions > DeleteAccountsAction


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.security.actions;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25 import org.apache.struts.Globals;
26 import org.apache.struts.action.ActionForm;
27 import org.apache.struts.action.ActionForward;
28 import org.apache.struts.action.ActionMapping;
29 import org.apache.struts.action.ActionMessage;
30 import org.apache.struts.action.ActionMessages;
31
32 import com.sslexplorer.core.CoreAttributeConstants;
33 import com.sslexplorer.core.CoreEvent;
34 import com.sslexplorer.core.CoreEventConstants;
35 import com.sslexplorer.core.CoreServlet;
36 import com.sslexplorer.core.UserDatabaseManager;
37 import com.sslexplorer.core.actions.AuthenticatedDispatchAction;
38 import com.sslexplorer.policyframework.PolicyConstants;
39 import com.sslexplorer.policyframework.PolicyUtil;
40 import com.sslexplorer.security.LogonControllerFactory;
41 import com.sslexplorer.security.SessionInfo;
42 import com.sslexplorer.security.User;
43 import com.sslexplorer.security.UserDatabase;
44
45 /**
46  * Action to delete a users account.
47  *
48  * @author James D Robinson <a HREF="mailto:james@3sp.com">&lt;james@3sp.com&gt;</a>
49  *
50  */

51 public class DeleteAccountsAction extends AuthenticatedDispatchAction {
52
53     /**
54      *
55      */

56     public DeleteAccountsAction() {
57         super();
58     }
59
60     /**
61      * @param mapping
62      * @param form
63      * @param request
64      * @param response
65      * @return ActionForward
66      * @throws Exception
67      */

68     public ActionForward onExecute(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
69                     throws Exception JavaDoc {
70         PolicyUtil.checkPermission(PolicyConstants.ACCOUNTS_AND_GROUPS_RESOURCE_TYPE, PolicyConstants.PERM_DELETE, request);
71
72         User currentUser = isSetupMode() ? null : LogonControllerFactory.getInstance().getUser(request);
73
74         String JavaDoc[] accounts = request.getParameterValues("username");
75         boolean found = false;
76         for (int i = 0; i < accounts.length; i++) {
77             if (currentUser != null && accounts[i].equals(currentUser.getPrincipalName())) {
78                 found = true;
79             }
80         }
81         if (!found) {
82             UserDatabase udb = UserDatabaseManager.getInstance().getUserDatabase(currentUser.getRealm());
83             for (int i = 0; accounts != null && i < accounts.length; i++) {
84                 User user = udb.getAccount(accounts[i]);
85                 if(udb.supportsAccountCreation()) {
86                     try {
87                         udb.deleteAccount(user);
88                         CoreServlet.getServlet().fireCoreEvent(new CoreEvent(this, CoreEventConstants.USER_REMOVED, null, null, CoreEvent.STATE_SUCCESSFUL)
89                                 .addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, user.getPrincipalName())
90                                 .addAttribute(CoreAttributeConstants.EVENT_ATTR_FULL_NAME, user.getFullname()));
91                     } catch (Exception JavaDoc e) {
92                         CoreServlet.getServlet().fireCoreEvent(new CoreEvent(this, CoreEventConstants.USER_REMOVED, null, null, e)
93                                 .addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, user.getPrincipalName())
94                                 .addAttribute(CoreAttributeConstants.EVENT_ATTR_FULL_NAME, user.getFullname()));
95                         throw e;
96                     }
97                 }
98             }
99         } else {
100             ActionMessages mesgs = new ActionMessages();
101             mesgs.add(Globals.ERROR_KEY, new ActionMessage("availableAccounts.cannotDeleteOwnAccount"));
102             saveErrors(request, mesgs);
103         }
104
105         return mapping.findForward("refresh");
106     }
107
108     /* (non-Javadoc)
109      * @see com.sslexplorer.core.actions.CoreAction#getNavigationContext(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
110      */

111     public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
112         return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT;
113     }
114
115     /**
116      * @param mapping
117      * @param form
118      * @param request
119      * @param response
120      * @return ActionForward
121      * @throws Exception
122      */

123     public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
124     throws Exception JavaDoc {
125         return mapping.findForward("success");
126     }
127 }
Popular Tags