-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
623 additions
and
1,093 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
CREATE | ||
OR REPLACE FUNCTION archive_context_documents_rpc ( | ||
_context_id uuid, | ||
_document_ids uuid[] | ||
) RETURNS BOOLEAN AS $body$ | ||
DECLARE | ||
_project_id uuid; | ||
_layer_id uuid; | ||
_document_id uuid; | ||
_row RECORD; | ||
BEGIN | ||
-- Find the project for this context | ||
SELECT p.id INTO _project_id FROM public.projects p | ||
INNER JOIN public.contexts c ON c.id = _context_id | ||
WHERE p.id = c.project_id; | ||
|
||
-- Check project policy that context documents can be updated by this user | ||
IF NOT check_action_policy_project(auth.uid(), 'context_documents', 'UPDATE', _project_id) THEN | ||
RETURN FALSE; | ||
END IF; | ||
|
||
-- Iterate through the document ids and archive them in project_documents and all context_documents | ||
FOREACH _document_id IN ARRAY _document_ids | ||
LOOP | ||
-- Archive the context_documents record | ||
UPDATE public.context_document cd | ||
SET is_archived = TRUE | ||
WHERE cd.document_id = _document_id AND cd.context_id = _context_id; | ||
|
||
-- Archive any related layers | ||
FOR _row IN SELECT * FROM public.layers l | ||
INNER JOIN public.layer_contexts lc ON lc.context_id = _context_id | ||
WHERE l.document_id = _document_id | ||
LOOP | ||
UPDATE public.layers | ||
SET is_archived = TRUE | ||
WHERE id = _row.id; | ||
|
||
UPDATE public.layer_contexts lc | ||
SET is_archived = TRUE | ||
WHERE lc.context_id = _context_id AND lc.layer_id = _row.id; | ||
END LOOP; | ||
|
||
END LOOP; | ||
|
||
RETURN TRUE; | ||
END | ||
$body$ LANGUAGE plpgsql SECURITY DEFINER; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
CREATE | ||
OR REPLACE FUNCTION archive_context_rpc ( | ||
_context_id uuid | ||
) RETURNS BOOLEAN AS $body$ | ||
DECLARE | ||
_project_id uuid; | ||
_layer_id uuid; | ||
_document_id uuid; | ||
_row RECORD; | ||
_row_2 RECORD; | ||
BEGIN | ||
-- Find the project for this context | ||
SELECT p.id INTO _project_id FROM public.projects p | ||
INNER JOIN public.contexts c ON c.id = _context_id | ||
WHERE p.id = c.project_id; | ||
|
||
-- Check project policy that context documents can be updated by this user | ||
IF NOT check_action_policy_project(auth.uid(), 'contexts', 'UPDATE', _project_id) THEN | ||
RAISE LOG 'Check action policy failed for project %', _project_id; | ||
RETURN FALSE; | ||
END IF; | ||
|
||
-- Iterate through the document ids in this context and archive them in all context_documents | ||
FOR _row IN SELECT * FROM public.context_documents cd WHERE cd.context_id = _context_id | ||
LOOP | ||
-- Archive the context_documents record | ||
UPDATE public.context_documents cd | ||
SET is_archived = TRUE | ||
WHERE cd.id = _row.id; | ||
|
||
-- Archive any related layers | ||
FOR _row_2 IN SELECT * FROM public.layers l | ||
INNER JOIN public.layer_contexts lc ON lc.context_id = _context_id | ||
WHERE l.document_id = _row.document_id | ||
LOOP | ||
UPDATE public.layers | ||
SET is_archived = TRUE | ||
WHERE id = _row_2.id; | ||
|
||
UPDATE public.layer_contexts lc | ||
SET is_archived = TRUE | ||
WHERE lc.context_id = _context_id AND lc.layer_id = _row_2.id; | ||
END LOOP; | ||
|
||
END LOOP; | ||
|
||
UPDATE public.contexts | ||
SET is_archived = TRUE | ||
WHERE id = _context_id; | ||
RETURN TRUE; | ||
END | ||
$body$ LANGUAGE plpgsql SECURITY DEFINER; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
DROP POLICY IF EXISTS "Users with correct policies can SELECT on context_documents" ON public.context_documents; | ||
|
||
CREATE POLICY "Users with correct policies can SELECT on context_documents" ON public.context_documents FOR SELECT TO authenticated | ||
USING ( | ||
is_archived IS FALSE AND | ||
(public.check_action_policy_organization(auth.uid(), 'context_documents', 'SELECT') OR | ||
public.check_action_policy_project_from_context(auth.uid(), 'context_documents', 'SELECT', context_id) OR | ||
public.check_action_policy_layer_from_context_select(auth.uid(), 'context_documents', context_id)) | ||
); | ||
|
||
DROP POLICY IF EXISTS "Users with correct policies can INSERT on context_documents" ON public.context_documents; | ||
|
||
CREATE POLICY "Users with correct policies can INSERT on context_documents" ON public.context_documents FOR INSERT TO authenticated | ||
WITH CHECK (public.check_action_policy_organization(auth.uid(), 'context_documents', 'INSERT') OR | ||
public.check_action_policy_project_from_context(auth.uid(), 'context_documents', 'INSERT', context_id) OR | ||
public.check_action_policy_layer_from_context(auth.uid(), 'context_documents', 'INSERT', context_id)); | ||
|
||
DROP POLICY IF EXISTS "Users with correct policies can UPDATE on context_documents" ON public.context_documents; | ||
|
||
CREATE POLICY "Users with correct policies can UPDATE on context_documents" ON public.context_documents FOR UPDATE TO authenticated | ||
USING ( | ||
public.check_action_policy_organization(auth.uid(), 'context_documents', 'UPDATE') OR | ||
public.check_action_policy_project_from_context(auth.uid(), 'context_documents', 'UPDATE', context_id) OR | ||
public.check_action_policy_layer_from_context(auth.uid(), 'context_documents', 'UPDATE', context_id) | ||
) | ||
WITH CHECK (public.check_action_policy_organization(auth.uid(), 'context_documents', 'UPDATE') OR | ||
public.check_action_policy_project_from_context(auth.uid(), 'context_documents', 'UPDATE', context_id) OR | ||
public.check_action_policy_layer_from_context(auth.uid(), 'context_documents', 'UPDATE', context_id)); | ||
|
||
DROP POLICY IF EXISTS "Users with correct policies can DELETE on context_documents" ON public.context_documents; | ||
|
||
CREATE POLICY "Users with correct policies can DELETE on context_documents" ON public.context_documents FOR DELETE TO authenticated | ||
USING (public.check_action_policy_organization(auth.uid(), 'context_documents', 'DELETE') OR | ||
public.check_action_policy_project_from_context(auth.uid(), 'context_documents', 'DELETE', context_id) OR | ||
public.check_action_policy_layer_from_context(auth.uid(), 'context_documents', 'DELETE', context_id)); |
Oops, something went wrong.