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
|
||||
}
|
||||
|
||||
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 int(params.OffsetOpt) > len(users)-1 {
|
||||
return []database.User{}, nil
|
||||
@ -225,6 +235,7 @@ func (q *fakeQuerier) GetUsers(_ context.Context, params database.GetUsersParams
|
||||
}
|
||||
users = users[:params.LimitOpt]
|
||||
}
|
||||
|
||||
tmp := make([]database.User, len(users))
|
||||
copy(tmp, users)
|
||||
|
||||
|
@ -1919,6 +1919,8 @@ WHERE
|
||||
)
|
||||
ELSE true
|
||||
END
|
||||
-- Start filters
|
||||
-- Filter by name, email or username
|
||||
AND CASE
|
||||
WHEN $2 :: text != '' THEN (
|
||||
email LIKE concat('%', $2, '%')
|
||||
@ -1926,18 +1928,29 @@ WHERE
|
||||
)
|
||||
ELSE true
|
||||
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
|
||||
-- Deterministic and consistent ordering of all users, even if they share
|
||||
-- a timestamp. This is to ensure consistent pagination.
|
||||
(created_at, id) ASC OFFSET $3
|
||||
(created_at, id) ASC OFFSET $4
|
||||
LIMIT
|
||||
-- A null limit means "no limit", so -1 means return all
|
||||
NULLIF($4 :: int, -1)
|
||||
NULLIF($5 :: int, -1)
|
||||
`
|
||||
|
||||
type GetUsersParams struct {
|
||||
AfterUser uuid.UUID `db:"after_user" json:"after_user"`
|
||||
Search string `db:"search" json:"search"`
|
||||
Status string `db:"status" json:"status"`
|
||||
OffsetOpt int32 `db:"offset_opt" json:"offset_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,
|
||||
arg.AfterUser,
|
||||
arg.Search,
|
||||
arg.Status,
|
||||
arg.OffsetOpt,
|
||||
arg.LimitOpt,
|
||||
)
|
||||
|
@ -76,6 +76,8 @@ WHERE
|
||||
)
|
||||
ELSE true
|
||||
END
|
||||
-- Start filters
|
||||
-- Filter by name, email or username
|
||||
AND CASE
|
||||
WHEN @search :: text != '' THEN (
|
||||
email LIKE concat('%', @search, '%')
|
||||
@ -83,6 +85,16 @@ WHERE
|
||||
)
|
||||
ELSE true
|
||||
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
|
||||
-- Deterministic and consistent ordering of all users, even if they share
|
||||
-- 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")
|
||||
offsetArg = r.URL.Query().Get("offset")
|
||||
searchName = r.URL.Query().Get("search")
|
||||
statusFilter = r.URL.Query().Get("status")
|
||||
)
|
||||
|
||||
// createdAfter is a user uuid.
|
||||
@ -136,6 +137,7 @@ func (api *api) users(rw http.ResponseWriter, r *http.Request) {
|
||||
OffsetOpt: int32(offset),
|
||||
LimitOpt: int32(pageLimit),
|
||||
Search: searchName,
|
||||
Status: statusFilter,
|
||||
})
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
|
@ -329,6 +329,8 @@ func TestUserByName(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetUsers(t *testing.T) {
|
||||
t.Parallel()
|
||||
t.Run("AllUsers", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
client := coderdtest.New(t, nil)
|
||||
user := coderdtest.CreateFirstUser(t, client)
|
||||
@ -343,6 +345,39 @@ func TestGetUsers(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.Len(t, users, 2)
|
||||
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) {
|
||||
|
@ -14,6 +14,13 @@ import (
|
||||
// Me is used as a replacement for your own ID.
|
||||
var Me = uuid.Nil
|
||||
|
||||
type UserStatus string
|
||||
|
||||
const (
|
||||
UserStatusActive UserStatus = "active"
|
||||
UserStatusSuspended UserStatus = "suspended"
|
||||
)
|
||||
|
||||
type UsersRequest struct {
|
||||
AfterUser uuid.UUID `json:"after_user"`
|
||||
Search string `json:"search"`
|
||||
@ -26,15 +33,10 @@ type UsersRequest struct {
|
||||
// To get the next page, use offset=<limit>*<page_number>.
|
||||
// Offset is 0 indexed, so the first record sits at offset 0.
|
||||
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.
|
||||
type User struct {
|
||||
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 {
|
||||
return User{}, readBodyAsError(res)
|
||||
}
|
||||
|
||||
var user 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("search", req.Search)
|
||||
q.Set("status", req.Status)
|
||||
r.URL.RawQuery = q.Encode()
|
||||
})
|
||||
if err != nil {
|
||||
|
@ -12,7 +12,7 @@ export interface AgentGitSSHKey {
|
||||
readonly private_key: string
|
||||
}
|
||||
|
||||
// From codersdk/users.go:94:6
|
||||
// From codersdk/users.go:96:6
|
||||
export interface AuthMethods {
|
||||
readonly password: boolean
|
||||
readonly github: boolean
|
||||
@ -30,7 +30,7 @@ export interface BuildInfoResponse {
|
||||
readonly version: string
|
||||
}
|
||||
|
||||
// From codersdk/users.go:48:6
|
||||
// From codersdk/users.go:50:6
|
||||
export interface CreateFirstUserRequest {
|
||||
readonly email: string
|
||||
readonly username: string
|
||||
@ -38,13 +38,13 @@ export interface CreateFirstUserRequest {
|
||||
readonly organization: string
|
||||
}
|
||||
|
||||
// From codersdk/users.go:56:6
|
||||
// From codersdk/users.go:58:6
|
||||
export interface CreateFirstUserResponse {
|
||||
readonly user_id: string
|
||||
readonly organization_id: string
|
||||
}
|
||||
|
||||
// From codersdk/users.go:89:6
|
||||
// From codersdk/users.go:91:6
|
||||
export interface CreateOrganizationRequest {
|
||||
readonly name: string
|
||||
}
|
||||
@ -77,7 +77,7 @@ export interface CreateTemplateVersionRequest {
|
||||
readonly parameter_values: CreateParameterRequest[]
|
||||
}
|
||||
|
||||
// From codersdk/users.go:61:6
|
||||
// From codersdk/users.go:63:6
|
||||
export interface CreateUserRequest {
|
||||
readonly email: string
|
||||
readonly username: string
|
||||
@ -100,7 +100,7 @@ export interface CreateWorkspaceRequest {
|
||||
readonly parameter_values: CreateParameterRequest[]
|
||||
}
|
||||
|
||||
// From codersdk/users.go:85:6
|
||||
// From codersdk/users.go:87:6
|
||||
export interface GenerateAPIKeyResponse {
|
||||
readonly key: string
|
||||
}
|
||||
@ -118,13 +118,13 @@ export interface GoogleInstanceIdentityToken {
|
||||
readonly json_web_token: string
|
||||
}
|
||||
|
||||
// From codersdk/users.go:74:6
|
||||
// From codersdk/users.go:76:6
|
||||
export interface LoginWithPasswordRequest {
|
||||
readonly email: string
|
||||
readonly password: string
|
||||
}
|
||||
|
||||
// From codersdk/users.go:80:6
|
||||
// From codersdk/users.go:82:6
|
||||
export interface LoginWithPasswordResponse {
|
||||
readonly session_token: string
|
||||
}
|
||||
@ -245,7 +245,7 @@ export interface UpdateActiveTemplateVersion {
|
||||
readonly id: string
|
||||
}
|
||||
|
||||
// From codersdk/users.go:68:6
|
||||
// From codersdk/users.go:70:6
|
||||
export interface UpdateUserProfileRequest {
|
||||
readonly email: string
|
||||
readonly username: string
|
||||
@ -266,7 +266,7 @@ export interface UploadResponse {
|
||||
readonly hash: string
|
||||
}
|
||||
|
||||
// From codersdk/users.go:39:6
|
||||
// From codersdk/users.go:41:6
|
||||
export interface User {
|
||||
readonly id: string
|
||||
readonly email: string
|
||||
@ -276,12 +276,13 @@ export interface User {
|
||||
readonly organization_ids: string[]
|
||||
}
|
||||
|
||||
// From codersdk/users.go:17:6
|
||||
// From codersdk/users.go:24:6
|
||||
export interface UsersRequest {
|
||||
readonly after_user: string
|
||||
readonly search: string
|
||||
readonly limit: number
|
||||
readonly offset: number
|
||||
readonly status: string
|
||||
}
|
||||
|
||||
// From codersdk/workspaces.go:18:6
|
||||
@ -378,7 +379,7 @@ export type ParameterScope = "organization" | "template" | "user" | "workspace"
|
||||
// From codersdk/provisionerdaemons.go:26:6
|
||||
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"
|
||||
|
||||
// From codersdk/workspaceresources.go:15:6
|
||||
|
Reference in New Issue
Block a user