Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use user tags to check PSA auth #1008

Merged
merged 10 commits into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions packages/api/test/fixtures/init-data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,9 @@ VALUES (
);

-- user 'test-pinning' is authorized
INSERT INTO pinning_authorization (user_id)
VALUES (4);
INSERT INTO pinning_authorization (user_id)
VALUES (5);
INSERT INTO public.user_tag (user_id, tag, value, reason)
VALUES (4, 'HasPsaAccess', true, 'test'),
(5, 'HasPsaAccess', true, 'test');

INSERT INTO content (cid)
VALUES ('bafybeid46f7zggioxjm5p2ze2l6s6wbqvoo4gzbdzfjtdosthmfyxdign4'),
Expand Down
10 changes: 6 additions & 4 deletions packages/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,18 @@ export class DBClient {
}

/**
* Check that a user is authorized to pin
* Check that a user is authorized to pin.
*
* @param {number} userId
* @returns {Promise<boolean>}
*/
async isPinningAuthorized (userId) {
const { error, count } = await this._client
.from('pinning_authorization')
.select('id', { count: 'exact' })
const { count, error } = await this._client
.from('user_tag')
.select('value', { count: 'exact' })
.eq('user_id', userId)
.eq('tag', 'HasPsaAccess')
.eq('value', true)
.filter('deleted_at', 'is', null)

if (error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
BEGIN TRANSACTION;
INSERT INTO public.user_tag
(
user_id,
tag,
value,
reason,
inserted_at
)
SELECT
user_id,
'HasPsaAccess' as tag,
'true' as value,
'Approved access' as reason,
inserted_at
FROM public.pinning_authorization
WHERE deleted_at IS NULL;

INSERT INTO public.user_tag
(
user_id,
tag,
value,
reason,
inserted_at,
deleted_at
)
SELECT
user_id,
'HasPsaAccess' as tag,
'false' as value,
'Revoked access' as reason,
inserted_at,
deleted_at
FROM public.pinning_authorization
WHERE deleted_at IS NOT NULL;

DROP TABLE public.pinning_authorization;
COMMIT;
Loading