KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/security/CmsPermissionSetCustom.java,v $
3  * Date : $Date: 2005/06/23 11:11:44 $
4  * Version: $Revision: 1.6 $
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.StringTokenizer JavaDoc;
35
36 /**
37  * A custom permission set that can be modified during runtime and contains both allowed and denied permissions as bitsets.<p>
38  *
39  * @author Alexander Kandzior
40  *
41  * @version $Revision: 1.6 $
42  *
43  * @since 6.0.0
44  */

45 public class CmsPermissionSetCustom extends CmsPermissionSet {
46
47     /**
48      * Constructor to create an empty permission set.<p>
49      */

50     public CmsPermissionSetCustom() {
51
52         super();
53     }
54
55     /**
56      * Constructor to create a permission set with preset allowed and denied permissions from another permission set.<p>
57      *
58      * The permissions are read from a string representation of permissions
59      * in the format <code>{{+|-}{r|w|v|c|d}}*</code>.<p>
60      *
61      * @param permissions the set of allowed and denied permissions
62      */

63     public CmsPermissionSetCustom(CmsPermissionSet permissions) {
64
65         m_allowed = permissions.m_allowed;
66         m_denied = permissions.m_denied;
67     }
68
69     /**
70      * Constructor to create a permission set with preset allowed permissions.<p>
71      *
72      * @param allowedPermissions bitset of allowed permissions
73      */

74     public CmsPermissionSetCustom(int allowedPermissions) {
75
76         super(allowedPermissions);
77
78     }
79
80     /**
81      * Constructor to create a permission set with preset allowed and denied permissions.<p>
82      *
83      * @param allowedPermissions the set of permissions to allow
84      * @param deniedPermissions the set of permissions to deny
85      */

86     public CmsPermissionSetCustom(int allowedPermissions, int deniedPermissions) {
87
88         super(allowedPermissions, deniedPermissions);
89     }
90
91     /**
92      * Constructor to create a permission set with preset allowed and denied permissions from a String.<p>
93      *
94      * The permissions are read from a string representation of permissions
95      * in the format <code>{{+|-}{r|w|v|c|d}}*</code>.<p>
96      *
97      * @param permissionString the string representation of allowed and denied permissions
98      */

99     public CmsPermissionSetCustom(String JavaDoc permissionString) {
100
101         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(permissionString, "+-", true);
102         m_allowed = 0;
103         m_denied = 0;
104
105         while (tok.hasMoreElements()) {
106             String JavaDoc prefix = tok.nextToken();
107             String JavaDoc suffix = tok.nextToken();
108             switch (suffix.charAt(0)) {
109                 case 'R':
110                 case 'r':
111                     if (prefix.charAt(0) == '+') {
112                         m_allowed |= CmsPermissionSet.PERMISSION_READ;
113                     }
114                     if (prefix.charAt(0) == '-') {
115                         m_denied |= CmsPermissionSet.PERMISSION_READ;
116                     }
117                     break;
118                 case 'W':
119                 case 'w':
120                     if (prefix.charAt(0) == '+') {
121                         m_allowed |= CmsPermissionSet.PERMISSION_WRITE;
122                     }
123                     if (prefix.charAt(0) == '-') {
124                         m_denied |= CmsPermissionSet.PERMISSION_WRITE;
125                     }
126                     break;
127                 case 'V':
128                 case 'v':
129                     if (prefix.charAt(0) == '+') {
130                         m_allowed |= CmsPermissionSet.PERMISSION_VIEW;
131                     }
132                     if (prefix.charAt(0) == '-') {
133                         m_denied |= CmsPermissionSet.PERMISSION_VIEW;
134                     }
135                     break;
136                 case 'C':
137                 case 'c':
138                     if (prefix.charAt(0) == '+') {
139                         m_allowed |= CmsPermissionSet.PERMISSION_CONTROL;
140                     }
141                     if (prefix.charAt(0) == '-') {
142                         m_denied |= CmsPermissionSet.PERMISSION_CONTROL;
143                     }
144                     break;
145                 case 'D':
146                 case 'd':
147                     if (prefix.charAt(0) == '+') {
148                         m_allowed |= CmsPermissionSet.PERMISSION_DIRECT_PUBLISH;
149                     }
150                     if (prefix.charAt(0) == '-') {
151                         m_denied |= CmsPermissionSet.PERMISSION_DIRECT_PUBLISH;
152                     }
153                     break;
154                 default:
155                     // ignore
156
break;
157             }
158         }
159     }
160
161     /**
162      * Sets permissions from another permission set additionally both as allowed and denied permissions.<p>
163      *
164      * @param permissionSet the set of permissions to set additionally.
165      */

166     public void addPermissions(CmsPermissionSet permissionSet) {
167
168         m_allowed |= permissionSet.m_allowed;
169         m_denied |= permissionSet.m_denied;
170     }
171
172     /**
173      * Returns a clone of this Objects instance.<p>
174      *
175      * @return a clone of this instance
176      */

177     public Object JavaDoc clone() {
178
179         return new CmsPermissionSetCustom(m_allowed, m_denied);
180     }
181
182     /**
183      * Sets permissions additionally as denied permissions.<p>
184      *
185      * @param permissions bitset of permissions to deny
186      */

187     public void denyPermissions(int permissions) {
188
189         m_denied |= permissions;
190     }
191
192     /**
193      * Sets permissions additionally as allowed permissions.<p>
194      *
195      * @param permissions bitset of permissions to allow
196      */

197     public void grantPermissions(int permissions) {
198
199         m_allowed |= permissions;
200     }
201
202     /**
203      * Set permissions from another permission set both as allowed and denied permissions.<p>
204      * Permissions formerly set are overwritten.
205      *
206      * @param permissionSet the set of permissions
207      */

208     public void setPermissions(CmsPermissionSet permissionSet) {
209
210         m_allowed = permissionSet.m_allowed;
211         m_denied = permissionSet.m_denied;
212     }
213
214     /**
215      * Sets permissions as allowed and denied permissions in the permission set.<p>
216      * Permissions formerly set are overwritten.
217      *
218      * @param allowedPermissions bitset of permissions to allow
219      * @param deniedPermissions bitset of permissions to deny
220      */

221     public void setPermissions(int allowedPermissions, int deniedPermissions) {
222
223         m_allowed = allowedPermissions;
224         m_denied = deniedPermissions;
225     }
226
227 }
Popular Tags