01: /*
02: * This file is part of PFIXCORE.
03: *
04: * PFIXCORE is free software; you can redistribute it and/or modify
05: * it under the terms of the GNU Lesser General Public License as published by
06: * the Free Software Foundation; either version 2 of the License, or
07: * (at your option) any later version.
08: *
09: * PFIXCORE is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public License
15: * along with PFIXCORE; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */
18: package de.schlund.pfixcore.auth;
19:
20: import java.util.SortedMap;
21: import java.util.TreeMap;
22:
23: /**
24: *
25: * @author mleidig@schlund.de
26: *
27: */
28: public class AuthenticationImpl implements Authentication {
29:
30: private boolean authenticated;
31: private SortedMap<String, Role> roles;
32: private RoleProvider roleProvider;
33:
34: public AuthenticationImpl(RoleProvider roleProvider) {
35: this .roleProvider = roleProvider;
36: roles = new TreeMap<String, Role>();
37: }
38:
39: public AuthenticationImpl(RoleProvider roleProvider,
40: String[] roleNames, boolean authenticated) {
41: this (roleProvider);
42: for (String roleName : roleNames) {
43: roles.put(roleName, roleProvider.getRole(roleName));
44: }
45: this .authenticated = authenticated;
46: }
47:
48: public synchronized boolean isAuthenticated() {
49: return authenticated;
50: }
51:
52: public synchronized void setAuthenticated(boolean authenticated) {
53: this .authenticated = authenticated;
54: }
55:
56: public synchronized Role[] getRoles() {
57: Role[] rolesCopy = new Role[roles.size()];
58: roles.values().toArray(rolesCopy);
59: return rolesCopy;
60: }
61:
62: public synchronized boolean hasRole(String roleName) {
63: return roles.containsKey(roleName);
64: }
65:
66: public synchronized boolean addRole(String roleName) {
67: if (!roles.containsKey(roleName)) {
68: Role role = roleProvider.getRole(roleName);
69: roles.put(roleName, role);
70: return true;
71: }
72: return false;
73: }
74:
75: public synchronized boolean revokeRole(String roleName) {
76: return roles.remove(roleName) != null;
77: }
78:
79: }
|