001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.repository.commonimpl;
017:
018: import org.outerx.daisy.x10.UserInfoDocument;
019: import org.outerj.daisy.repository.user.Role;
020:
021: import java.util.HashSet;
022: import java.util.Iterator;
023: import java.util.Set;
024:
025: public class AuthenticatedUserImpl implements AuthenticatedUser {
026: private long userId;
027: private String password;
028: private long[] activeRoleIds;
029: private long[] availableRoleIds;
030: private String login;
031:
032: public AuthenticatedUserImpl(long userId, String password,
033: long[] activeRoleIds, long[] availableRoleIds, String login) {
034: this .userId = userId;
035: this .password = password;
036: this .activeRoleIds = activeRoleIds;
037: this .availableRoleIds = availableRoleIds;
038: this .login = login;
039: }
040:
041: public long getId() {
042: return userId;
043: }
044:
045: public String getLogin() {
046: return login;
047: }
048:
049: public String getPassword() {
050: return password;
051: }
052:
053: public long[] getActiveRoleIds() {
054: return activeRoleIds.clone();
055: }
056:
057: public long[] getAvailableRoleIds() {
058: return availableRoleIds.clone();
059: }
060:
061: public boolean isInRole(long roleId) {
062: for (long activeRoleId : activeRoleIds)
063: if (activeRoleId == roleId)
064: return true;
065: return false;
066: }
067:
068: public boolean isInAdministratorRole() {
069: return isInRole(Role.ADMINISTRATOR);
070: }
071:
072: public void setActiveRoleIds(long[] roleIds) {
073: if (roleIds.length < 1)
074: throw new IllegalArgumentException(
075: "Error setting active roles: at least one role must be specified.");
076:
077: // check that the user has all of the specified roles, and remove duplicates.
078: Set<Long> roles = new HashSet<Long>();
079: for (long roleId : roleIds) {
080: if (!hasRole(roleId))
081: throw new IllegalArgumentException(
082: "Error setting active roles: user \"" + login
083: + "\" does not have this role: \""
084: + roleId + "\".");
085: roles.add(new Long(roleId));
086: }
087:
088: long[] newActiveRoles = new long[roles.size()];
089: Iterator it = roles.iterator();
090: int i = 0;
091: while (it.hasNext()) {
092: newActiveRoles[i] = ((Long) it.next()).longValue();
093: i++;
094: }
095: this .activeRoleIds = newActiveRoles;
096: }
097:
098: private boolean hasRole(long roleId) {
099: for (long availableRoleId : availableRoleIds) {
100: if (availableRoleId == roleId)
101: return true;
102: }
103: return false;
104: }
105:
106: public UserInfoDocument getXml() {
107: UserInfoDocument userInfoDocument = UserInfoDocument.Factory
108: .newInstance();
109: UserInfoDocument.UserInfo userInfo = userInfoDocument
110: .addNewUserInfo();
111:
112: userInfo.setUserId(userId);
113: userInfo.addNewActiveRoleIds().setRoleIdArray(
114: getActiveRoleIds());
115: userInfo.addNewAvailableRoleIds().setRoleIdArray(
116: getAvailableRoleIds());
117:
118: return userInfoDocument;
119: }
120: }
|