feat: Allow deleting users (#4028)

* Add deleted column to the users table

* Fix user indexes

* Add frontend

* Add test
This commit is contained in:
Kyle Carberry
2022-09-12 23:24:20 +00:00
committed by GitHub
parent a2098254cd
commit 850a83097c
26 changed files with 498 additions and 70 deletions
+67 -31
View File
@@ -1450,7 +1450,7 @@ WHERE
type ParameterValuesParams struct {
Scopes []ParameterScope `db:"scopes" json:"scopes"`
ScopeIds []uuid.UUID `db:"scope_ids" json:"scope_ids"`
Ids []uuid.UUID `db:"ids" json:"ids"`
IDs []uuid.UUID `db:"ids" json:"ids"`
Names []string `db:"names" json:"names"`
}
@@ -1458,7 +1458,7 @@ func (q *sqlQuerier) ParameterValues(ctx context.Context, arg ParameterValuesPar
rows, err := q.db.QueryContext(ctx, parameterValues,
pq.Array(arg.Scopes),
pq.Array(arg.ScopeIds),
pq.Array(arg.Ids),
pq.Array(arg.IDs),
pq.Array(arg.Names),
)
if err != nil {
@@ -2205,7 +2205,7 @@ type GetTemplatesWithFilterParams struct {
Deleted bool `db:"deleted" json:"deleted"`
OrganizationID uuid.UUID `db:"organization_id" json:"organization_id"`
ExactName string `db:"exact_name" json:"exact_name"`
Ids []uuid.UUID `db:"ids" json:"ids"`
IDs []uuid.UUID `db:"ids" json:"ids"`
}
func (q *sqlQuerier) GetTemplatesWithFilter(ctx context.Context, arg GetTemplatesWithFilterParams) ([]Template, error) {
@@ -2213,7 +2213,7 @@ func (q *sqlQuerier) GetTemplatesWithFilter(ctx context.Context, arg GetTemplate
arg.Deleted,
arg.OrganizationID,
arg.ExactName,
pq.Array(arg.Ids),
pq.Array(arg.IDs),
)
if err != nil {
return nil, err
@@ -2884,7 +2884,7 @@ SELECT
FROM
users
WHERE
status = 'active'::public.user_status
status = 'active'::public.user_status AND deleted = false
`
func (q *sqlQuerier) GetActiveUserCount(ctx context.Context) (int64, error) {
@@ -2937,12 +2937,12 @@ func (q *sqlQuerier) GetAuthorizationUserRoles(ctx context.Context, userID uuid.
const getUserByEmailOrUsername = `-- name: GetUserByEmailOrUsername :one
SELECT
id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, login_type, avatar_url
id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, login_type, avatar_url, deleted
FROM
users
WHERE
LOWER(username) = LOWER($1)
OR email = $2
(LOWER(username) = LOWER($1) OR email = $2)
AND deleted = $3
LIMIT
1
`
@@ -2950,10 +2950,11 @@ LIMIT
type GetUserByEmailOrUsernameParams struct {
Username string `db:"username" json:"username"`
Email string `db:"email" json:"email"`
Deleted bool `db:"deleted" json:"deleted"`
}
func (q *sqlQuerier) GetUserByEmailOrUsername(ctx context.Context, arg GetUserByEmailOrUsernameParams) (User, error) {
row := q.db.QueryRowContext(ctx, getUserByEmailOrUsername, arg.Username, arg.Email)
row := q.db.QueryRowContext(ctx, getUserByEmailOrUsername, arg.Username, arg.Email, arg.Deleted)
var i User
err := row.Scan(
&i.ID,
@@ -2966,13 +2967,14 @@ func (q *sqlQuerier) GetUserByEmailOrUsername(ctx context.Context, arg GetUserBy
pq.Array(&i.RBACRoles),
&i.LoginType,
&i.AvatarURL,
&i.Deleted,
)
return i, err
}
const getUserByID = `-- name: GetUserByID :one
SELECT
id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, login_type, avatar_url
id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, login_type, avatar_url, deleted
FROM
users
WHERE
@@ -2995,6 +2997,7 @@ func (q *sqlQuerier) GetUserByID(ctx context.Context, id uuid.UUID) (User, error
pq.Array(&i.RBACRoles),
&i.LoginType,
&i.AvatarURL,
&i.Deleted,
)
return i, err
}
@@ -3003,7 +3006,7 @@ const getUserCount = `-- name: GetUserCount :one
SELECT
COUNT(*)
FROM
users
users WHERE deleted = false
`
func (q *sqlQuerier) GetUserCount(ctx context.Context) (int64, error) {
@@ -3015,15 +3018,16 @@ func (q *sqlQuerier) GetUserCount(ctx context.Context) (int64, error) {
const getUsers = `-- name: GetUsers :many
SELECT
id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, login_type, avatar_url
id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, login_type, avatar_url, deleted
FROM
users
WHERE
CASE
users.deleted = $1
AND CASE
-- This allows using the last element on a page as effectively a cursor.
-- This is an important option for scripts that need to paginate without
-- duplicating or missing data.
WHEN $1 :: uuid != '00000000-00000000-00000000-00000000' THEN (
WHEN $2 :: uuid != '00000000-00000000-00000000-00000000' THEN (
-- The pagination cursor is the last ID of the previous page.
-- The query is ordered by the created_at field, so select all
-- rows after the cursor.
@@ -3033,7 +3037,7 @@ WHERE
FROM
users
WHERE
id = $1
id = $2
)
)
ELSE true
@@ -3041,9 +3045,9 @@ WHERE
-- Start filters
-- Filter by name, email or username
AND CASE
WHEN $2 :: text != '' THEN (
email ILIKE concat('%', $2, '%')
OR username ILIKE concat('%', $2, '%')
WHEN $3 :: text != '' THEN (
email ILIKE concat('%', $3, '%')
OR username ILIKE concat('%', $3, '%')
)
ELSE true
END
@@ -3051,29 +3055,30 @@ WHERE
AND CASE
-- @status needs to be a text because it can be empty, If it was
-- user_status enum, it would not.
WHEN cardinality($3 :: user_status[]) > 0 THEN
status = ANY($3 :: user_status[])
WHEN cardinality($4 :: user_status[]) > 0 THEN
status = ANY($4 :: user_status[])
ELSE true
END
-- Filter by rbac_roles
AND CASE
-- @rbac_role allows filtering by rbac roles. If 'member' is included, show everyone, as
-- everyone is a member.
WHEN cardinality($4 :: text[]) > 0 AND 'member' != ANY($4 :: text[]) THEN
rbac_roles && $4 :: text[]
WHEN cardinality($5 :: text[]) > 0 AND 'member' != ANY($5 :: text[]) THEN
rbac_roles && $5 :: text[]
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 $5
(created_at, id) ASC OFFSET $6
LIMIT
-- A null limit means "no limit", so 0 means return all
NULLIF($6 :: int, 0)
NULLIF($7 :: int, 0)
`
type GetUsersParams struct {
Deleted bool `db:"deleted" json:"deleted"`
AfterID uuid.UUID `db:"after_id" json:"after_id"`
Search string `db:"search" json:"search"`
Status []UserStatus `db:"status" json:"status"`
@@ -3084,6 +3089,7 @@ type GetUsersParams struct {
func (q *sqlQuerier) GetUsers(ctx context.Context, arg GetUsersParams) ([]User, error) {
rows, err := q.db.QueryContext(ctx, getUsers,
arg.Deleted,
arg.AfterID,
arg.Search,
pq.Array(arg.Status),
@@ -3109,6 +3115,7 @@ func (q *sqlQuerier) GetUsers(ctx context.Context, arg GetUsersParams) ([]User,
pq.Array(&i.RBACRoles),
&i.LoginType,
&i.AvatarURL,
&i.Deleted,
); err != nil {
return nil, err
}
@@ -3124,11 +3131,16 @@ func (q *sqlQuerier) GetUsers(ctx context.Context, arg GetUsersParams) ([]User,
}
const getUsersByIDs = `-- name: GetUsersByIDs :many
SELECT id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, login_type, avatar_url FROM users WHERE id = ANY($1 :: uuid [ ])
SELECT id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, login_type, avatar_url, deleted FROM users WHERE id = ANY($1 :: uuid [ ]) AND deleted = $2
`
func (q *sqlQuerier) GetUsersByIDs(ctx context.Context, ids []uuid.UUID) ([]User, error) {
rows, err := q.db.QueryContext(ctx, getUsersByIDs, pq.Array(ids))
type GetUsersByIDsParams struct {
IDs []uuid.UUID `db:"ids" json:"ids"`
Deleted bool `db:"deleted" json:"deleted"`
}
func (q *sqlQuerier) GetUsersByIDs(ctx context.Context, arg GetUsersByIDsParams) ([]User, error) {
rows, err := q.db.QueryContext(ctx, getUsersByIDs, pq.Array(arg.IDs), arg.Deleted)
if err != nil {
return nil, err
}
@@ -3147,6 +3159,7 @@ func (q *sqlQuerier) GetUsersByIDs(ctx context.Context, ids []uuid.UUID) ([]User
pq.Array(&i.RBACRoles),
&i.LoginType,
&i.AvatarURL,
&i.Deleted,
); err != nil {
return nil, err
}
@@ -3174,7 +3187,7 @@ INSERT INTO
login_type
)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8) RETURNING id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, login_type, avatar_url
($1, $2, $3, $4, $5, $6, $7, $8) RETURNING id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, login_type, avatar_url, deleted
`
type InsertUserParams struct {
@@ -3211,10 +3224,30 @@ func (q *sqlQuerier) InsertUser(ctx context.Context, arg InsertUserParams) (User
pq.Array(&i.RBACRoles),
&i.LoginType,
&i.AvatarURL,
&i.Deleted,
)
return i, err
}
const updateUserDeletedByID = `-- name: UpdateUserDeletedByID :exec
UPDATE
users
SET
deleted = $2
WHERE
id = $1
`
type UpdateUserDeletedByIDParams struct {
ID uuid.UUID `db:"id" json:"id"`
Deleted bool `db:"deleted" json:"deleted"`
}
func (q *sqlQuerier) UpdateUserDeletedByID(ctx context.Context, arg UpdateUserDeletedByIDParams) error {
_, err := q.db.ExecContext(ctx, updateUserDeletedByID, arg.ID, arg.Deleted)
return err
}
const updateUserHashedPassword = `-- name: UpdateUserHashedPassword :exec
UPDATE
users
@@ -3243,7 +3276,7 @@ SET
avatar_url = $4,
updated_at = $5
WHERE
id = $1 RETURNING id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, login_type, avatar_url
id = $1 RETURNING id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, login_type, avatar_url, deleted
`
type UpdateUserProfileParams struct {
@@ -3274,6 +3307,7 @@ func (q *sqlQuerier) UpdateUserProfile(ctx context.Context, arg UpdateUserProfil
pq.Array(&i.RBACRoles),
&i.LoginType,
&i.AvatarURL,
&i.Deleted,
)
return i, err
}
@@ -3286,7 +3320,7 @@ SET
rbac_roles = ARRAY(SELECT DISTINCT UNNEST($1 :: text[]))
WHERE
id = $2
RETURNING id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, login_type, avatar_url
RETURNING id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, login_type, avatar_url, deleted
`
type UpdateUserRolesParams struct {
@@ -3308,6 +3342,7 @@ func (q *sqlQuerier) UpdateUserRoles(ctx context.Context, arg UpdateUserRolesPar
pq.Array(&i.RBACRoles),
&i.LoginType,
&i.AvatarURL,
&i.Deleted,
)
return i, err
}
@@ -3319,7 +3354,7 @@ SET
status = $2,
updated_at = $3
WHERE
id = $1 RETURNING id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, login_type, avatar_url
id = $1 RETURNING id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, login_type, avatar_url, deleted
`
type UpdateUserStatusParams struct {
@@ -3342,6 +3377,7 @@ func (q *sqlQuerier) UpdateUserStatus(ctx context.Context, arg UpdateUserStatusP
pq.Array(&i.RBACRoles),
&i.LoginType,
&i.AvatarURL,
&i.Deleted,
)
return i, err
}