in redback-rbac/redback-rbac-providers/redback-rbac-ldap/src/main/java/org/apache/archiva/redback/rbac/ldap/LdapRbacManager.java [1117:1193]
public UserAssignment saveUserAssignment( UserAssignment userAssignment )
throws RbacManagerException
{
LdapConnection ldapConnection = null;
DirContext context = null;
try
{
if ( !userManager.userExists( userAssignment.getPrincipal() ) )
{
User user = userManager.createUser( userAssignment.getPrincipal(), null, null );
userManager.addUser( user );
}
ldapConnection = ldapConnectionFactory.getConnection();
context = ldapConnection.getDirContext();
List<String> allRoles = ldapRoleMapper.getAllRoles( context );
List<String> currentUserRoles =
ldapRoleMapper.getRoles( userAssignment.getPrincipal(), context, getRealRoles() );
Map<String, String> currentUserIds = currentUserRoles.stream( ).map( roleName -> {
try
{
return Optional.of( rbacImpl.getRole( roleName ) );
}
catch ( RbacManagerException e )
{
return Optional.<Role>empty( );
}
} ).filter( Optional::isPresent ).map(Optional::get)
.collect( Collectors.toMap( Role::getName, Role::getId ) );
for ( String roleId : userAssignment.getRoleIds() )
{
Role rbacRole = rbacImpl.getRoleById( roleId );
String roleName = rbacRole.getName( );
if ( !currentUserRoles.contains( roleName ) && writableLdap )
{
// role exists in ldap ?
if ( !allRoles.contains( roleName ) )
{
ldapRoleMapper.saveRole( roleName, context );
allRoles.add( roleName );
}
ldapRoleMapper.saveUserRole( roleName, userAssignment.getPrincipal(), context );
currentUserRoles.add( roleName );
currentUserIds.put( roleName, rbacRole.getId( ) );
}
}
for ( String roleName : currentUserRoles )
{
if ( !userAssignment.getRoleIds().contains( currentUserIds.get(roleName) ) && writableLdap )
{
ldapRoleMapper.removeUserRole( roleName, userAssignment.getPrincipal(), context );
}
}
userAssignmentsCache.put( userAssignment.getPrincipal(), userAssignment );
return userAssignment;
}
catch ( UserManagerException e )
{
throw new RbacManagerException( e.getMessage(), e );
}
catch ( MappingException e )
{
throw new RbacManagerException( e.getMessage(), e );
}
catch ( LdapException e )
{
throw new RbacManagerException( e.getMessage(), e );
}
finally
{
closeContext( context );
closeLdapConnection( ldapConnection );
}
}