9
9
"github.com/google/uuid"
10
10
"golang.org/x/xerrors"
11
11
12
+ "github.com/coder/coder/v2/coderd/audit"
12
13
"github.com/coder/coder/v2/coderd/database"
13
14
"github.com/coder/coder/v2/coderd/database/dbtime"
14
15
"github.com/coder/coder/v2/coderd/httpapi"
@@ -41,8 +42,22 @@ func (*API) organization(rw http.ResponseWriter, r *http.Request) {
41
42
// @Success 201 {object} codersdk.Organization
42
43
// @Router /organizations [post]
43
44
func (api * API ) postOrganizations (rw http.ResponseWriter , r * http.Request ) {
44
- ctx := r .Context ()
45
- apiKey := httpmw .APIKey (r )
45
+ var (
46
+ // organizationID is required before the audit log entry is created.
47
+ organizationID = uuid .New ()
48
+ ctx = r .Context ()
49
+ apiKey = httpmw .APIKey (r )
50
+ auditor = api .Auditor .Load ()
51
+ aReq , commitAudit = audit .InitRequest [database.Organization ](rw , & audit.RequestParams {
52
+ Audit : * auditor ,
53
+ Log : api .Logger ,
54
+ Request : r ,
55
+ Action : database .AuditActionCreate ,
56
+ OrganizationID : organizationID ,
57
+ })
58
+ )
59
+ aReq .Old = database.Organization {}
60
+ defer commitAudit ()
46
61
47
62
var req codersdk.CreateOrganizationRequest
48
63
if ! httpapi .Read (ctx , rw , r , & req ) {
@@ -78,7 +93,7 @@ func (api *API) postOrganizations(rw http.ResponseWriter, r *http.Request) {
78
93
}
79
94
80
95
organization , err = tx .InsertOrganization (ctx , database.InsertOrganizationParams {
81
- ID : uuid . New () ,
96
+ ID : organizationID ,
82
97
Name : req .Name ,
83
98
DisplayName : req .DisplayName ,
84
99
Description : req .Description ,
@@ -119,6 +134,7 @@ func (api *API) postOrganizations(rw http.ResponseWriter, r *http.Request) {
119
134
return
120
135
}
121
136
137
+ aReq .New = organization
122
138
httpapi .Write (ctx , rw , http .StatusCreated , convertOrganization (organization ))
123
139
}
124
140
@@ -133,8 +149,20 @@ func (api *API) postOrganizations(rw http.ResponseWriter, r *http.Request) {
133
149
// @Success 200 {object} codersdk.Organization
134
150
// @Router /organizations/{organization} [patch]
135
151
func (api * API ) patchOrganization (rw http.ResponseWriter , r * http.Request ) {
136
- ctx := r .Context ()
137
- organization := httpmw .OrganizationParam (r )
152
+ var (
153
+ ctx = r .Context ()
154
+ organization = httpmw .OrganizationParam (r )
155
+ auditor = api .Auditor .Load ()
156
+ aReq , commitAudit = audit .InitRequest [database.Organization ](rw , & audit.RequestParams {
157
+ Audit : * auditor ,
158
+ Log : api .Logger ,
159
+ Request : r ,
160
+ Action : database .AuditActionWrite ,
161
+ OrganizationID : organization .ID ,
162
+ })
163
+ )
164
+ aReq .Old = organization
165
+ defer commitAudit ()
138
166
139
167
var req codersdk.UpdateOrganizationRequest
140
168
if ! httpapi .Read (ctx , rw , r , & req ) {
@@ -208,6 +236,7 @@ func (api *API) patchOrganization(rw http.ResponseWriter, r *http.Request) {
208
236
return
209
237
}
210
238
239
+ aReq .New = organization
211
240
httpapi .Write (ctx , rw , http .StatusOK , convertOrganization (organization ))
212
241
}
213
242
@@ -220,8 +249,20 @@ func (api *API) patchOrganization(rw http.ResponseWriter, r *http.Request) {
220
249
// @Success 200 {object} codersdk.Response
221
250
// @Router /organizations/{organization} [delete]
222
251
func (api * API ) deleteOrganization (rw http.ResponseWriter , r * http.Request ) {
223
- ctx := r .Context ()
224
- organization := httpmw .OrganizationParam (r )
252
+ var (
253
+ ctx = r .Context ()
254
+ organization = httpmw .OrganizationParam (r )
255
+ auditor = api .Auditor .Load ()
256
+ aReq , commitAudit = audit .InitRequest [database.Organization ](rw , & audit.RequestParams {
257
+ Audit : * auditor ,
258
+ Log : api .Logger ,
259
+ Request : r ,
260
+ Action : database .AuditActionDelete ,
261
+ OrganizationID : organization .ID ,
262
+ })
263
+ )
264
+ aReq .Old = organization
265
+ defer commitAudit ()
225
266
226
267
if organization .IsDefault {
227
268
httpapi .Write (ctx , rw , http .StatusBadRequest , codersdk.Response {
@@ -239,6 +280,7 @@ func (api *API) deleteOrganization(rw http.ResponseWriter, r *http.Request) {
239
280
return
240
281
}
241
282
283
+ aReq .New = database.Organization {}
242
284
httpapi .Write (ctx , rw , http .StatusOK , codersdk.Response {
243
285
Message : "Organization has been deleted." ,
244
286
})
0 commit comments