KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > commonimpl > AuthenticatedUserImpl


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;
17
18 import org.outerx.daisy.x10.UserInfoDocument;
19 import org.outerj.daisy.repository.user.Role;
20
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 public class AuthenticatedUserImpl implements AuthenticatedUser {
25     private long userId;
26     private String JavaDoc password;
27     private long[] activeRoleIds;
28     private long[] availableRoleIds;
29     private String JavaDoc login;
30
31     public AuthenticatedUserImpl(long userId, String JavaDoc password, long[] activeRoleIds, long[] availableRoleIds, String JavaDoc login) {
32         this.userId = userId;
33         this.password = password;
34         this.activeRoleIds = activeRoleIds;
35         this.availableRoleIds = availableRoleIds;
36         this.login = login;
37     }
38
39     public long getId() {
40         return userId;
41     }
42
43     public String JavaDoc getLogin() {
44         return login;
45     }
46
47     public String JavaDoc getPassword() {
48         return password;
49     }
50
51     public long[] getActiveRoleIds() {
52         return (long[])activeRoleIds.clone();
53     }
54
55     public long[] getAvailableRoleIds() {
56         return (long[])availableRoleIds.clone();
57     }
58
59     public boolean isInRole(long roleId) {
60         for (int i = 0; i < activeRoleIds.length; i++)
61             if (activeRoleIds[i] == roleId)
62                 return true;
63         return false;
64     }
65
66     public boolean isInAdministratorRole() {
67         return isInRole(Role.ADMINISTRATOR);
68     }
69
70     public void setActiveRoleIds(long[] roleIds) {
71         if (roleIds.length < 1)
72             throw new IllegalArgumentException JavaDoc("Error setting active roles: at least one role must be specified.");
73
74         // check that the user has all of the specified roles, and remove duplicates.
75
HashSet JavaDoc roles = new HashSet JavaDoc();
76         for (int i = 0; i < roleIds.length; i++) {
77             if (!hasRole(roleIds[i]))
78                 throw new IllegalArgumentException JavaDoc("Error setting active roles: user \"" + login + "\" does not have this role: \"" + roleIds[i] + "\".");
79             roles.add(new Long JavaDoc(roleIds[i]));
80         }
81
82         long[] newActiveRoles = new long[roles.size()];
83         Iterator JavaDoc it = roles.iterator();
84         int i = 0;
85         while (it.hasNext()) {
86             newActiveRoles[i] = ((Long JavaDoc)it.next()).longValue();
87             i++;
88         }
89         this.activeRoleIds = newActiveRoles;
90     }
91
92     private boolean hasRole(long roleId) {
93         for (int i = 0; i < availableRoleIds.length; i++) {
94             if (availableRoleIds[i] == roleId)
95                 return true;
96         }
97         return false;
98     }
99
100     public UserInfoDocument getXml() {
101         UserInfoDocument userInfoDocument = UserInfoDocument.Factory.newInstance();
102         UserInfoDocument.UserInfo userInfo = userInfoDocument.addNewUserInfo();
103
104         userInfo.setUserId(userId);
105         userInfo.addNewActiveRoleIds().setRoleIdArray(getActiveRoleIds());
106         userInfo.addNewAvailableRoleIds().setRoleIdArray(getAvailableRoleIds());
107
108         return userInfoDocument;
109     }
110 }
111
Popular Tags