Search in sources :

Example 6 with UserRole

use of org.pmiops.workbench.model.UserRole in project workbench by all-of-us.

the class WorkspacesControllerTest method testCloneWorkspace.

@Test
public void testCloneWorkspace() throws Exception {
    Workspace workspace = createDefaultWorkspace();
    workspace = workspacesController.createWorkspace(workspace).getBody();
    // The original workspace is shared with one other user.
    User writerUser = new User();
    writerUser.setEmail("writerfriend@gmail.com");
    writerUser.setUserId(124L);
    writerUser.setFreeTierBillingProjectName("TestBillingProject2");
    writerUser.setDisabled(false);
    writerUser = userDao.save(writerUser);
    ShareWorkspaceRequest shareWorkspaceRequest = new ShareWorkspaceRequest();
    shareWorkspaceRequest.setWorkspaceEtag(workspace.getEtag());
    UserRole creator = new UserRole();
    creator.setEmail(LOGGED_IN_USER_EMAIL);
    creator.setRole(WorkspaceAccessLevel.OWNER);
    shareWorkspaceRequest.addItemsItem(creator);
    UserRole writer = new UserRole();
    writer.setEmail(writerUser.getEmail());
    writer.setRole(WorkspaceAccessLevel.WRITER);
    shareWorkspaceRequest.addItemsItem(writer);
    when(fireCloudService.updateWorkspaceACL(anyString(), anyString(), anyListOf(WorkspaceACLUpdate.class))).thenReturn(new WorkspaceACLUpdateResponseList());
    workspacesController.shareWorkspace(workspace.getNamespace(), workspace.getName(), shareWorkspaceRequest);
    stubGetWorkspace(workspace.getNamespace(), workspace.getName(), LOGGED_IN_USER_EMAIL, WorkspaceAccessLevel.OWNER);
    CloneWorkspaceRequest req = new CloneWorkspaceRequest();
    Workspace modWorkspace = new Workspace();
    modWorkspace.setName("cloned");
    modWorkspace.setNamespace("cloned-ns");
    ResearchPurpose modPurpose = new ResearchPurpose();
    modPurpose.setAncestry(true);
    modWorkspace.setResearchPurpose(modPurpose);
    req.setWorkspace(modWorkspace);
    stubGetWorkspace(modWorkspace.getNamespace(), modWorkspace.getName(), LOGGED_IN_USER_EMAIL, WorkspaceAccessLevel.OWNER);
    Workspace workspace2 = workspacesController.cloneWorkspace(workspace.getNamespace(), workspace.getId(), req).getBody().getWorkspace();
    assertWithMessage("get and clone responses are inconsistent").that(workspace2).isEqualTo(workspacesController.getWorkspace(workspace2.getNamespace(), workspace2.getId()).getBody().getWorkspace());
    assertThat(workspace2.getName()).isEqualTo(modWorkspace.getName());
    assertThat(workspace2.getNamespace()).isEqualTo(modWorkspace.getNamespace());
    assertThat(workspace2.getResearchPurpose()).isEqualTo(modPurpose);
    // Original description should have been copied.
    assertThat(workspace2.getDescription()).isEqualTo(workspace.getDescription());
    // User roles should *not* be copied.
    assertThat(workspace2.getUserRoles().size()).isEqualTo(1);
    assertThat(workspace2.getUserRoles().get(0).getRole()).isEqualTo(WorkspaceAccessLevel.OWNER);
}
Also used : User(org.pmiops.workbench.db.model.User) CloneWorkspaceRequest(org.pmiops.workbench.model.CloneWorkspaceRequest) UserRole(org.pmiops.workbench.model.UserRole) WorkspaceACLUpdateResponseList(org.pmiops.workbench.firecloud.model.WorkspaceACLUpdateResponseList) ShareWorkspaceRequest(org.pmiops.workbench.model.ShareWorkspaceRequest) Workspace(org.pmiops.workbench.model.Workspace) WorkspaceACLUpdate(org.pmiops.workbench.firecloud.model.WorkspaceACLUpdate) ResearchPurpose(org.pmiops.workbench.model.ResearchPurpose) DataJpaTest(org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest) Test(org.junit.Test)

Example 7 with UserRole

use of org.pmiops.workbench.model.UserRole in project workbench by all-of-us.

the class WorkspacesController method shareWorkspace.

@Override
public ResponseEntity<ShareWorkspaceResponse> shareWorkspace(String workspaceNamespace, String workspaceId, ShareWorkspaceRequest request) {
    if (Strings.isNullOrEmpty(request.getWorkspaceEtag())) {
        throw new BadRequestException("Missing required update field 'workspaceEtag'");
    }
    org.pmiops.workbench.db.model.Workspace dbWorkspace = workspaceService.getRequired(workspaceNamespace, workspaceId);
    int version = Etags.toVersion(request.getWorkspaceEtag());
    if (dbWorkspace.getVersion() != version) {
        throw new ConflictException("Attempted to modify user roles with outdated workspace etag");
    }
    Set<WorkspaceUserRole> dbUserRoles = new HashSet<WorkspaceUserRole>();
    for (UserRole user : request.getItems()) {
        WorkspaceUserRole newUserRole = new WorkspaceUserRole();
        User newUser = userDao.findUserByEmail(user.getEmail());
        if (newUser == null) {
            throw new BadRequestException(String.format("User %s doesn't exist", user.getEmail()));
        }
        newUserRole.setUser(newUser);
        newUserRole.setRole(user.getRole());
        dbUserRoles.add(newUserRole);
    }
    // This automatically enforces owner role.
    dbWorkspace = workspaceService.updateUserRoles(dbWorkspace, dbUserRoles);
    ShareWorkspaceResponse resp = new ShareWorkspaceResponse();
    resp.setWorkspaceEtag(Etags.fromVersion(dbWorkspace.getVersion()));
    return ResponseEntity.ok(resp);
}
Also used : ShareWorkspaceResponse(org.pmiops.workbench.model.ShareWorkspaceResponse) User(org.pmiops.workbench.db.model.User) ConflictException(org.pmiops.workbench.exceptions.ConflictException) WorkspaceUserRole(org.pmiops.workbench.db.model.WorkspaceUserRole) WorkspaceUserRole(org.pmiops.workbench.db.model.WorkspaceUserRole) UserRole(org.pmiops.workbench.model.UserRole) BadRequestException(org.pmiops.workbench.exceptions.BadRequestException) HashSet(java.util.HashSet)

Aggregations

UserRole (org.pmiops.workbench.model.UserRole)7 Workspace (org.pmiops.workbench.model.Workspace)6 Test (org.junit.Test)5 ShareWorkspaceRequest (org.pmiops.workbench.model.ShareWorkspaceRequest)5 DataJpaTest (org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest)5 User (org.pmiops.workbench.db.model.User)4 WorkspaceACLUpdate (org.pmiops.workbench.firecloud.model.WorkspaceACLUpdate)4 WorkspaceACLUpdateResponseList (org.pmiops.workbench.firecloud.model.WorkspaceACLUpdateResponseList)4 ShareWorkspaceResponse (org.pmiops.workbench.model.ShareWorkspaceResponse)3 ConflictException (org.pmiops.workbench.exceptions.ConflictException)2 ResearchPurpose (org.pmiops.workbench.model.ResearchPurpose)2 HashSet (java.util.HashSet)1 WorkspaceUserRole (org.pmiops.workbench.db.model.WorkspaceUserRole)1 BadRequestException (org.pmiops.workbench.exceptions.BadRequestException)1 CloneWorkspaceRequest (org.pmiops.workbench.model.CloneWorkspaceRequest)1