From 7384751d6b616b322d467d048da093f1a1544d2c Mon Sep 17 00:00:00 2001 From: Masahiko AMANO Date: Sat, 5 Jul 2025 15:38:31 +0300 Subject: [PATCH] fix(backend/storage/postgres): fix `FileGetAccess` function --- backend/internal/storage/postgres/files.go | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/backend/internal/storage/postgres/files.go b/backend/internal/storage/postgres/files.go index 02338c5..e5f96c9 100644 --- a/backend/internal/storage/postgres/files.go +++ b/backend/internal/storage/postgres/files.go @@ -16,21 +16,15 @@ import ( func FileGetAccess(user_id int, file_id string) (canView, canEdit bool, err error) { ctx := context.Background() row := connPool.QueryRow(ctx, ` - WITH extras AS ( - SELECT - (SELECT creator_id FROM data.files WHERE id=$2)=$1 AS is_creator, - (SELECT is_admin FROM system.users WHERE id=$1) AS is_admin - ) - SELECT - af.view OR (SELECT is_creator OR is_admin FROM extras), - af.edit OR (SELECT is_creator OR is_admin FROM extras) - FROM acl.files af - WHERE af.user_id=$1 AND af.file_id=$2 + SELECT + COALESCE(a.view, FALSE) OR f.creator_id=$1 OR COALESCE(u.is_admin, FALSE), + COALESCE(a.edit, FALSE) OR f.creator_id=$1 OR COALESCE(u.is_admin, FALSE) + FROM data.files f + LEFT JOIN acl.files a ON a.file_id=f.id AND a.user_id=$1 + LEFT JOIN system.users u ON u.id=$1 + WHERE f.id=$2 `, user_id, file_id) err = row.Scan(&canView, &canEdit) - if err == pgx.ErrNoRows { - err = nil - } return }