-
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.
Merge branch 'develop' of https://github.com/recogito/recogito-server …
…into develop
- Loading branch information
Showing
17 changed files
with
1,545 additions
and
31 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
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,32 @@ | ||
CREATE | ||
OR REPLACE FUNCTION archive_document_rpc ( | ||
_document_id uuid | ||
) RETURNS BOOLEAN AS $body$ | ||
DECLARE | ||
_row public.documents % rowtype; | ||
BEGIN | ||
-- Check project policy that project documents can be updated by this user | ||
IF NOT (check_action_policy_organization(auth.uid(), 'documents', 'UPDATE')) | ||
THEN | ||
RETURN FALSE; | ||
END IF; | ||
|
||
-- Get the document | ||
SELECT * INTO _row FROM public.documents d WHERE d.id = _document_id; | ||
|
||
-- If the user is the creator or an Org Admin, archive the document | ||
IF _row.created_by = auth.uid() OR is_admin_organization(auth.uid()) | ||
THEN | ||
IF NOT EXISTS(SELECT 1 FROM public.project_documents pd WHERE pd.id = _document_id AND pd.is_archived IS FALSE ) | ||
THEN | ||
UPDATE public.documents d | ||
SET is_archived = TRUE | ||
WHERE d.id = _document_id; | ||
|
||
RETURN TRUE; | ||
END IF; | ||
END IF; | ||
|
||
RETURN FALSE; | ||
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,103 @@ | ||
CREATE | ||
OR REPLACE FUNCTION lock_project_rpc ( | ||
_project_id uuid | ||
) RETURNS BOOLEAN | ||
AS $body$ | ||
DECLARE | ||
_project_read_only_group_id uuid; | ||
_project_group_ids uuid[]; | ||
_project_admin_ids uuid[]; | ||
_project_group_id uuid; | ||
_row_group_users public.group_users % rowtype; | ||
_read_only_layer_role uuid; | ||
_context_ids uuid[]; | ||
_context_id uuid; | ||
_user_id uuid; | ||
BEGIN | ||
-- Must have Update privs on project | ||
IF NOT (check_action_policy_organization(auth.uid(), 'projects', 'UPDATE') | ||
OR check_action_policy_project(auth.uid(), 'projects', 'UPDATE', _project_id)) | ||
THEN | ||
RETURN FALSE; | ||
END IF; | ||
|
||
-- Select the read only project default group | ||
SELECT pg.id INTO _project_read_only_group_id | ||
FROM public.project_groups pg | ||
WHERE pg.project_id = _project_id | ||
AND pg.is_read_only IS TRUE; | ||
|
||
-- Create an array of project_group ids | ||
_project_group_ids := ARRAY( | ||
SELECT pg.id | ||
FROM public.project_groups pg | ||
WHERE pg.project_id = _project_id | ||
AND pg.is_read_only IS NOT TRUE | ||
); | ||
|
||
-- Create an array of user ids | ||
_project_admin_ids := ARRAY( | ||
SELECT gu.user_id | ||
FROM public.group_users gu | ||
WHERE gu.type_id = ANY(_project_group_ids) | ||
); | ||
|
||
-- For each project group user, set them to read-only | ||
FOREACH _project_group_id IN ARRAY _project_group_ids | ||
LOOP | ||
UPDATE public.group_users | ||
SET type_id = _project_read_only_group_id | ||
WHERE type_id = _project_group_id | ||
AND group_type = 'project'; | ||
END LOOP; | ||
|
||
-- If we do not have a read-only layer default group then fail | ||
IF NOT EXISTS(SELECT 1 FROM public.default_groups dgx WHERE dgx.group_type = 'layer' AND dgx.is_read_only IS TRUE) | ||
THEN | ||
ROLLBACK; | ||
RETURN FALSE; | ||
END IF; | ||
|
||
-- Get the read only role from default groups | ||
SELECT dgx.role_id INTO _read_only_layer_role FROM public.default_groups dgx WHERE dgx.group_type = 'layer' AND dgx.is_read_only IS TRUE; | ||
|
||
-- Get an array of context ids for this project | ||
_context_ids := ARRAY( | ||
SELECT c.id | ||
FROM public.contexts c | ||
WHERE c.project_id = _project_id | ||
); | ||
|
||
-- Set all context users to read-only | ||
FOREACH _context_id IN ARRAY _context_ids | ||
LOOP | ||
UPDATE public.context_users | ||
SET role_id = _read_only_layer_role | ||
WHERE _context_id = _context_id; | ||
END LOOP; | ||
|
||
-- Add the admins to each context as read-only | ||
FOREACH _context_id IN ARRAY _context_ids | ||
LOOP | ||
FOREACH _user_id IN ARRAY _project_admin_ids | ||
LOOP | ||
INSERT INTO public.context_users | ||
(role_id, user_id, context_id) | ||
VALUES (_read_only_layer_role, _user_id, _context_id) | ||
ON CONFLICT(user_id, context_id) | ||
DO NOTHING; | ||
END LOOP; | ||
END LOOP; | ||
|
||
-- Set the admins to the read only project group | ||
|
||
-- Update the project | ||
UPDATE public.projects | ||
SET is_locked = TRUE | ||
WHERE id = _project_id; | ||
|
||
-- Success | ||
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,65 @@ | ||
CREATE | ||
OR REPLACE FUNCTION fix_corrupted_baselayers () | ||
RETURNS void | ||
AS $body$ | ||
DECLARE | ||
_default_ctx_id uuid; | ||
_proj_doc_array uuid[]; | ||
_proj_row public.projects % rowtype; | ||
_ctx_doc_row public.context_documents; | ||
_layer_context_row public.layer_contexts % rowtype; | ||
BEGIN | ||
-- This script fixes corrupted baselayers using the following steps | ||
-- 1. Iterate through all projects | ||
FOR _proj_row IN SELECT * FROM public.projects p | ||
LOOP | ||
-- 2. For each project get the default context | ||
SELECT c.id INTO _default_ctx_id FROM public.contexts c | ||
WHERE c.project_id = _proj_row.id | ||
AND c.is_project_default IS TRUE | ||
AND c.is_archived IS NOT TRUE; | ||
|
||
-- 3. Create an array of project_document ids | ||
_proj_doc_array := ARRAY(SELECT pd.document_id FROM public.project_documents pd WHERE pd.project_id = _proj_row.id AND pd.is_archived IS NOT TRUE); | ||
RAISE LOG 'Doc Array for project %: %', _proj_row.id, _proj_doc_array; | ||
|
||
-- 4. Archive any context documents that reference documents not in the project_documents array | ||
|
||
IF EXISTS( | ||
SELECT 1 a | ||
FROM public.context_documents cd | ||
WHERE cd.context_id = _default_ctx_id | ||
AND cd.is_archived IS NOT TRUE | ||
AND NOT (cd.document_id = ANY(_proj_doc_array))) | ||
THEN | ||
FOR _ctx_doc_row IN SELECT * | ||
FROM public.context_documents cd | ||
WHERE cd.context_id = _default_ctx_id | ||
AND cd.is_archived IS NOT TRUE | ||
AND NOT (cd.document_id = ANY(_proj_doc_array)) | ||
LOOP | ||
UPDATE public.context_documents cd | ||
SET is_archived = TRUE | ||
WHERE cd.id = _ctx_doc_row.id; | ||
|
||
-- 5. Archive any layer contexts and layers associated with these documents | ||
FOR _layer_context_row IN SELECT * | ||
FROM public.layer_contexts lc | ||
INNER JOIN public.layers l ON l.project_id = _proj_row.id | ||
AND (l.document_id = _ctx_doc_row.document_id OR NOT (l.document_id = ANY(_proj_doc_array))) | ||
AND l.is_archived IS NOT TRUE | ||
WHERE lc.layer_id = l.id AND lc.context_id = _default_ctx_id | ||
LOOP | ||
UPDATE public.layer_contexts lc | ||
SET is_archived = TRUE | ||
WHERE lc.id = _layer_context_row.id; | ||
|
||
UPDATE public.layers l | ||
SET is_archived = TRUE | ||
WHERE l.id = _layer_context_row.layer_id; | ||
END LOOP; | ||
END LOOP; | ||
END IF; | ||
END LOOP; | ||
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
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
Oops, something went wrong.