KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > security > authority > SimpleAuthorityServiceImpl


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.security.authority;
18
19 import java.util.Collections JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import org.alfresco.model.ContentModel;
24 import org.alfresco.repo.security.authentication.AuthenticationComponent;
25 import org.alfresco.service.cmr.repository.NodeRef;
26 import org.alfresco.service.cmr.repository.NodeService;
27 import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
28 import org.alfresco.service.cmr.security.AuthorityService;
29 import org.alfresco.service.cmr.security.AuthorityType;
30 import org.alfresco.service.cmr.security.PermissionService;
31 import org.alfresco.service.cmr.security.PersonService;
32
33 /**
34  * The default implementation of the authority service.
35  *
36  * @author Andy Hind
37  */

38 public class SimpleAuthorityServiceImpl implements AuthorityService
39 {
40     private PersonService personService;
41
42     private NodeService nodeService;
43
44     private Set JavaDoc<String JavaDoc> adminSet = Collections.singleton(PermissionService.ADMINISTRATOR_AUTHORITY);
45
46     private Set JavaDoc<String JavaDoc> guestSet = Collections.singleton(PermissionService.GUEST_AUTHORITY);
47
48     private Set JavaDoc<String JavaDoc> allSet = Collections.singleton(PermissionService.ALL_AUTHORITIES);
49
50     private Set JavaDoc<String JavaDoc> adminUsers;
51
52     private AuthenticationComponent authenticationComponent;
53
54     public SimpleAuthorityServiceImpl()
55     {
56         super();
57     }
58
59     public void setNodeService(NodeService nodeService)
60     {
61         this.nodeService = nodeService;
62     }
63
64     public void setPersonService(PersonService personService)
65     {
66         this.personService = personService;
67     }
68
69     /**
70      * Currently the admin authority is granted only to the ALFRESCO_ADMIN_USER
71      * user.
72      */

73     public boolean hasAdminAuthority()
74     {
75         String JavaDoc currentUserName = authenticationComponent.getCurrentUserName();
76         return ((currentUserName != null) && adminUsers.contains(currentUserName));
77     }
78
79     // IOC
80

81     public void setAuthenticationComponent(AuthenticationComponent authenticationComponent)
82     {
83         this.authenticationComponent = authenticationComponent;
84     }
85
86     public void setAdminUsers(Set JavaDoc<String JavaDoc> adminUsers)
87     {
88         this.adminUsers = adminUsers;
89     }
90  
91     public Set JavaDoc<String JavaDoc> getAuthorities()
92     {
93         Set JavaDoc<String JavaDoc> authorities = new HashSet JavaDoc<String JavaDoc>();
94         String JavaDoc currentUserName = authenticationComponent.getCurrentUserName();
95         if (adminUsers.contains(currentUserName))
96         {
97             authorities.addAll(adminSet);
98         }
99         if(AuthorityType.getAuthorityType(currentUserName) != AuthorityType.GUEST)
100         {
101            authorities.addAll(allSet);
102         }
103         return authorities;
104     }
105
106     public Set JavaDoc<String JavaDoc> getAllAuthorities(AuthorityType type)
107     {
108         Set JavaDoc<String JavaDoc> authorities = new HashSet JavaDoc<String JavaDoc>();
109         switch (type)
110         {
111         case ADMIN:
112             authorities.addAll(adminSet);
113             break;
114         case EVERYONE:
115             authorities.addAll(allSet);
116             break;
117         case GUEST:
118             authorities.addAll(guestSet);
119             break;
120         case GROUP:
121             authorities.addAll(allSet);
122             break;
123         case OWNER:
124              break;
125         case ROLE:
126             break;
127         case USER:
128             for (NodeRef personRef : personService.getAllPeople())
129             {
130                 authorities.add(DefaultTypeConverter.INSTANCE.convert(String JavaDoc.class, nodeService.getProperty(personRef,
131                         ContentModel.PROP_USERNAME)));
132             }
133             break;
134         default:
135             break;
136         }
137         return authorities;
138     }
139
140     public void addAuthority(String JavaDoc parentName, String JavaDoc childName)
141     {
142         
143     }
144
145     
146     public String JavaDoc createAuthority(AuthorityType type, String JavaDoc parentName, String JavaDoc shortName)
147     {
148        return "";
149     }
150
151     public void deleteAuthority(String JavaDoc name)
152     {
153       
154     }
155
156     public Set JavaDoc<String JavaDoc> getAllRootAuthorities(AuthorityType type)
157     {
158         return getAllAuthorities(type);
159     }
160
161     public Set JavaDoc<String JavaDoc> getContainedAuthorities(AuthorityType type, String JavaDoc name, boolean immediate)
162     {
163         return Collections.<String JavaDoc>emptySet();
164     }
165
166     public Set JavaDoc<String JavaDoc> getContainingAuthorities(AuthorityType type, String JavaDoc name, boolean immediate)
167     {
168         return Collections.<String JavaDoc>emptySet();
169     }
170
171     public String JavaDoc getName(AuthorityType type, String JavaDoc shortName)
172     {
173         if (type.isFixedString())
174         {
175             return type.getFixedString();
176         }
177         else if (type.isPrefixed())
178         {
179             return type.getPrefixString() + shortName;
180         }
181         else
182         {
183             return shortName;
184         }
185     }
186
187     public String JavaDoc getShortName(String JavaDoc name)
188     {
189         AuthorityType type = AuthorityType.getAuthorityType(name);
190         if (type.isFixedString())
191         {
192             return "";
193         }
194         else if (type.isPrefixed())
195         {
196             return name.substring(type.getPrefixString().length());
197         }
198         else
199         {
200             return name;
201         }
202
203     }
204
205     public void removeAuthority(String JavaDoc parentName, String JavaDoc childName)
206     {
207         
208     }
209
210 }
211
Popular Tags