KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (C) 2006 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.repo.security.permissions.PermissionServiceSPI;
26 import org.alfresco.service.cmr.repository.NodeRef;
27 import org.alfresco.service.cmr.repository.NodeService;
28 import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
29 import org.alfresco.service.cmr.security.AuthorityService;
30 import org.alfresco.service.cmr.security.AuthorityType;
31 import org.alfresco.service.cmr.security.PermissionService;
32 import org.alfresco.service.cmr.security.PersonService;
33
34 /**
35  * The default implementation of the authority service.
36  *
37  * @author Andy Hind
38  */

39 public class AuthorityServiceImpl implements AuthorityService
40 {
41     private PersonService personService;
42
43     private NodeService nodeService;
44
45     private AuthorityDAO authorityDAO;
46     
47     private PermissionServiceSPI permissionServiceSPI;
48
49     private Set JavaDoc<String JavaDoc> adminSet = Collections.singleton(PermissionService.ADMINISTRATOR_AUTHORITY);
50
51     private Set JavaDoc<String JavaDoc> guestSet = Collections.singleton(PermissionService.GUEST_AUTHORITY);
52
53     private Set JavaDoc<String JavaDoc> allSet = Collections.singleton(PermissionService.ALL_AUTHORITIES);
54
55     private Set JavaDoc<String JavaDoc> adminUsers;
56
57     private AuthenticationComponent authenticationComponent;
58     
59     public AuthorityServiceImpl()
60     {
61         super();
62     }
63
64     public void setNodeService(NodeService nodeService)
65     {
66         this.nodeService = nodeService;
67     }
68
69     public void setPersonService(PersonService personService)
70     {
71         this.personService = personService;
72     }
73
74     public void setAuthorityDAO(AuthorityDAO authorityDAO)
75     {
76         this.authorityDAO = authorityDAO;
77     }
78     
79     public void setPermissionServiceSPI(PermissionServiceSPI permissionServiceSPI)
80     {
81         this.permissionServiceSPI = permissionServiceSPI;
82     }
83     
84     /**
85      * Currently the admin authority is granted only to the ALFRESCO_ADMIN_USER
86      * user.
87      */

88     public boolean hasAdminAuthority()
89     {
90         String JavaDoc currentUserName = authenticationComponent.getCurrentUserName();
91         return ((currentUserName != null) && adminUsers.contains(currentUserName));
92     }
93
94     // IOC
95

96     public void setAuthenticationComponent(AuthenticationComponent authenticationComponent)
97     {
98         this.authenticationComponent = authenticationComponent;
99     }
100
101     public void setAdminUsers(Set JavaDoc<String JavaDoc> adminUsers)
102     {
103         this.adminUsers = adminUsers;
104     }
105
106     public Set JavaDoc<String JavaDoc> getAuthorities()
107     {
108         Set JavaDoc<String JavaDoc> authorities = new HashSet JavaDoc<String JavaDoc>();
109         String JavaDoc currentUserName = authenticationComponent.getCurrentUserName();
110         if (adminUsers.contains(currentUserName))
111         {
112             authorities.addAll(adminSet);
113         }
114         if(AuthorityType.getAuthorityType(currentUserName) != AuthorityType.GUEST)
115         {
116            authorities.addAll(allSet);
117         }
118         authorities.addAll(getContainingAuthorities(null, currentUserName, false));
119         return authorities;
120     }
121
122     public Set JavaDoc<String JavaDoc> getAllAuthorities(AuthorityType type)
123     {
124         Set JavaDoc<String JavaDoc> authorities = new HashSet JavaDoc<String JavaDoc>();
125         switch (type)
126         {
127         case ADMIN:
128             authorities.addAll(adminSet);
129             break;
130         case EVERYONE:
131             authorities.addAll(allSet);
132             break;
133         case GUEST:
134             authorities.addAll(guestSet);
135             break;
136         case GROUP:
137             authorities.addAll(authorityDAO.getAllAuthorities(type));
138             break;
139         case OWNER:
140              break;
141         case ROLE:
142             authorities.addAll(authorityDAO.getAllAuthorities(type));
143             break;
144         case USER:
145             for (NodeRef personRef : personService.getAllPeople())
146             {
147                 authorities.add(DefaultTypeConverter.INSTANCE.convert(String JavaDoc.class, nodeService.getProperty(personRef,
148                         ContentModel.PROP_USERNAME)));
149             }
150             break;
151         default:
152             break;
153         }
154         return authorities;
155     }
156
157     public void addAuthority(String JavaDoc parentName, String JavaDoc childName)
158     {
159         authorityDAO.addAuthority(parentName, childName);
160     }
161
162     private void checkTypeIsMutable(AuthorityType type)
163     {
164         if((type == AuthorityType.GROUP) || (type == AuthorityType.ROLE))
165         {
166             return;
167         }
168         else
169         {
170             throw new AuthorityException("Trying to modify a fixed authority");
171         }
172     }
173     
174     public String JavaDoc createAuthority(AuthorityType type, String JavaDoc parentName, String JavaDoc shortName)
175     {
176         checkTypeIsMutable(type);
177         String JavaDoc name = getName(type, shortName);
178         authorityDAO.createAuthority(parentName, name);
179         return name;
180     }
181
182     public void deleteAuthority(String JavaDoc name)
183     {
184         AuthorityType type = AuthorityType.getAuthorityType(name);
185         checkTypeIsMutable(type);
186         authorityDAO.deleteAuthority(name);
187         permissionServiceSPI.deletePermissions(name);
188     }
189
190     public Set JavaDoc<String JavaDoc> getAllRootAuthorities(AuthorityType type)
191     {
192         return authorityDAO.getAllRootAuthorities(type);
193     }
194
195     public Set JavaDoc<String JavaDoc> getContainedAuthorities(AuthorityType type, String JavaDoc name, boolean immediate)
196     {
197         return authorityDAO.getContainedAuthorities(type, name, immediate);
198     }
199
200     public Set JavaDoc<String JavaDoc> getContainingAuthorities(AuthorityType type, String JavaDoc name, boolean immediate)
201     {
202         return authorityDAO.getContainingAuthorities(type, name, immediate);
203     }
204
205     public String JavaDoc getName(AuthorityType type, String JavaDoc shortName)
206     {
207         if (type.isFixedString())
208         {
209             return type.getFixedString();
210         }
211         else if (type.isPrefixed())
212         {
213             return type.getPrefixString() + shortName;
214         }
215         else
216         {
217             return shortName;
218         }
219     }
220
221     public String JavaDoc getShortName(String JavaDoc name)
222     {
223         AuthorityType type = AuthorityType.getAuthorityType(name);
224         if (type.isFixedString())
225         {
226             return "";
227         }
228         else if (type.isPrefixed())
229         {
230             return name.substring(type.getPrefixString().length());
231         }
232         else
233         {
234             return name;
235         }
236
237     }
238
239     public void removeAuthority(String JavaDoc parentName, String JavaDoc childName)
240     {
241         authorityDAO.removeAuthority(parentName, childName);
242     }
243
244 }
245
Popular Tags