fix: scope combined chat source filters (#26137)

This commit is contained in:
Danielle Maywood
2026-06-08 15:05:27 +01:00
committed by GitHub
parent 9db70b6ec8
commit d751b46a19
17 changed files with 167 additions and 76 deletions
+1 -4
View File
@@ -750,9 +750,6 @@ type chatQuerier interface {
}
func (q *sqlQuerier) GetAuthorizedChats(ctx context.Context, arg GetChatsParams, prepared rbac.PreparedAuthorized) ([]GetChatsRow, error) {
if arg.OwnedOnly && arg.SharedOnly {
return nil, xerrors.New("owned_only and shared_only cannot both be true")
}
if (arg.OwnedOnly || arg.SharedOnly) && arg.ViewerID == uuid.Nil {
return nil, xerrors.New("viewer_id required when owned_only or shared_only is true")
}
@@ -774,8 +771,8 @@ func (q *sqlQuerier) GetAuthorizedChats(ctx context.Context, arg GetChatsParams,
query := fmt.Sprintf("-- name: GetAuthorizedChats :many\n%s", filtered)
rows, err := q.db.QueryContext(ctx, query,
arg.OwnedOnly,
arg.ViewerID,
arg.SharedOnly,
arg.ViewerID,
arg.SharedWithUserID,
pq.Array(arg.SharedWithGroupIds),
arg.Archived,
+7 -5
View File
@@ -1587,12 +1587,14 @@ func TestGetAuthorizedChatsACLSharing(t *testing.T) {
require.Equal(t, sharedACL, sharedOnly[0].Chat.UserACL)
require.Empty(t, sharedOnly[0].Chat.GroupACL)
_, err = db.GetAuthorizedChats(ctx, database.GetChatsParams{
OwnedOnly: true,
SharedOnly: true,
ViewerID: recipient.ID,
ownedAndShared, err := db.GetAuthorizedChats(ctx, database.GetChatsParams{
OwnedOnly: true,
SharedOnly: true,
ViewerID: recipient.ID,
SharedWithUserID: recipient.ID,
}, preparedRecipient)
require.ErrorContains(t, err, "owned_only and shared_only")
require.NoError(t, err)
require.ElementsMatch(t, []uuid.UUID{ownerChat.ID, recipientChat.ID}, chatIDs(ownedAndShared))
authzdb := dbauthz.New(db, authorizer, slogtest.Make(t, &slogtest.Options{}), coderdtest.AccessControlStorePointer())
recipientCtx := dbauthz.As(ctx, recipientSubject)
+10 -11
View File
@@ -8023,19 +8023,18 @@ SELECT
FROM
chats_expanded
WHERE
CASE
WHEN $1::boolean THEN chats_expanded.owner_id = $2::uuid
ELSE true
END
AND CASE
WHEN $3::boolean THEN
chats_expanded.owner_id != $2::uuid
(
(NOT $1::boolean AND NOT $2::boolean)
OR ($1::boolean AND chats_expanded.owner_id = $3::uuid)
OR (
$2::boolean
AND chats_expanded.owner_id != $3::uuid
AND (
chats_expanded.user_acl ? ($4::uuid)::text
OR chats_expanded.group_acl ?| $5::text[]
)
ELSE true
END
)
)
AND CASE
WHEN $6 :: boolean IS NULL THEN true
ELSE chats_expanded.archived = $6 :: boolean
@@ -8174,8 +8173,8 @@ LIMIT
type GetChatsParams struct {
OwnedOnly bool `db:"owned_only" json:"owned_only"`
ViewerID uuid.UUID `db:"viewer_id" json:"viewer_id"`
SharedOnly bool `db:"shared_only" json:"shared_only"`
ViewerID uuid.UUID `db:"viewer_id" json:"viewer_id"`
SharedWithUserID uuid.UUID `db:"shared_with_user_id" json:"shared_with_user_id"`
SharedWithGroupIds []string `db:"shared_with_group_ids" json:"shared_with_group_ids"`
Archived sql.NullBool `db:"archived" json:"archived"`
@@ -8200,8 +8199,8 @@ type GetChatsRow struct {
func (q *sqlQuerier) GetChats(ctx context.Context, arg GetChatsParams) ([]GetChatsRow, error) {
rows, err := q.db.QueryContext(ctx, getChats,
arg.OwnedOnly,
arg.ViewerID,
arg.SharedOnly,
arg.ViewerID,
arg.SharedWithUserID,
pq.Array(arg.SharedWithGroupIds),
arg.Archived,
+8 -9
View File
@@ -481,19 +481,18 @@ SELECT
FROM
chats_expanded
WHERE
CASE
WHEN @owned_only::boolean THEN chats_expanded.owner_id = @viewer_id::uuid
ELSE true
END
AND CASE
WHEN @shared_only::boolean THEN
chats_expanded.owner_id != @viewer_id::uuid
(
(NOT @owned_only::boolean AND NOT @shared_only::boolean)
OR (@owned_only::boolean AND chats_expanded.owner_id = @viewer_id::uuid)
OR (
@shared_only::boolean
AND chats_expanded.owner_id != @viewer_id::uuid
AND (
chats_expanded.user_acl ? (@shared_with_user_id::uuid)::text
OR chats_expanded.group_acl ?| @shared_with_group_ids::text[]
)
ELSE true
END
)
)
AND CASE
WHEN sqlc.narg('archived') :: boolean IS NULL THEN true
ELSE chats_expanded.archived = sqlc.narg('archived') :: boolean