Skip to content

chore: move AsSystemRestricted to caller #10163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2023
Merged
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
16 changes: 9 additions & 7 deletions coderd/httpmw/userparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ func ExtractUserParam(db database.Store, redirectToLoginOnMe bool) func(http.Han
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
user, ok := extractUserContext(ctx, db, rw, r, redirectToLoginOnMe)
// We need to call as SystemRestricted because this middleware is called from
// organizations/{organization}/members/{user}/ paths, and we need to allow
// org-admins to call these paths --- they might not have sitewide read permissions on users.
// nolint:gocritic
user, ok := extractUserContext(dbauthz.AsSystemRestricted(ctx), db, rw, r, redirectToLoginOnMe)
if !ok {
// response already handled
return
Expand Down Expand Up @@ -77,8 +81,7 @@ func extractUserContext(ctx context.Context, db database.Store, rw http.Response
})
return database.User{}, false
}
//nolint:gocritic // System needs to be able to get user from param.
user, err := db.GetUserByID(dbauthz.AsSystemRestricted(ctx), apiKey.UserID)
user, err := db.GetUserByID(ctx, apiKey.UserID)
if httpapi.Is404Error(err) {
httpapi.ResourceNotFound(rw)
return database.User{}, false
Expand All @@ -94,8 +97,7 @@ func extractUserContext(ctx context.Context, db database.Store, rw http.Response
}

if userID, err := uuid.Parse(userQuery); err == nil {
//nolint:gocritic // If the userQuery is a valid uuid
user, err = db.GetUserByID(dbauthz.AsSystemRestricted(ctx), userID)
user, err = db.GetUserByID(ctx, userID)
if err != nil {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: userErrorMessage,
Expand All @@ -106,8 +108,8 @@ func extractUserContext(ctx context.Context, db database.Store, rw http.Response
return user, true
}

// nolint:gocritic // Try as a username last
user, err := db.GetUserByEmailOrUsername(dbauthz.AsSystemRestricted(ctx), database.GetUserByEmailOrUsernameParams{
// Try as a username last
user, err := db.GetUserByEmailOrUsername(ctx, database.GetUserByEmailOrUsernameParams{
Username: userQuery,
})
if err != nil {
Expand Down