KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > commonimpl > user > CommonUserManager


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.repository.commonimpl.user;
17
18 import org.outerj.daisy.repository.commonimpl.AuthenticatedUser;
19 import org.outerj.daisy.repository.user.*;
20 import org.outerj.daisy.repository.RepositoryException;
21 import org.outerj.daisy.repository.RepositoryListener;
22 import org.outerx.daisy.x10.PublicUserInfoDocument;
23 import org.outerx.daisy.x10.PublicUserInfosDocument;
24
25 public class CommonUserManager {
26     private final UserManagementStrategy userManagementStrategy;
27     private UserCache userCache;
28
29     public CommonUserManager(UserManagementStrategy userManagementStrategy, UserCache userCache) {
30         this.userManagementStrategy = userManagementStrategy;
31         this.userCache = userCache;
32     }
33
34     public RepositoryListener getCacheListener() {
35         return userCache;
36     }
37
38     public Users getUsers(AuthenticatedUser user) throws RepositoryException {
39         return userManagementStrategy.loadUsers(user);
40     }
41
42     public long[] getUserIds(AuthenticatedUser user) throws RepositoryException {
43         return userManagementStrategy.getUserIds(user);
44     }
45
46     public PublicUserInfoDocument getPublicUserInfo(long userId) throws RepositoryException {
47         return userCache.getUser(userId).getPublicUserInfo();
48     }
49
50     public PublicUserInfosDocument getPublicUserInfos(AuthenticatedUser user) throws RepositoryException {
51         long[] ids = userManagementStrategy.getUserIds(user);
52         PublicUserInfoDocument.PublicUserInfo[] publicUserInfos = new PublicUserInfoDocument.PublicUserInfo[ids.length];
53         for (int i = 0; i < ids.length; i++) {
54             publicUserInfos[i] = getPublicUserInfo(ids[i]).getPublicUserInfo();
55         }
56         PublicUserInfosDocument publicUserInfosDocument = PublicUserInfosDocument.Factory.newInstance();
57         publicUserInfosDocument.addNewPublicUserInfos().setPublicUserInfoArray(publicUserInfos);
58         return publicUserInfosDocument;
59     }
60
61     public Roles getRoles(AuthenticatedUser user) throws RepositoryException {
62         return userManagementStrategy.loadRoles(user);
63     }
64
65     public User createUser(String JavaDoc login, AuthenticatedUser user) {
66         return new UserImpl(userManagementStrategy, login, user);
67     }
68
69     public void deleteUser(long userId, AuthenticatedUser user) throws RepositoryException {
70         userManagementStrategy.deleteUser(userId, user);
71     }
72     
73     public Role createRole(String JavaDoc roleName, AuthenticatedUser user) {
74         return new RoleImpl(userManagementStrategy, roleName, user);
75     }
76
77     public void deleteRole(long roleId, AuthenticatedUser user) throws RepositoryException {
78         userManagementStrategy.deleteRole(roleId, user);
79     }
80
81     public User getUser(String JavaDoc login, boolean updateable, AuthenticatedUser user) throws RepositoryException {
82         if (updateable) {
83             return userManagementStrategy.getUser(login, user);
84         } else {
85             User theUser = userCache.getUser(login);
86             if (theUser.getId() != user.getId() && !user.isInAdministratorRole())
87                 throw new UserManagementException("Only administrators can access user records of other users.");
88             return theUser;
89         }
90     }
91
92     public Role getRole(String JavaDoc name, boolean updateable, AuthenticatedUser user) throws RepositoryException {
93         if (updateable) {
94             return userManagementStrategy.getRole(name, user);
95         } else {
96             return userCache.getRole(name);
97         }
98     }
99
100     public User getUser(long userId, boolean updateable, AuthenticatedUser user) throws RepositoryException {
101         if (updateable) {
102             return userManagementStrategy.getUser(userId, user);
103         } else {
104             if (userId != user.getId() && !user.isInAdministratorRole())
105                 throw new UserManagementException("Only administrators can access user records of other users.");
106             return userCache.getUser(userId);
107         }
108     }
109
110     public Role getRole(long roleId, boolean updateable, AuthenticatedUser user) throws RepositoryException {
111         if (updateable) {
112             return userManagementStrategy.getRole(roleId, user);
113         } else {
114             return userCache.getRole(roleId);
115         }
116     }
117
118     public String JavaDoc getUserDisplayName(long userId) throws RepositoryException {
119         return userCache.getUser(userId).getDisplayName();
120     }
121
122     public String JavaDoc getUserLogin(long userId) throws RepositoryException {
123         return userCache.getUser(userId).getLogin();
124     }
125
126     public long getUserId(String JavaDoc login) throws RepositoryException {
127         return userCache.getUser(login).getId();
128     }
129
130     public String JavaDoc getRoleDisplayName(long roleId) throws RepositoryException {
131         return userCache.getRole(roleId).getName();
132     }
133
134     public Users getUsersByEmail(String JavaDoc email, AuthenticatedUser user) throws RepositoryException {
135         return userManagementStrategy.getUsersByEmail(email, user);
136     }
137
138     public AuthenticationSchemeInfos getAuthenticationSchemes(AuthenticatedUser user) throws RepositoryException {
139         return userManagementStrategy.getAuthenticationSchemes(user);
140     }
141 }
142
Popular Tags