KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > user > Permissions


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25
26 package org.snipsnap.user;
27
28 import org.radeox.util.logging.Logger;
29
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.StringTokenizer JavaDoc;
34
35 /**
36  * Permissions holds a ACL list with permissions and
37  * roles
38  *
39  * @author stephan
40  * @version $Id: Permissions.java 1606 2004-05-17 10:56:18Z leo $
41  */

42
43 public class Permissions {
44   public final static String JavaDoc EDIT_SNIP = "Edit";
45   public final static String JavaDoc ATTACH_TO_SNIP = "Attach";
46   public final static String JavaDoc POST_TO_SNIP = "Post";
47
48   private Map JavaDoc permissions;
49
50   public Permissions() {
51   }
52
53   public Permissions(Map JavaDoc permissions) {
54     this.permissions = permissions;
55   }
56
57   public Permissions(String JavaDoc permissions) {
58     //@TODO late deserialization
59
this.permissions = deserialize(permissions);
60   }
61
62   private void init() {
63     if (null == permissions) {
64       permissions = new HashMap JavaDoc();
65     }
66   }
67
68   public String JavaDoc toString() {
69     return serialize();
70   }
71
72   public boolean empty() {
73     return null == permissions || permissions.isEmpty();
74   }
75
76   public void remove(String JavaDoc permission, String JavaDoc role) {
77     init();
78     if (permissions.containsKey(permission)) {
79       Roles roles = (Roles) permissions.get(permission);
80       roles.remove(role);
81       // remove permission if there are no more roles
82
if (roles.isEmpty()) {
83         Logger.debug("Empty.");
84         permissions.remove(permission);
85       } else {
86         Logger.debug("not Empty." + permissions.toString());
87       }
88     }
89   }
90
91   public void add(String JavaDoc permission) {
92     init();
93     if (!permissions.containsKey(permission)) {
94       permissions.put(permission, new Roles());
95     }
96   }
97
98   public void add(String JavaDoc permission, String JavaDoc role) {
99     init();
100     if (!permissions.containsKey(permission)) {
101       permissions.put(permission, new Roles());
102     }
103     ((Roles) permissions.get(permission)).add(role);
104     return;
105   }
106
107   public void add(String JavaDoc permission, Roles roles) {
108     init();
109     if (!permissions.containsKey(permission)) {
110       permissions.put(permission, new Roles());
111     }
112     ((Roles) permissions.get(permission)).addAll(roles);
113     return;
114   }
115
116   public boolean exists(String JavaDoc permission, Roles roles) {
117     // if no permission is set, then return false
118
if (null == permissions || !permissions.containsKey(permission)) { return false; }
119     Roles permRoles = (Roles) permissions.get(permission);
120     return permRoles.containsAny(roles);
121   }
122
123   public boolean check(String JavaDoc permission, Roles roles) {
124     // Policy: If no permission is set, everything is allowed
125
if (null == permissions || !permissions.containsKey(permission)) { return true; }
126     Roles permRoles = (Roles) permissions.get(permission);
127 // Logger.debug("--check");
128
// Logger.debug("permission="+ permission);
129
// Logger.debug("roles="+roles);
130
// Logger.debug("object roles="+permRoles);
131
// Logger.debug("result=" + permRoles.containsAny(roles));
132
return permRoles.containsAny(roles);
133   }
134
135   public Map JavaDoc deserialize(String JavaDoc permissions) {
136     Map JavaDoc perms = new HashMap JavaDoc();
137     if (permissions == null || "".equals(permissions)) { return perms; }
138
139     StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(permissions, "|");
140     while (tokenizer.hasMoreTokens()) {
141       String JavaDoc permission = tokenizer.nextToken();
142       Roles roles = getRoles(permission);
143       permission = getPermission(permission);
144       perms.put(permission, roles);
145     }
146
147     return perms;
148   }
149
150   private String JavaDoc serialize() {
151     if (null == permissions || permissions.isEmpty()) { return ""; }
152
153     StringBuffer JavaDoc permBuffer = new StringBuffer JavaDoc();
154     Iterator JavaDoc iterator = permissions.keySet().iterator();
155     while (iterator.hasNext()) {
156       String JavaDoc permission = (String JavaDoc) iterator.next();
157       permBuffer.append(permission);
158       permBuffer.append(":");
159       Roles roles = (Roles) permissions.get(permission);
160       Iterator JavaDoc rolesIterator = roles.iterator();
161       while (rolesIterator.hasNext()) {
162         String JavaDoc role = (String JavaDoc) rolesIterator.next();
163         permBuffer.append(role);
164         if (rolesIterator.hasNext()) {
165           permBuffer.append(",");
166         }
167       }
168       if (iterator.hasNext()) {
169         permBuffer.append("|");
170       }
171     }
172     return permBuffer.toString();
173   }
174
175   private String JavaDoc after(String JavaDoc string, String JavaDoc delimiter) {
176     return string.substring(string.indexOf(delimiter) + 1);
177   }
178
179   private String JavaDoc before(String JavaDoc string, String JavaDoc delimiter) {
180     return string.substring(0, string.indexOf(delimiter));
181   }
182
183   private String JavaDoc getPermission(String JavaDoc rolesString) {
184     return before(rolesString, ":");
185   }
186
187   private Roles getRoles(String JavaDoc rolesString) {
188     Roles roles = new Roles();
189     StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(after(rolesString, ":"), ",");
190     while (tokenizer.hasMoreTokens()) {
191       String JavaDoc role = tokenizer.nextToken();
192       roles.add(role);
193     }
194     return roles;
195   }
196
197 }
198
Popular Tags