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;
110 @Named( value = "cache#userAssignments" )
111 private Cache<String, UserAssignment> userAssignmentsCache;
113 private boolean writableLdap = false;
116 public void initialize()
118 this.writableLdap = userConf.getBoolean( UserConfigurationKeys.LDAP_WRITABLE, this.writableLdap );
122 public void addChildRole( Role role, Role childRole )
123 throws RbacObjectInvalidException, RbacManagerException
125 this.rbacImpl.addChildRole( role, childRole );
128 public void addListener( RBACManagerListener listener )
130 super.addListener( listener );
131 this.rbacImpl.addListener( listener );
134 public Operation createOperation( String name )
135 throws RbacManagerException
137 return this.rbacImpl.createOperation( name );
140 public Permission createPermission( String name )
141 throws RbacManagerException
143 return this.rbacImpl.createPermission( name );
146 public Permission createPermission( String name, String operationName, String resourceIdentifier )
147 throws RbacManagerException
149 return this.rbacImpl.createPermission( name, operationName, resourceIdentifier );
152 public Resource createResource( String identifier )
153 throws RbacManagerException
155 return this.rbacImpl.createResource( identifier );
158 public Role createRole( String name )
160 return this.rbacImpl.createRole( name );
163 public UserAssignment createUserAssignment( String username )
164 throws RbacManagerException
166 // TODO ldap cannot or isWritable ldap ?
167 return this.rbacImpl.createUserAssignment( username );
170 public void eraseDatabase()
174 LdapConnection ldapConnection = null;
175 DirContext context = null;
178 ldapConnection = ldapConnectionFactory.getConnection();
179 context = ldapConnection.getDirContext();
180 ldapRoleMapper.removeAllRoles( context );
182 catch ( MappingException e )
184 log.warn( "skip error removing all roles {}", e.getMessage() );
186 catch ( LdapException e )
188 log.warn( "skip error removing all roles {}", e.getMessage() );
192 closeContext( context );
193 closeLdapConnection( ldapConnection );
196 this.rolesCache.clear();
197 this.userAssignmentsCache.clear();
198 this.rbacImpl.eraseDatabase();
202 * @see org.apache.archiva.redback.rbac.RBACManager#getAllAssignableRoles()
204 public List<Role> getAllAssignableRoles()
205 throws RbacManagerException
209 Collection<Collection<String>> roleNames = ldapRoleMapper.getLdapGroupMappings().values();
211 Set<Role> roles = new HashSet<Role>();
213 for ( Collection<String> names : roleNames )
215 for ( String name : names )
217 roles.add( new RoleImpl( name ) );
221 return new ArrayList<Role>( roles );
223 catch ( MappingException e )
225 throw new RbacManagerException( e.getMessage(), e );
229 public List<Operation> getAllOperations()
230 throws RbacManagerException
232 return this.rbacImpl.getAllOperations();
235 public List<Permission> getAllPermissions()
236 throws RbacManagerException
238 return this.rbacImpl.getAllPermissions();
241 public List<Resource> getAllResources()
242 throws RbacManagerException
244 return this.rbacImpl.getAllResources();
247 public List<Role> getAllRoles()
248 throws RbacManagerException
250 LdapConnection ldapConnection = null;
251 DirContext context = null;
254 ldapConnection = ldapConnectionFactory.getConnection();
255 context = ldapConnection.getDirContext();
257 List<String> groups = ldapRoleMapper.getAllGroups( context );
258 return mapToRoles( groups );
260 catch ( MappingException e )
262 throw new RbacManagerException( e.getMessage(), e );
264 catch ( LdapException e )
266 throw new RbacManagerException( e.getMessage(), e );
270 closeContext( context );
271 closeLdapConnection( ldapConnection );
273 //return this.rbacImpl.getAllRoles();
277 public List<UserAssignment> getAllUserAssignments()
278 throws RbacManagerException
280 LdapConnection ldapConnection = null;
281 DirContext context = null;
284 ldapConnection = ldapConnectionFactory.getConnection();
285 context = ldapConnection.getDirContext();
286 Map<String, Collection<String>> usersWithRoles = ldapController.findUsersWithRoles( context );
287 List<UserAssignment> userAssignments = new ArrayList<UserAssignment>( usersWithRoles.size() );
289 for ( Map.Entry<String, Collection<String>> entry : usersWithRoles.entrySet() )
291 UserAssignment userAssignment = new UserAssignmentImpl( entry.getKey(), entry.getValue() );
292 userAssignments.add( userAssignment );
293 userAssignmentsCache.put( userAssignment.getPrincipal(), userAssignment );
296 return userAssignments;
298 catch ( LdapControllerException e )
300 throw new RbacManagerException( e.getMessage(), e );
302 catch ( LdapException e )
304 throw new RbacManagerException( e.getMessage(), e );
308 closeContext( context );
309 closeLdapConnection( ldapConnection );
313 protected void closeLdapConnection( LdapConnection ldapConnection )
315 if ( ldapConnection != null )
317 ldapConnection.close();
321 protected void closeContext( DirContext context )
323 if ( context != null )
329 catch ( NamingException e )
331 log.warn( "skip issue closing context: {}", e.getMessage() );
337 * public Map<String, List<Permission>> getAssignedPermissionMap( String username )
338 * throws RbacManagerException
340 * return this.rbacImpl.getAssignedPermissionMap( username );
344 /*public Set<Permission> getAssignedPermissions( String username )
345 throws RbacObjectNotFoundException, RbacManagerException
348 return this.rbacImpl.getAssignedPermissions( username );
350 private List<Role> mapToRoles( List<String> groups )
351 throws MappingException, RbacManagerException
353 if ( groups == null || groups.isEmpty() )
355 return Collections.emptyList();
358 List<Role> roles = new ArrayList<Role>( groups.size() );
359 Map<String, Collection<String>> mappedGroups = ldapRoleMapper.getLdapGroupMappings();
360 for ( String group : groups )
362 Collection<String> roleNames = mappedGroups.get( group );
363 if ( roleNames != null )
365 for ( String roleName : roleNames )
367 Role role = this.rbacImpl.getRole( roleName );
368 role = ( role == null ) ? new RoleImpl( roleName ) : role;
371 rolesCache.put( role.getName(), role );
376 else if ( this.ldapRoleMapper.isUseDefaultRoleName() )
381 role = this.rbacImpl.getRole( group );
383 catch ( RbacObjectNotFoundException e )
385 // if it's mapped role to a group it doesn't exist in jdo
387 role = ( role == null ) ? new RoleImpl( group ) : role;
390 rolesCache.put( role.getName(), role );
400 protected List<String> getRealRoles()
401 throws RbacManagerException
403 List<Role> roles = this.rbacImpl.getAllRoles();
404 List<String> roleNames = new ArrayList<String>( roles.size() );
405 for ( Role role : roles )
407 roleNames.add( role.getName() );
412 public Collection<Role> getAssignedRoles( String username )
413 throws RbacManagerException
416 LdapConnection ldapConnection = null;
417 DirContext context = null;
422 ldapConnection = ldapConnectionFactory.getConnection();
423 context = ldapConnection.getDirContext();
424 List<String> roleNames = ldapRoleMapper.getRoles( username, context, getRealRoles() );
426 if ( roleNames.isEmpty() )
428 return Collections.emptyList();
431 List<Role> roles = new ArrayList<Role>( roleNames.size() );
433 for ( String name : roleNames )
435 roles.add( this.rbacImpl.getRole( name ) );// new RoleImpl( name ) );
440 catch ( MappingException e )
442 throw new RbacManagerException( e.getMessage(), e );
444 catch ( LdapException e )
446 throw new RbacManagerException( e.getMessage(), e );
450 public Collection<Role> getAssignedRoles( UserAssignment userAssignment )
451 throws RbacManagerException
453 return getAssignedRoles( userAssignment.getPrincipal() );
456 public Map<String, Role> getChildRoles( Role role )
457 throws RbacManagerException
459 return this.rbacImpl.getChildRoles( role );
462 public Map<String, Role> getParentRoles( Role role )
463 throws RbacManagerException
465 return this.rbacImpl.getParentRoles( role );
469 public Collection<Role> getEffectivelyAssignedRoles( String username )
470 throws RbacManagerException
473 return this.rbacImpl.getEffectivelyAssignedRoles( username );
477 * public Collection<Role> getEffectivelyUnassignedRoles( String username )
478 * throws RbacManagerException
481 * return this.rbacImpl.getEffectivelyUnassignedRoles( username );
485 public Set<Role> getEffectiveRoles( Role role )
486 throws RbacManagerException
488 return this.rbacImpl.getEffectiveRoles( role );
491 public Resource getGlobalResource()
492 throws RbacManagerException
494 return this.rbacImpl.getGlobalResource();
497 public Operation getOperation( String operationName )
498 throws RbacManagerException
500 return this.rbacImpl.getOperation( operationName );
503 public Permission getPermission( String permissionName )
504 throws RbacManagerException
506 return this.rbacImpl.getPermission( permissionName );
509 public Resource getResource( String resourceIdentifier )
510 throws RbacManagerException
512 return this.rbacImpl.getResource( resourceIdentifier );
515 public Role getRole( String roleName )
516 throws RbacManagerException
519 Role role = rolesCache.get( roleName );
524 LdapConnection ldapConnection = null;
525 DirContext context = null;
526 //verify it's a ldap group
529 ldapConnection = ldapConnectionFactory.getConnection();
530 context = ldapConnection.getDirContext();
531 if ( !ldapRoleMapper.hasRole( context, roleName ) )
536 catch ( MappingException e )
538 throw new RbacManagerException( e.getMessage(), e );
540 catch ( LdapException e )
542 throw new RbacManagerException( e.getMessage(), e );
544 role = this.rbacImpl.getRole( roleName );
545 role = ( role == null ) ? new RoleImpl( roleName ) : role;
547 rolesCache.put( roleName, role );
552 public Map<String, Role> getRoles( Collection<String> roleNames )
553 throws RbacManagerException
555 return this.rbacImpl.getRoles( roleNames );
558 public Collection<Role> getUnassignedRoles( String username )
559 throws RbacManagerException
561 LdapConnection ldapConnection = null;
563 DirContext context = null;
568 ldapConnection = ldapConnectionFactory.getConnection();
570 context = ldapConnection.getDirContext();
572 List<String> allRoles = ldapRoleMapper.getAllRoles( context );
573 final List<String> userRoles = ldapRoleMapper.getRoles( username, context, getRealRoles() );
575 List<Role> unassignedRoles = new ArrayList<Role>();
577 for ( String roleName : allRoles )
579 if ( !userRoles.contains( roleName ) )
581 unassignedRoles.add( rbacImpl.getRole( roleName ) );
584 return unassignedRoles;
586 catch ( MappingException e )
588 throw new RbacManagerException( e.getMessage(), e );
590 catch ( LdapException e )
592 throw new RbacManagerException( e.getMessage(), e );
596 closeContext( context );
597 closeLdapConnection( ldapConnection );
601 public UserAssignment getUserAssignment( String username )
602 throws RbacManagerException
604 UserAssignment ua = userAssignmentsCache.get( username );
609 LdapConnection ldapConnection = null;
610 DirContext context = null;
613 ldapConnection = ldapConnectionFactory.getConnection();
614 context = ldapConnection.getDirContext();
615 List<String> roles = ldapRoleMapper.getRoles( username, context, getRealRoles() );
617 ua = new UserAssignmentImpl( username, roles );
619 userAssignmentsCache.put( username, ua );
623 catch ( MappingException e )
625 throw new RbacManagerException( e.getMessage(), e );
627 catch ( LdapException e )
629 throw new RbacManagerException( e.getMessage(), e );
633 closeContext( context );
634 closeLdapConnection( ldapConnection );
637 //return this.rbacImpl.getUserAssignment( username );
640 public List<UserAssignment> getUserAssignmentsForRoles( Collection<String> roleNames )
641 throws RbacManagerException
644 return this.rbacImpl.getUserAssignmentsForRoles( roleNames );
647 public boolean operationExists( Operation operation )
649 return this.rbacImpl.operationExists( operation );
652 public boolean operationExists( String name )
654 return this.rbacImpl.operationExists( name );
657 public boolean permissionExists( Permission permission )
659 return this.rbacImpl.permissionExists( permission );
662 public boolean permissionExists( String name )
664 return this.rbacImpl.permissionExists( name );
667 public void rbacInit( boolean freshdb )
669 if ( rbacImpl instanceof RBACManagerListener )
671 ( (RBACManagerListener) this.rbacImpl ).rbacInit( freshdb );
675 public void rbacPermissionRemoved( Permission permission )
677 if ( rbacImpl instanceof RBACManagerListener )
679 ( (RBACManagerListener) this.rbacImpl ).rbacPermissionRemoved( permission );
684 public void rbacPermissionSaved( Permission permission )
686 if ( rbacImpl instanceof RBACManagerListener )
688 ( (RBACManagerListener) this.rbacImpl ).rbacPermissionSaved( permission );
693 public void rbacRoleRemoved( Role role )
695 if ( rbacImpl instanceof RBACManagerListener )
697 ( (RBACManagerListener) this.rbacImpl ).rbacRoleRemoved( role );
702 public void rbacRoleSaved( Role role )
704 if ( rbacImpl instanceof RBACManagerListener )
706 ( (RBACManagerListener) this.rbacImpl ).rbacRoleSaved( role );
711 public void rbacUserAssignmentRemoved( UserAssignment userAssignment )
713 if ( rbacImpl instanceof RBACManagerListener )
715 ( (RBACManagerListener) this.rbacImpl ).rbacUserAssignmentRemoved( userAssignment );
720 public void rbacUserAssignmentSaved( UserAssignment userAssignment )
722 if ( rbacImpl instanceof RBACManagerListener )
724 ( (RBACManagerListener) this.rbacImpl ).rbacUserAssignmentSaved( userAssignment );
729 public void removeListener( RBACManagerListener listener )
731 this.rbacImpl.removeListener( listener );
734 public void removeOperation( Operation operation )
735 throws RbacManagerException
737 this.rbacImpl.removeOperation( operation );
740 public void removeOperation( String operationName )
741 throws RbacManagerException
743 this.rbacImpl.removeOperation( operationName );
746 public void removePermission( Permission permission )
747 throws RbacManagerException
749 this.rbacImpl.removePermission( permission );
752 public void removePermission( String permissionName )
753 throws RbacManagerException
755 this.rbacImpl.removePermission( permissionName );
758 public void removeResource( Resource resource )
759 throws RbacManagerException
761 this.rbacImpl.removeResource( resource );
764 public void removeResource( String resourceIdentifier )
765 throws RbacManagerException
767 this.rbacImpl.removeResource( resourceIdentifier );
770 public void removeRole( Role role )
771 throws RbacManagerException
773 RBACObjectAssertions.assertValid( role );
775 if ( role.isPermanent() )
777 throw new RbacPermanentException( "Unable to delete permanent role [" + role.getName() + "]" );
779 rolesCache.remove( role.getName() );
782 LdapConnection ldapConnection = null;
783 DirContext context = null;
786 ldapConnection = ldapConnectionFactory.getConnection();
787 context = ldapConnection.getDirContext();
788 ldapRoleMapper.removeRole( role.getName(), context );
790 catch ( MappingException e )
792 throw new RbacManagerException( e.getMessage(), e );
794 catch ( LdapException e )
796 throw new RbacManagerException( e.getMessage(), e );
798 fireRbacRoleRemoved( role );
802 public void removeRole( String roleName )
803 throws RbacManagerException
805 if ( roleName == null )
809 removeRole( new RoleImpl( roleName ) );
812 public void removeUserAssignment( String username )
813 throws RbacManagerException
815 // TODO ldap cannot or isWritable ldap ?
816 userAssignmentsCache.remove( username );
817 this.rbacImpl.removeUserAssignment( username );
820 public void removeUserAssignment( UserAssignment userAssignment )
821 throws RbacManagerException
823 if ( userAssignment != null )
825 userAssignmentsCache.remove( userAssignment.getPrincipal() );
827 // TODO ldap cannot or isWritable ldap ?
828 this.rbacImpl.removeUserAssignment( userAssignment );
831 public boolean resourceExists( Resource resource )
833 return this.rbacImpl.resourceExists( resource );
836 public boolean resourceExists( String identifier )
838 return this.rbacImpl.resourceExists( identifier );
842 public boolean roleExists( Role role )
843 throws RbacManagerException
849 return roleExists( role.getName() );
853 public boolean roleExists( String name )
854 throws RbacManagerException
856 if ( StringUtils.isEmpty( name ) )
860 if ( rolesCache.get( name ) != null )
864 LdapConnection ldapConnection = null;
865 DirContext context = null;
868 ldapConnection = ldapConnectionFactory.getConnection();
869 context = ldapConnection.getDirContext();
870 if ( rolesCache.hasKey( name ) )
874 return ldapRoleMapper.hasRole( context, name );
876 catch ( MappingException e )
878 throw new RbacManagerException( e.getMessage(), e );
880 catch ( LdapException e )
882 throw new RbacManagerException( e.getMessage(), e );
886 closeContext( context );
887 closeLdapConnection( ldapConnection );
891 public Operation saveOperation( Operation operation )
892 throws RbacManagerException
894 return this.rbacImpl.saveOperation( operation );
897 public Permission savePermission( Permission permission )
898 throws RbacManagerException
900 return this.rbacImpl.savePermission( permission );
903 public Resource saveResource( Resource resource )
904 throws RbacManagerException
906 return this.rbacImpl.saveResource( resource );
909 public synchronized Role saveRole( Role role )
910 throws RbacManagerException
914 LdapConnection ldapConnection = null;
915 DirContext context = null;
918 ldapConnection = ldapConnectionFactory.getConnection();
919 context = ldapConnection.getDirContext();
920 ldapRoleMapper.saveRole( role.getName(), context );
922 if ( !role.getChildRoleNames().isEmpty() )
924 for ( String roleName : role.getChildRoleNames() )
926 ldapRoleMapper.saveRole( roleName, context );
929 fireRbacRoleSaved( role );
931 catch ( MappingException e )
933 throw new RbacManagerException( e.getMessage(), e );
935 catch ( LdapException e )
937 throw new RbacManagerException( e.getMessage(), e );
940 role = this.rbacImpl.saveRole( role );
941 rolesCache.put( role.getName(), role );
944 //return new RoleImpl( role.getName(), role.getPermissions() );
947 public synchronized void saveRoles( Collection<Role> roles )
948 throws RbacManagerException
952 LdapConnection ldapConnection = null;
953 DirContext context = null;
957 ldapConnection = ldapConnectionFactory.getConnection();
958 context = ldapConnection.getDirContext();
959 for ( Role role : roles )
961 ldapRoleMapper.saveRole( role.getName(), context );
962 fireRbacRoleSaved( role );
965 catch ( MappingException e )
967 throw new RbacManagerException( e.getMessage(), e );
969 catch ( LdapException e )
971 throw new RbacManagerException( e.getMessage(), e );
974 this.rbacImpl.saveRoles( roles );
978 public UserAssignment saveUserAssignment( UserAssignment userAssignment )
979 throws RbacManagerException
981 LdapConnection ldapConnection = null;
982 DirContext context = null;
985 if ( !userManager.userExists( userAssignment.getPrincipal() ) )
987 User user = userManager.createUser( userAssignment.getPrincipal(), null, null );
988 userManager.addUser( user );
990 ldapConnection = ldapConnectionFactory.getConnection();
991 context = ldapConnection.getDirContext();
992 List<String> allRoles = ldapRoleMapper.getAllRoles( context );
994 List<String> currentUserRoles =
995 ldapRoleMapper.getRoles( userAssignment.getPrincipal(), context, getRealRoles() );
997 for ( String role : userAssignment.getRoleNames() )
999 if ( !currentUserRoles.contains( role ) && writableLdap )
1001 // role exists in ldap ?
1002 if ( !allRoles.contains( role ) )
1004 ldapRoleMapper.saveRole( role, context );
1005 allRoles.add( role );
1007 ldapRoleMapper.saveUserRole( role, userAssignment.getPrincipal(), context );
1008 currentUserRoles.add( role );
1012 for ( String role : currentUserRoles )
1014 if ( !userAssignment.getRoleNames().contains( role ) && writableLdap )
1016 ldapRoleMapper.removeUserRole( role, userAssignment.getPrincipal(), context );
1020 userAssignmentsCache.put( userAssignment.getPrincipal(), userAssignment );
1021 return userAssignment;
1023 catch ( UserManagerException e )
1025 throw new RbacManagerException( e.getMessage(), e );
1027 catch ( MappingException e )
1029 throw new RbacManagerException( e.getMessage(), e );
1031 catch ( LdapException e )
1033 throw new RbacManagerException( e.getMessage(), e );
1037 closeContext( context );
1038 closeLdapConnection( ldapConnection );
1042 public boolean userAssignmentExists( String principal )
1044 if ( userAssignmentsCache.hasKey( principal ) )
1048 LdapConnection ldapConnection = null;
1049 DirContext context = null;
1052 ldapConnection = ldapConnectionFactory.getConnection();
1053 context = ldapConnection.getDirContext();
1054 List<String> roles = ldapRoleMapper.getRoles( principal, context, getRealRoles() );
1055 if ( roles == null || roles.isEmpty() )
1061 catch ( RbacManagerException e )
1063 log.warn( "fail to call userAssignmentExists: {}", e.getMessage() );
1065 catch ( LdapException e )
1067 log.warn( "fail to call userAssignmentExists: {}", e.getMessage() );
1069 catch ( MappingException e )
1071 log.warn( "fail to call userAssignmentExists: {}", e.getMessage() );
1075 closeContext( context );
1076 closeLdapConnection( ldapConnection );
1081 public boolean userAssignmentExists( UserAssignment assignment )
1083 if ( assignment == null )
1087 return this.userAssignmentExists( assignment.getPrincipal() );
1090 public RBACManager getRbacImpl()
1095 public void setRbacImpl( RBACManager rbacImpl )
1097 this.rbacImpl = rbacImpl;
1100 public boolean isWritableLdap()
1102 return writableLdap;
1105 public void setWritableLdap( boolean writableLdap )
1107 this.writableLdap = writableLdap;
1110 public LdapRoleMapper getLdapRoleMapper()
1112 return ldapRoleMapper;
1115 public void setLdapRoleMapper( LdapRoleMapper ldapRoleMapper )
1117 this.ldapRoleMapper = ldapRoleMapper;
1120 private static class RoleImpl
1121 extends AbstractRole
1123 private String name;
1125 private String description;
1127 private List<Permission> permissions = new ArrayList<Permission>();
1129 private List<String> childRoleNames = new ArrayList<String>();
1131 private RoleImpl( String name )
1136 private RoleImpl( String name, List<Permission> permissions )
1139 this.permissions = permissions;
1142 public void addPermission( Permission permission )
1144 this.permissions.add( permission );
1147 public void addChildRoleName( String name )
1149 this.childRoleNames.add( name );
1152 public List<String> getChildRoleNames()
1154 return this.childRoleNames;
1157 public String getDescription()
1159 return this.description;
1162 public String getName()
1167 public List<Permission> getPermissions()
1169 return this.permissions;
1172 public boolean isAssignable()
1177 public void removePermission( Permission permission )
1179 this.permissions.remove( permission );
1182 public void setAssignable( boolean assignable )
1187 public void setChildRoleNames( List<String> names )
1189 this.childRoleNames = names;
1192 public void setDescription( String description )
1194 this.description = description;
1197 public void setName( String name )
1202 public void setPermissions( List<Permission> permissions )
1204 this.permissions = permissions;
1207 public boolean isPermanent()
1212 public void setPermanent( boolean permanent )
1218 public String toString()
1220 final StringBuilder sb = new StringBuilder();
1221 sb.append( "RoleImpl" );
1222 sb.append( "{name='" ).append( name ).append( '\'' );
1224 return sb.toString();
1228 public boolean equals( Object o )
1234 if ( o == null || getClass() != o.getClass() )
1239 RoleImpl role = (RoleImpl) o;
1241 if ( name != null ? !name.equals( role.name ) : role.name != null )
1250 public int hashCode()
1252 return name != null ? name.hashCode() : 0;
1256 private static class UserAssignmentImpl
1257 implements UserAssignment
1259 private String username;
1261 private List<String> roleNames;
1263 private boolean permanent;
1265 private UserAssignmentImpl( String username, Collection<String> roleNames )
1267 this.username = username;
1269 if ( roleNames == null )
1271 this.roleNames = new ArrayList<String>();
1275 this.roleNames = new ArrayList<String>( roleNames );
1279 public String getPrincipal()
1281 return this.username;
1284 public List<String> getRoleNames()
1286 return this.roleNames;
1289 public void addRoleName( Role role )
1295 this.roleNames.add( role.getName() );
1298 public void addRoleName( String roleName )
1300 if ( roleName == null )
1304 this.roleNames.add( roleName );
1307 public void removeRoleName( Role role )
1313 this.roleNames.remove( role.getName() );
1316 public void removeRoleName( String roleName )
1318 if ( roleName == null )
1322 this.roleNames.remove( roleName );
1325 public void setPrincipal( String principal )
1327 this.username = principal;
1330 public void setRoleNames( List<String> roles )
1332 this.roleNames = roles;
1335 public boolean isPermanent()
1337 return this.permanent;
1340 public void setPermanent( boolean permanent )
1342 this.permanent = permanent;
1346 public String toString()
1348 final StringBuilder sb = new StringBuilder();
1349 sb.append( "UserAssignmentImpl" );
1350 sb.append( "{username='" ).append( username ).append( '\'' );
1351 sb.append( ", roleNames=" ).append( roleNames );
1352 sb.append( ", permanent=" ).append( permanent );
1354 return sb.toString();