@@ -40,7 +40,7 @@ func (api *API) postOrgRoles(rw http.ResponseWriter, r *http.Request) {
40
40
Audit : * auditor ,
41
41
Log : api .Logger ,
42
42
Request : r ,
43
- Action : database .AuditActionWrite ,
43
+ Action : database .AuditActionCreate ,
44
44
OrganizationID : organization .ID ,
45
45
})
46
46
)
@@ -55,28 +55,7 @@ func (api *API) postOrgRoles(rw http.ResponseWriter, r *http.Request) {
55
55
return
56
56
}
57
57
58
- originalRoles , err := db .CustomRoles (ctx , database.CustomRolesParams {
59
- LookupRoles : []database.NameOrganizationPair {
60
- {
61
- Name : req .Name ,
62
- OrganizationID : organization .ID ,
63
- },
64
- },
65
- ExcludeOrgRoles : false ,
66
- // Linter requires all fields to be set. This field is not actually required.
67
- OrganizationID : organization .ID ,
68
- })
69
- // If it is a 404 (not found) error, ignore it.
70
- if err != nil && ! httpapi .Is404Error (err ) {
71
- httpapi .InternalServerError (rw , err )
72
- return
73
- }
74
- if len (originalRoles ) == 1 {
75
- // For auditing changes to a role.
76
- aReq .Old = originalRoles [0 ]
77
- }
78
-
79
- inserted , err := db .UpsertCustomRole (ctx , database.UpsertCustomRoleParams {
58
+ inserted , err := db .InsertCustomRole (ctx , database.InsertCustomRoleParams {
80
59
Name : req .Name ,
81
60
DisplayName : req .DisplayName ,
82
61
OrganizationID : uuid.NullUUID {
@@ -115,7 +94,7 @@ func (api *API) postOrgRoles(rw http.ResponseWriter, r *http.Request) {
115
94
// @Tags Members
116
95
// @Success 200 {array} codersdk.Role
117
96
// @Router /organizations/{organization}/members/roles [patch]
118
- func (api * API ) patchOrgRoles (rw http.ResponseWriter , r * http.Request ) {
97
+ func (api * API ) putOrgRoles (rw http.ResponseWriter , r * http.Request ) {
119
98
var (
120
99
ctx = r .Context ()
121
100
db = api .Database
@@ -136,38 +115,7 @@ func (api *API) patchOrgRoles(rw http.ResponseWriter, r *http.Request) {
136
115
return
137
116
}
138
117
139
- // This check is not ideal, but we cannot enforce a unique role name in the db against
140
- // the built-in role names.
141
- if rbac .ReservedRoleName (req .Name ) {
142
- httpapi .Write (ctx , rw , http .StatusBadRequest , codersdk.Response {
143
- Message : "Reserved role name" ,
144
- Detail : fmt .Sprintf ("%q is a reserved role name, and not allowed to be used" , req .Name ),
145
- })
146
- return
147
- }
148
-
149
- if err := httpapi .NameValid (req .Name ); err != nil {
150
- httpapi .Write (ctx , rw , http .StatusBadRequest , codersdk.Response {
151
- Message : "Invalid role name" ,
152
- Detail : err .Error (),
153
- })
154
- return
155
- }
156
-
157
- // Only organization permissions are allowed to be granted
158
- if len (req .SitePermissions ) > 0 {
159
- httpapi .Write (ctx , rw , http .StatusBadRequest , codersdk.Response {
160
- Message : "Invalid request, not allowed to assign site wide permissions for an organization role." ,
161
- Detail : "organization scoped roles may not contain site wide permissions" ,
162
- })
163
- return
164
- }
165
-
166
- if len (req .UserPermissions ) > 0 {
167
- httpapi .Write (ctx , rw , http .StatusBadRequest , codersdk.Response {
168
- Message : "Invalid request, not allowed to assign user permissions for an organization role." ,
169
- Detail : "organization scoped roles may not contain user permissions" ,
170
- })
118
+ if ! validOrganizationRoleRequest (ctx , req , rw ) {
171
119
return
172
120
}
173
121
@@ -192,7 +140,7 @@ func (api *API) patchOrgRoles(rw http.ResponseWriter, r *http.Request) {
192
140
aReq .Old = originalRoles [0 ]
193
141
}
194
142
195
- inserted , err := db .UpsertCustomRole (ctx , database.UpsertCustomRoleParams {
143
+ updated , err := db .UpdateCustomRole (ctx , database.UpdateCustomRoleParams {
196
144
Name : req .Name ,
197
145
DisplayName : req .DisplayName ,
198
146
OrganizationID : uuid.NullUUID {
@@ -214,9 +162,9 @@ func (api *API) patchOrgRoles(rw http.ResponseWriter, r *http.Request) {
214
162
})
215
163
return
216
164
}
217
- aReq .New = inserted
165
+ aReq .New = updated
218
166
219
- httpapi .Write (ctx , rw , http .StatusOK , db2sdk .Role (inserted ))
167
+ httpapi .Write (ctx , rw , http .StatusOK , db2sdk .Role (updated ))
220
168
}
221
169
222
170
// deleteOrgRole will remove a custom role from an organization
0 commit comments