KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/security/CmsPermissionSet.java,v $
3  * Date : $Date: 2005/07/11 15:55:07 $
4  * Version: $Revision: 1.24 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (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 GmbH, 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 java.util.HashMap JavaDoc;
35 import java.util.Set JavaDoc;
36
37 /**
38  * An immutable permission set that contains both allowed and denied permissions as bitsets.<p>
39  *
40  * Currently supported permissions are:<ul>
41  * <li><code>{@link CmsPermissionSet#PERMISSION_READ}</code> (r) the right to read the contents of a resource</li>
42  * <li><code>{@link CmsPermissionSet#PERMISSION_WRITE}</code> (w) the right to write the contents of a resource</li>
43  * <li><code>{@link CmsPermissionSet#PERMISSION_VIEW}</code> (v) the right to see a resource in listings (workplace)</li>
44  * <li><code>{@link CmsPermissionSet#PERMISSION_CONTROL}</code> (c) the right to set permissions of a resource</li>
45  * <li><code>{@link CmsPermissionSet#PERMISSION_DIRECT_PUBLISH}</code> (d) the right direct publish a resource even without publish project permissions</li></ul><p>
46  *
47  * @author Carsten Weinholz
48  *
49  * @version $Revision: 1.24 $
50  *
51  * @since 6.0.0
52  */

53 public class CmsPermissionSet {
54
55     /** Permission set to check control access. */
56     public static final CmsPermissionSet ACCESS_CONTROL = new CmsPermissionSet(CmsPermissionSet.PERMISSION_CONTROL);
57
58     /** Permission set to check direct publish permissions. */
59     public static final CmsPermissionSet ACCESS_DIRECT_PUBLISH = new CmsPermissionSet(
60         CmsPermissionSet.PERMISSION_DIRECT_PUBLISH);
61
62     /** Permission set to check read access. */
63     public static final CmsPermissionSet ACCESS_READ = new CmsPermissionSet(CmsPermissionSet.PERMISSION_READ);
64
65     /** Permission set to check view access. */
66     public static final CmsPermissionSet ACCESS_VIEW = new CmsPermissionSet(CmsPermissionSet.PERMISSION_VIEW);
67
68     /** Permission set to check write access. */
69     public static final CmsPermissionSet ACCESS_WRITE = new CmsPermissionSet(CmsPermissionSet.PERMISSION_WRITE);
70
71     /** The permission to control a resource. */
72     public static final int PERMISSION_CONTROL = 8;
73
74     /** The permission to direct publish a resource. */
75     public static final int PERMISSION_DIRECT_PUBLISH = 16;
76
77     /** No permissions for a resource (used especially for denied permissions). */
78     public static final int PERMISSION_EMPTY = 0;
79
80     /** All allowed permissions for a resource. */
81     public static final int PERMISSION_FULL = CmsPermissionSet.PERMISSION_READ
82         + CmsPermissionSet.PERMISSION_WRITE
83         + CmsPermissionSet.PERMISSION_VIEW
84         + CmsPermissionSet.PERMISSION_CONTROL
85         + CmsPermissionSet.PERMISSION_DIRECT_PUBLISH;
86
87     /** The permission to read a resource. */
88     public static final int PERMISSION_READ = 1;
89
90     /** The permission to view a resource. */
91     public static final int PERMISSION_VIEW = 4;
92
93     /** The permission to write a resource. */
94     public static final int PERMISSION_WRITE = 2;
95
96     /** HashMap of all available permissions. */
97     private static HashMap JavaDoc m_permissions;
98
99     /** The set of allowed permissions. */
100     protected int m_allowed;
101
102     /** The set of denied permissions. */
103     protected int m_denied;
104
105     /**
106      * Constructor to create a permission set with preset allowed and denied permissions.<p>
107      *
108      * @param allowedPermissions the set of permissions to allow
109      * @param deniedPermissions the set of permissions to deny
110      */

111     public CmsPermissionSet(int allowedPermissions, int deniedPermissions) {
112
113         m_allowed = allowedPermissions;
114         m_denied = deniedPermissions;
115     }
116
117     /**
118      * Constructor to create an empty permission set.<p>
119      */

120     protected CmsPermissionSet() {
121
122         // noop
123
}
124
125     /**
126      * Constructor to create a permission set with preset allowed permissions.<p>
127      *
128      * @param allowedPermissions bitset of allowed permissions
129      */

130     protected CmsPermissionSet(int allowedPermissions) {
131
132         m_allowed = allowedPermissions;
133         m_denied = 0;
134     }
135
136     /**
137      * Returns the message keys of each permission known in the system.<p>
138      *
139      * @return Enumeration of message keys
140      */

141     public static Set JavaDoc getPermissionKeys() {
142
143         return permissions().keySet();
144     }
145
146     /**
147      * Returns the value of a single permission.<p>
148      *
149      * @param key the key of the permission
150      * @return the value of the given permission
151      */

152     public static int getPermissionValue(String JavaDoc key) {
153
154         return ((Integer JavaDoc)permissions().get(key)).intValue();
155     }
156
157     /**
158      * Initializes and returns the hashtable of all permissions known in the system.<p>
159      *
160      * @return hastable with permission keys and values
161      */

162     private static HashMap JavaDoc permissions() {
163         if (m_permissions == null) {
164             m_permissions = new HashMap JavaDoc();
165             m_permissions.put("GUI_PERMISSION_TYPE_READ_0", new Integer JavaDoc(CmsPermissionSet.PERMISSION_READ));
166             m_permissions.put("GUI_PERMISSION_TYPE_WRITE_0", new Integer JavaDoc(CmsPermissionSet.PERMISSION_WRITE));
167             m_permissions.put("GUI_PERMISSION_TYPE_VIEW_0", new Integer JavaDoc(CmsPermissionSet.PERMISSION_VIEW));
168             m_permissions.put("GUI_PERMISSION_TYPE_CONTROL_0", new Integer JavaDoc(CmsPermissionSet.PERMISSION_CONTROL));
169             m_permissions.put("GUI_PERMISSION_TYPE_DIRECT_PUBLISH_0", new Integer JavaDoc(CmsPermissionSet.PERMISSION_DIRECT_PUBLISH));
170         }
171         return m_permissions;
172     }
173
174     /**
175      * @see java.lang.Object#equals(java.lang.Object)
176      */

177     public boolean equals(Object JavaDoc obj) {
178
179         if (obj == this) {
180             return true;
181         }
182         if (obj instanceof CmsPermissionSet) {
183             CmsPermissionSet other = (CmsPermissionSet)obj;
184             return (other.m_allowed == m_allowed) && (other.m_denied == m_denied);
185         }
186         return false;
187     }
188
189     /**
190      * Returns the currently allowed permissions of ths permission set.<p>
191      *
192      * @return the allowed permissions as bitset
193      */

194     public int getAllowedPermissions() {
195
196         return m_allowed;
197     }
198
199     /**
200      * Returns the currently denied permissions of this permission set.<p>
201      *
202      * @return the denied permissions as bitset.
203      */

204     public int getDeniedPermissions() {
205
206         return m_denied;
207     }
208
209     /**
210      * Returns the permissions calculated from this permission set.<p>
211      * These are all permissions allowed but not denied.
212      *
213      * @return the resulting permission set
214      */

215     public int getPermissions() {
216
217         return m_allowed & ~m_denied;
218     }
219
220     /**
221      * Returns the string representation of the current permissions in this permission set.<p>
222      *
223      * @return string of the format {{+|-}{r|w|v|c|d}}*
224      */

225     public String JavaDoc getPermissionString() {
226
227         StringBuffer JavaDoc p = new StringBuffer JavaDoc("");
228
229         if ((m_denied & CmsPermissionSet.PERMISSION_READ) > 0) {
230             p.append("-r");
231         } else if (requiresReadPermission()) {
232             p.append("+r");
233         }
234
235         if ((m_denied & CmsPermissionSet.PERMISSION_WRITE) > 0) {
236             p.append("-w");
237         } else if (requiresWritePermission()) {
238             p.append("+w");
239         }
240
241         if ((m_denied & CmsPermissionSet.PERMISSION_VIEW) > 0) {
242             p.append("-v");
243         } else if (requiresViewPermission()) {
244             p.append("+v");
245         }
246
247         if ((m_denied & CmsPermissionSet.PERMISSION_CONTROL) > 0) {
248             p.append("-c");
249         } else if (requiresControlPermission()) {
250             p.append("+c");
251         }
252
253         if ((m_denied & CmsPermissionSet.PERMISSION_DIRECT_PUBLISH) > 0) {
254             p.append("-d");
255         } else if (requiresDirectPublishPermission()) {
256             p.append("+d");
257         }
258
259         return p.toString();
260     }
261
262     /**
263      * @see java.lang.Object#hashCode()
264      */

265     public int hashCode() {
266
267         return m_allowed * m_denied;
268     }
269
270     /**
271      * Returns true if control permissions (+c) are required by this permission set.<p>
272      *
273      * @return true if control permissions (+c) are required by this permission set
274      */

275     public boolean requiresControlPermission() {
276
277         return 0 < (m_allowed & CmsPermissionSet.PERMISSION_CONTROL);
278     }
279
280     /**
281      * Returns true if direct publish permissions (+d) are required by this permission set.<p>
282      *
283      * @return true if direct publish permissions (+d) are required by this permission set
284      */

285     public boolean requiresDirectPublishPermission() {
286
287         return 0 < (m_allowed & CmsPermissionSet.PERMISSION_DIRECT_PUBLISH);
288     }
289
290     /**
291      * Returns true if read permissions (+r) are required by this permission set.<p>
292      *
293      * @return true if read permissions (+r) are required by this permission set
294      */

295     public boolean requiresReadPermission() {
296
297         return 0 < (m_allowed & CmsPermissionSet.PERMISSION_READ);
298     }
299
300     /**
301      * Returns true if view permissions (+v) are required by this permission set.<p>
302      *
303      * @return true if view permissions (+v) are required by this permission set
304      */

305     public boolean requiresViewPermission() {
306
307         return 0 < (m_allowed & CmsPermissionSet.PERMISSION_VIEW);
308     }
309
310     /**
311      * Returns true if write permissions (+w) are required by this permission set.<p>
312      *
313      * @return true if write permissions (+w) are required by this permission set
314      */

315     public boolean requiresWritePermission() {
316
317         return 0 < (m_allowed & CmsPermissionSet.PERMISSION_WRITE);
318     }
319
320     /**
321      * Returns the String representation of this permission set object.<p>
322      *
323      * @see java.lang.Object#toString()
324      */

325     public String JavaDoc toString() {
326
327         return "[PermissionSet:] " + getPermissionString();
328     }
329 }
Popular Tags