KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > security > Authorization


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.security;
11
12 import java.util.Set JavaDoc;
13 import java.util.HashSet JavaDoc;
14
15 import org.mmbase.bridge.Query;
16 import org.mmbase.storage.search.Constraint;
17
18 /**
19  * The abstract implementation of the Authorization. To make your own implementation of
20  * authorization, you have to extend this class, and implement the abstract methods.
21  *
22  * @author Eduard Witteveen
23  * @author Michiel Meeuwissen
24  * @version $Id: Authorization.java,v 1.23.2.1 2006/09/07 12:46:22 pierre Exp $
25  */

26 public abstract class Authorization extends Configurable {
27
28     /**
29      * This method should be overrided by an extending class.
30      * It has to be called, when a new Node has been created.
31      * This way, the authentication can create default rights
32      * for this object, depending on the UserContext and generate
33      * logging information.
34      * @param user The UserContext, containing the information
35      * about the user.
36      * @param nodeid The id of the MMObjectNode, which has just been added to
37      * the MMBase cloud.
38      */

39     public abstract void create(UserContext user, int nodeid);
40
41     /**
42      * This method should be overrided by an extending class.
43      * It has to be called, when a Node has been changed.
44      * This way, the authentication can generate log information
45      * for this object, which can be used for accountability
46      * @param user The UserContext, containing the information about the user.
47      * @param nodeid The id of the MMObjectNode, which has just been changed
48      * in the cloud.
49      */

50     public abstract void update(UserContext user, int nodeid);
51
52     /**
53      * This method should be overrided by an extending class.
54      * It has to be called, when a Node has been removed from
55      * the cloud.
56      * This way, the authentication can generate log information
57      * for this node, and remove the authorization object which
58      * belongs to this node.
59      * @param user The UserContext, containing the information
60      * about the user.
61      * @param nodeid The id of the MMObjectNode, which has just been removed
62      * in the cloud.
63      */

64     public abstract void remove(UserContext user, int nodeid);
65
66     /**
67      * This method should be overrided by an extending class.
68      * This method checks if an operation is permitted on a certain node done
69      * by a certain user.
70      * @param user The UserContext, containing the information the user.
71      * @param nodeid The id of the MMObjectNode, which has to be checked.
72      * It the action is CREATE then this will be interpreted as a typedef node.
73      * @param operation The operation which will be performed.
74      * @return <code>true</code> if the operation is permitted,
75      * <code>false</code> if the operation is not permitted,
76      */

77     public abstract boolean check(UserContext user, int nodeid, Operation operation);
78
79
80
81     /**
82      * This method wraps the check-method with the same arguments. The only difference being that it
83      * throws on exception if the specified operation is not permitted.
84      *
85      * It is wise to override check, and not verify (And I wonder why this method is not simply final).
86      *
87      * @exception SecurityException If the assertion fails
88      * @see #check(UserContext, int, Operation)
89      */

90     public void verify(UserContext user, int nodeid, Operation operation) throws SecurityException JavaDoc {
91         if (!check(user, nodeid, operation)) {
92             throw new SecurityException JavaDoc("Operation '" + operation + "' on " + nodeid + " was NOT permitted to " + user.getIdentifier());
93         }
94     }
95
96     /**
97      * This method should be overrided by an extending class.
98      * This method checks if the creation of a certain relation or changing
99      * the source or destination of a certain relation done by a certain
100      * user is permitted.
101      *
102      * @param user The UserContext, containing the information about the user.
103      * @param nodeid The id of the relation which has to be checked. If the operation is CREATE
104      * then this will be interpreted as the typedef node (extending insrel) for the relation to be
105      * created.
106      * @param srcnodeid The id of the (new) source node of the relation.
107      * @param dstnodeid The id of the (new) destination node of the relation.
108      * @param operation The operation which will be performed (CREATE (create
109      * relation) or CHANGE_RELATION (source and/or destination
110      * are changed).
111      * @return <code>true</code> if the operation is permitted,
112      * <code>false</code> if the operation is not permitted,
113      */

114     public abstract boolean check(UserContext user, int nodeid, int srcnodeid, int dstnodeid, Operation operation);
115
116     /**
117      * This method wraps the check-method with the same arguments. The only difference being that it
118      * throws on exception if the specified operation is not permitted.
119      *
120      * It is wise to override check, and not verify (And I wonder why this method is not simply final).
121      *
122      * @exception SecurityException If the assertion fails
123      * @see #check(UserContext, int, int, int, Operation)
124      */

125     public void verify(UserContext user, int nodeid, int srcnodeid, int dstnodeid, Operation operation) throws SecurityException JavaDoc {
126         if (!check(user, nodeid, srcnodeid, dstnodeid, operation)) {
127             throw new SecurityException JavaDoc("Operation '" + operation + "' on " + nodeid + " was NOT permitted to " + user.getIdentifier());
128         }
129     }
130
131     /**
132      * This method could be overrided by an extending class.
133      * This method returns the context of a specific node.
134      * @param user The UserContext, containing the information about the user.
135      * @param nodeid The id of the MMObjectNode, which has to be asserted.
136      * @return the context setting of the node.
137      * @exception SecurityException If operation is not allowed(needs read rights)
138      */

139     public abstract String JavaDoc getContext(UserContext user, int nodeid) throws SecurityException JavaDoc;
140
141     /**
142      * This method could be overrided by an extending class.
143      * This method changes the rights on a node, by telling
144      * the authorization that it should use the context which
145      * is defined.
146      * @param user The UserContext, containing the information about the user.
147      * @param nodeid The id of the MMObjectNode, which has to be asserted.
148      * @param context The context which rights the node will get
149      * @exception SecurityException If operation is not allowed
150      * @exception SecurityException If context is not known
151      */

152     public abstract void setContext(UserContext user, int nodeid, String JavaDoc context) throws SecurityException JavaDoc;
153
154     /**
155      * This method could be overrided by an extending class.
156      * This method returns a list of contexts which can be
157      * used to change the node.
158      * @param user The UserContext, containing the information
159      * about the user.
160      * @param nodeid The id of the MMObjectNode, which has to be asserted.
161      * @return a <code>Set</code> of <code>String</code>s which
162      * represent a context in readable form..
163      * @exception SecurityException
164      */

165     public abstract Set JavaDoc getPossibleContexts(UserContext user, int nodeid) throws SecurityException JavaDoc ;
166
167
168     /**
169      * This method could be overrided by an extending class.
170      * This method returns a list of contexts availabel to a user when creating or searching for an object.
171      * The default implementation returns only the user's own default context.
172      * @param user The UserContext, containing the information
173      * about the user.
174      * @return a <code>Set</code> of <code>String</code>s which
175      * represent a context in readable form..
176      * @exception SecurityException
177      */

178      public Set JavaDoc getPossibleContexts(UserContext user) throws SecurityException JavaDoc {
179          Set JavaDoc contexts = new HashSet JavaDoc();
180          contexts.add(user.getOwnerField());
181          return contexts;
182      }
183
184     /**
185      * Checks rights on a query. This means that the query is explored and (if possible) a
186      * constraint for it is constructed, which, if appied to the query, makes it return only
187      * checked results for the given user.
188      *
189      * Of course, this will normally only be implemented for the 'READ' operation.
190      *
191      * The constraint is <em>not</em> applied automaticly. This has to be done by using BasicQuery.setSecurityConstraint().
192      *
193      * @param user The UserContext, for which the query must be considered
194      * @param query The query to be explored
195      * @return A {@link QueryCheck} structure (containing whether the constriant is sufficient, and the
196      * new constraint or null).
197      *
198      * @since MMBase-1.7
199      */

200
201     public QueryCheck check(UserContext user, Query query, Operation operation) {
202         return NO_CHECK;
203     }
204
205
206     /**
207      * Constant which can be used as a result for the check query function. It means: 'No extra
208      * contraints to be added, and the query's result will have to be postprocessed for security.
209      *
210      * @since MMBase-1.7
211      */

212     public static final QueryCheck NO_CHECK = new QueryCheck(false, null);
213
214     /**
215      * Constant which can be used as a result for the check query function. It means: 'No extra
216      * contraints to be added, but the query's result will <em>not</em> have to be postprocessed for
217      * security. This means that there are no restrictions on the given operation at all (normally:
218      * 'read' is permit to everybody).
219      *
220      * @since MMBase-1.7
221      */

222     public static final QueryCheck COMPLETE_CHECK = new QueryCheck(true, null);
223
224
225     /**
226      * Defines the result of a security check on a query. Such a result has two members: A
227      * 'Constraint' which has to be added to the query and a boolean which sais if the query (with
228      * the given Constraint) has now been fully checked and that it's result does not need further
229      * postprocessing.
230      *
231      * @since MMBase-1.7
232      */

233
234     public static class QueryCheck {
235         Constraint constraint;
236         boolean check;
237         public QueryCheck(boolean ch, Constraint co) {
238             check = ch; constraint = co;
239         }
240         /**
241          * Whether the contained result completely checks for security.
242          */

243         public boolean isChecked() {
244             return check;
245         }
246         /**
247          * The stored constraint. This can be null if no constraint was needed (if checked), or no helpfull
248          * constraint could be constructed (if not checked).
249          */

250         public Constraint getConstraint() {
251             return constraint;
252         }
253         /**
254          * {@inheritDoc}
255          * Used for debugging.
256          * @since MMBase-1.8
257          */

258         public String JavaDoc toString() {
259             return (check ? "CHECKED: " : "NOT CHECKED: ") + constraint;
260         }
261
262
263     }
264 }
265
Popular Tags