001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/security/drm/model/Group.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstr. 19
030: 53115 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042: ---------------------------------------------------------------------------*/
043: package org.deegree.security.drm.model;
044:
045: import java.util.HashSet;
046:
047: import org.deegree.security.GeneralSecurityException;
048: import org.deegree.security.drm.SecurityAccess;
049: import org.deegree.security.drm.SecurityRegistry;
050:
051: /**
052: * Implementation of group-objects. <code>Groups</code> s can be members of other
053: * <code>Groups</code> and have associated <code>Role</code>s.
054: *
055: * @author <a href="mailto:mschneider@lat-lon.de">Markus Schneider </a>
056: * @version $Revision: 9346 $
057: */
058: public class Group extends SecurableObject {
059:
060: public final static int ID_SEC_ADMIN = 2;
061:
062: /**
063: * Creates a new <code>Group</code> -instance.
064: *
065: * @param id
066: * @param name
067: * @param title
068: * @param registry
069: */
070: public Group(int id, String name, String title,
071: SecurityRegistry registry) {
072: this .id = id;
073: this .name = name;
074: this .title = title;
075: this .registry = registry;
076: }
077:
078: /**
079: * Returns the <code>User</code> s that are DIRECT (i.e. not via group membership) members of
080: * this group.
081: *
082: * @param securityAccess
083: * @throws GeneralSecurityException
084: */
085: public User[] getUsers(SecurityAccess securityAccess)
086: throws GeneralSecurityException {
087: return registry.getUsersInGroup(securityAccess, this );
088: }
089:
090: /**
091: * Returns the <code>Groups</code> s that are DIRECT members (i.e. not via inheritance) of
092: * this group.
093: *
094: * @param securityAccess
095: * @throws GeneralSecurityException
096: */
097: public Group[] getGroups(SecurityAccess securityAccess)
098: throws GeneralSecurityException {
099: return registry.getGroupsInGroup(securityAccess, this );
100: }
101:
102: /**
103: * Returns <code>Role</code> s that this group is associated with directly.
104: *
105: * @param securityAccess
106: * @throws GeneralSecurityException
107: */
108: public Role[] getRoles(SecurityAccess securityAccess)
109: throws GeneralSecurityException {
110: return registry.getRolesForGroup(securityAccess, this );
111: }
112:
113: /**
114: * Returns the <code>Privileges</code> that the <code>Group</code> has.
115: *
116: * @param securityAccess
117: * @throws GeneralSecurityException
118: */
119: public Privilege[] getPrivileges(SecurityAccess securityAccess)
120: throws GeneralSecurityException {
121: Role[] roles = securityAccess.getAllRolesForGroup(this );
122: HashSet<Privilege> privilegeSet = new HashSet<Privilege>();
123:
124: // gather privileges for all associated roles
125: for (int i = 0; i < roles.length; i++) {
126: Privilege[] rolePrivileges = registry.getPrivilegesForRole(
127: securityAccess, roles[i]);
128: for (int j = 0; j < rolePrivileges.length; j++) {
129: privilegeSet.add(rolePrivileges[j]);
130: }
131: }
132: return privilegeSet.toArray(new Privilege[privilegeSet.size()]);
133: }
134:
135: /**
136: * Returns the rights that this <code>Group</code> has on the given
137: * <code>SecurableObject</code>.
138: *
139: * @param securityAccess
140: */
141: public RightSet getRights(SecurityAccess securityAccess,
142: SecurableObject object) throws GeneralSecurityException {
143: Role[] roles = securityAccess.getAllRolesForGroup(this );
144: RightSet rights = null;
145: for (int i = 0; i < roles.length; i++) {
146: Right[] roleRights = registry.getRights(securityAccess,
147: object, roles[i]);
148: switch (i) {
149: case 0: {
150: rights = new RightSet(roleRights);
151: break;
152: }
153: default: {
154: rights.merge(new RightSet(roleRights));
155: }
156: }
157: }
158: return rights;
159: }
160:
161: /**
162: * Returns a <code>String</code> representation of this object.
163: */
164: public String toString(SecurityAccess securityAccess) {
165: StringBuffer sb = new StringBuffer("Name: ").append(name);
166:
167: try {
168: sb.append(", Users (Members): [");
169: User[] users = getUsers(securityAccess);
170: for (int i = 0; i < users.length; i++) {
171: sb.append(users[i].getName());
172: if (i != users.length - 1) {
173: sb.append(", ");
174: }
175: }
176: sb.append("]");
177:
178: sb.append(", Groups (Members): [");
179: Group[] groups = getGroups(securityAccess);
180: for (int i = 0; i < groups.length; i++) {
181: sb.append(groups[i].getName());
182: if (i != groups.length - 1) {
183: sb.append(", ");
184: }
185: }
186: sb.append("]");
187:
188: sb.append(", Roles: [");
189: Role[] roles = getRoles(securityAccess);
190: for (int i = 0; i < roles.length; i++) {
191: sb.append(roles[i].getName());
192: if (i != roles.length - 1) {
193: sb.append(", ");
194: }
195: }
196: sb.append("]");
197: } catch (GeneralSecurityException e) {
198: e.printStackTrace();
199: }
200: return sb.toString();
201: }
202: }
|