diff --git a/cypress/e2e/01_workspaces.cy.ts b/cypress/e2e/01_workspaces.cy.ts index 08861aaa6..2e41d95e6 100644 --- a/cypress/e2e/01_workspaces.cy.ts +++ b/cypress/e2e/01_workspaces.cy.ts @@ -39,6 +39,7 @@ describe('Edit Mission', () => { headers: { 'x-jwt': `${value}` }, body: { uuid: Workspaces[0].uuid, + owner_pubkey: Workspaces[0].owner_pubkey, mission: 'This is a sample mission for workspace' } }).then((resp) => { @@ -59,6 +60,7 @@ describe('Edit Tactics', () => { headers: { 'x-jwt': `${value}` }, body: { uuid: Workspaces[0].uuid, + owner_pubkey: Workspaces[0].owner_pubkey, mission: 'This is a sample tactics and objectives for workspace' } }).then((resp) => { @@ -79,6 +81,7 @@ describe('Edit Schematics Url', () => { headers: { 'x-jwt': `${value}` }, body: { uuid: Workspaces[0].uuid, + owner_pubkey: Workspaces[0].owner_pubkey, mission: 'This is a sample schematic url for workspaces' } }).then((resp) => { diff --git a/db/structs.go b/db/structs.go index 7f1bf8ea6..0b06d36a7 100644 --- a/db/structs.go +++ b/db/structs.go @@ -495,20 +495,23 @@ type Organization struct { } type Workspace struct { - ID uint `json:"id"` - Uuid string `json:"uuid"` - Name string `gorm:"unique;not null" json:"name"` - OwnerPubKey string `json:"owner_pubkey"` - Img string `json:"img"` - Created *time.Time `json:"created"` - Updated *time.Time `json:"updated"` - Show bool `json:"show"` - Deleted bool `gorm:"default:false" json:"deleted"` - BountyCount int64 `json:"bounty_count,omitempty"` - Budget uint `json:"budget,omitempty"` - Website string `json:"website" validate:"omitempty,uri"` - Github string `json:"github" validate:"omitempty,uri"` - Description string `json:"description" validate:"omitempty,lte=120"` + ID uint `json:"id"` + Uuid string `json:"uuid"` + Name string `gorm:"unique;not null" json:"name"` + OwnerPubKey string `json:"owner_pubkey"` + Img string `json:"img"` + Created *time.Time `json:"created"` + Updated *time.Time `json:"updated"` + Show bool `json:"show"` + Deleted bool `gorm:"default:false" json:"deleted"` + BountyCount int64 `json:"bounty_count,omitempty"` + Budget uint `json:"budget,omitempty"` + Website string `json:"website" validate:"omitempty,uri"` + Github string `json:"github" validate:"omitempty,uri"` + Description string `json:"description" validate:"omitempty,lte=120"` + Mission string `json:"mission"` + Tactics string `json:"tactics"` + SchematicUrl string `json:"schematic_url"` } type WorkspaceShort struct { diff --git a/handlers/workspaces.go b/handlers/workspaces.go index 6ad121f5f..0b152c96d 100644 --- a/handlers/workspaces.go +++ b/handlers/workspaces.go @@ -744,3 +744,54 @@ func (oh *workspaceHandler) DeleteWorkspace(w http.ResponseWriter, r *http.Reque w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(workspace) } + +func (oh *workspaceHandler) UpdateWorkspace(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string) + if pubKeyFromAuth == "" { + fmt.Println("no pubkey from auth") + w.WriteHeader(http.StatusUnauthorized) + return + } + + workspace := db.Workspace{} + body, _ := io.ReadAll(r.Body) + r.Body.Close() + err := json.Unmarshal(body, &workspace) + + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusNotAcceptable) + return + } + + if pubKeyFromAuth != workspace.OwnerPubKey { + hasRole := db.UserHasAccess(pubKeyFromAuth, workspace.Uuid, db.EditOrg) + if !hasRole { + fmt.Println(pubKeyFromAuth) + fmt.Println(workspace.OwnerPubKey) + fmt.Println("mismatched pubkey") + w.WriteHeader(http.StatusUnauthorized) + json.NewEncoder(w).Encode("Don't have access to Edit workspace") + return + } + } + + // Validate struct data + err = db.Validate.Struct(workspace) + if err != nil { + w.WriteHeader(http.StatusBadRequest) + msg := fmt.Sprintf("Error: did not pass validation test : %s", err) + json.NewEncoder(w).Encode(msg) + return + } + + p, err := oh.db.CreateOrEditWorkspace(workspace) + if err != nil { + w.WriteHeader(http.StatusBadRequest) + return + } + + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(p) +} diff --git a/routes/workspaces.go b/routes/workspaces.go index 54b740c9f..2ac1cfd08 100644 --- a/routes/workspaces.go +++ b/routes/workspaces.go @@ -38,6 +38,10 @@ func WorkspaceRoutes() chi.Router { r.Get("/poll/invoices/{uuid}", workspaceHandlers.PollBudgetInvoices) r.Get("/invoices/count/{uuid}", handlers.GetInvoicesCount) r.Delete("/delete/{uuid}", workspaceHandlers.DeleteWorkspace) + + r.Post("/mission", workspaceHandlers.UpdateWorkspace) + r.Post("/tactics", workspaceHandlers.UpdateWorkspace) + r.Post("/schematicurl", workspaceHandlers.UpdateWorkspace) }) return r }