This repository has been archived by the owner on Jun 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.go
138 lines (113 loc) · 3.43 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package organisation
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"github.com/factly/kavach-server/model"
"github.com/factly/kavach-server/util/keto"
"github.com/factly/x/errorx"
"github.com/factly/x/loggerx"
"github.com/factly/x/renderx"
"github.com/factly/x/validationx"
)
type organisation struct {
Title string `json:"title" validate:"required"`
Slug string `json:"slug"`
Description string `json:"description"`
FeaturedMediumID uint `json:"featured_medium_id"`
}
// create - Create organisation
// @Summary Create organisation
// @Description Create organisation
// @Tags Organisation
// @ID add-organisation
// @Consume json
// @Produce json
// @Param X-User header string true "User ID"
// @Param Organisation body organisation true "Organisation Object"
// @Success 201 {object} orgWithRole
// @Failure 400 {array} string
// @Router /organisations [post]
func create(w http.ResponseWriter, r *http.Request) {
userID, err := strconv.Atoi(r.Header.Get("X-User"))
if err != nil {
errorx.Render(w, errorx.Parser(errorx.InvalidID()))
return
}
org := &organisation{}
err = json.NewDecoder(r.Body).Decode(&org)
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DecodeError()))
return
}
validationError := validationx.Check(org)
if validationError != nil {
loggerx.Error(errors.New("validation error"))
errorx.Render(w, validationError)
return
}
mediumID := &org.FeaturedMediumID
if org.FeaturedMediumID == 0 {
mediumID = nil
}
organisation := &model.Organisation{
Title: org.Title,
Slug: org.Slug,
Description: org.Description,
FeaturedMediumID: mediumID,
}
tx := model.DB.WithContext(context.WithValue(r.Context(), userContext, userID)).Begin()
err = tx.Model(&model.Organisation{}).Create(&organisation).Error
if err != nil {
tx.Rollback()
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DBError()))
return
}
tx.Model(&model.Organisation{}).Preload("Medium").First(&organisation)
permission := model.OrganisationUser{}
permission.OrganisationID = organisation.ID
permission.UserID = uint(userID)
permission.Role = "owner"
err = tx.Model(&model.OrganisationUser{}).Create(&permission).Error
if err != nil {
tx.Rollback()
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DBError()))
return
}
result := orgWithRole{}
result.Organisation = *organisation
result.Permission = permission
/* creating role of admins */
reqRole := &model.Role{}
reqRole.ID = "roles:org:" + fmt.Sprint(organisation.ID) + ":admin"
reqRole.Members = []string{fmt.Sprint(userID)}
err = keto.UpdateRole("/engines/acp/ory/regex/roles", reqRole)
if err != nil {
tx.Rollback()
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.NetworkError()))
return
}
/* creating policy for admins */
reqPolicy := &model.Policy{}
reqPolicy.ID = "org:" + fmt.Sprint(organisation.ID) + ":admins"
reqPolicy.Subjects = []string{reqRole.ID}
reqPolicy.Resources = []string{"resources:org:" + fmt.Sprint(organisation.ID) + ":<.*>"}
reqPolicy.Actions = []string{"actions:org:" + fmt.Sprint(organisation.ID) + ":<.*>"}
reqPolicy.Effect = "allow"
err = keto.UpdatePolicy("/engines/acp/ory/regex/policies", reqPolicy)
if err != nil {
tx.Rollback()
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.NetworkError()))
return
}
tx.Commit()
renderx.JSON(w, http.StatusCreated, result)
}