You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@archiva.apache.org by ma...@apache.org on 2020/12/22 23:07:05 UTC
[archiva-redback-core] 03/03: Adding role method
This is an automated email from the ASF dual-hosted git repository.
martin_s pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/archiva-redback-core.git
commit 7de377dcb7ebf99a7853bc446f3f8186e6650747
Author: Martin Stockhammer <ma...@apache.org>
AuthorDate: Wed Dec 23 00:06:54 2020 +0100
Adding role method
---
.../redback/rest/api/services/v2/RoleService.java | 40 ++++++++++
.../rest/services/v2/BaseRedbackService.java | 8 +-
.../rest/services/v2/DefaultRoleService.java | 29 +++++++
.../rest/services/v2/NativeRoleServiceTest.java | 88 +++++++++++++++++++++-
4 files changed, 163 insertions(+), 2 deletions(-)
diff --git a/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/services/v2/RoleService.java b/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/services/v2/RoleService.java
index ca7f688..86e20b9 100644
--- a/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/services/v2/RoleService.java
+++ b/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/services/v2/RoleService.java
@@ -420,6 +420,46 @@ public interface RoleService
@QueryParam("order") @DefaultValue( "asc" ) String order
) throws RedbackServiceException;
+
+ @Path("{roleId}/unassigned")
+ @GET
+ @Produces({APPLICATION_JSON})
+ @RedbackAuthorization(permissions = RedbackRoleConstants.USER_MANAGEMENT_RBAC_ADMIN_OPERATION)
+ @Operation( summary = "Returns the users not assigned to the given role",
+ parameters = {
+ @Parameter(name = "q", description = "Search term"),
+ @Parameter(name = "offset", description = "The offset of the first element returned"),
+ @Parameter(name = "limit", description = "Maximum number of items to return in the response"),
+ @Parameter(name = "orderBy", description = "List of attribute used for sorting (user_id, fullName, email, created"),
+ @Parameter(name = "order", description = "The sort order. Either ascending (asc) or descending (desc)"),
+ @Parameter(name = "recurse", description = "If not present, or set to 'false' or '0', only users assigned directly to this role are returned."+
+ " If present and set to 'parentsOnly', the list of users assigned to all parents of the given role up to the root."+
+ " If present and set to any other value than 'parentsOnly', 'false' or '0', the users assigned to this role or any parent role in the hierarchy"+
+ " up to the root are returned.")
+ },
+ security = {
+ @SecurityRequirement( name = RedbackRoleConstants.USER_MANAGEMENT_RBAC_ADMIN_OPERATION )
+ },
+ responses = {
+ @ApiResponse( responseCode = "200",
+ description = "If the users could be retrieved"
+ ),
+ @ApiResponse( responseCode = "404", description = "Role instance does not exist",
+ content = @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = RedbackRestError.class )) ),
+ @ApiResponse( responseCode = "403", description = "The authenticated user has not the permission for role assignment.",
+ content = @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = RedbackRestError.class )) )
+ }
+ )
+ PagedResult<UserInfo> getUnassignedUsers(@PathParam( "roleId" ) String roleId,
+ @QueryParam("recurse") String recurse,
+ @QueryParam("q") @DefaultValue( "" ) String searchTerm,
+ @QueryParam( "offset" ) @DefaultValue( "0" ) Integer offset,
+ @QueryParam( "limit" ) @DefaultValue( value = DEFAULT_PAGE_LIMIT ) Integer limit,
+ @QueryParam( "orderBy") @DefaultValue( "id" ) List<String> orderBy,
+ @QueryParam("order") @DefaultValue( "asc" ) String order
+ ) throws RedbackServiceException;
+
+
/**
* Updates a role. Attributes that are empty or null will be ignored.
*
diff --git a/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/v2/BaseRedbackService.java b/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/v2/BaseRedbackService.java
index a2b693d..01fd95e 100644
--- a/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/v2/BaseRedbackService.java
+++ b/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/v2/BaseRedbackService.java
@@ -38,6 +38,7 @@ import org.slf4j.LoggerFactory;
import javax.inject.Named;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
@@ -153,7 +154,11 @@ public class BaseRedbackService
{
try
{
- return rbacManager.getUserAssignmentsForRoles( recurseRoles( rbacRole ).map( role -> role.getId( ) ).filter(roleId -> ((!parentsOnly) || ( !rbacRole.getId().equals(roleId)))).collect( Collectors.toList( ) ) )
+ List<String> roles = recurseRoles( rbacRole ).map( role -> role.getId( ) ).filter( roleId -> ( ( !parentsOnly ) || ( !rbacRole.getId( ).equals( roleId ) ) ) ).collect( Collectors.toList( ) );
+ if (roles.size()==0) {
+ return Collections.emptyList( );
+ }
+ return rbacManager.getUserAssignmentsForRoles( roles )
.stream( ).map( assignment -> getRedbackUser( assignment.getPrincipal( ) ) ).collect( Collectors.toList( ) );
}
catch ( RuntimeException e )
@@ -235,6 +240,7 @@ public class BaseRedbackService
{
Predicate<User> filter = USER_QUERY_HELPER.getQueryFilter( q );
long size = rawUsers.stream( ).filter( filter ).count( );
+ System.out.println( "Total " + size );
List<UserInfo> users = rawUsers.stream( )
.filter( filter )
.sorted( USER_QUERY_HELPER.getComparator( orderBy, ascending ) ).skip( offset ).limit( limit )
diff --git a/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/v2/DefaultRoleService.java b/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/v2/DefaultRoleService.java
index b8d78a5..d70f556 100644
--- a/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/v2/DefaultRoleService.java
+++ b/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/v2/DefaultRoleService.java
@@ -58,6 +58,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
+import java.util.Set;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
import java.util.stream.Collectors;
@@ -450,6 +451,34 @@ public class DefaultRoleService extends BaseRedbackService
}
@Override
+ public PagedResult<UserInfo> getUnassignedUsers( String roleId, String recurse, String searchTerm, Integer offset, Integer limit, List<String> orderBy, String order ) throws RedbackServiceException
+ {
+ boolean ascending = isAscending( order );
+ boolean recursePresent = Util.isFlagSet( uriInfo, "recurse" );
+ boolean parentsOnly = "parentsOnly".equals( recurse );
+ try
+ {
+ org.apache.archiva.redback.rbac.Role rbacRole = rbacManager.getRoleById( roleId );
+ final Set<String> assignedUsers = (recursePresent ? getAssignedRedbackUsersRecursive( rbacRole, parentsOnly ) : getAssignedRedbackUsers( rbacRole ))
+ .stream( ).map( user -> user.getId() ).collect( Collectors.toSet());
+ List<? extends User> rawUsers = userManager.getUsers( ascending ).stream( ).filter( user -> !assignedUsers.contains( user.getId( ) ) ).collect( Collectors.toList( ) );
+ return getUserInfoPagedResult( rawUsers, searchTerm, offset, limit, orderBy, ascending );
+ }
+ catch ( RbacObjectNotFoundException e )
+ {
+ throw new RedbackServiceException( ErrorMessage.of( MessageKeys.ERR_ROLE_NOT_FOUND, e.getMessage( ) ), 404 );
+ }
+ catch ( RbacManagerException e )
+ {
+ throw new RedbackServiceException( ErrorMessage.of( MessageKeys.ERR_RBACMANAGER_FAIL, e.getMessage( ) ) );
+ }
+ catch ( UserManagerException e )
+ {
+ throw new RedbackServiceException( ErrorMessage.of( MessageKeys.ERR_USERMANAGER_FAIL, e.getMessage( ) ) );
+ }
+ }
+
+ @Override
public RoleInfo updateRole( String roleId, Role role ) throws RedbackServiceException
{
try
diff --git a/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/v2/NativeRoleServiceTest.java b/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/v2/NativeRoleServiceTest.java
index 48028c8..0b6c33c 100644
--- a/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/v2/NativeRoleServiceTest.java
+++ b/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/v2/NativeRoleServiceTest.java
@@ -501,7 +501,6 @@ public class NativeRoleServiceTest extends AbstractNativeRestServices
Response result = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
.when( )
.get( "archiva-global-repository-observer/user" )
- .prettyPeek()
.then( ).statusCode( 200 ).extract( ).response( );
assertNotNull(result);
PagedResult<UserInfo> userResult = result.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
@@ -520,6 +519,49 @@ public class NativeRoleServiceTest extends AbstractNativeRestServices
}
@Test
+ void getUnAssignedUsersNonRecursive( )
+ {
+ String token = getAdminToken( );
+ Map<String, Object> jsonAsMap = new HashMap<>( );
+ jsonAsMap.put( "user_id", "aragorn" );
+ jsonAsMap.put( "email", "aragorn@lordoftherings.org" );
+ jsonAsMap.put( "full_name", "Aragorn King of Gondor " );
+ jsonAsMap.put( "password", "pAssw0rD" );
+
+ try
+ {
+ given( ).spec( getRequestSpec( token, getUserServicePath( ) ) ).contentType( JSON )
+ .body( jsonAsMap )
+ .when( )
+ .post( )
+ .then( ).statusCode( 201 );
+ given( ).spec( getRequestSpec( token ) ).contentType( JSON )
+ .when( )
+ .put( "archiva-global-repository-observer/user/aragorn" )
+ .then( ).statusCode( 200 );
+ Response result = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
+ .when( )
+ .get( "archiva-global-repository-observer/unassigned" )
+ .prettyPeek()
+ .then( ).statusCode( 200 ).extract( ).response( );
+ assertNotNull(result);
+ PagedResult<UserInfo> userResult = result.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
+ assertNotNull( userResult );
+ assertEquals( 2, userResult.getPagination( ).getTotalCount( ) );
+ List<UserInfo> users = result.getBody( ).jsonPath( ).getList( "data", UserInfo.class );
+ assertFalse( users.stream( ).filter(user -> "aragorn".equals(user.getUserId())).findAny().isPresent() );
+ }
+ finally
+ {
+ given( ).spec( getRequestSpec( token, getUserServicePath( ) ) ).contentType( JSON )
+ .when( )
+ .delete( "aragorn" ).then( ).statusCode( 200 );
+ }
+
+ }
+
+
+ @Test
void getAssignedUsersRecursive( )
{
String token = getAdminToken( );
@@ -563,6 +605,50 @@ public class NativeRoleServiceTest extends AbstractNativeRestServices
}
@Test
+ void getUnAssignedUsersRecursive( )
+ {
+ String token = getAdminToken( );
+ Map<String, Object> jsonAsMap = new HashMap<>( );
+ jsonAsMap.put( "user_id", "aragorn" );
+ jsonAsMap.put( "email", "aragorn@lordoftherings.org" );
+ jsonAsMap.put( "full_name", "Aragorn King of Gondor " );
+ jsonAsMap.put( "password", "pAssw0rD" );
+
+ try
+ {
+ given( ).spec( getRequestSpec( token, getUserServicePath( ) ) ).contentType( JSON )
+ .body( jsonAsMap )
+ .when( )
+ .post( )
+ .then( ).statusCode( 201 );
+ given( ).spec( getRequestSpec( token ) ).contentType( JSON )
+ .when( )
+ .put( "archiva-global-repository-observer/user/aragorn" )
+ .then( ).statusCode( 200 );
+ Response result = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
+ .when( )
+ .param( "recurse" )
+ .get( "archiva-global-repository-observer/unassigned" )
+ .prettyPeek()
+ .then( ).statusCode( 200 ).extract( ).response( );
+ assertNotNull(result);
+ PagedResult<UserInfo> userResult = result.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
+ assertNotNull( userResult );
+ assertEquals( 1, userResult.getPagination( ).getTotalCount( ) );
+ List<UserInfo> users = result.getBody( ).jsonPath( ).getList( "data", UserInfo.class );
+ assertTrue( "guest".equals( users.get( 0 ).getUserId( ) ) );
+ }
+ finally
+ {
+ given( ).spec( getRequestSpec( token, getUserServicePath( ) ) ).contentType( JSON )
+ .when( )
+ .delete( "aragorn" ).then( ).statusCode( 200 );
+ }
+
+ }
+
+
+ @Test
void getAssignedUsersRecursiveParentsOnly( )
{
String token = getAdminToken( );