KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > security > itemactions > EditAccountTableAction


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.itemactions;
21
22 import com.sslexplorer.boot.Util;
23 import com.sslexplorer.core.UserDatabaseManager;
24 import com.sslexplorer.policyframework.Permission;
25 import com.sslexplorer.policyframework.PolicyConstants;
26 import com.sslexplorer.policyframework.PolicyDatabaseFactory;
27 import com.sslexplorer.policyframework.ResourceType;
28 import com.sslexplorer.policyframework.itemactions.AbstractPathAction;
29 import com.sslexplorer.security.SessionInfo;
30 import com.sslexplorer.security.User;
31 import com.sslexplorer.security.UserDatabase;
32 import com.sslexplorer.security.UserItem;
33 import com.sslexplorer.table.AvailableTableItemAction;
34
35 /**
36  * Class which provides the functionality for editing a user.
37  */

38 public class EditAccountTableAction extends AbstractPathAction {
39     private final boolean requiresPasswordSupport;
40     private final boolean requiresAccountCreationSupport;
41
42     /**
43      * @param actionName
44      * @param weight
45      * @param important
46      * @param requiredPath
47      */

48     public EditAccountTableAction(String JavaDoc actionName, int weight, boolean important, String JavaDoc requiredPath) {
49         this(actionName, weight, important, requiredPath, false, false);
50     }
51
52     /**
53      * @param actionName
54      * @param messageResourcesKey
55      * @param weight
56      * @param important
57      * @param requiredPath
58      */

59     public EditAccountTableAction(String JavaDoc actionName, String JavaDoc messageResourcesKey, int weight, boolean important, String JavaDoc requiredPath) {
60         this(actionName, messageResourcesKey, weight, important, requiredPath, false, false);
61     }
62     
63     /**
64      * @param actionName
65      * @param weight
66      * @param important
67      * @param requiredPath
68      * @param requiresPasswordSupport
69      * @param requiresAccountCreationSupport
70      */

71     public EditAccountTableAction(String JavaDoc actionName, int weight, boolean important, String JavaDoc requiredPath, boolean requiresPasswordSupport, boolean requiresAccountCreationSupport) {
72         this(actionName, "security", weight, important, requiredPath, requiresPasswordSupport, requiresAccountCreationSupport);
73     }
74     
75     /**
76      * @param actionName
77      * @param messageResourcesKey
78      * @param weight
79      * @param important
80      * @param requiredPath
81      * @param requiresPasswordSupport
82      * @param requiresAccountCreationSupport
83      */

84     public EditAccountTableAction(String JavaDoc actionName, String JavaDoc messageResourcesKey, int weight, boolean important, String JavaDoc requiredPath, boolean requiresPasswordSupport, boolean requiresAccountCreationSupport) {
85         super(actionName, messageResourcesKey, weight, important, SessionInfo.MANAGEMENT_CONSOLE_CONTEXT, requiredPath);
86         this.requiresPasswordSupport = requiresPasswordSupport;
87         this.requiresAccountCreationSupport = requiresAccountCreationSupport;
88     }
89
90     /*
91      * (non-Javadoc)
92      * @see com.sslexplorer.table.TableItemAction#getPath(com.sslexplorer.table.AvailableTableItemAction)
93      */

94     @Override JavaDoc
95     public String JavaDoc getPath(AvailableTableItemAction availableItem) {
96         UserItem item = (UserItem) availableItem.getRowItem();
97         String JavaDoc principalName = item.getUser().getPrincipalName();
98         String JavaDoc encodedPrincipalName = Util.urlEncode(principalName);
99         return getPath(encodedPrincipalName, availableItem);
100     }
101
102     /*
103      * (non-Javadoc)
104      * @see com.sslexplorer.table.TableItemAction#isEnabled(com.sslexplorer.table.AvailableTableItemAction)
105      */

106     public boolean isEnabled(AvailableTableItemAction availableItem) {
107         try {
108             User user = availableItem.getSessionInfo().getUser();
109             UserItem item = (UserItem) availableItem.getRowItem();
110             return isPermitted(user) && isEnabled(item);
111         } catch (Exception JavaDoc e) {
112             return false;
113         }
114     }
115     
116     private boolean isPermitted(User user) throws Exception JavaDoc {
117         ResourceType resourceType = PolicyDatabaseFactory.getInstance().getResourceType(PolicyConstants.ACCOUNTS_AND_GROUPS_RESOURCE_TYPE_ID);
118         Permission[] permissions = getPermissions(resourceType);
119         boolean isPermitted = permissions.length == 0 ? true : PolicyDatabaseFactory.getInstance().isPermitted(resourceType, permissions, user, true);
120         
121         UserDatabase userDatabase = UserDatabaseManager.getInstance().getUserDatabase(user.getRealm().getResourceId());
122         if(requiresPasswordSupport) {
123             isPermitted = isPermitted && userDatabase.supportsPasswordChange();
124         }
125         if(requiresAccountCreationSupport) {
126             isPermitted = isPermitted && userDatabase.supportsAccountCreation();
127         }
128         return isPermitted;
129     }
130     
131     /**
132      * @param resourceType
133      * @return Permission[]
134      */

135     public Permission[] getPermissions(ResourceType resourceType) {
136         return new Permission[] {resourceType.getPermission(PolicyConstants.PERM_CREATE_AND_ASSIGN_ID)};
137     }
138     
139     /**
140      * @param userItem
141      * @return boolean
142      * @throws Exception
143      */

144     public boolean isEnabled(UserItem userItem) throws Exception JavaDoc {
145         return true;
146     }
147 }
Popular Tags