-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworkspaces.go
46 lines (38 loc) · 1.22 KB
/
workspaces.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package rest
import (
"net/http"
"github.com/equinor/flowify-workflows-server/pkg/workspace"
"github.com/equinor/flowify-workflows-server/user"
"github.com/gorilla/mux"
)
func RegisterWorkspaceRoutes(r *mux.Route) {
s := r.Subrouter()
const intype = "application/json"
const outtype = "application/json"
s.Use(CheckContentHeaderMiddleware(intype))
s.Use(CheckAcceptRequestHeaderMiddleware(outtype))
s.Use(SetContentTypeMiddleware(outtype))
s.HandleFunc("/workspaces/", WorkspacesListHandler()).Methods(http.MethodGet)
}
func WorkspacesListHandler() http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
wss := GetWorkspaceAccess(r.Context())
lst := []workspace.WorkspaceGetRequest{}
usr := user.GetUser(r.Context())
for _, ws := range wss {
wsgr := workspace.WorkspaceGetRequest{Name: ws.Name, Description: ws.Description}
roles := []string{}
if ws.UserHasAccess(usr) {
roles = append(roles, "user")
}
if ws.UserHasAdminAccess(usr) {
roles = append(roles, "admin")
}
wsgr.Roles = roles
lst = append(lst, wsgr)
}
WriteResponse(w, http.StatusOK, nil, struct {
Items []workspace.WorkspaceGetRequest `json:"items"`
}{Items: lst}, "workspace")
})
}