Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions coderd/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
// @Router /audit [get]
func (api *API) auditLogs(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
apiKey := httpmw.APIKey(r)

page, ok := parsePagination(rw, r)
if !ok {
Expand All @@ -55,6 +56,11 @@ func (api *API) auditLogs(rw http.ResponseWriter, r *http.Request) {
filter.Offset = int32(page.Offset)
filter.Limit = int32(page.Limit)

if filter.Username == "me" {
filter.UserID = apiKey.UserID
filter.Username = ""
}

dblogs, err := api.Database.GetAuditLogsOffset(ctx, filter)
if err != nil {
httpapi.InternalServerError(rw, err)
Expand Down
28 changes: 18 additions & 10 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion coderd/database/queries/auditlogs.sql
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,16 @@ WHERE
action = @action :: audit_action
ELSE true
END
-- Filter by user_id
AND CASE
WHEN @user_id :: uuid != '00000000-0000-0000-0000-000000000000'::uuid THEN
user_id = @user_id
ELSE true
END
-- Filter by username
AND CASE
WHEN @username :: text != '' THEN
users.username = @username
user_id = (SELECT id FROM users WHERE lower(username) = lower(@username) AND deleted = false)
ELSE true
END
-- Filter by user_email
Expand Down
2 changes: 1 addition & 1 deletion docs/admin/audit-logs.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ The supported filters are:
- `resource_id` - The ID of the resource.
- `resource_target` - The name of the resource. Can be used instead of `resource_id`.
- `action`- The action applied to a resource. You can [find here](https://pkg.go.dev/github.com/coder/coder/codersdk#AuditAction) all the actions that are supported.
- `username` - The username of the user who triggered the action.
- `username` - The username of the user who triggered the action. You can also use `me` as a convenient alias for the logged-in user.
- `email` - The email of the user who triggered the action.
- `date_from` - The inclusive start date with format `YYYY-MM-DD`.
- `date_to` - The inclusive end date with format `YYYY-MM-DD`.
Expand Down