2022-04-01 15:45:23 -05:00
|
|
|
-- name: GetUserByID :one
|
|
|
|
|
SELECT
|
|
|
|
|
*
|
|
|
|
|
FROM
|
|
|
|
|
users
|
|
|
|
|
WHERE
|
|
|
|
|
id = $1
|
|
|
|
|
LIMIT
|
|
|
|
|
1;
|
|
|
|
|
|
2022-05-13 20:41:21 -05:00
|
|
|
-- name: GetUsersByIDs :many
|
2022-10-10 13:03:54 -05:00
|
|
|
-- This shouldn't check for deleted, because it's frequently used
|
|
|
|
|
-- to look up references to actions. eg. a user could build a workspace
|
|
|
|
|
-- for another user, then be deleted... we still want them to appear!
|
|
|
|
|
SELECT * FROM users WHERE id = ANY(@ids :: uuid [ ]);
|
2022-05-13 20:41:21 -05:00
|
|
|
|
2022-04-01 15:45:23 -05:00
|
|
|
-- name: GetUserByEmailOrUsername :one
|
|
|
|
|
SELECT
|
|
|
|
|
*
|
|
|
|
|
FROM
|
|
|
|
|
users
|
|
|
|
|
WHERE
|
2022-09-26 22:51:58 -05:00
|
|
|
(LOWER(username) = LOWER(@username) OR LOWER(email) = LOWER(@email))
|
2022-09-12 18:24:20 -05:00
|
|
|
AND deleted = @deleted
|
2022-04-01 15:45:23 -05:00
|
|
|
LIMIT
|
|
|
|
|
1;
|
|
|
|
|
|
|
|
|
|
-- name: GetUserCount :one
|
|
|
|
|
SELECT
|
|
|
|
|
COUNT(*)
|
|
|
|
|
FROM
|
2022-09-12 18:24:20 -05:00
|
|
|
users WHERE deleted = false;
|
2022-04-01 15:45:23 -05:00
|
|
|
|
2022-08-29 16:45:40 -07:00
|
|
|
-- name: GetActiveUserCount :one
|
|
|
|
|
SELECT
|
|
|
|
|
COUNT(*)
|
|
|
|
|
FROM
|
|
|
|
|
users
|
|
|
|
|
WHERE
|
2022-09-12 18:24:20 -05:00
|
|
|
status = 'active'::public.user_status AND deleted = false;
|
2022-08-29 16:45:40 -07:00
|
|
|
|
2022-04-01 15:45:23 -05:00
|
|
|
-- name: InsertUser :one
|
|
|
|
|
INSERT INTO
|
|
|
|
|
users (
|
|
|
|
|
id,
|
|
|
|
|
email,
|
2022-04-24 20:08:26 -05:00
|
|
|
username,
|
2022-04-01 15:45:23 -05:00
|
|
|
hashed_password,
|
|
|
|
|
created_at,
|
2022-04-29 09:04:19 -05:00
|
|
|
updated_at,
|
2022-08-17 18:00:53 -05:00
|
|
|
rbac_roles,
|
|
|
|
|
login_type
|
2022-04-01 15:45:23 -05:00
|
|
|
)
|
|
|
|
|
VALUES
|
2022-08-17 18:00:53 -05:00
|
|
|
($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *;
|
2022-04-12 11:05:21 -03:00
|
|
|
|
|
|
|
|
-- name: UpdateUserProfile :one
|
|
|
|
|
UPDATE
|
|
|
|
|
users
|
|
|
|
|
SET
|
|
|
|
|
email = $2,
|
2022-04-24 20:08:26 -05:00
|
|
|
username = $3,
|
2022-09-04 11:44:27 -05:00
|
|
|
avatar_url = $4,
|
|
|
|
|
updated_at = $5
|
2022-04-12 11:05:21 -03:00
|
|
|
WHERE
|
|
|
|
|
id = $1 RETURNING *;
|
2022-04-18 12:19:47 -05:00
|
|
|
|
2022-04-29 09:04:19 -05:00
|
|
|
-- name: UpdateUserRoles :one
|
|
|
|
|
UPDATE
|
2022-08-17 18:00:53 -05:00
|
|
|
users
|
2022-04-29 09:04:19 -05:00
|
|
|
SET
|
|
|
|
|
-- Remove all duplicates from the roles.
|
|
|
|
|
rbac_roles = ARRAY(SELECT DISTINCT UNNEST(@granted_roles :: text[]))
|
|
|
|
|
WHERE
|
2022-08-17 18:00:53 -05:00
|
|
|
id = @id
|
2022-04-29 09:04:19 -05:00
|
|
|
RETURNING *;
|
|
|
|
|
|
2022-05-06 09:20:08 -05:00
|
|
|
-- name: UpdateUserHashedPassword :exec
|
|
|
|
|
UPDATE
|
|
|
|
|
users
|
|
|
|
|
SET
|
|
|
|
|
hashed_password = $2
|
|
|
|
|
WHERE
|
|
|
|
|
id = $1;
|
|
|
|
|
|
2022-09-12 18:24:20 -05:00
|
|
|
-- name: UpdateUserDeletedByID :exec
|
|
|
|
|
UPDATE
|
|
|
|
|
users
|
|
|
|
|
SET
|
|
|
|
|
deleted = $2
|
|
|
|
|
WHERE
|
|
|
|
|
id = $1;
|
|
|
|
|
|
2022-04-18 12:19:47 -05:00
|
|
|
-- name: GetUsers :many
|
|
|
|
|
SELECT
|
|
|
|
|
*
|
|
|
|
|
FROM
|
2022-04-22 15:27:55 -05:00
|
|
|
users
|
|
|
|
|
WHERE
|
2022-09-12 18:24:20 -05:00
|
|
|
users.deleted = @deleted
|
|
|
|
|
AND CASE
|
2022-04-22 15:27:55 -05:00
|
|
|
-- 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.
|
2022-05-10 10:44:09 +03:00
|
|
|
WHEN @after_id :: 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.
|
|
|
|
|
(created_at, id) > (
|
|
|
|
|
SELECT
|
|
|
|
|
created_at, id
|
|
|
|
|
FROM
|
|
|
|
|
users
|
|
|
|
|
WHERE
|
|
|
|
|
id = @after_id
|
2022-04-22 15:27:55 -05:00
|
|
|
)
|
2022-05-10 10:44:09 +03:00
|
|
|
)
|
|
|
|
|
ELSE true
|
2022-04-22 15:27:55 -05:00
|
|
|
END
|
2022-04-29 08:29:53 -05:00
|
|
|
-- Start filters
|
|
|
|
|
-- Filter by name, email or username
|
2022-04-22 15:27:55 -05:00
|
|
|
AND CASE
|
|
|
|
|
WHEN @search :: text != '' THEN (
|
2022-06-24 19:55:28 -04:00
|
|
|
email ILIKE concat('%', @search, '%')
|
|
|
|
|
OR username ILIKE concat('%', @search, '%')
|
2022-05-31 08:06:42 -05:00
|
|
|
)
|
2022-04-29 08:29:53 -05:00
|
|
|
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.
|
2022-06-24 10:02:23 -05:00
|
|
|
WHEN cardinality(@status :: user_status[]) > 0 THEN
|
2022-05-31 08:06:42 -05:00
|
|
|
status = ANY(@status :: user_status[])
|
2022-06-24 10:02:23 -05:00
|
|
|
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(@rbac_role :: text[]) > 0 AND 'member' != ANY(@rbac_role :: text[]) THEN
|
|
|
|
|
rbac_roles && @rbac_role :: text[]
|
|
|
|
|
ELSE true
|
2022-04-22 15:27:55 -05:00
|
|
|
END
|
2022-04-29 08:29:53 -05:00
|
|
|
-- End of filters
|
2022-04-22 15:27:55 -05:00
|
|
|
ORDER BY
|
2022-08-17 18:00:53 -05:00
|
|
|
-- Deterministic and consistent ordering of all users, even if they share
|
|
|
|
|
-- a timestamp. This is to ensure consistent pagination.
|
2022-04-22 15:27:55 -05:00
|
|
|
(created_at, id) ASC OFFSET @offset_opt
|
|
|
|
|
LIMIT
|
2022-06-26 15:23:25 -05:00
|
|
|
-- A null limit means "no limit", so 0 means return all
|
|
|
|
|
NULLIF(@limit_opt :: int, 0);
|
2022-04-26 09:00:07 -05:00
|
|
|
|
|
|
|
|
-- name: UpdateUserStatus :one
|
|
|
|
|
UPDATE
|
|
|
|
|
users
|
|
|
|
|
SET
|
|
|
|
|
status = $2,
|
|
|
|
|
updated_at = $3
|
|
|
|
|
WHERE
|
|
|
|
|
id = $1 RETURNING *;
|
2022-05-03 16:10:19 -05:00
|
|
|
|
2022-09-26 10:31:03 -05:00
|
|
|
-- name: UpdateUserLastSeenAt :one
|
|
|
|
|
UPDATE
|
|
|
|
|
users
|
|
|
|
|
SET
|
|
|
|
|
last_seen_at = $2,
|
|
|
|
|
updated_at = $3
|
|
|
|
|
WHERE
|
|
|
|
|
id = $1 RETURNING *;
|
|
|
|
|
|
2022-05-03 16:10:19 -05:00
|
|
|
|
2022-06-01 09:07:50 -05:00
|
|
|
-- name: GetAuthorizationUserRoles :one
|
|
|
|
|
-- This function returns roles for authorization purposes. Implied member roles
|
|
|
|
|
-- are included.
|
2022-05-03 16:10:19 -05:00
|
|
|
SELECT
|
2022-06-01 09:07:50 -05:00
|
|
|
-- username is returned just to help for logging purposes
|
|
|
|
|
-- status is used to enforce 'suspended' users, as all roles are ignored
|
|
|
|
|
-- when suspended.
|
|
|
|
|
id, username, status,
|
2022-10-10 15:37:06 -05:00
|
|
|
-- All user roles, including their org roles.
|
2022-06-01 09:07:50 -05:00
|
|
|
array_cat(
|
|
|
|
|
-- All users are members
|
2022-10-10 15:37:06 -05:00
|
|
|
array_append(users.rbac_roles, 'member'),
|
|
|
|
|
(
|
|
|
|
|
SELECT
|
|
|
|
|
array_agg(org_roles)
|
|
|
|
|
FROM
|
|
|
|
|
organization_members,
|
|
|
|
|
-- All org_members get the org-member role for their orgs
|
|
|
|
|
unnest(
|
|
|
|
|
array_append(roles, 'organization-member:' || organization_members.organization_id::text)
|
|
|
|
|
) AS org_roles
|
|
|
|
|
WHERE
|
|
|
|
|
user_id = users.id
|
|
|
|
|
)
|
|
|
|
|
) :: text[] AS roles,
|
|
|
|
|
-- All groups the user is in.
|
|
|
|
|
(
|
|
|
|
|
SELECT
|
|
|
|
|
array_agg(
|
|
|
|
|
group_members.group_id :: text
|
|
|
|
|
)
|
|
|
|
|
FROM
|
|
|
|
|
group_members
|
|
|
|
|
WHERE
|
|
|
|
|
user_id = users.id
|
|
|
|
|
) :: text[] AS groups
|
2022-05-03 16:10:19 -05:00
|
|
|
FROM
|
|
|
|
|
users
|
|
|
|
|
WHERE
|
2022-08-17 18:00:53 -05:00
|
|
|
id = @user_id;
|