Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/#777 #783

Merged
merged 8 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions grails-app/controllers/au/org/ala/profile/api/ApiController.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import au.ala.org.ws.security.RequireApiKey
import au.org.ala.profile.hub.BaseController
import au.org.ala.profile.hub.MapService
import au.org.ala.profile.hub.ProfileService
import au.org.ala.profile.security.GrantAccess
import au.org.ala.profile.security.RequiresAccessToken
import grails.converters.JSON

Expand All @@ -24,6 +25,7 @@ import au.org.ala.plugins.openapi.Path
type = SecuritySchemeType.HTTP,
scheme = "bearer"
)

@RequireApiKey()
class ApiController extends BaseController {
static namespace = "v1"
Expand Down Expand Up @@ -106,6 +108,43 @@ class ApiController extends BaseController {
}
}

@GrantAccess
@Path("/api/opus")
@Operation(
summary = "Get all public collections",
operationId = "/api/opus",
method = "GET",
responses = [
@ApiResponse(
responseCode = "200",
content = @Content(
mediaType = "application/json",
array = @ArraySchema(
schema = @Schema(
implementation = CollectionList.class
)
)
)
),
@ApiResponse(responseCode = "400",
description = "opusId is a required parameter"),
@ApiResponse(responseCode = "403",
description = "You do not have the necessary permissions to perform this action."),
@ApiResponse(responseCode = "405",
description = "An unexpected error has occurred while processing your request."),
@ApiResponse(responseCode = "404",
description = "Collection not found"),
@ApiResponse(responseCode = "500",
description = "An unexpected error has occurred while processing your request.")
]
)
def getListCollections () {
List opus = profileService.getOpus() as List
List filtered = opus.findAll(it-> !it.privateCollection)
.collect{new CollectionList(uuid: it.uuid, shortName:it.shortName, title:it.title, thumbnailUrl:it.thumbnailUrl, description:it.description)}
render filtered as JSON
}

@Path("/api/opus/{opusId}/profile")
@Operation(
summary = "List profiles in a collection",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package au.org.ala.profile.api

import au.org.ala.profile.hub.ProfileService
import au.org.ala.profile.security.RequiresAccessToken
import au.org.ala.profile.security.GrantAccess
import au.org.ala.web.AuthService
import grails.converters.JSON
import org.apache.http.HttpStatus
Expand Down Expand Up @@ -38,6 +39,8 @@ class ApiInterceptor {
} else {
authorised = true
}
} else if (method?.isAnnotationPresent(GrantAccess)){
authorised = true
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ class UrlMappings {
get "/opus/$opusId/profile/$profileId/image" (version: "1.0", controller: "api", action: "getImages", namespace: "v1")
get "/opus/$opusId/profile/$profileId/attribute/$attributeId" (version: "1.0", controller: "api", action: "getAttributes", namespace: "v1")
get "/opus/$opusId/profile/$profileId/draft" (version: "1.0", controller: "api", action: "getDraftProfile", namespace: "v1")
get "/opus" (version: "1.0", controller: "api", action: "getListCollections", namespace: "v1")
}

"/openapi/$action?/$id?(.$format)?"(controller: "openApi")
Expand Down
12 changes: 12 additions & 0 deletions src/main/groovy/au/org/ala/profile/api/CollectionList.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package au.org.ala.profile.api

import groovy.transform.ToString

@ToString
class CollectionList {
String uuid
String shortName
String title
String thumbnailUrl
String description
}
13 changes: 13 additions & 0 deletions src/main/groovy/au/org/ala/profile/security/GrantAccess.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package au.org.ala.profile.security

import java.lang.annotation.*

/**
* Annotation to check that a valid collection-specific access token has been provided.
*/
@Target([ElementType.TYPE, ElementType.METHOD])
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface GrantAccess {

}
11 changes: 11 additions & 0 deletions src/test/groovy/au/org/ala/profile/api/ApiControllerSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,15 @@ class ApiControllerSpec extends Specification implements ControllerUnitTest<ApiC
'opus1' | '123' | null | 404
'opus1' | '123' | 'a,b' | 200
}

void "getOpusList should be provided"() {
setup:
profileService.getOpusList()>> [[uuid: 'abc',shortName:'alatest',title:'title1',desciption:'desc1',thubnailUrl:'test.png']]

when:
controller.getListCollections()

then:
response.status == 200
}
}
Loading