KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > util > ejb > SecurityUtil


1 /*
2  * Coefficient - facilitates project based collaboration
3  * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
4  * PO Box 395
5  * Pretoria 0001, RSA
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package za.org.coefficient.util.ejb;
21
22 import za.org.coefficient.authentication.CoefficientUser;
23 import za.org.coefficient.authentication.ProjectMember;
24 import za.org.coefficient.authentication.Role;
25 import za.org.coefficient.core.Project;
26 import za.org.coefficient.util.common.InvokerFactory;
27
28 import java.io.Serializable JavaDoc;
29
30 import java.security.MessageDigest JavaDoc;
31 import java.security.NoSuchAlgorithmException JavaDoc;
32
33 import java.util.ArrayList JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.TreeMap JavaDoc;
38
39 /**
40  * This is a utility class that allows access to users and roles in
41  * the system
42  */

43 public class SecurityUtil implements Serializable JavaDoc {
44     //~ Static fields/initializers =============================================
45

46     public static final String JavaDoc SITE_ADMIN_ROLE_DESC = "Site Administrator";
47     public static final String JavaDoc SITE_MODERATOR_ROLE_DESC = "Site Moderator";
48     public static final String JavaDoc PROJECT_CHAMPION_ROLE_DESC = "Project Champion";
49     public static final String JavaDoc PROJECT_MEMBER_ROLE_DESC = "Project Member";
50     public static final String JavaDoc SITE_MEMBER_ROLE_DESC = "Site Member";
51     public static final String JavaDoc GUEST_ROLE_DESC = "Guest";
52     public static final long SITE_ADMIN_ROLE_VAL = 0;
53     public static final long SITE_MODERATOR_ROLE_VAL = 50;
54     public static final long PROJECT_CHAMPION_ROLE_VAL = 100;
55     public static final long PROJECT_MEMBER_ROLE_VAL = 200;
56     public static final long SITE_MEMBER_ROLE_VAL = 250;
57     public static final long GUEST_ROLE_VAL = 300;
58     private static final String JavaDoc[] HEX_ALPHABET =
59     {
60         "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D",
61         "E", "F"
62     };
63     private static HashMap JavaDoc roleDescCache = new HashMap JavaDoc();
64     private static TreeMap JavaDoc roleValCache = new TreeMap JavaDoc();
65
66     // This is used to seed data in a virgin system
67
static {
68         try {
69             // NOTE: if you ever plan on supporting dynamic role creation then
70
// the roles should be cached better
71
List JavaDoc roles = (List JavaDoc)InvokerFactory.getRemoteInvoker()
72                 .invokeMethodOnService("Role", "getAllRoles", new Object JavaDoc[0]);
73             for (Iterator JavaDoc it = roles.iterator(); it.hasNext();) {
74                 Role role = (Role) it.next();
75                 roleDescCache.put(role.getDescription(), role);
76                 roleValCache.put(new Long JavaDoc(role.getRoleValue()), role);
77             }
78
79             // Make sure we initialize users
80
InvokerFactory.getRemoteInvoker()
81                 .invokeGetterOnModule("UserAdmin", "moduleName");
82         } catch (Exception JavaDoc e) {
83             e.printStackTrace();
84         }
85     }
86
87     //~ Methods ================================================================
88

89     public static List JavaDoc getAllUsers() {
90         List JavaDoc retVals = null;
91         try {
92             retVals = (List JavaDoc)InvokerFactory.getRemoteInvoker()
93                 .invokeGetterOnModule("UserAdmin", "getAllUsers");
94         } catch (Exception JavaDoc e) {
95             //swallow the exception and return an empty list
96
retVals = new ArrayList JavaDoc();
97         }
98
99         return retVals;
100     }
101
102     /**
103      *
104      * This is used to determine the correct role in the role hierarchy for
105      * a user. The hierarchy is from highest to lowest:
106      * 1. SecurityUtil.SITE_ADMIN_ROLE_DESC - a system/project role
107      * 2. SecurityUtil.PROJECT_CHAMPION_ROLE_DESC - a project role
108      * 3. SecurityUtil.PROJECT_MEMBER_ROLE_DESC - a project role
109      * 4. SecurityUtil.SITE_MEMBER_ROLE_DESC - a system role
110      * 5. SecurityUtil.GUEST_ROLE_DESC - a system/project role
111      *
112      *
113      * @param user is the user to determine the role for, if null then this
114      * method will return the guest role
115      * @param project is the project obtained from a context. If null then
116      * the role returned will be obtained
117      * from the system role contained in the user
118      * @return is the highest role associated with the current user given
119      * the project information
120      */

121     public static Role getHighestRoleForUser(CoefficientUser user,
122         Project project) {
123         if (user == null) {
124             return getRoleForDescription(GUEST_ROLE_DESC);
125         } else {
126             Role retVal = user.getSystemRole();
127             if (retVal.getDescription()
128                       .equals(SITE_ADMIN_ROLE_DESC)) {
129                 return retVal;
130             } else if (project != null) {
131                 List JavaDoc projectMembers = project.getMembers();
132                 for (Iterator JavaDoc it = projectMembers.iterator(); it.hasNext();) {
133                     ProjectMember member = (ProjectMember) it.next();
134                     if (member.getCoefficientUser()
135                               .equals(user)) {
136                         if (member.getProjectRole()
137                                   .getRoleValue() < retVal.getRoleValue()) {
138                             retVal = member.getProjectRole();
139                         }
140                     }
141                 }
142             }
143
144             return retVal;
145         }
146     }
147
148     public static Role getRoleForDescription(String JavaDoc roleDescription) {
149         return (Role) roleDescCache.get(roleDescription);
150     }
151
152     public static synchronized Role getRoleForValue(long roleValue) {
153         return (Role) roleValCache.get(new Long JavaDoc(roleValue));
154     }
155
156     public static synchronized List JavaDoc getRoles() {
157         return new ArrayList JavaDoc(roleValCache.values());
158     }
159
160     public static List JavaDoc getUsersWithSystemRole(Role role) {
161         List JavaDoc retVals = null;
162         try {
163             retVals = (List JavaDoc)InvokerFactory.getRemoteInvoker()
164                 .invokeMethodOnModule("UserAdmin", "getUsersWithSystemRole",
165                                       new Object JavaDoc[]{role});
166         } catch (Exception JavaDoc e) {
167             //swallow the exception and return an empty list
168
retVals = new ArrayList JavaDoc();
169         }
170
171         return retVals;
172     }
173
174     /**
175      * Computes an md5 hash of a string.
176      * @param text the hashed string
177      * @return the string hash
178      * @exception NullPointerException if text is null
179      */

180     public static byte[] md5(String JavaDoc text) {
181         // arguments check
182
if (text == null) {
183             throw new NullPointerException JavaDoc("null text");
184         }
185
186         try {
187             MessageDigest JavaDoc md = MessageDigest.getInstance("MD5");
188             md.update(text.getBytes());
189
190             return md.digest();
191         } catch (NoSuchAlgorithmException JavaDoc e) {
192             throw new RuntimeException JavaDoc("Cannot find MD5 algorithm");
193         }
194     }
195
196     /**
197      * Computes an md5 hash and returns the result as a string
198      * made of hexadecimal HEX_ALPHABET whose.
199      * @param text the hashed string
200      * @return the string hash
201      * @exception NullPointerException if text is null
202      */

203     public static String JavaDoc md5AsHexString(String JavaDoc text) {
204         byte[] bytes = md5(text);
205         StringBuffer JavaDoc hex = new StringBuffer JavaDoc();
206         for (int i = 0; i < bytes.length; i++) {
207             hex.append(HEX_ALPHABET[(bytes[i] & 0XF0) >> 4]);
208             hex.append(HEX_ALPHABET[bytes[i] & 0X0F]);
209         }
210
211         return hex.toString();
212     }
213 }
214
Popular Tags