KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > security > CmsPrincipal


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/security/CmsPrincipal.java,v $
3  * Date : $Date: 2006/03/27 14:52:48 $
4  * Version: $Revision: 1.2 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2002 - 2005 Alkacon Software (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.security;
33
34 import org.opencms.file.CmsGroup;
35 import org.opencms.file.CmsObject;
36 import org.opencms.file.CmsUser;
37 import org.opencms.main.CmsException;
38 import org.opencms.util.CmsStringUtil;
39 import org.opencms.util.CmsUUID;
40
41 import java.util.Iterator JavaDoc;
42 import java.util.List JavaDoc;
43
44 /**
45  * Common methods shared among user and group principals,
46  * also contains several utility functions to deal with principal instances.<p>
47  *
48  * @author Alexander Kandzior
49  *
50  * @version $Revision: 1.2 $
51  *
52  * @since 6.2.0
53  */

54 public abstract class CmsPrincipal implements I_CmsPrincipal {
55
56     /** The description of this principal. */
57     protected String JavaDoc m_description;
58
59     /** The flags of this principal. */
60     protected int m_flags;
61
62     /** The unique id of this principal. */
63     protected CmsUUID m_id;
64
65     /** The name of this principal. */
66     protected String JavaDoc m_name;
67
68     /**
69      * Empty constructor for subclassing.<p>
70      */

71     protected CmsPrincipal() {
72
73         // empty constructor for subclassing
74
}
75
76     /**
77      * Filters out all principals with flags greater than <code>{@link I_CmsPrincipal#FLAG_CORE_LIMIT}</code>.<p>
78      *
79      * The given parameter list is directly modified, so the returned list is the same object as the input list.<p>
80      *
81      * @param principals a list of <code>{@link CmsPrincipal}</code> objects
82      *
83      * @return the filtered principal list
84      */

85     public static List JavaDoc filterCore(List JavaDoc principals) {
86
87         Iterator JavaDoc it = principals.iterator();
88         while (it.hasNext()) {
89             CmsPrincipal p = (CmsPrincipal)it.next();
90             if (p.getFlags() > I_CmsPrincipal.FLAG_CORE_LIMIT) {
91                 it.remove();
92             }
93         }
94         return principals;
95     }
96
97     /**
98      * Filters out all principals that do not have the given flag set,
99      * but leaving principals with flags less than <code>{@link I_CmsPrincipal#FLAG_CORE_LIMIT}</code> untouched.<p>
100      *
101      * The given parameter list is directly modified, so the returned list is the same object as the input list.<p>
102      *
103      * @param principals a list of <code>{@link CmsPrincipal}</code> objects
104      * @param flag the flag for filtering
105      *
106      * @return the filtered principal list
107      */

108     public static List JavaDoc filterCoreFlag(List JavaDoc principals, int flag) {
109
110         Iterator JavaDoc it = principals.iterator();
111         while (it.hasNext()) {
112             CmsPrincipal p = (CmsPrincipal)it.next();
113             if (p.getFlags() > I_CmsPrincipal.FLAG_CORE_LIMIT && (p.getFlags() & flag) != flag) {
114                 it.remove();
115             }
116         }
117         return principals;
118     }
119
120     /**
121      * Filters out all principals that do not have the given flag set.<p>
122      *
123      * The given parameter list is directly modified, so the returned list is the same object as the input list.<p>
124      *
125      * @param principals the list of <code>{@link CmsPrincipal}</code> objects
126      * @param flag the flag for filtering
127      *
128      * @return the filtered principal list
129      */

130     public static List JavaDoc filterFlag(List JavaDoc principals, int flag) {
131
132         Iterator JavaDoc it = principals.iterator();
133         while (it.hasNext()) {
134             CmsPrincipal p = (CmsPrincipal)it.next();
135             if ((p.getFlags() & flag) != flag) {
136                 it.remove();
137             }
138         }
139         return principals;
140     }
141
142     /**
143      * Returns the provided group name prefixed with <code>{@link I_CmsPrincipal#PRINCIPAL_GROUP}.</code>.<p>
144      *
145      * @param name the name to add the prefix to
146      * @return the provided group name prefixed with <code>{@link I_CmsPrincipal#PRINCIPAL_GROUP}.</code>
147      */

148     public static String JavaDoc getPrefixedGroup(String JavaDoc name) {
149
150         StringBuffer JavaDoc result = new StringBuffer JavaDoc(name.length() + 10);
151         result.append(I_CmsPrincipal.PRINCIPAL_GROUP);
152         result.append('.');
153         result.append(name);
154         return result.toString();
155     }
156
157     /**
158      * Returns the provided user name prefixed with <code>{@link I_CmsPrincipal#PRINCIPAL_USER}.</code>.<p>
159      *
160      * @param name the name to add the prefix to
161      * @return the provided user name prefixed with <code>{@link I_CmsPrincipal#PRINCIPAL_USER}.</code>
162      */

163     public static String JavaDoc getPrefixedUser(String JavaDoc name) {
164
165         StringBuffer JavaDoc result = new StringBuffer JavaDoc(name.length() + 10);
166         result.append(I_CmsPrincipal.PRINCIPAL_USER);
167         result.append('.');
168         result.append(name);
169         return result.toString();
170     }
171
172     /**
173      * Utility function to read a prefixed principal from the OpenCms database using the
174      * provided OpenCms user context.<p>
175      *
176      * The principal must be either prefixed with <code>{@link I_CmsPrincipal#PRINCIPAL_GROUP}.</code> or
177      * <code>{@link I_CmsPrincipal#PRINCIPAL_USER}.</code>.<p>
178      *
179      * @param cms the OpenCms user context to use when reading the principal
180      * @param name the prefixed principal name
181      *
182      * @return the principal read from the OpenCms database
183      *
184      * @throws CmsException in case the principal could not be read
185      */

186     public static I_CmsPrincipal readPrefixedPrincipal(CmsObject cms, String JavaDoc name) throws CmsException {
187
188         if (CmsStringUtil.isNotEmpty(name)) {
189             String JavaDoc upperCaseName = name.toUpperCase();
190             if (upperCaseName.startsWith(I_CmsPrincipal.PRINCIPAL_GROUP)) {
191                 // this principal is a group
192
String JavaDoc groupName = name.substring(I_CmsPrincipal.PRINCIPAL_GROUP.length() + 1);
193                 return cms.readGroup(groupName);
194             } else if (upperCaseName.startsWith(I_CmsPrincipal.PRINCIPAL_USER)) {
195                 // this principal is a user
196
String JavaDoc userName = name.substring(I_CmsPrincipal.PRINCIPAL_USER.length() + 1);
197                 return cms.readUser(userName);
198             }
199         }
200         // invalid principal name was given
201
throw new CmsSecurityException(Messages.get().container(Messages.ERR_INVALID_PRINCIPAL_1, name));
202     }
203
204     /**
205      * Utility function to read a principal of the given type from the OpenCms database using the
206      * provided OpenCms user context.<p>
207      *
208      * The type must either be <code>{@link I_CmsPrincipal#PRINCIPAL_GROUP}</code> or
209      * <code>{@link I_CmsPrincipal#PRINCIPAL_USER}</code>.<p>
210      *
211      * @param cms the OpenCms user context to use when reading the principal
212      * @param type the principal type
213      * @param name the principal name
214      *
215      * @return the principal read from the OpenCms database
216      *
217      * @throws CmsException in case the principal could not be read
218      */

219     public static I_CmsPrincipal readPrincipal(CmsObject cms, String JavaDoc type, String JavaDoc name) throws CmsException {
220
221         if (CmsStringUtil.isNotEmpty(type)) {
222             String JavaDoc upperCaseType = type.toUpperCase();
223             if (PRINCIPAL_GROUP.equals(upperCaseType)) {
224                 // this principal is a group
225
return cms.readGroup(name);
226             } else if (PRINCIPAL_USER.equals(upperCaseType)) {
227                 // this principal is a user
228
return cms.readUser(name);
229             }
230         }
231         // invalid principal type was given
232
throw new CmsSecurityException(Messages.get().container(Messages.ERR_INVALID_PRINCIPAL_TYPE_2, type, name));
233     }
234
235     /**
236      * @see java.lang.Object#equals(java.lang.Object)
237      */

238     public boolean equals(Object JavaDoc obj) {
239
240         if (obj == this) {
241             return true;
242         }
243         if (obj instanceof I_CmsPrincipal) {
244             if (m_id != null) {
245                 return m_id.equals(((I_CmsPrincipal)obj).getId());
246             }
247         }
248         return false;
249     }
250
251     /**
252      * @see org.opencms.security.I_CmsPrincipal#getDescription()
253      */

254     public String JavaDoc getDescription() {
255
256         return m_description;
257     }
258
259     /**
260      * @see org.opencms.security.I_CmsPrincipal#getFlags()
261      */

262     public int getFlags() {
263
264         return m_flags;
265     }
266
267     /**
268      * @see org.opencms.security.I_CmsPrincipal#getId()
269      */

270     public CmsUUID getId() {
271
272         return m_id;
273     }
274
275     /**
276      * @see java.security.Principal#getName()
277      */

278     public String JavaDoc getName() {
279
280         return m_name;
281     }
282
283     /**
284      * @see org.opencms.security.I_CmsPrincipal#getPrefixedName()
285      */

286     public String JavaDoc getPrefixedName() {
287
288         if (isUser()) {
289             return getPrefixedUser(getName());
290         } else if (isGroup()) {
291             return getPrefixedGroup(getName());
292         }
293         return getName();
294     }
295
296     /**
297      * @see java.lang.Object#hashCode()
298      */

299     public int hashCode() {
300
301         if (m_id != null) {
302             return m_id.hashCode();
303         }
304         return CmsUUID.getNullUUID().hashCode();
305     }
306
307     /**
308      * @see org.opencms.security.I_CmsPrincipal#isEnabled()
309      */

310     public boolean isEnabled() {
311
312         return (getFlags() & I_CmsPrincipal.FLAG_DISABLED) == 0;
313     }
314
315     /**
316      * @see org.opencms.security.I_CmsPrincipal#isGroup()
317      */

318     public boolean isGroup() {
319
320         return this instanceof CmsGroup;
321     }
322
323     /**
324      * @see org.opencms.security.I_CmsPrincipal#isUser()
325      */

326     public boolean isUser() {
327
328         return this instanceof CmsUser;
329     }
330
331     /**
332      * @see org.opencms.security.I_CmsPrincipal#setDescription(java.lang.String)
333      */

334     public void setDescription(String JavaDoc description) {
335
336         m_description = description;
337     }
338
339     /**
340      * @see org.opencms.security.I_CmsPrincipal#setEnabled(boolean)
341      */

342     public void setEnabled(boolean enabled) {
343
344         if (enabled != isEnabled()) {
345             // toggle disabled flag if required
346
setFlags(getFlags() ^ I_CmsPrincipal.FLAG_DISABLED);
347         }
348     }
349
350     /**
351      * @see org.opencms.security.I_CmsPrincipal#setFlags(int)
352      */

353     public void setFlags(int value) {
354
355         m_flags = value;
356     }
357
358     /**
359      * @see org.opencms.security.I_CmsPrincipal#setName(java.lang.String)
360      */

361     public void setName(String JavaDoc name) {
362
363         checkName(name);
364         m_name = name;
365     }
366 }
Popular Tags