Skip to content

Commit

Permalink
Merge pull request #167 from pixlise/development
Browse files Browse the repository at this point in the history
Public User Data Exporting
  • Loading branch information
RyanStonebraker authored Aug 31, 2023
2 parents 4a3eb43 + 1b72a17 commit 323a3b6
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
25 changes: 22 additions & 3 deletions api/endpoints/Export.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,37 @@ type exportFilesParams struct {
func registerExportHandler(router *apiRouter.ApiObjectRouter) {
const pathPrefix = "export"

router.AddGenericHandler(handlers.MakeEndpointPath(pathPrefix+"/files", datasetIdentifier), apiRouter.MakeMethodPermission("POST", permission.PermExportMap), exportFilesPost)
router.AddGenericHandler(handlers.MakeEndpointPath(pathPrefix+"/files", datasetIdentifier), apiRouter.MakeMethodPermission("POST", permission.PermPublic), exportFilesPost)
}

func exportFilesPost(params handlers.ApiHandlerGenericParams) error {
datasetID := params.PathParams[datasetIdentifier]

// Read in body
body, err := ioutil.ReadAll(params.Request.Body)
if err != nil {
return api.MakeBadRequestError(err)
}

isPublicUser := !params.UserInfo.Permissions[permission.PermExportMap]
isDatasetViewable := !isPublicUser

if isPublicUser {
publicObjectsAuth, err := permission.GetPublicObjectsAuth(params.Svcs.FS, params.Svcs.Config.ConfigBucket, isPublicUser)
if err != nil {
return err
}

isDatasetViewable, err = permission.CheckIsObjectInPublicSet(publicObjectsAuth.Datasets, datasetID)
if err != nil {
return err
}
}

if !isDatasetViewable {
return api.MakeBadRequestError(fmt.Errorf("Dataset %v is not public", datasetID))
}

var req exportFilesParams
err = json.Unmarshal(body, &req)
if err != nil {
Expand All @@ -78,8 +99,6 @@ func exportFilesPost(params handlers.ApiHandlerGenericParams) error {

// We need to export a ZIP file containing what is identified in File IDs

datasetID := params.PathParams[datasetIdentifier]

// Get the quantification file - if it's a shared file, quantUserID should be empty
quantPath := filepaths.GetUserQuantPath(params.UserInfo.UserID, datasetID, "")
quantID := req.QuantID
Expand Down
41 changes: 41 additions & 0 deletions api/endpoints/Export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"net/http"

"github.com/pixlise/core/v3/core/awsutil"
"github.com/pixlise/core/v3/core/pixlUser"
)

func Example_registerExportHandlerSunny() {
Expand All @@ -33,6 +34,16 @@ func Example_registerExportHandlerSunny() {

svcs := MakeMockSvcs(&mockS3, nil, nil, nil)
svcs.Exporter = &exp

mockUser := pixlUser.UserInfo{
Name: "Niko Bellic",
UserID: "600f2a0806b6c70071d3d174",
Email: "[email protected]",
Permissions: map[string]bool{
"export:map": true,
},
}
svcs.JWTReader = MockJWTReader{InfoToReturn: &mockUser}
apiRouter := MakeRouter(svcs)

req, _ := http.NewRequest("POST", "/export/files/983561", bytes.NewReader([]byte(`{
Expand Down Expand Up @@ -72,6 +83,16 @@ func Example_registerExportHandlerMissingFileName() {

svcs := MakeMockSvcs(&mockS3, nil, nil, nil)
svcs.Exporter = &exp
mockUser := pixlUser.UserInfo{
Name: "Niko Bellic",
UserID: "600f2a0806b6c70071d3d174",
Email: "[email protected]",
Permissions: map[string]bool{
"export:map": true,
},
}
svcs.JWTReader = MockJWTReader{InfoToReturn: &mockUser}

apiRouter := MakeRouter(svcs)

req, _ := http.NewRequest("POST", "/export/files/983561", bytes.NewReader([]byte(`{
Expand All @@ -95,6 +116,16 @@ func Example_registerExportHandlerMissingColumn() {

svcs := MakeMockSvcs(&mockS3, nil, nil, nil)
svcs.Exporter = &exp
mockUser := pixlUser.UserInfo{
Name: "Niko Bellic",
UserID: "600f2a0806b6c70071d3d174",
Email: "[email protected]",
Permissions: map[string]bool{
"export:map": true,
},
}
svcs.JWTReader = MockJWTReader{InfoToReturn: &mockUser}

apiRouter := MakeRouter(svcs)

req, _ := http.NewRequest("POST", "/export/files/983561", bytes.NewReader([]byte(`{
Expand All @@ -119,6 +150,16 @@ func Example_registerExportHandlerBadJSONBody() {

svcs := MakeMockSvcs(&mockS3, nil, nil, nil)
svcs.Exporter = &exp
mockUser := pixlUser.UserInfo{
Name: "Niko Bellic",
UserID: "600f2a0806b6c70071d3d174",
Email: "[email protected]",
Permissions: map[string]bool{
"export:map": true,
},
}
svcs.JWTReader = MockJWTReader{InfoToReturn: &mockUser}

apiRouter := MakeRouter(svcs)

req, _ := http.NewRequest("POST", "/export/files/983561", bytes.NewReader([]byte(`{
Expand Down

0 comments on commit 323a3b6

Please sign in to comment.