mirror of
https://github.com/coder/coder.git
synced 2025-07-18 14:17:22 +00:00
feat: add filter by status on GET /users (#1206)
This commit is contained in:
@ -212,6 +212,16 @@ func (q *fakeQuerier) GetUsers(_ context.Context, params database.GetUsersParams
|
|||||||
users = tmp
|
users = tmp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if params.Status != "" {
|
||||||
|
usersFilteredByStatus := make([]database.User, 0, len(users))
|
||||||
|
for i, user := range users {
|
||||||
|
if params.Status == string(user.Status) {
|
||||||
|
usersFilteredByStatus = append(usersFilteredByStatus, users[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
users = usersFilteredByStatus
|
||||||
|
}
|
||||||
|
|
||||||
if params.OffsetOpt > 0 {
|
if params.OffsetOpt > 0 {
|
||||||
if int(params.OffsetOpt) > len(users)-1 {
|
if int(params.OffsetOpt) > len(users)-1 {
|
||||||
return []database.User{}, nil
|
return []database.User{}, nil
|
||||||
@ -225,6 +235,7 @@ func (q *fakeQuerier) GetUsers(_ context.Context, params database.GetUsersParams
|
|||||||
}
|
}
|
||||||
users = users[:params.LimitOpt]
|
users = users[:params.LimitOpt]
|
||||||
}
|
}
|
||||||
|
|
||||||
tmp := make([]database.User, len(users))
|
tmp := make([]database.User, len(users))
|
||||||
copy(tmp, users)
|
copy(tmp, users)
|
||||||
|
|
||||||
|
@ -1919,6 +1919,8 @@ WHERE
|
|||||||
)
|
)
|
||||||
ELSE true
|
ELSE true
|
||||||
END
|
END
|
||||||
|
-- Start filters
|
||||||
|
-- Filter by name, email or username
|
||||||
AND CASE
|
AND CASE
|
||||||
WHEN $2 :: text != '' THEN (
|
WHEN $2 :: text != '' THEN (
|
||||||
email LIKE concat('%', $2, '%')
|
email LIKE concat('%', $2, '%')
|
||||||
@ -1926,18 +1928,29 @@ WHERE
|
|||||||
)
|
)
|
||||||
ELSE true
|
ELSE true
|
||||||
END
|
END
|
||||||
|
-- Filter by status
|
||||||
|
AND CASE
|
||||||
|
-- @status needs to be a text because it can be empty, If it was
|
||||||
|
-- user_status enum, it would not.
|
||||||
|
WHEN $3 :: text != '' THEN (
|
||||||
|
status = $3 :: user_status
|
||||||
|
)
|
||||||
|
ELSE true
|
||||||
|
END
|
||||||
|
-- End of filters
|
||||||
ORDER BY
|
ORDER BY
|
||||||
-- Deterministic and consistent ordering of all users, even if they share
|
-- Deterministic and consistent ordering of all users, even if they share
|
||||||
-- a timestamp. This is to ensure consistent pagination.
|
-- a timestamp. This is to ensure consistent pagination.
|
||||||
(created_at, id) ASC OFFSET $3
|
(created_at, id) ASC OFFSET $4
|
||||||
LIMIT
|
LIMIT
|
||||||
-- A null limit means "no limit", so -1 means return all
|
-- A null limit means "no limit", so -1 means return all
|
||||||
NULLIF($4 :: int, -1)
|
NULLIF($5 :: int, -1)
|
||||||
`
|
`
|
||||||
|
|
||||||
type GetUsersParams struct {
|
type GetUsersParams struct {
|
||||||
AfterUser uuid.UUID `db:"after_user" json:"after_user"`
|
AfterUser uuid.UUID `db:"after_user" json:"after_user"`
|
||||||
Search string `db:"search" json:"search"`
|
Search string `db:"search" json:"search"`
|
||||||
|
Status string `db:"status" json:"status"`
|
||||||
OffsetOpt int32 `db:"offset_opt" json:"offset_opt"`
|
OffsetOpt int32 `db:"offset_opt" json:"offset_opt"`
|
||||||
LimitOpt int32 `db:"limit_opt" json:"limit_opt"`
|
LimitOpt int32 `db:"limit_opt" json:"limit_opt"`
|
||||||
}
|
}
|
||||||
@ -1946,6 +1959,7 @@ func (q *sqlQuerier) GetUsers(ctx context.Context, arg GetUsersParams) ([]User,
|
|||||||
rows, err := q.db.QueryContext(ctx, getUsers,
|
rows, err := q.db.QueryContext(ctx, getUsers,
|
||||||
arg.AfterUser,
|
arg.AfterUser,
|
||||||
arg.Search,
|
arg.Search,
|
||||||
|
arg.Status,
|
||||||
arg.OffsetOpt,
|
arg.OffsetOpt,
|
||||||
arg.LimitOpt,
|
arg.LimitOpt,
|
||||||
)
|
)
|
||||||
|
@ -76,6 +76,8 @@ WHERE
|
|||||||
)
|
)
|
||||||
ELSE true
|
ELSE true
|
||||||
END
|
END
|
||||||
|
-- Start filters
|
||||||
|
-- Filter by name, email or username
|
||||||
AND CASE
|
AND CASE
|
||||||
WHEN @search :: text != '' THEN (
|
WHEN @search :: text != '' THEN (
|
||||||
email LIKE concat('%', @search, '%')
|
email LIKE concat('%', @search, '%')
|
||||||
@ -83,6 +85,16 @@ WHERE
|
|||||||
)
|
)
|
||||||
ELSE true
|
ELSE true
|
||||||
END
|
END
|
||||||
|
-- Filter by status
|
||||||
|
AND CASE
|
||||||
|
-- @status needs to be a text because it can be empty, If it was
|
||||||
|
-- user_status enum, it would not.
|
||||||
|
WHEN @status :: text != '' THEN (
|
||||||
|
status = @status :: user_status
|
||||||
|
)
|
||||||
|
ELSE true
|
||||||
|
END
|
||||||
|
-- End of filters
|
||||||
ORDER BY
|
ORDER BY
|
||||||
-- Deterministic and consistent ordering of all users, even if they share
|
-- Deterministic and consistent ordering of all users, even if they share
|
||||||
-- a timestamp. This is to ensure consistent pagination.
|
-- a timestamp. This is to ensure consistent pagination.
|
||||||
|
@ -94,6 +94,7 @@ func (api *api) users(rw http.ResponseWriter, r *http.Request) {
|
|||||||
limitArg = r.URL.Query().Get("limit")
|
limitArg = r.URL.Query().Get("limit")
|
||||||
offsetArg = r.URL.Query().Get("offset")
|
offsetArg = r.URL.Query().Get("offset")
|
||||||
searchName = r.URL.Query().Get("search")
|
searchName = r.URL.Query().Get("search")
|
||||||
|
statusFilter = r.URL.Query().Get("status")
|
||||||
)
|
)
|
||||||
|
|
||||||
// createdAfter is a user uuid.
|
// createdAfter is a user uuid.
|
||||||
@ -136,6 +137,7 @@ func (api *api) users(rw http.ResponseWriter, r *http.Request) {
|
|||||||
OffsetOpt: int32(offset),
|
OffsetOpt: int32(offset),
|
||||||
LimitOpt: int32(pageLimit),
|
LimitOpt: int32(pageLimit),
|
||||||
Search: searchName,
|
Search: searchName,
|
||||||
|
Status: statusFilter,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||||
|
@ -329,6 +329,8 @@ func TestUserByName(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetUsers(t *testing.T) {
|
func TestGetUsers(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
t.Run("AllUsers", func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
client := coderdtest.New(t, nil)
|
client := coderdtest.New(t, nil)
|
||||||
user := coderdtest.CreateFirstUser(t, client)
|
user := coderdtest.CreateFirstUser(t, client)
|
||||||
@ -343,6 +345,39 @@ func TestGetUsers(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Len(t, users, 2)
|
require.Len(t, users, 2)
|
||||||
require.Len(t, users[0].OrganizationIDs, 1)
|
require.Len(t, users[0].OrganizationIDs, 1)
|
||||||
|
})
|
||||||
|
t.Run("ActiveUsers", func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
client := coderdtest.New(t, nil)
|
||||||
|
first := coderdtest.CreateFirstUser(t, client)
|
||||||
|
active := make([]codersdk.User, 0)
|
||||||
|
alice, err := client.CreateUser(context.Background(), codersdk.CreateUserRequest{
|
||||||
|
Email: "alice@email.com",
|
||||||
|
Username: "alice",
|
||||||
|
Password: "password",
|
||||||
|
OrganizationID: first.OrganizationID,
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
active = append(active, alice)
|
||||||
|
|
||||||
|
bruno, err := client.CreateUser(context.Background(), codersdk.CreateUserRequest{
|
||||||
|
Email: "bruno@email.com",
|
||||||
|
Username: "bruno",
|
||||||
|
Password: "password",
|
||||||
|
OrganizationID: first.OrganizationID,
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
active = append(active, bruno)
|
||||||
|
|
||||||
|
_, err = client.SuspendUser(context.Background(), first.UserID)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
users, err := client.Users(context.Background(), codersdk.UsersRequest{
|
||||||
|
Status: string(codersdk.UserStatusActive),
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.ElementsMatch(t, active, users)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOrganizationsByUser(t *testing.T) {
|
func TestOrganizationsByUser(t *testing.T) {
|
||||||
|
@ -14,6 +14,13 @@ import (
|
|||||||
// Me is used as a replacement for your own ID.
|
// Me is used as a replacement for your own ID.
|
||||||
var Me = uuid.Nil
|
var Me = uuid.Nil
|
||||||
|
|
||||||
|
type UserStatus string
|
||||||
|
|
||||||
|
const (
|
||||||
|
UserStatusActive UserStatus = "active"
|
||||||
|
UserStatusSuspended UserStatus = "suspended"
|
||||||
|
)
|
||||||
|
|
||||||
type UsersRequest struct {
|
type UsersRequest struct {
|
||||||
AfterUser uuid.UUID `json:"after_user"`
|
AfterUser uuid.UUID `json:"after_user"`
|
||||||
Search string `json:"search"`
|
Search string `json:"search"`
|
||||||
@ -26,15 +33,10 @@ type UsersRequest struct {
|
|||||||
// To get the next page, use offset=<limit>*<page_number>.
|
// To get the next page, use offset=<limit>*<page_number>.
|
||||||
// Offset is 0 indexed, so the first record sits at offset 0.
|
// Offset is 0 indexed, so the first record sits at offset 0.
|
||||||
Offset int `json:"offset"`
|
Offset int `json:"offset"`
|
||||||
|
// Filter users by status
|
||||||
|
Status string `json:"status"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserStatus string
|
|
||||||
|
|
||||||
const (
|
|
||||||
UserStatusActive UserStatus = "active"
|
|
||||||
UserStatusSuspended UserStatus = "suspended"
|
|
||||||
)
|
|
||||||
|
|
||||||
// User represents a user in Coder.
|
// User represents a user in Coder.
|
||||||
type User struct {
|
type User struct {
|
||||||
ID uuid.UUID `json:"id" validate:"required"`
|
ID uuid.UUID `json:"id" validate:"required"`
|
||||||
@ -165,6 +167,7 @@ func (c *Client) SuspendUser(ctx context.Context, userID uuid.UUID) (User, error
|
|||||||
if res.StatusCode != http.StatusOK {
|
if res.StatusCode != http.StatusOK {
|
||||||
return User{}, readBodyAsError(res)
|
return User{}, readBodyAsError(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
var user User
|
var user User
|
||||||
return user, json.NewDecoder(res.Body).Decode(&user)
|
return user, json.NewDecoder(res.Body).Decode(&user)
|
||||||
}
|
}
|
||||||
@ -243,6 +246,7 @@ func (c *Client) Users(ctx context.Context, req UsersRequest) ([]User, error) {
|
|||||||
}
|
}
|
||||||
q.Set("offset", strconv.Itoa(req.Offset))
|
q.Set("offset", strconv.Itoa(req.Offset))
|
||||||
q.Set("search", req.Search)
|
q.Set("search", req.Search)
|
||||||
|
q.Set("status", req.Status)
|
||||||
r.URL.RawQuery = q.Encode()
|
r.URL.RawQuery = q.Encode()
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -12,7 +12,7 @@ export interface AgentGitSSHKey {
|
|||||||
readonly private_key: string
|
readonly private_key: string
|
||||||
}
|
}
|
||||||
|
|
||||||
// From codersdk/users.go:94:6
|
// From codersdk/users.go:96:6
|
||||||
export interface AuthMethods {
|
export interface AuthMethods {
|
||||||
readonly password: boolean
|
readonly password: boolean
|
||||||
readonly github: boolean
|
readonly github: boolean
|
||||||
@ -30,7 +30,7 @@ export interface BuildInfoResponse {
|
|||||||
readonly version: string
|
readonly version: string
|
||||||
}
|
}
|
||||||
|
|
||||||
// From codersdk/users.go:48:6
|
// From codersdk/users.go:50:6
|
||||||
export interface CreateFirstUserRequest {
|
export interface CreateFirstUserRequest {
|
||||||
readonly email: string
|
readonly email: string
|
||||||
readonly username: string
|
readonly username: string
|
||||||
@ -38,13 +38,13 @@ export interface CreateFirstUserRequest {
|
|||||||
readonly organization: string
|
readonly organization: string
|
||||||
}
|
}
|
||||||
|
|
||||||
// From codersdk/users.go:56:6
|
// From codersdk/users.go:58:6
|
||||||
export interface CreateFirstUserResponse {
|
export interface CreateFirstUserResponse {
|
||||||
readonly user_id: string
|
readonly user_id: string
|
||||||
readonly organization_id: string
|
readonly organization_id: string
|
||||||
}
|
}
|
||||||
|
|
||||||
// From codersdk/users.go:89:6
|
// From codersdk/users.go:91:6
|
||||||
export interface CreateOrganizationRequest {
|
export interface CreateOrganizationRequest {
|
||||||
readonly name: string
|
readonly name: string
|
||||||
}
|
}
|
||||||
@ -77,7 +77,7 @@ export interface CreateTemplateVersionRequest {
|
|||||||
readonly parameter_values: CreateParameterRequest[]
|
readonly parameter_values: CreateParameterRequest[]
|
||||||
}
|
}
|
||||||
|
|
||||||
// From codersdk/users.go:61:6
|
// From codersdk/users.go:63:6
|
||||||
export interface CreateUserRequest {
|
export interface CreateUserRequest {
|
||||||
readonly email: string
|
readonly email: string
|
||||||
readonly username: string
|
readonly username: string
|
||||||
@ -100,7 +100,7 @@ export interface CreateWorkspaceRequest {
|
|||||||
readonly parameter_values: CreateParameterRequest[]
|
readonly parameter_values: CreateParameterRequest[]
|
||||||
}
|
}
|
||||||
|
|
||||||
// From codersdk/users.go:85:6
|
// From codersdk/users.go:87:6
|
||||||
export interface GenerateAPIKeyResponse {
|
export interface GenerateAPIKeyResponse {
|
||||||
readonly key: string
|
readonly key: string
|
||||||
}
|
}
|
||||||
@ -118,13 +118,13 @@ export interface GoogleInstanceIdentityToken {
|
|||||||
readonly json_web_token: string
|
readonly json_web_token: string
|
||||||
}
|
}
|
||||||
|
|
||||||
// From codersdk/users.go:74:6
|
// From codersdk/users.go:76:6
|
||||||
export interface LoginWithPasswordRequest {
|
export interface LoginWithPasswordRequest {
|
||||||
readonly email: string
|
readonly email: string
|
||||||
readonly password: string
|
readonly password: string
|
||||||
}
|
}
|
||||||
|
|
||||||
// From codersdk/users.go:80:6
|
// From codersdk/users.go:82:6
|
||||||
export interface LoginWithPasswordResponse {
|
export interface LoginWithPasswordResponse {
|
||||||
readonly session_token: string
|
readonly session_token: string
|
||||||
}
|
}
|
||||||
@ -245,7 +245,7 @@ export interface UpdateActiveTemplateVersion {
|
|||||||
readonly id: string
|
readonly id: string
|
||||||
}
|
}
|
||||||
|
|
||||||
// From codersdk/users.go:68:6
|
// From codersdk/users.go:70:6
|
||||||
export interface UpdateUserProfileRequest {
|
export interface UpdateUserProfileRequest {
|
||||||
readonly email: string
|
readonly email: string
|
||||||
readonly username: string
|
readonly username: string
|
||||||
@ -266,7 +266,7 @@ export interface UploadResponse {
|
|||||||
readonly hash: string
|
readonly hash: string
|
||||||
}
|
}
|
||||||
|
|
||||||
// From codersdk/users.go:39:6
|
// From codersdk/users.go:41:6
|
||||||
export interface User {
|
export interface User {
|
||||||
readonly id: string
|
readonly id: string
|
||||||
readonly email: string
|
readonly email: string
|
||||||
@ -276,12 +276,13 @@ export interface User {
|
|||||||
readonly organization_ids: string[]
|
readonly organization_ids: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
// From codersdk/users.go:17:6
|
// From codersdk/users.go:24:6
|
||||||
export interface UsersRequest {
|
export interface UsersRequest {
|
||||||
readonly after_user: string
|
readonly after_user: string
|
||||||
readonly search: string
|
readonly search: string
|
||||||
readonly limit: number
|
readonly limit: number
|
||||||
readonly offset: number
|
readonly offset: number
|
||||||
|
readonly status: string
|
||||||
}
|
}
|
||||||
|
|
||||||
// From codersdk/workspaces.go:18:6
|
// From codersdk/workspaces.go:18:6
|
||||||
@ -378,7 +379,7 @@ export type ParameterScope = "organization" | "template" | "user" | "workspace"
|
|||||||
// From codersdk/provisionerdaemons.go:26:6
|
// From codersdk/provisionerdaemons.go:26:6
|
||||||
export type ProvisionerJobStatus = "canceled" | "canceling" | "failed" | "pending" | "running" | "succeeded"
|
export type ProvisionerJobStatus = "canceled" | "canceling" | "failed" | "pending" | "running" | "succeeded"
|
||||||
|
|
||||||
// From codersdk/users.go:31:6
|
// From codersdk/users.go:17:6
|
||||||
export type UserStatus = "active" | "suspended"
|
export type UserStatus = "active" | "suspended"
|
||||||
|
|
||||||
// From codersdk/workspaceresources.go:15:6
|
// From codersdk/workspaceresources.go:15:6
|
||||||
|
Reference in New Issue
Block a user