-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
create.go
124 lines (106 loc) · 3.24 KB
/
create.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2021 Harness Inc. All rights reserved.
// Use of this source code is governed by the Polyform Free Trial License
// that can be found in the LICENSE.md file for this repository.
package space
import (
"encoding/json"
"net/http"
"strings"
"time"
"github.com/harness/gitness/internal/api/render"
"github.com/harness/gitness/internal/api/request"
"github.com/harness/gitness/internal/guard"
"github.com/harness/gitness/internal/paths"
"github.com/harness/gitness/internal/store"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/check"
"github.com/harness/gitness/types/enum"
"github.com/rs/zerolog/hlog"
)
type spaceCreateRequest struct {
PathName string `json:"pathName"`
ParentID int64 `json:"parentId"`
Name string `json:"name"`
Description string `json:"description"`
IsPublic bool `json:"isPublic"`
}
/*
* HandleCreate returns an http.HandlerFunc that creates a new space.
*/
func HandleCreate(guard *guard.Guard, spaceStore store.SpaceStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
log := hlog.FromRequest(r)
in := new(spaceCreateRequest)
err := json.NewDecoder(r.Body).Decode(in)
if err != nil {
render.BadRequestf(w, "Invalid request body: %s.", err)
return
}
// get current principal (will be enforced to not be nil via explicit check or guard.Enforce)
principal, _ := request.PrincipalFrom(ctx)
// Collect parent path along the way - needed for duplicate error message
parentPath := ""
/*
* AUTHORIZATION
* Can only be done once we know the parent space
*/
if in.ParentID <= 0 {
// TODO: Restrict top level space creation.
if principal == nil {
render.Unauthorized(w)
return
}
} else {
// Create is a special case - we need the parent path
var parent *types.Space
parent, err = spaceStore.Find(ctx, in.ParentID)
if err != nil {
log.Err(err).Msgf("Failed to get space with id '%d'.", in.ParentID)
render.UserfiedErrorOrInternal(w, err)
return
}
scope := &types.Scope{SpacePath: parent.Path}
resource := &types.Resource{
Type: enum.ResourceTypeSpace,
Name: "",
}
if !guard.Enforce(w, r, scope, resource, enum.PermissionSpaceCreate) {
return
}
parentPath = parent.Path
}
// create new space object
space := &types.Space{
PathName: strings.ToLower(in.PathName),
ParentID: in.ParentID,
Name: in.Name,
Description: in.Description,
IsPublic: in.IsPublic,
CreatedBy: principal.ID,
Created: time.Now().UnixMilli(),
Updated: time.Now().UnixMilli(),
}
// validate space
if err = check.Space(space); err != nil {
render.UserfiedErrorOrInternal(w, err)
return
}
// Validate path length (Due to racing conditions we can't be 100% sure on the path here only best effort
// to have a quick failure)
path := paths.Concatinate(parentPath, space.PathName)
if err = check.Path(path, true); err != nil {
render.UserfiedErrorOrInternal(w, err)
return
}
// create in store
err = spaceStore.Create(ctx, space)
if err != nil {
log.Error().Err(err).
Msg("Space creation failed.")
render.UserfiedErrorOrInternal(w, err)
return
}
render.JSON(w, http.StatusOK, space)
}
}