KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > util > workflow > InfoGlueAuthorizationCondition


1 package org.infoglue.cms.util.workflow;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.List JavaDoc;
5 import java.util.Map JavaDoc;
6
7 import org.apache.log4j.Logger;
8 import org.infoglue.cms.controllers.kernel.impl.simple.UserControllerProxy;
9 import org.infoglue.cms.security.InfoGluePrincipal;
10 import org.infoglue.cms.security.InfoGlueRole;
11
12 import com.opensymphony.module.propertyset.PropertySet;
13 import com.opensymphony.workflow.Condition;
14 import com.opensymphony.workflow.WorkflowContext;
15
16 /**
17  * This action checks if the user has a particular role.
18  *
19  * @author Mattias Bogeblad
20  */

21
22 public class InfoGlueAuthorizationCondition implements Condition
23 {
24     private final static Logger logger = Logger.getLogger(InfoGlueAuthorizationCondition.class.getName());
25
26     public boolean passesCondition(Map JavaDoc transientVars, Map JavaDoc args, PropertySet ps)
27     {
28         boolean passesCondition = true;
29         
30         try
31         {
32             WorkflowContext context = (WorkflowContext) transientVars.get("context");
33             String JavaDoc roleName = (String JavaDoc)args.get("roleName");
34             String JavaDoc userName = (String JavaDoc)args.get("userName");
35             
36             logger.info("passesCondition.............");
37             logger.info("caller:" + context.getCaller());
38             logger.info("roleName:" + roleName);
39             logger.info("userName:" + userName);
40             
41             InfoGluePrincipal principal = UserControllerProxy.getController().getUser(context.getCaller());
42             
43             if(userName != null && userName.length() > 0 && !principal.getName().equals(userName))
44                 passesCondition = false;
45             
46             if(roleName != null && roleName.length() > 0)
47             {
48                 boolean hasRole = false;
49                 List JavaDoc roles = principal.getRoles();
50                 Iterator JavaDoc rolesIterator = roles.iterator();
51                 while(rolesIterator.hasNext())
52                 {
53                     InfoGlueRole role = (InfoGlueRole)rolesIterator.next();
54                     if(role.getName().equalsIgnoreCase(roleName))
55                         hasRole = true;
56                 }
57                 
58                 if(!hasRole)
59                     passesCondition = false;
60             }
61         }
62         catch (Exception JavaDoc e)
63         {
64             logger.error("A severe error occurred when checking workflow authorization:" + e.getMessage(), e);
65         }
66         
67         return passesCondition;
68     }
69 }
Popular Tags