KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > security > ModelAuthorizationRealm


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.core.security;
10
11 import java.util.Iterator JavaDoc;
12 import java.util.Set JavaDoc;
13
14 /**
15  * A realm implementation on top of a scheme and a model.
16  *
17  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
18  * @version $Revision: 1.6 $
19  */

20 public class ModelAuthorizationRealm implements AuthorizationRealm
21 {
22
23    private final Model model;
24    private final SchemeStore store;
25
26    public ModelAuthorizationRealm(Model model, SchemeStore store)
27    {
28       this.model = model;
29       this.store = store;
30    }
31    
32    public Scheme getScheme(String JavaDoc domain)
33    {
34       if (domain == null)
35       {
36          throw new IllegalArgumentException JavaDoc();
37       }
38
39       // Try to get the active scheme
40
Scheme scheme = store.getScheme(domain);
41
42       // Otherwise return the default scheme
43
if (scheme == null)
44       {
45          scheme = model.getDefaultScheme();
46       }
47       return scheme;
48    }
49
50    public Model getModel()
51    {
52       return model;
53    }
54    
55    public boolean hasPermission(String JavaDoc domain, String JavaDoc role, String JavaDoc permission)
56    {
57       return hasPermission(domain, role, new String JavaDoc[0], permission);
58    }
59
60    public boolean hasPermission(String JavaDoc domain, Set JavaDoc roles, String JavaDoc permission)
61    {
62       return hasPermission(domain, roles, new String JavaDoc[0], permission);
63    }
64
65    public boolean hasPermission(String JavaDoc domain, String JavaDoc role, String JavaDoc[] path, String JavaDoc permission)
66    {
67       if (role == null)
68       {
69          throw new IllegalArgumentException JavaDoc();
70       }
71       if (path == null)
72       {
73          throw new IllegalArgumentException JavaDoc();
74       }
75       if (permission == null)
76       {
77          throw new IllegalArgumentException JavaDoc();
78       }
79       for (int index = path.length;index >= 0;index--)
80       {
81          String JavaDoc[] tmp = new String JavaDoc[index];
82          System.arraycopy(path, 0, tmp, 0, index);
83          Scheme scheme = getScheme(domain);
84          Set JavaDoc ownedPermissions = scheme.get(tmp, role);
85          for (Iterator JavaDoc j = ownedPermissions.iterator();j.hasNext();)
86          {
87             String JavaDoc ownedPermission = (String JavaDoc)j.next();
88             if (model.implies(ownedPermission, permission))
89             {
90                return true;
91             }
92          }
93       }
94       return false;
95    }
96
97    public boolean hasPermission(String JavaDoc domain, Set JavaDoc roles, String JavaDoc[] path, String JavaDoc permission)
98    {
99       if (roles == null)
100       {
101          throw new IllegalArgumentException JavaDoc();
102       }
103       if (path == null)
104       {
105          throw new IllegalArgumentException JavaDoc();
106       }
107       if (permission == null)
108       {
109          throw new IllegalArgumentException JavaDoc();
110       }
111       for (int index = path.length;index >= 0;index--)
112       {
113          String JavaDoc[] tmp = new String JavaDoc[index];
114          System.arraycopy(path, 0, tmp, 0, index);
115          Scheme scheme = getScheme(domain);
116          Set JavaDoc ownedPermissions = scheme.get(tmp, roles);
117          for (Iterator JavaDoc j = ownedPermissions.iterator();j.hasNext();)
118          {
119             String JavaDoc ownedPermission = (String JavaDoc)j.next();
120             if (model.implies(ownedPermission, permission))
121             {
122                return true;
123             }
124          }
125       }
126       return false;
127    }
128
129    public boolean hasPermission(String JavaDoc role, String JavaDoc permission)
130    {
131       return hasPermission("", role, permission);
132    }
133
134    public boolean hasPermission(Set JavaDoc roles, String JavaDoc permission)
135    {
136       return hasPermission("", roles, permission);
137    }
138
139    public boolean hasPermission(String JavaDoc role, String JavaDoc[] path, String JavaDoc permission)
140    {
141       return hasPermission("", role, path, permission);
142    }
143
144    public boolean hasPermission(Set JavaDoc roles, String JavaDoc[] path, String JavaDoc permission)
145    {
146       return hasPermission("", roles, path, permission);
147    }
148 }
149
Popular Tags