001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/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, SecurityRegistry registry ) {
071            this.id = id;
072            this.name = name;
073            this.title = title;
074            this.registry = registry;
075        }
076    
077        /**
078         * Returns the <code>User</code> s that are DIRECT (i.e. not via group membership) members of
079         * this group.
080         * 
081         * @param securityAccess
082         * @throws GeneralSecurityException
083         */
084        public User[] getUsers( SecurityAccess securityAccess )
085                                throws GeneralSecurityException {
086            return registry.getUsersInGroup( securityAccess, this );
087        }
088    
089        /**
090         * Returns the <code>Groups</code> s that are DIRECT members (i.e. not via inheritance) of
091         * this group.
092         * 
093         * @param securityAccess
094         * @throws GeneralSecurityException
095         */
096        public Group[] getGroups( SecurityAccess securityAccess )
097                                throws GeneralSecurityException {
098            return registry.getGroupsInGroup( securityAccess, this );
099        }
100    
101        /**
102         * Returns <code>Role</code> s that this group is associated with directly.
103         * 
104         * @param securityAccess
105         * @throws GeneralSecurityException
106         */
107        public Role[] getRoles( SecurityAccess securityAccess )
108                                throws GeneralSecurityException {
109            return registry.getRolesForGroup( securityAccess, this );
110        }
111    
112        /**
113         * Returns the <code>Privileges</code> that the <code>Group</code> has.
114         * 
115         * @param securityAccess
116         * @throws GeneralSecurityException
117         */
118        public Privilege[] getPrivileges( SecurityAccess securityAccess )
119                                throws GeneralSecurityException {
120            Role[] roles = securityAccess.getAllRolesForGroup( this );
121            HashSet<Privilege> privilegeSet = new HashSet<Privilege>();
122    
123            // gather privileges for all associated roles
124            for ( int i = 0; i < roles.length; i++ ) {
125                Privilege[] rolePrivileges = registry.getPrivilegesForRole( securityAccess, roles[i] );
126                for ( int j = 0; j < rolePrivileges.length; j++ ) {
127                    privilegeSet.add( rolePrivileges[j] );
128                }
129            }
130            return privilegeSet.toArray( new Privilege[privilegeSet.size()] );
131        }
132    
133        /**
134         * Returns the rights that this <code>Group</code> has on the given
135         * <code>SecurableObject</code>.
136         * 
137         * @param securityAccess
138         */
139        public RightSet getRights( SecurityAccess securityAccess, SecurableObject object )
140                                throws GeneralSecurityException {
141            Role[] roles = securityAccess.getAllRolesForGroup( this );
142            RightSet rights = null;
143            for ( int i = 0; i < roles.length; i++ ) {
144                Right[] roleRights = registry.getRights( securityAccess, object, roles[i] );
145                switch ( i ) {
146                case 0: {
147                    rights = new RightSet( roleRights );
148                    break;
149                }
150                default: {
151                    rights.merge( new RightSet( roleRights ) );
152                }
153                }
154            }
155            return rights;
156        }
157    
158        /**
159         * Returns a <code>String</code> representation of this object.
160         */
161        public String toString( SecurityAccess securityAccess ) {
162            StringBuffer sb = new StringBuffer( "Name: " ).append( name );
163    
164            try {
165                sb.append( ", Users (Members): [" );
166                User[] users = getUsers( securityAccess );
167                for ( int i = 0; i < users.length; i++ ) {
168                    sb.append( users[i].getName() );
169                    if ( i != users.length - 1 ) {
170                        sb.append( ", " );
171                    }
172                }
173                sb.append( "]" );
174    
175                sb.append( ", Groups (Members): [" );
176                Group[] groups = getGroups( securityAccess );
177                for ( int i = 0; i < groups.length; i++ ) {
178                    sb.append( groups[i].getName() );
179                    if ( i != groups.length - 1 ) {
180                        sb.append( ", " );
181                    }
182                }
183                sb.append( "]" );
184    
185                sb.append( ", Roles: [" );
186                Role[] roles = getRoles( securityAccess );
187                for ( int i = 0; i < roles.length; i++ ) {
188                    sb.append( roles[i].getName() );
189                    if ( i != roles.length - 1 ) {
190                        sb.append( ", " );
191                    }
192                }
193                sb.append( "]" );
194            } catch ( GeneralSecurityException e ) {
195                e.printStackTrace();
196            }
197            return sb.toString();
198        }
199    }