1 /***
2 * ObjectPolicy.java
3 *
4 * This file is part of the creme library.
5 * The creme library intends to ease the development effort of large
6 * applications by providing easy to use support classes.
7 *
8 * Copyright (C) 2003 Denis Bregeon
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library 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 GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 * contact information: dbregeon@sourceforge.net
25 */
26 package org.jcreme.permissioning;
27
28 import java.util.Arrays;
29 import java.util.HashSet;
30 import java.util.Hashtable;
31 import java.util.Vector;
32
33 /***
34 *
35 * @author $Author: dbregeon $
36 * @version $Revision: 1.1 $
37 */
38 public class ObjectPolicy {
39 protected static final Hashtable objectPolicies = new Hashtable();
40
41 protected Object policiedObject = null;
42
43 protected Hashtable authorizedPurposes = new Hashtable();
44
45 /***
46 *
47 * @param o
48 * @throws InstantiationException
49 */
50 public ObjectPolicy(PoliciedObject o) throws InstantiationException {
51 if (o == null) {
52 throw new InstantiationException("Cannot have null policiedObject");
53 }
54 this.policiedObject = o;
55 objectPolicies.put(o, this);
56 }
57
58 /***
59 *
60 * @return
61 */
62 public Object getPoliciedObject() {
63 return this.policiedObject;
64 }
65
66 /***
67 *
68 * @param purpose
69 * @param access
70 */
71 public void addPurpose(Purpose purpose, AccessType access) {
72 if ((purpose != null) && (access != null)) {
73 HashSet authorizedAccesses = (HashSet) this.authorizedPurposes
74 .get(purpose);
75 if (authorizedAccesses == null) {
76 authorizedAccesses = new HashSet();
77 this.authorizedPurposes.put(purpose, authorizedAccesses);
78 }
79 authorizedAccesses.add(access);
80 }
81 }
82
83 /***
84 *
85 * @param purpose
86 * @param access
87 */
88 public void removePurpose(Purpose purpose, AccessType access) {
89 if ((purpose != null) && (access != null)) {
90 HashSet authorizedAccesses = (HashSet) this.authorizedPurposes
91 .get(purpose);
92 if (authorizedAccesses != null) {
93 authorizedAccesses.remove(access);
94 }
95 }
96 }
97
98 /***
99 * @param user
100 * used to check eventual ownership.
101 * @param purpose
102 * @param access
103 * @return
104 */
105 public boolean accessAuthorized(User user, Purpose purpose,
106 AccessType access) {
107 boolean result = false;
108 if (access != null) {
109 Vector purposesToCheck = new Vector();
110 Purpose p = purpose;
111 while ((!result) && (p != null)) {
112 HashSet authorizedAccesses = (HashSet) this.authorizedPurposes
113 .get(p);
114 result = (authorizedAccesses != null)
115 && (authorizedAccesses.contains(access));
116 if (!result) {
117 purposesToCheck.addAll(Arrays.asList(p.getParents()));
118 if (purposesToCheck.size() > 0) {
119 p = (Purpose) purposesToCheck.remove(0);
120 } else {
121 p = null;
122 }
123 }
124 }
125 }
126 return result;
127 }
128
129 /***
130 *
131 * @param o
132 * @return
133 */
134 public static ObjectPolicy getObjectPolicy(Object o) {
135 ObjectPolicy result = null;
136 if (o != null) {
137 result = (ObjectPolicy) objectPolicies.get(o);
138 }
139 return result;
140 }
141
142 /***
143 *
144 * @return
145 */
146 public static ObjectPolicy[] getObjectPolicies() {
147 return (ObjectPolicy[]) objectPolicies.values().toArray(
148 new ObjectPolicy[0]);
149 }
150
151 }