KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > webman > acl > eventhandler > ACConvenience


1 package de.webman.acl.eventhandler;
2
3 import java.util.*;
4 import java.sql.*;
5 import com.teamkonzept.web.*;
6 import com.teamkonzept.lib.*;
7 import com.teamkonzept.db.*;
8 import de.webman.acl.*;
9 import de.webman.acl.db.*;
10
11 /**
12  * <b>ACConvenience</b> is a collection of functions for
13  * transforming lists of access control data into lists
14  * suitable for template processing.
15  *
16  * @author $Author: uli $
17  * @version $Revision: 1.4 $
18  */

19 public class ACConvenience
20 {
21
22     /**
23      * Returns the login object associated with the specified event.
24      *
25      * @param event the event.
26      * @return the login object associated with the specified event.
27      * @throws TKException if an error occurred during login object
28      * retrieval.
29      */

30     public static Login getWMLogin (TKEvent event)
31         throws TKException
32     {
33         return LoginFactory.getInstance().getLogin(event.getRemoteUser());
34     }
35
36     /**
37      * Returns a hashtable suitable for template processing.
38      *
39      * @param tasks a list of tasks.
40      * @return a hashtable suitable for template processing.
41      * @throws TKException if an error occurred during the
42      * conversion.
43      */

44     public static TKHashtable makeContextTaskList (TKVector tasks)
45         throws TKException
46     {
47         if (tasks == null || tasks.size() == 0)
48         {
49             return null;
50         }
51
52         TKHashtable taskList = new TKHashtable();
53         Enumeration iterator = tasks.elements();
54
55         while (iterator.hasMoreElements())
56         {
57             Task task = (Task) iterator.nextElement();
58             Context context = task.getContext();
59
60             TKHashtable contextTable = (TKHashtable) taskList.get(context.getID());
61
62             if (contextTable == null)
63             {
64                 contextTable = new TKHashtable();
65                 contextTable.put("CONTEXT_NAME", context.getName());
66                 contextTable.put("CONTEXT_SHORTCUT", context.getShortcut());
67
68                 taskList.put(context.getID(), contextTable);
69             }
70
71             TKVector contextTasks = (TKVector) contextTable.get("CONTEXT_TASKS");
72
73             if (contextTasks == null)
74             {
75                 contextTasks = new TKVector();
76
77                 contextTable.put("CONTEXT_TASKS", contextTasks);
78             }
79
80             TKHashtable taskTable = new TKHashtable();
81             taskTable.put("TASK_ID", task.getID());
82             taskTable.put("TASK_NAME", task.getName());
83
84             contextTasks.addElement(taskTable);
85         }
86
87         return taskList;
88     }
89
90
91     /**
92         Collects all Policies (direct and inherited) for a user on a given
93         content node.
94         @param login the login object
95         @param context the context ID
96         @param node the node ID
97         @param policyVector the vector tha will be filled with policies
98     */

99     public static void getNodePolicies(Login login, Integer JavaDoc context, Integer JavaDoc node, TKVector policyVector, Login startLogin)
100         throws Throwable JavaDoc
101     {
102         if (policyVector == null)
103         {
104             return;
105         }
106         // remember the login for the first time
107
if (startLogin == null)
108         {
109             startLogin = login;
110         }
111
112         // first retrieve all applicable policies from my parents recursively
113
TKVector parents = login.getParents();
114         Enumeration pe = parents.elements();
115         while (pe.hasMoreElements())
116         {
117             getNodePolicies((Login) pe.nextElement(), context, node, policyVector, startLogin);
118         }
119
120         // now get my own policies
121
Context c = ContextFactory.getInstance().getContext(context);
122         TKVector directPolicies = login.getPolicies(c, Policy.CONTENT_TREE_ID, node);
123         Enumeration e = directPolicies.elements();
124         while ( e.hasMoreElements() )
125         {
126             Policy policy = (Policy) e.nextElement();
127             Login policyLogin = policy.getLogin();
128             Role policyRole = policy.getRole();
129             Integer JavaDoc policyRef = policy.getObjectReference();
130             TKHashtable hash = new TKHashtable();
131             hash.put("POLICY_ID", policy.getID());
132             TKHashtable nodeHash = getNodeInfo(policyRef);
133             if (nodeHash != null)
134             {
135                 hash.put("POLICY_NODE_ID", nodeHash.get("CONTENT_NODE_ID"));
136                 hash.put("POLICY_NODE_NAME", nodeHash.get("CONTENT_NODE_NAME"));
137                 hash.put("POLICY_NODE_SHORTNAME", nodeHash.get("CONTENT_NODE_SHORTNAME"));
138                 hash.put("POLICY_NODE_TYPE", nodeHash.get("CONTENT_NODE_TYPE"));
139             }
140             if (policyLogin.isProfile() && policyLogin.getID() != startLogin.getID())
141             {
142                 hash.put("POLICY_GROUP_ID", (policyLogin != null ? policyLogin.getID().toString() : ""));
143                 hash.put("POLICY_GROUP_NAME", (policyLogin != null ? policyLogin.getName() : ""));
144             }
145             else
146             {
147                 hash.put("POLICY_USER_ID", (policyLogin != null ? policyLogin.getID().toString() : ""));
148                 hash.put("POLICY_USER_NAME", (policyLogin != null ? policyLogin.getName() : ""));
149             }
150             hash.put("POLICY_ROLE_ID", (policyRole != null ? policyRole.getID().toString() : ""));
151             hash.put("POLICY_ROLE_NAME", (policyRole != null ? policyRole.getName() : ""));
152             if (policy.isAllowed())
153             {
154                 hash.put("POLICY_MODE_ALLOW", "1");
155             }
156             else
157             {
158                 hash.put("POLICY_MODE_DENY", "1");
159             }
160
161             policyVector.addElement(hash);
162         }
163     }
164
165     /**
166         retrieves Information about a content node from the database
167         (quick and dirty)
168     */

169     public static TKHashtable getNodeInfo(Integer JavaDoc nodeId)
170         throws Throwable JavaDoc
171     {
172         if (nodeId == null)
173         {
174             return null;
175         }
176         TKQuery q = TKDBManager.newQuery(TKDBGetNodeInfo.class);
177         q.setQueryParams("CONTENT_NODE_ID", nodeId);
178         q.execute();
179         ResultSet rs = q.fetchResultSet();
180         TKHashtable results = new TKHashtable();
181         if (rs != null && rs.next()) {
182             String JavaDoc id = rs.getString("CONTENT_NODE_ID");
183             results.put("CONTENT_NODE_ID", id);
184
185             String JavaDoc name = rs.getString("CONTENT_NODE_NAME");
186             results.put("CONTENT_NODE_NAME", name);
187
188             String JavaDoc shortname = rs.getString("CONTENT_NODE_SHORTNAME");
189             results.put("CONTENT_NODE_SHORTNAME", shortname);
190
191             String JavaDoc type = rs.getString("CONTENT_NODE_TYPE");
192             results.put("CONTENT_NODE_TYPE", type);
193         }
194         return results;
195     }
196
197     /**
198        Transforms a vector of User objects into a vector with only name, id pairs.
199     */

200     public static TKVector makeUserList( TKVector vec )
201         throws TKException
202     {
203         return makeUserList(vec , (Profile)null);
204     }
205
206     /**
207     Transforms a vector of User objects into a vector with only name, id pairs,
208     also checks if a user is member of a group p.
209     */

210     public static TKVector makeUserList( TKVector vec, Profile p)
211         throws TKException
212     {
213         if (vec == null) return null;
214
215         Enumeration e = vec.elements();
216         TKVector res = new TKVector();
217         while ( e.hasMoreElements() )
218         {
219             User u = (User) e.nextElement();
220             TKHashtable hash = new TKHashtable();
221             hash.put( "USER_ID", u.getID() );
222             hash.put( "USER_NAME", u.getName() );
223             hash.put( "USER_LOGIN", u.getLogin() );
224             if ( p != null && p.hasChild(u))
225             {
226                 hash.put( "VALID", "1" );
227             }
228             res.addElement( hash );
229         }
230         return ( res.size() > 0 ? res : null);
231     }
232
233
234     /**
235        Transforms a vector of Group objects into a vector with only name, id pairs.
236     */

237     public static TKVector makeGroupList( TKVector vec )
238         throws TKException
239     {
240         return makeGroupList(vec , (Login)null);
241     }
242
243     /**
244        Transforms a vector of Group objects into a vector with only name, id pairs.
245        also checks if this login belongs to a group!
246     */

247     public static TKVector makeGroupList( TKVector vec, Login login )
248         throws TKException
249     {
250         if (vec == null) return null;
251         Enumeration e = vec.elements();
252         TKVector res = new TKVector();
253         while ( e.hasMoreElements() ) {
254             Profile p = (Profile) e.nextElement();
255             if (login == null || (login != null && (! p.getID().equals(login.getID()))))
256             {
257                 TKHashtable hash = new TKHashtable();
258                 hash.put( "GROUP_ID", p.getID() );
259                 hash.put( "GROUP_NAME", p.getName() );
260                 hash.put( "GROUP_LOGIN", p.getLogin() );
261                 if (login != null && login.isUser() && p.hasChild(login))
262                 {
263                     hash.put( "VALID","1");
264                 }
265                 else if (login != null && login.isProfile() && ((Profile)login).hasChild(p))
266                 {
267                     hash.put( "VALID","1");
268                 }
269
270                 res.addElement( hash );
271             }
272         }
273         return ( res.size() > 0 ? res : null );
274     }
275
276    /**
277        Transforms a vector of Role objects into a vector with only name, id pairs.
278     */

279     public static TKVector makeRoleList( TKVector vec )
280         throws TKException
281     {
282         if (vec == null) return null;
283         Enumeration e = vec.elements();
284         TKVector res = new TKVector();
285         while ( e.hasMoreElements() ) {
286             Role r= (Role) e.nextElement();
287             TKHashtable hash = new TKHashtable();
288             hash.put( "ROLE_ID", r.getID() );
289             hash.put( "ROLE_NAME", r.getName() );
290             res.addElement( hash );
291         }
292         return ( res.size() > 0 ? res : null );
293     }
294
295     /**
296        Transforms a vector of Task objects into a Hashtable with
297        all necessary information for a printable Report.
298     */

299     public static TKHashtable makeTaskReport( TKVector taskvec)
300         throws TKException
301     {
302         if (taskvec == null)
303         {
304             return null;
305         }
306         Enumeration e = taskvec.elements();
307         TKHashtable res = new TKHashtable();
308         while (e.hasMoreElements())
309         {
310             Object JavaDoc o = e.nextElement();
311             Task t = (Task) o;
312             //Task t = (Task) e.nextElement();
313
// insert this task into the right context-slot
314
Context c = t.getContext();
315             TKVector vec = (TKVector) res.get(c.getName());
316             if (vec == null)
317             {
318                 vec = new TKVector();
319                 res.put(c.getName(), vec);
320             }
321             TKHashtable hash = new TKHashtable();
322             hash.put( "TASK_ID", t.getID() );
323             hash.put( "TASK_NAME", t.getName() );
324             // get all roles that reference this task
325
TKVector roles = RoleFactory.getInstance().getRoles(t);
326             TKVector rolevec = new TKVector();
327             Enumeration re = roles.elements();
328             while (re.hasMoreElements())
329             {
330                 Role r = (Role) re.nextElement();
331                 TKHashtable rolehash = new TKHashtable();
332                 rolehash.put( "ROLE_NAME", r.getName());
333                 rolevec.addElement(rolehash);
334             }
335             hash.put( "TASK_ROLES", rolevec);
336             // get all actions of this task
337
TKVector actions = t.getActions();
338             //TKVector actvec = new TKVector();
339
re = actions.elements();
340             while (re.hasMoreElements())
341             {
342
343             }
344             vec.addElement(hash);
345         }
346         return res;
347     }
348
349    /**
350        Transforms a vector of Task objects into a vector with only name, id pairs.
351     */

352     public static TKVector makeTaskList( TKVector vec)
353         throws TKException
354     {
355         return makeTaskList(vec, (Role)null);
356     }
357
358     /**
359        Transforms a vector of Task objects into a vector with only name, id pairs.
360        Checks if a task is defined for the given role.
361     */

362     public static TKVector makeTaskList( TKVector vec, Role r)
363         throws TKException
364     {
365         if (vec == null) return null;
366         Enumeration e = vec.elements();
367         TKVector res = new TKVector();
368         while ( e.hasMoreElements() ) {
369             Task t = (Task) e.nextElement();
370             TKHashtable hash = new TKHashtable();
371             hash.put( "TASK_ID", t.getID() );
372             hash.put( "TASK_NAME", t.getName() );
373             if (r != null && r.hasTask(t))
374             {
375                 hash.put("VALID", "1");
376             }
377             res.addElement(hash);
378         }
379         return ( res.size() > 0 ? res : null );
380     }
381
382     /**
383        Transforms a vector of Context objects into a vector with only name, id pairs.
384     */

385     public static TKVector makeContextList( TKVector vec )
386         throws TKException
387     {
388         return makeContextList(vec, (Task)null);
389     }
390
391     /**
392        Transforms a vector of Context objects into a vector with only name, id pairs.
393     */

394     public static TKVector makeContextList( TKVector vec, Task t )
395         throws TKException
396     {
397         if (vec == null) return null;
398         Enumeration e = vec.elements();
399         TKVector res = new TKVector();
400         while ( e.hasMoreElements() ) {
401             Context r= (Context) e.nextElement();
402             TKHashtable hash = new TKHashtable();
403             hash.put( "CONTEXT_ID", r.getID() );
404             hash.put( "CONTEXT_NAME", r.getName() );
405             hash.put( "CONTEXT_SHORT", r.getShortcut() );
406             if (t != null && r.getID().equals(t.getContextID()))
407             {
408                 hash.put("VALID", "1");
409             }
410             res.addElement( hash );
411         }
412         return ( res.size() > 0 ? res : null );
413     }
414
415     /**
416        Transforms a vector of Action objects into a vector with only name, id pairs.
417     */

418     public static TKVector makeActionList( TKVector vec, Task t )
419         throws TKException
420     {
421         if (vec == null) return null;
422         Enumeration e = vec.elements();
423         TKVector res = new TKVector();
424         while ( e.hasMoreElements() ) {
425             Action r= (Action) e.nextElement();
426             TKHashtable hash = new TKHashtable();
427             hash.put( "ACTION_ID", r.getID() );
428             hash.put( "ACTION_NAME", r.getName() );
429             if (t != null && t.hasAction(r))
430             {
431                 hash.put("VALID", "1");
432             }
433             res.addElement( hash );
434         }
435         return ( res.size() > 0 ? res : null );
436     }
437
438     /**
439         Transforms a Vector of Policies to a Vector with Role and Context data
440     */

441     public static TKVector makeGlobalRightsList( TKVector vec )
442         throws Throwable JavaDoc
443     {
444         if (vec == null )
445         {
446             return null;
447         }
448         Enumeration e = vec.elements();
449         TKVector res = new TKVector();
450         while ( e.hasMoreElements() )
451         {
452             Policy p = (Policy) e.nextElement();
453
454             // only GLOBAL rights are allowed in this vector!!
455
if (p.getObjectReference() == null)
456             {
457                 Role r = p.getRole();
458                 Context c = p.getContext();
459                 TKHashtable hash = new TKHashtable();
460                 hash.put( "POLICY_ID", p.getID() );
461                 hash.put( "ROLE_ID", r.getID() );
462                 hash.put( "ROLE_NAME", r.getName() );
463                 hash.put( "CONTEXT_ID", c.getID() );
464                 hash.put( "CONTEXT_NAME", c.getName() );
465                 res.addElement(hash);
466             }
467         }
468         return ( res.size() > 0 ? res : null );
469     }
470
471     // ditte sollte mal in sowas wie WebManEvent!! (Alex!!)
472
/**
473         Fills all allowed events into a template
474     */

475     public static void fillEventsIntoTemplate(TemplateBasic t, TKEvent evt, Integer JavaDoc contextId)
476         throws Throwable JavaDoc
477     {
478         Context c = ContextFactory.getInstance().getContext(contextId);
479         Login l = getWMLogin(evt);
480
481         TKVector vec = l.getAllowedEvents(c.getID());
482         if (vec != null)
483         {
484             Enumeration e = vec.elements();
485             while (e.hasMoreElements())
486             {
487                 Event ev = (Event) e.nextElement();
488                 t.set(ev.getName(), "1");
489             }
490         }
491     }
492
493 }
494
Popular Tags