Skip to content

Commit

Permalink
WIP for #239
Browse files Browse the repository at this point in the history
  • Loading branch information
oharsta committed Oct 18, 2024
1 parent 9649248 commit 3386315
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
2 changes: 1 addition & 1 deletion server/src/main/java/access/api/UserRoleOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public UserRoleOperations(UserRoleResource roleResource) {

public ResponseEntity<List<UserRole>> userRolesByRole(Long roleId,
RoleValidator roleValidator) {
LOG.debug("/roles/");
LOG.debug("/userRolesByRole/");
Role role = this.roleResource.getRoleRepository().findById(roleId).orElseThrow(() -> new NotFoundException("Role not found"));
roleValidator.validate(role);
List<UserRole> userRoles = this.roleResource.getUserRoleRepository().findByRole(role);
Expand Down
17 changes: 17 additions & 0 deletions server/src/main/java/access/internal/InternalInviteController.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

Expand Down Expand Up @@ -82,6 +84,20 @@ public InternalInviteController(RoleRepository roleRepository,
this.invitationOperations = new InvitationOperations(this);
}

@GetMapping("/roles")
@PreAuthorize("hasRole('SP_DASHBOARD')")
public ResponseEntity<List<Role>> rolesByApplication(@Parameter(hidden = true) @AuthenticationPrincipal RemoteUser remoteUser) {
LOG.debug(String.format("/roles for user %s", remoteUser.getName()));

List<Role> roles = remoteUser.getApplications()
.stream()
.map(application -> roleRepository.findByApplicationUsagesApplicationManageId(application.getManageId()))
.flatMap(Collection::stream)
.toList();
manage.addManageMetaData(roles);
return ResponseEntity.ok(roles);
}

@GetMapping("/roles/{id}")
@PreAuthorize("hasRole('SP_DASHBOARD')")
public ResponseEntity<Role> role(@PathVariable("id") Long id,
Expand Down Expand Up @@ -147,6 +163,7 @@ public ResponseEntity<InvitationResponse> newInvitation(@Validated @RequestBody

@GetMapping("user_roles/{roleId}")
@PreAuthorize("hasRole('SP_DASHBOARD')")
@Transactional
public ResponseEntity<List<UserRole>> byRole(@PathVariable("roleId") Long roleId,
@Parameter(hidden = true) @AuthenticationPrincipal RemoteUser remoteUser) {
return this.userRoleOperations.userRolesByRole(roleId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package access.internal;

import access.AbstractTest;
import access.AccessCookieFilter;
import access.manage.EntityType;
import access.model.Authority;
import access.model.InvitationRequest;
import access.model.Language;
import access.model.Role;
import access.model.*;
import io.restassured.common.mapper.TypeRef;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -63,6 +59,20 @@ void updateWithAPIUser() {
}


@Test
void roleByApplication() {
List<Role> roles = given()
.when()
.auth().preemptive().basic("sp_dashboard", "secret")
.accept(ContentType.JSON)
.contentType(ContentType.JSON)
.get("/api/internal/invite/roles")
.as(new TypeRef<>() {
});

assertEquals(1, roles.size());
}

@Test
void findRole() {
Role role = roleRepository.findByName("Research").get();
Expand Down Expand Up @@ -96,7 +106,7 @@ void deleteRole() {
}

@Test
void newInvitation() throws Exception {
void newInvitation() {
stubForManageProviderById(EntityType.SAML20_SP, "4");
List<Long> roleIdentifiers = List.of(roleRepository.findByName("Research").get().getId());

Expand Down Expand Up @@ -126,4 +136,20 @@ void newInvitation() throws Exception {
assertEquals(1, ((List) results.get("recipientInvitationURLs")).size());
}

@Test
void userRolesByRole() {
Long roleId = roleRepository.findByName("Research").get().getId();
List<UserRole> userRoles = given()
.when()
.auth().preemptive().basic("sp_dashboard", "secret")
.accept(ContentType.JSON)
.contentType(ContentType.JSON)
.pathParam("roleId", roleId)
.get("/api/internal/invite/user_roles/{roleId}")
.as(new TypeRef<>() {
});

assertEquals(1, userRoles.size());
}

}

0 comments on commit 3386315

Please sign in to comment.