Skip to content

Commit 6996ff5

Browse files
BrunoQuaresmakylecarbs
authored andcommitted
feat: add PUT /api/v2/users/:user-id/suspend endpoint (#1154)
1 parent cc6cfb2 commit 6996ff5

File tree

14 files changed

+202
-25
lines changed

14 files changed

+202
-25
lines changed

coderd/audit/table.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ var AuditableResources = auditMap(map[any]map[string]Action{
4141
"hashed_password": ActionSecret, // A user can change their own password.
4242
"created_at": ActionIgnore, // Never changes.
4343
"updated_at": ActionIgnore, // Changes, but is implicit and not helpful in a diff.
44+
"status": ActionTrack, // A user can update another user status
4445
},
4546
&database.Workspace{}: {
4647
"id": ActionIgnore, // Never changes.

coderd/coderd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ func New(options *Options) (http.Handler, func()) {
182182
r.Use(httpmw.ExtractUserParam(options.Database))
183183
r.Get("/", api.userByName)
184184
r.Put("/profile", api.putUserProfile)
185+
r.Put("/suspend", api.putUserSuspend)
185186
r.Get("/organizations", api.organizationsByUser)
186187
r.Post("/organizations", api.postOrganizationsByUser)
187188
r.Post("/keys", api.postAPIKey)

coderd/database/databasefake/databasefake.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,7 @@ func (q *fakeQuerier) InsertUser(_ context.Context, arg database.InsertUserParam
11381138
CreatedAt: arg.CreatedAt,
11391139
UpdatedAt: arg.UpdatedAt,
11401140
Username: arg.Username,
1141+
Status: database.UserStatusActive,
11411142
}
11421143
q.users = append(q.users, user)
11431144
return user, nil
@@ -1159,6 +1160,22 @@ func (q *fakeQuerier) UpdateUserProfile(_ context.Context, arg database.UpdateUs
11591160
return database.User{}, sql.ErrNoRows
11601161
}
11611162

1163+
func (q *fakeQuerier) UpdateUserStatus(_ context.Context, arg database.UpdateUserStatusParams) (database.User, error) {
1164+
q.mutex.Lock()
1165+
defer q.mutex.Unlock()
1166+
1167+
for index, user := range q.users {
1168+
if user.ID != arg.ID {
1169+
continue
1170+
}
1171+
user.Status = arg.Status
1172+
user.UpdatedAt = arg.UpdatedAt
1173+
q.users[index] = user
1174+
return user, nil
1175+
}
1176+
return database.User{}, sql.ErrNoRows
1177+
}
1178+
11621179
func (q *fakeQuerier) InsertWorkspace(_ context.Context, arg database.InsertWorkspaceParams) (database.Workspace, error) {
11631180
q.mutex.Lock()
11641181
defer q.mutex.Unlock()

coderd/database/dump.sql

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ALTER TABLE ONLY users
2+
DROP COLUMN IF EXISTS status;
3+
4+
DROP TYPE user_status;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CREATE TYPE user_status AS ENUM ('active', 'suspended');
2+
3+
ALTER TABLE ONLY users
4+
ADD COLUMN IF NOT EXISTS status user_status NOT NULL DEFAULT 'active';

coderd/database/models.go

Lines changed: 26 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/querier.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

Lines changed: 41 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/users.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,12 @@ ORDER BY
9090
LIMIT
9191
-- A null limit means "no limit", so -1 means return all
9292
NULLIF(@limit_opt :: int, -1);
93+
94+
-- name: UpdateUserStatus :one
95+
UPDATE
96+
users
97+
SET
98+
status = $2,
99+
updated_at = $3
100+
WHERE
101+
id = $1 RETURNING *;

0 commit comments

Comments
 (0)