Skip to content
This repository has been archived by the owner on Jan 21, 2021. It is now read-only.

Commit

Permalink
Merge pull request #29 from thehyve/gb-dev-acces
Browse files Browse the repository at this point in the history
Gb dev acces
  • Loading branch information
PiotrZakrzewski authored Sep 23, 2016
2 parents 38a1eaa + 26c186f commit 0077557
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,39 @@ package org.transmartproject.rest

import grails.rest.Link
import grails.rest.render.util.AbstractLinkingRenderer
import org.springframework.beans.factory.annotation.Autowired
import org.transmartproject.rest.misc.CurrentUser

import javax.annotation.Resource

import org.transmartproject.core.ontology.StudiesResource
import org.transmartproject.core.ontology.Study
import org.transmartproject.rest.marshallers.ContainerResponseWrapper

import static org.transmartproject.core.users.ProtectedOperation.WellKnownOperations.READ

class StudyController {

static responseFormats = ['json', 'hal']

@Resource
StudiesResource studiesResourceService

@Autowired
CurrentUser currentUser

/** GET request on /studies/
* This will return the list of studies, where each study will be rendered in its short format
*/
def index() {
respond wrapStudies(studiesResourceService.studySet)
def studies = studiesResourceService.studySet
studies.each { study ->
boolean access = currentUser.canPerform(READ, study)
study.access = access
}
def studiesImpl = wrapStudies(studies)
//Checks to which studies the user has access.
respond studiesImpl
}

/** GET request on /studies/${id}
Expand All @@ -54,7 +68,11 @@ class StudyController {
* @param name the name of the study
*/
def show(String id) {
respond studiesResourceService.getStudyById(id)
def studyImpl = studiesResourceService.getStudyById(id)
//Check if the user has access to the specific study.
boolean access = currentUser.canPerform(READ, studyImpl)
studyImpl.access = access
respond studyImpl
}

private wrapStudies(Object source) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,16 @@ class StudySerializationHelper extends AbstractHalOrJsonSerializationHelper<Stud
@Override
Map<String, Object> convertToMap(Study study) {
def term = new OntologyTermWrapper(study.ontologyTerm, true)
[id: study.id, ontologyTerm: term]
def mapResponse = [id: study.id, ontologyTerm: term]
if (study.hasProperty('access')){
mapResponse['access'] = study.access
}
mapResponse
}

@Override
Set<String> getEmbeddedEntities(Study object) {
['ontologyTerm'] as Set
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class StudiesResourceTests extends ResourceTestCase {
assertThat JSON, hasEntry(is('studies'), contains(
allOf(
hasEntry('id', 'STUDY_ID_1'),
hasEntry('access', true),
hasEntry(is('ontologyTerm'), allOf(
hasEntry('name', 'study1'),
hasEntry('fullName', '\\foo\\study1\\'),
Expand All @@ -49,6 +50,7 @@ class StudiesResourceTests extends ResourceTestCase {
),
allOf(
hasEntry('id', 'STUDY_ID_2'),
hasEntry('access', true),
hasEntry(is('ontologyTerm'), allOf(
hasEntry('name', 'study2'),
hasEntry('fullName', '\\foo\\study2\\'),
Expand All @@ -57,6 +59,7 @@ class StudiesResourceTests extends ResourceTestCase {
),
allOf(
hasEntry('id', 'STUDY_ID_3'),
hasEntry('access', true),
hasEntry(is('ontologyTerm'), allOf(
hasEntry('name', 'study3'),
hasEntry('fullName', '\\foo\\study3\\'),
Expand All @@ -77,6 +80,7 @@ class StudiesResourceTests extends ResourceTestCase {
contains(
allOf(
hasEntry('id', 'STUDY_ID_1'),
hasEntry('access', true),
halIndexResponse("/$version/studies/study_id_1", ['ontologyTerm':
allOf(
hasEntry('name', 'study1'),
Expand All @@ -87,6 +91,7 @@ class StudiesResourceTests extends ResourceTestCase {
),
allOf(
hasEntry('id', 'STUDY_ID_2'),
hasEntry('access', true),
halIndexResponse("/$version/studies/study_id_2", ['ontologyTerm':
allOf(
hasEntry('name', 'study2'),
Expand All @@ -97,6 +102,7 @@ class StudiesResourceTests extends ResourceTestCase {
),
allOf(
hasEntry('id', 'STUDY_ID_3'),
hasEntry('access', true),
halIndexResponse("/$version/studies/study_id_3", ['ontologyTerm':
allOf(
hasEntry('name', 'study3'),
Expand All @@ -116,6 +122,7 @@ class StudiesResourceTests extends ResourceTestCase {

assertThat JSON, allOf(
hasEntry('id', 'STUDY_ID_1'),
hasEntry('access', true),
hasEntry(is('ontologyTerm'), allOf(
hasEntry('name', 'study1'),
hasEntry('fullName', '\\foo\\study1\\'),
Expand All @@ -133,6 +140,7 @@ class StudiesResourceTests extends ResourceTestCase {

assertThat result, allOf(
hasEntry('id', 'STUDY_ID_1'),
hasEntry('access', true),
halIndexResponse("/$version/studies/${studyId}".toLowerCase(), [
'ontologyTerm': allOf(
hasEntry('name', 'study1'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ class StudyMarshallerTests {
private static final String ONTOLOGY_KEY = '\\\\foo bar\\foo\\test_study\\'
private static final String ONTOLOGY_FULL_NAME = '\\foo\\test_study\\'
private static final String version = 'v1'
private static final boolean ACCESS = true

Study getMockStudy() {
createStudy(STUDY_ID, ONTOLOGY_KEY)
createStudy(STUDY_ID, ONTOLOGY_KEY, ACCESS)
}

@Test
Expand All @@ -59,6 +60,7 @@ class StudyMarshallerTests {
JsonSlurper slurper = new JsonSlurper()
assertThat slurper.parseText(json.toString()), allOf(
hasEntry('id', STUDY_ID),
hasEntry('access', ACCESS),
hasEntry(is('ontologyTerm'), allOf(
hasEntry('name', ONTOLOGY_TERM_NAME),
hasEntry('fullName', ONTOLOGY_FULL_NAME),
Expand All @@ -78,6 +80,7 @@ class StudyMarshallerTests {
JsonSlurper slurper = new JsonSlurper()
assertThat slurper.parseText(stringResult), allOf(
hasEntry('id', STUDY_ID),
hasEntry('access', ACCESS),
hasEntry(is('_links'),
hasEntry(is('self'),
hasEntry('href', "/$version/studies/${STUDY_ID.toLowerCase()}".toString()))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ class StubStudyLoadingService extends StudyLoadingService {
storedStudy
}

static Study createStudy(String studyId, String key) {
static Study createStudy(String studyId, String key, boolean access = null) {
def study
study = [
getId: { -> studyId },
getAccess: { -> access},
getOntologyTerm: { ->
[
getName: { -> getComponents(key, -1) },
Expand Down

0 comments on commit 0077557

Please sign in to comment.