1 package org.apache.archiva.redback.rbac.ldap;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import com.google.common.base.Function;
23 import com.google.common.collect.Lists;
24 import org.apache.archiva.redback.common.ldap.MappingException;
25 import org.apache.archiva.redback.common.ldap.connection.LdapConnection;
26 import org.apache.archiva.redback.common.ldap.connection.LdapConnectionFactory;
27 import org.apache.archiva.redback.common.ldap.connection.LdapException;
28 import org.apache.archiva.redback.common.ldap.role.LdapRoleMapper;
29 import org.apache.archiva.redback.components.cache.Cache;
30 import org.apache.archiva.redback.configuration.UserConfiguration;
31 import org.apache.archiva.redback.configuration.UserConfigurationKeys;
32 import org.apache.archiva.redback.rbac.AbstractRBACManager;
33 import org.apache.archiva.redback.rbac.AbstractRole;
34 import org.apache.archiva.redback.rbac.Operation;
35 import org.apache.archiva.redback.rbac.Permission;
36 import org.apache.archiva.redback.rbac.RBACManager;
37 import org.apache.archiva.redback.rbac.RBACManagerListener;
38 import org.apache.archiva.redback.rbac.RBACObjectAssertions;
39 import org.apache.archiva.redback.rbac.RbacManagerException;
40 import org.apache.archiva.redback.rbac.RbacObjectInvalidException;
41 import org.apache.archiva.redback.rbac.RbacObjectNotFoundException;
42 import org.apache.archiva.redback.rbac.RbacPermanentException;
43 import org.apache.archiva.redback.rbac.Resource;
44 import org.apache.archiva.redback.rbac.Role;
45 import org.apache.archiva.redback.rbac.UserAssignment;
46 import org.apache.archiva.redback.users.User;
47 import org.apache.archiva.redback.users.UserManager;
48 import org.apache.archiva.redback.users.UserManagerException;
49 import org.apache.archiva.redback.users.ldap.ctl.LdapController;
50 import org.apache.archiva.redback.users.ldap.ctl.LdapControllerException;
51 import org.apache.commons.lang.StringUtils;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54 import org.springframework.stereotype.Service;
56 import javax.annotation.PostConstruct;
57 import javax.inject.Inject;
58 import javax.inject.Named;
59 import javax.naming.NamingException;
60 import javax.naming.directory.DirContext;
61 import java.util.ArrayList;
62 import java.util.Collection;
63 import java.util.Collections;
64 import java.util.HashSet;
65 import java.util.List;
70 * LdapRbacManager will read datas from ldap for mapping groups to role.
71 * Write operations will delegate to cached implementation.
73 * @author Olivier Lamy
75 @Service( "rbacManager#ldap" )
76 public class LdapRbacManager
77 extends AbstractRBACManager
78 implements RBACManager, RBACManagerListener
81 private Logger log = LoggerFactory.getLogger( getClass() );
84 @Named( value = "rbacManager#cached" )
85 private RBACManager rbacImpl;
88 @Named( value = "ldapRoleMapper#default" )
89 private LdapRoleMapper ldapRoleMapper;
92 @Named( value = "userConfiguration#default" )
93 private UserConfiguration userConf;
96 @Named( value = "userManager#ldap" )
97 private UserManager userManager;
100 private LdapConnectionFactory ldapConnectionFactory;
103 private LdapController ldapController;
106 @Named( value = "cache#ldapRoles" )
107 private Cache<String, Role> rolesCache;
109 private boolean writableLdap = false;
113 public void initialize()
115 this.writableLdap = userConf.getBoolean( UserConfigurationKeys.LDAP_WRITABLE, this.writableLdap );
119 public void addChildRole( Role role, Role childRole )
120 throws RbacObjectInvalidException, RbacManagerException
122 this.rbacImpl.addChildRole( role, childRole );
125 public void addListener( RBACManagerListener listener )
127 super.addListener( listener );
128 this.rbacImpl.addListener( listener );
131 public Operation createOperation( String name )
132 throws RbacManagerException
134 return this.rbacImpl.createOperation( name );
137 public Permission createPermission( String name )
138 throws RbacManagerException
140 return this.rbacImpl.createPermission( name );
143 public Permission createPermission( String name, String operationName, String resourceIdentifier )
144 throws RbacManagerException
146 return this.rbacImpl.createPermission( name, operationName, resourceIdentifier );
149 public Resource createResource( String identifier )
150 throws RbacManagerException
152 return this.rbacImpl.createResource( identifier );
155 public Role createRole( String name )
157 return this.rbacImpl.createRole( name );
160 public UserAssignment createUserAssignment( String username )
161 throws RbacManagerException
163 // TODO ldap cannot or isWritable ldap ?
164 return this.rbacImpl.createUserAssignment( username );
167 public void eraseDatabase()
171 LdapConnection ldapConnection = null;
172 DirContext context = null;
175 ldapConnection = ldapConnectionFactory.getConnection();
176 context = ldapConnection.getDirContext();
177 ldapRoleMapper.removeAllRoles( context );
179 catch ( MappingException e )
181 log.warn( "skip error removing all roles {}", e.getMessage() );
183 catch ( LdapException e )
185 log.warn( "skip error removing all roles {}", e.getMessage() );
189 closeContext( context );
190 closeLdapConnection( ldapConnection );
193 this.rbacImpl.eraseDatabase();
197 * @see org.apache.archiva.redback.rbac.RBACManager#getAllAssignableRoles()
199 public List<Role> getAllAssignableRoles()
200 throws RbacManagerException
204 Collection<Collection<String>> roleNames = ldapRoleMapper.getLdapGroupMappings().values();
206 Set<Role> roles = new HashSet<Role>();
208 for ( Collection<String> names : roleNames )
210 for ( String name : names )
212 roles.add( new RoleImpl( name ) );
216 return new ArrayList<Role>( roles );
218 catch ( MappingException e )
220 throw new RbacManagerException( e.getMessage(), e );
224 public List<Operation> getAllOperations()
225 throws RbacManagerException
227 return this.rbacImpl.getAllOperations();
230 public List<Permission> getAllPermissions()
231 throws RbacManagerException
233 return this.rbacImpl.getAllPermissions();
236 public List<Resource> getAllResources()
237 throws RbacManagerException
239 return this.rbacImpl.getAllResources();
242 public List<Role> getAllRoles()
243 throws RbacManagerException
245 LdapConnection ldapConnection = null;
246 DirContext context = null;
249 ldapConnection = ldapConnectionFactory.getConnection();
250 context = ldapConnection.getDirContext();
252 List<String> groups = ldapRoleMapper.getAllGroups( context );
253 return mapToRoles( groups );
255 catch ( MappingException e )
257 throw new RbacManagerException( e.getMessage(), e );
259 catch ( LdapException e )
261 throw new RbacManagerException( e.getMessage(), e );
265 closeContext( context );
266 closeLdapConnection( ldapConnection );
268 //return this.rbacImpl.getAllRoles();
272 public List<UserAssignment> getAllUserAssignments()
273 throws RbacManagerException
275 LdapConnection ldapConnection = null;
276 DirContext context = null;
279 ldapConnection = ldapConnectionFactory.getConnection();
280 context = ldapConnection.getDirContext();
281 Map<String, Collection<String>> usersWithRoles = ldapController.findUsersWithRoles( context );
282 List<UserAssignment> userAssignments = new ArrayList<UserAssignment>( usersWithRoles.size() );
284 for ( Map.Entry<String, Collection<String>> entry : usersWithRoles.entrySet() )
286 UserAssignment userAssignment = new UserAssignmentImpl( entry.getKey(), entry.getValue() );
287 userAssignments.add( userAssignment );
290 return userAssignments;
292 catch ( LdapControllerException e )
294 throw new RbacManagerException( e.getMessage(), e );
296 catch ( LdapException e )
298 throw new RbacManagerException( e.getMessage(), e );
302 closeContext( context );
303 closeLdapConnection( ldapConnection );
307 protected void closeLdapConnection( LdapConnection ldapConnection )
309 if ( ldapConnection != null )
311 ldapConnection.close();
315 protected void closeContext( DirContext context )
317 if ( context != null )
323 catch ( NamingException e )
325 log.warn( "skip issue closing context: {}", e.getMessage() );
331 * public Map<String, List<Permission>> getAssignedPermissionMap( String username )
332 * throws RbacManagerException
334 * return this.rbacImpl.getAssignedPermissionMap( username );
338 /*public Set<Permission> getAssignedPermissions( String username )
339 throws RbacObjectNotFoundException, RbacManagerException
342 return this.rbacImpl.getAssignedPermissions( username );
344 private List<Role> mapToRoles( List<String> groups )
345 throws MappingException, RbacManagerException
347 if ( groups == null || groups.isEmpty() )
349 return Collections.emptyList();
352 List<Role> roles = new ArrayList<Role>( groups.size() );
353 Map<String, Collection<String>> mappedGroups = ldapRoleMapper.getLdapGroupMappings();
354 for ( String group : groups )
356 Collection<String> roleNames = mappedGroups.get( group );
357 if ( roleNames != null )
359 for ( String roleName : roleNames )
361 Role role = getRole( roleName );
373 public Collection<Role> getAssignedRoles( String username )
374 throws RbacManagerException
377 LdapConnection ldapConnection = null;
378 DirContext context = null;
383 ldapConnection = ldapConnectionFactory.getConnection();
384 context = ldapConnection.getDirContext();
385 List<String> roleNames = ldapRoleMapper.getRoles( username, context );
387 if ( roleNames.isEmpty() )
389 return Collections.emptyList();
392 List<Role> roles = new ArrayList<Role>( roleNames.size() );
394 for ( String name : roleNames )
396 roles.add( this.rbacImpl.getRole( name ) );// new RoleImpl( name ) );
401 catch ( MappingException e )
403 throw new RbacManagerException( e.getMessage(), e );
405 catch ( LdapException e )
407 throw new RbacManagerException( e.getMessage(), e );
411 public Collection<Role> getAssignedRoles( UserAssignment userAssignment )
412 throws RbacManagerException
414 return getAssignedRoles( userAssignment.getPrincipal() );
417 public Map<String, Role> getChildRoles( Role role )
418 throws RbacManagerException
420 return this.rbacImpl.getChildRoles( role );
423 public Map<String, Role> getParentRoles( Role role )
424 throws RbacManagerException
426 return this.rbacImpl.getParentRoles( role );
430 public Collection<Role> getEffectivelyAssignedRoles( String username )
431 throws RbacManagerException
434 return this.rbacImpl.getEffectivelyAssignedRoles( username );
438 * public Collection<Role> getEffectivelyUnassignedRoles( String username )
439 * throws RbacManagerException
442 * return this.rbacImpl.getEffectivelyUnassignedRoles( username );
446 public Set<Role> getEffectiveRoles( Role role )
447 throws RbacManagerException
449 return this.rbacImpl.getEffectiveRoles( role );
452 public Resource getGlobalResource()
453 throws RbacManagerException
455 return this.rbacImpl.getGlobalResource();
458 public Operation getOperation( String operationName )
459 throws RbacManagerException
461 return this.rbacImpl.getOperation( operationName );
464 public Permission getPermission( String permissionName )
465 throws RbacManagerException
467 return this.rbacImpl.getPermission( permissionName );
470 public Resource getResource( String resourceIdentifier )
471 throws RbacManagerException
473 return this.rbacImpl.getResource( resourceIdentifier );
476 public Role getRole( String roleName )
477 throws RbacManagerException
480 Role role = rolesCache.get( roleName );
485 LdapConnection ldapConnection = null;
486 DirContext context = null;
487 //verify it's a ldap group
490 ldapConnection = ldapConnectionFactory.getConnection();
491 context = ldapConnection.getDirContext();
492 if ( !ldapRoleMapper.hasRole( context, roleName ) )
497 catch ( MappingException e )
499 throw new RbacManagerException( e.getMessage(), e );
501 catch ( LdapException e )
503 throw new RbacManagerException( e.getMessage(), e );
505 role = this.rbacImpl.getRole( roleName );
506 role = ( role == null ) ? new RoleImpl( roleName ) : role;
508 rolesCache.put( roleName, role );
513 public Map<String, Role> getRoles( Collection<String> roleNames )
514 throws RbacManagerException
516 return this.rbacImpl.getRoles( roleNames );
519 public Collection<Role> getUnassignedRoles( String username )
520 throws RbacManagerException
522 LdapConnection ldapConnection = null;
524 DirContext context = null;
529 ldapConnection = ldapConnectionFactory.getConnection();
531 context = ldapConnection.getDirContext();
533 List<String> allRoles = ldapRoleMapper.getAllRoles( context );
534 final List<String> userRoles = ldapRoleMapper.getRoles( username, context );
536 List<Role> unassignedRoles = new ArrayList<Role>();
538 for ( String roleName : allRoles )
540 if ( !userRoles.contains( roleName ) )
542 unassignedRoles.add( rbacImpl.getRole( roleName ) );
545 return unassignedRoles;
547 catch ( MappingException e )
549 throw new RbacManagerException( e.getMessage(), e );
551 catch ( LdapException e )
553 throw new RbacManagerException( e.getMessage(), e );
557 closeContext( context );
558 closeLdapConnection( ldapConnection );
562 public UserAssignment getUserAssignment( String username )
563 throws RbacManagerException
565 LdapConnection ldapConnection = null;
566 DirContext context = null;
569 ldapConnection = ldapConnectionFactory.getConnection();
570 context = ldapConnection.getDirContext();
571 List<String> roles = ldapRoleMapper.getRoles( username, context );
573 return new UserAssignmentImpl( username, roles );
575 catch ( MappingException e )
577 throw new RbacManagerException( e.getMessage(), e );
579 catch ( LdapException e )
581 throw new RbacManagerException( e.getMessage(), e );
585 closeContext( context );
586 closeLdapConnection( ldapConnection );
589 //return this.rbacImpl.getUserAssignment( username );
592 public List<UserAssignment> getUserAssignmentsForRoles( Collection<String> roleNames )
593 throws RbacManagerException
596 return this.rbacImpl.getUserAssignmentsForRoles( roleNames );
599 public boolean operationExists( Operation operation )
601 return this.rbacImpl.operationExists( operation );
604 public boolean operationExists( String name )
606 return this.rbacImpl.operationExists( name );
609 public boolean permissionExists( Permission permission )
611 return this.rbacImpl.permissionExists( permission );
614 public boolean permissionExists( String name )
616 return this.rbacImpl.permissionExists( name );
619 public void rbacInit( boolean freshdb )
621 if ( rbacImpl instanceof RBACManagerListener )
623 ( (RBACManagerListener) this.rbacImpl ).rbacInit( freshdb );
627 public void rbacPermissionRemoved( Permission permission )
629 if ( rbacImpl instanceof RBACManagerListener )
631 ( (RBACManagerListener) this.rbacImpl ).rbacPermissionRemoved( permission );
636 public void rbacPermissionSaved( Permission permission )
638 if ( rbacImpl instanceof RBACManagerListener )
640 ( (RBACManagerListener) this.rbacImpl ).rbacPermissionSaved( permission );
645 public void rbacRoleRemoved( Role role )
647 if ( rbacImpl instanceof RBACManagerListener )
649 ( (RBACManagerListener) this.rbacImpl ).rbacRoleRemoved( role );
654 public void rbacRoleSaved( Role role )
656 if ( rbacImpl instanceof RBACManagerListener )
658 ( (RBACManagerListener) this.rbacImpl ).rbacRoleSaved( role );
663 public void rbacUserAssignmentRemoved( UserAssignment userAssignment )
665 if ( rbacImpl instanceof RBACManagerListener )
667 ( (RBACManagerListener) this.rbacImpl ).rbacUserAssignmentRemoved( userAssignment );
672 public void rbacUserAssignmentSaved( UserAssignment userAssignment )
674 if ( rbacImpl instanceof RBACManagerListener )
676 ( (RBACManagerListener) this.rbacImpl ).rbacUserAssignmentSaved( userAssignment );
681 public void removeListener( RBACManagerListener listener )
683 this.rbacImpl.removeListener( listener );
686 public void removeOperation( Operation operation )
687 throws RbacManagerException
689 this.rbacImpl.removeOperation( operation );
692 public void removeOperation( String operationName )
693 throws RbacManagerException
695 this.rbacImpl.removeOperation( operationName );
698 public void removePermission( Permission permission )
699 throws RbacManagerException
701 this.rbacImpl.removePermission( permission );
704 public void removePermission( String permissionName )
705 throws RbacManagerException
707 this.rbacImpl.removePermission( permissionName );
710 public void removeResource( Resource resource )
711 throws RbacManagerException
713 this.rbacImpl.removeResource( resource );
716 public void removeResource( String resourceIdentifier )
717 throws RbacManagerException
719 this.rbacImpl.removeResource( resourceIdentifier );
722 public void removeRole( Role role )
723 throws RbacManagerException
725 RBACObjectAssertions.assertValid( role );
727 if ( role.isPermanent() )
729 throw new RbacPermanentException( "Unable to delete permanent role [" + role.getName() + "]" );
731 rolesCache.remove( role.getName() );
734 LdapConnection ldapConnection = null;
735 DirContext context = null;
738 ldapConnection = ldapConnectionFactory.getConnection();
739 context = ldapConnection.getDirContext();
740 ldapRoleMapper.removeRole( role.getName(), context );
742 catch ( MappingException e )
744 throw new RbacManagerException( e.getMessage(), e );
746 catch ( LdapException e )
748 throw new RbacManagerException( e.getMessage(), e );
750 fireRbacRoleRemoved( role );
754 public void removeRole( String roleName )
755 throws RbacManagerException
757 if ( roleName == null )
761 removeRole( new RoleImpl( roleName ) );
764 public void removeUserAssignment( String username )
765 throws RbacManagerException
767 // TODO ldap cannot or isWritable ldap ?
768 this.rbacImpl.removeUserAssignment( username );
771 public void removeUserAssignment( UserAssignment userAssignment )
772 throws RbacManagerException
774 // TODO ldap cannot or isWritable ldap ?
775 this.rbacImpl.removeUserAssignment( userAssignment );
778 public boolean resourceExists( Resource resource )
780 return this.rbacImpl.resourceExists( resource );
783 public boolean resourceExists( String identifier )
785 return this.rbacImpl.resourceExists( identifier );
789 public boolean roleExists( Role role )
790 throws RbacManagerException
796 return roleExists( role.getName() );
800 public boolean roleExists( String name )
801 throws RbacManagerException
803 if ( StringUtils.isEmpty( name ) )
807 if ( rolesCache.get( name ) != null )
811 LdapConnection ldapConnection = null;
812 DirContext context = null;
815 ldapConnection = ldapConnectionFactory.getConnection();
816 context = ldapConnection.getDirContext();
817 if ( rolesCache.hasKey( name ) )
821 return ldapRoleMapper.hasRole( context, name );
823 catch ( MappingException e )
825 throw new RbacManagerException( e.getMessage(), e );
827 catch ( LdapException e )
829 throw new RbacManagerException( e.getMessage(), e );
833 closeContext( context );
834 closeLdapConnection( ldapConnection );
838 public Operation saveOperation( Operation operation )
839 throws RbacManagerException
841 return this.rbacImpl.saveOperation( operation );
844 public Permission savePermission( Permission permission )
845 throws RbacManagerException
847 return this.rbacImpl.savePermission( permission );
850 public Resource saveResource( Resource resource )
851 throws RbacManagerException
853 return this.rbacImpl.saveResource( resource );
856 public synchronized Role saveRole( Role role )
857 throws RbacManagerException
861 LdapConnection ldapConnection = null;
862 DirContext context = null;
865 ldapConnection = ldapConnectionFactory.getConnection();
866 context = ldapConnection.getDirContext();
867 ldapRoleMapper.saveRole( role.getName(), context );
869 if ( !role.getChildRoleNames().isEmpty() )
871 for ( String roleName : role.getChildRoleNames() )
873 ldapRoleMapper.saveRole( roleName, context );
876 fireRbacRoleSaved( role );
878 catch ( MappingException e )
880 throw new RbacManagerException( e.getMessage(), e );
882 catch ( LdapException e )
884 throw new RbacManagerException( e.getMessage(), e );
887 role = this.rbacImpl.saveRole( role );
888 rolesCache.put( role.getName(), role );
891 //return new RoleImpl( role.getName(), role.getPermissions() );
894 public synchronized void saveRoles( Collection<Role> roles )
895 throws RbacManagerException
899 LdapConnection ldapConnection = null;
900 DirContext context = null;
904 ldapConnection = ldapConnectionFactory.getConnection();
905 context = ldapConnection.getDirContext();
906 for ( Role role : roles )
908 ldapRoleMapper.saveRole( role.getName(), context );
909 fireRbacRoleSaved( role );
912 catch ( MappingException e )
914 throw new RbacManagerException( e.getMessage(), e );
916 catch ( LdapException e )
918 throw new RbacManagerException( e.getMessage(), e );
921 this.rbacImpl.saveRoles( roles );
925 public UserAssignment saveUserAssignment( UserAssignment userAssignment )
926 throws RbacManagerException
928 LdapConnection ldapConnection = null;
929 DirContext context = null;
932 if ( !userManager.userExists( userAssignment.getPrincipal() ) )
934 User user = userManager.createUser( userAssignment.getPrincipal(), null, null );
935 userManager.addUser( user );
937 ldapConnection = ldapConnectionFactory.getConnection();
938 context = ldapConnection.getDirContext();
939 List<String> allRoles = ldapRoleMapper.getAllRoles( context );
941 List<String> currentUserRoles = ldapRoleMapper.getRoles( userAssignment.getPrincipal(), context );
943 for ( String role : userAssignment.getRoleNames() )
945 if ( !currentUserRoles.contains( role ) && writableLdap )
947 // role exists in ldap ?
948 if ( !allRoles.contains( role ) )
950 ldapRoleMapper.saveRole( role, context );
951 allRoles.add( role );
953 ldapRoleMapper.saveUserRole( role, userAssignment.getPrincipal(), context );
954 currentUserRoles.add( role );
958 for ( String role : currentUserRoles )
960 if ( !userAssignment.getRoleNames().contains( role ) && writableLdap )
962 ldapRoleMapper.removeUserRole( role, userAssignment.getPrincipal(), context );
966 return userAssignment;
968 catch ( UserManagerException e )
970 throw new RbacManagerException( e.getMessage(), e );
972 catch ( MappingException e )
974 throw new RbacManagerException( e.getMessage(), e );
976 catch ( LdapException e )
978 throw new RbacManagerException( e.getMessage(), e );
982 closeContext( context );
983 closeLdapConnection( ldapConnection );
987 public boolean userAssignmentExists( String principal )
989 LdapConnection ldapConnection = null;
990 DirContext context = null;
993 ldapConnection = ldapConnectionFactory.getConnection();
994 context = ldapConnection.getDirContext();
995 List<String> roles = ldapRoleMapper.getRoles( principal, context );
996 if ( roles == null || roles.isEmpty() )
1002 catch ( LdapException e )
1004 log.warn( "fail to call userAssignmentExists: {}", e.getMessage() );
1006 catch ( MappingException e )
1008 log.warn( "fail to call userAssignmentExists: {}", e.getMessage() );
1012 closeContext( context );
1013 closeLdapConnection( ldapConnection );
1018 public boolean userAssignmentExists( UserAssignment assignment )
1020 if ( assignment == null )
1024 return this.userAssignmentExists( assignment.getPrincipal() );
1027 public RBACManager getRbacImpl()
1032 public void setRbacImpl( RBACManager rbacImpl )
1034 this.rbacImpl = rbacImpl;
1037 public boolean isWritableLdap()
1039 return writableLdap;
1042 public void setWritableLdap( boolean writableLdap )
1044 this.writableLdap = writableLdap;
1047 public LdapRoleMapper getLdapRoleMapper()
1049 return ldapRoleMapper;
1052 public void setLdapRoleMapper( LdapRoleMapper ldapRoleMapper )
1054 this.ldapRoleMapper = ldapRoleMapper;
1057 private static class RoleImpl
1058 extends AbstractRole
1060 private String name;
1062 private String description;
1064 private List<Permission> permissions = new ArrayList<Permission>();
1066 private List<String> childRoleNames = new ArrayList<String>();
1068 private RoleImpl( String name )
1073 private RoleImpl( String name, List<Permission> permissions )
1076 this.permissions = permissions;
1079 public void addPermission( Permission permission )
1081 this.permissions.add( permission );
1084 public void addChildRoleName( String name )
1086 this.childRoleNames.add( name );
1089 public List<String> getChildRoleNames()
1091 return this.childRoleNames;
1094 public String getDescription()
1096 return this.description;
1099 public String getName()
1104 public List<Permission> getPermissions()
1106 return this.permissions;
1109 public boolean isAssignable()
1114 public void removePermission( Permission permission )
1116 this.permissions.remove( permission );
1119 public void setAssignable( boolean assignable )
1124 public void setChildRoleNames( List<String> names )
1126 this.childRoleNames = names;
1129 public void setDescription( String description )
1131 this.description = description;
1134 public void setName( String name )
1139 public void setPermissions( List<Permission> permissions )
1141 this.permissions = permissions;
1144 public boolean isPermanent()
1149 public void setPermanent( boolean permanent )
1155 public String toString()
1157 final StringBuilder sb = new StringBuilder();
1158 sb.append( "RoleImpl" );
1159 sb.append( "{name='" ).append( name ).append( '\'' );
1161 return sb.toString();
1165 public boolean equals( Object o )
1171 if ( o == null || getClass() != o.getClass() )
1176 RoleImpl role = (RoleImpl) o;
1178 if ( name != null ? !name.equals( role.name ) : role.name != null )
1187 public int hashCode()
1189 return name != null ? name.hashCode() : 0;
1193 private static class UserAssignmentImpl
1194 implements UserAssignment
1196 private String username;
1198 private List<String> roleNames;
1200 private boolean permanent;
1202 private UserAssignmentImpl( String username, Collection<String> roleNames )
1204 this.username = username;
1206 if ( roleNames == null )
1208 this.roleNames = new ArrayList<String>();
1212 this.roleNames = new ArrayList<String>( roleNames );
1216 public String getPrincipal()
1218 return this.username;
1221 public List<String> getRoleNames()
1223 return this.roleNames;
1226 public void addRoleName( Role role )
1232 this.roleNames.add( role.getName() );
1235 public void addRoleName( String roleName )
1237 if ( roleName == null )
1241 this.roleNames.add( roleName );
1244 public void removeRoleName( Role role )
1250 this.roleNames.remove( role.getName() );
1253 public void removeRoleName( String roleName )
1255 if ( roleName == null )
1259 this.roleNames.remove( roleName );
1262 public void setPrincipal( String principal )
1264 this.username = principal;
1267 public void setRoleNames( List<String> roles )
1269 this.roleNames = roles;
1272 public boolean isPermanent()
1274 return this.permanent;
1277 public void setPermanent( boolean permanent )
1279 this.permanent = permanent;
1283 public String toString()
1285 final StringBuilder sb = new StringBuilder();
1286 sb.append( "UserAssignmentImpl" );
1287 sb.append( "{username='" ).append( username ).append( '\'' );
1288 sb.append( ", roleNames=" ).append( roleNames );
1289 sb.append( ", permanent=" ).append( permanent );
1291 return sb.toString();