Skip to content

Commit

Permalink
Merge pull request #39 from recogito/develop
Browse files Browse the repository at this point in the history
Merge to main
  • Loading branch information
lwjameson authored Nov 26, 2024
2 parents cff4aa9 + 794e8cf commit b354047
Show file tree
Hide file tree
Showing 21 changed files with 1,865 additions and 33 deletions.
2 changes: 1 addition & 1 deletion SQL Scripts/functions/archive_document_rpc.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ BEGIN
-- 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 )
IF NOT EXISTS(SELECT 1 FROM public.project_documents pd WHERE pd.document_id = _document_id AND pd.is_archived IS FALSE )
THEN
UPDATE public.documents d
SET is_archived = TRUE
Expand Down
12 changes: 7 additions & 5 deletions SQL Scripts/functions/create_default_layer_groups.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ DECLARE
_description varchar;
_is_admin bool;
_is_default bool;
_is_read_only bool;
BEGIN
FOR _role_id, _name, _description, _is_admin, _is_default IN SELECT role_id, name, description, is_admin, is_default
FROM public.default_groups
WHERE group_type = 'layer'
FOR _role_id, _name, _description, _is_admin, _is_default, _is_read_only
IN SELECT role_id, name, description, is_admin, is_default, is_read_only
FROM public.default_groups
WHERE group_type = 'layer'
LOOP
_layer_group_id = extensions.uuid_generate_v4();
INSERT INTO public.layer_groups
(id, layer_id, role_id, name, description, is_admin, is_default)
VALUES (_layer_group_id, NEW.id, _role_id, _name, _description, _is_admin, _is_default);
(id, layer_id, role_id, name, description, is_admin, is_default, is_read_only)
VALUES (_layer_group_id, NEW.id, _role_id, _name, _description, _is_admin, _is_default, _is_read_only);

IF _is_admin IS TRUE AND NEW.created_by IS NOT NULL THEN
INSERT INTO public.group_users (group_type, type_id, user_id)
Expand Down
12 changes: 7 additions & 5 deletions SQL Scripts/functions/create_default_project_groups.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ DECLARE
_description varchar;
_is_admin bool;
_is_default bool;
_is_read_only bool;
BEGIN
FOR _role_id, _name, _description, _is_admin, _is_default IN SELECT role_id, name, description, is_admin, is_default
FROM public.default_groups
WHERE group_type = 'project'
FOR _role_id, _name, _description, _is_admin, _is_default, _is_read_only
IN SELECT role_id, name, description, is_admin, is_default, is_read_only
FROM public.default_groups
WHERE group_type = 'project'
LOOP
_project_group_id = extensions.uuid_generate_v4();
INSERT INTO public.project_groups
(id, project_id, role_id, name, description, is_admin, is_default)
VALUES (_project_group_id, NEW.id, _role_id, _name, _description, _is_admin, _is_default);
(id, project_id, role_id, name, description, is_admin, is_default, is_read_only)
VALUES (_project_group_id, NEW.id, _role_id, _name, _description, _is_admin, _is_default, _is_read_only);

IF _is_admin IS TRUE AND NEW.created_by IS NOT NULL THEN
INSERT INTO public.group_users (group_type, type_id, user_id)
Expand Down
13 changes: 7 additions & 6 deletions SQL Scripts/functions/create_project_rpc.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
CREATE
OR REPLACE FUNCTION create_project_rpc (
OR REPLACE FUNCTION create_project_rpc (
_name VARCHAR,
_description VARCHAR,
_is_open_join BOOLEAN,
_is_open_edit BOOLEAN
_is_open_edit BOOLEAN,
_document_view_right DOCUMENT_VIEW_TYPE
) RETURNS SETOF public.projects AS $body$
DECLARE
_project_id uuid := gen_random_uuid(); -- The id of the new project
Expand All @@ -12,16 +13,16 @@ DECLARE
BEGIN
IF NOT check_action_policy_organization(auth.uid(), 'projects', 'INSERT') THEN
RETURN;
END IF;
END IF;

INSERT INTO public.projects (id, created_by, created_at, name, description, is_open_join, is_open_edit) VALUES (_project_id, auth.uid(), NOW(), _name, _description, _is_open_join, _is_open_edit);
INSERT INTO public.projects (id, created_by, created_at, name, description, is_open_join, is_open_edit, document_view_right) VALUES (_project_id, auth.uid(), NOW(), _name, _description, _is_open_join, _is_open_edit, _document_view_right);

INSERT INTO public.contexts (id, created_by, created_at, project_id, is_project_default) VALUES (_context_id, auth.uid(), NOW(), _project_id, TRUE);

SELECT (id) INTO _default_context_definition_id FROM public.tag_definitions t WHERE t.scope = 'system' AND t.name = 'DEFAULT_CONTEXT';

INSERT INTO public.tags (created_by, created_at, tag_definition_id, target_id) VALUES (auth.uid(), NOW(), _default_context_definition_id, _context_id);
INSERT INTO public.tags (created_by, created_at, tag_definition_id, target_id) VALUES (auth.uid(), NOW(), _default_context_definition_id, _context_id);

RETURN QUERY SELECT * FROM public.projects WHERE id = _project_id;
END
$body$ LANGUAGE plpgsql SECURITY DEFINER;
103 changes: 103 additions & 0 deletions SQL Scripts/functions/lock_project_rpc.sql
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;
7 changes: 6 additions & 1 deletion SQL Scripts/tables/default_groups.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ CREATE TABLE public.default_groups
description varchar NOT NULL,
role_id uuid REFERENCES public.roles NOT NULL,
is_admin bool DEFAULT FALSE,
is_default bool DEFAULT FALSE
is_default bool DEFAULT FALSE,
is_read_only bool DEFAULT FALSE
);

-- Changes 6/6/23 --
Expand All @@ -25,3 +26,7 @@ ALTER TABLE public.default_groups
-- Changes 7/26/23 --
ALTER TABLE public.default_groups
ADD COLUMN is_archived bool DEFAULT FALSE;

-- Changes 9/20/24 --
ALTER TABLE public.default_groups
ADD COLUMN is_read_only bool DEFAULT FALSE;
8 changes: 7 additions & 1 deletion SQL Scripts/tables/layer_groups.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ CREATE TABLE PUBLIC.LAYER_GROUPS (
DESCRIPTION VARCHAR,
ROLE_ID UUID REFERENCES PUBLIC.ROLES NOT NULL,
IS_ADMIN BOOL DEFAULT FALSE,
IS_DEFAULT BOOLEAN DEFAULT FALSE
IS_DEFAULT BOOLEAN DEFAULT FALSE,
IS_READ_ONLY BOOLEAN DEFAULT FALSE
);

-- Changes 05/01/23 ---
Expand Down Expand Up @@ -67,3 +68,8 @@ ADD COLUMN IS_ADMIN BOOL DEFAULT FALSE;
-- Changes 11/30/23 --
ALTER TABLE PUBLIC.LAYER_GROUPS
ADD COLUMN IS_DEFAULT BOOLEAN DEFAULT FALSE;

-- Changes 9/20/24 --
ALTER TABLE PUBLIC.LAYER_GROUPS
ADD COLUMN is_read_only BOOLEAN DEFAULT FALSE;

9 changes: 7 additions & 2 deletions SQL Scripts/tables/organization_groups.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ CREATE TABLE public .organization_groups (
description VARCHAR,
role_id UUID REFERENCES public .roles UNIQUE NOT NULL,
is_admin BOOLEAN DEFAULT FALSE,
is_default BOOLEAN DEFAULT FALSE
is_default BOOLEAN DEFAULT FALSE,
is_read_only BOOLEAN DEFAULT FALSE
);
-- Changes 05/08/23
ALTER TABLE public .organization_groups
Expand Down Expand Up @@ -39,4 +40,8 @@ ALTER TABLE public .organization_groups
ADD COLUMN is_admin bool DEFAULT FALSE;
-- Changes 11/27/23 --
ALTER TABLE public .organization_groups
ADD COLUMN is_default bool DEFAULT FALSE;
ADD COLUMN is_default bool DEFAULT FALSE;

-- Changes 9/23/24 --
ALTER TABLE public.organization_groups
ADD COLUMN is_read_only bool DEFAULT FALSE;
9 changes: 7 additions & 2 deletions SQL Scripts/tables/project_groups.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ CREATE TABLE PUBLIC.PROJECT_GROUPS (
DESCRIPTION VARCHAR,
ROLE_ID UUID REFERENCES PUBLIC.ROLES NOT NULL,
IS_ADMIN BOOL DEFAULT FALSE,
IS_DEFAULT BOOLEAN DEFAULT FALSE
IS_DEFAULT BOOLEAN DEFAULT FALSE,
IS_READ_ONLY BOOLEAN DEFAULT FALSE
);

-- Changes 05/08/23
Expand Down Expand Up @@ -53,4 +54,8 @@ ADD COLUMN IS_ADMIN BOOL DEFAULT FALSE;

-- Changes 11/30/23 --
ALTER TABLE PUBLIC.PROJECT_GROUPS
ADD COLUMN IS_DEFAULT BOOLEAN DEFAULT FALSE;
ADD COLUMN IS_DEFAULT BOOLEAN DEFAULT FALSE;

-- Changed 9/20/24
ALTER TABLE public.project_groups
ADD COLUMN is_read_only BOOLEAN DEFAULT FALSE;
14 changes: 12 additions & 2 deletions SQL Scripts/tables/projects.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ CREATE TABLE
NAME VARCHAR,
description VARCHAR,
is_open_join BOOLEAN DEFAULT FALSE,
is_open_edit BOOLEAN DEFAULT FALSE
is_open_edit BOOLEAN DEFAULT FALSE,
is_locked BOOLEAN DEFAULT FALSE,
document_view_right DOCUMENT_VIEW_TYPE DEFAULT 'closed'
);

-- Changes 04/21/23 --
Expand All @@ -33,4 +35,12 @@ ALTER TABLE public.projects
ADD COLUMN is_open_join BOOLEAN DEFAULT FALSE;

ALTER TABLE public.projects
ADD COLUMN is_open_edit BOOLEAN DEFAULT FALSE;
ADD COLUMN is_open_edit BOOLEAN DEFAULT FALSE;

-- Changes 9/20/24
ALTER TABLE public.projects
ADD COLUMN is_locked BOOLEAN DEFAULT FALSE;

-- Changes 10/18/24
ALTER TABLE public.projects
ADD COLUMN document_view_right DOCUMENT_VIEW_TYPE DEFAULT 'closed';
7 changes: 6 additions & 1 deletion SQL Scripts/tables/tag_definitions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ CREATE TABLE tag_definitions
name varchar NOT NULL,
target_type tag_target_types NOT NULL,
scope tag_scope_types NOT NULL,
scope_id uuid
scope_id uuid,
metadata json NOT NULL DEFAULT {}
);

-- Changes 05/26/23 --
Expand All @@ -26,3 +27,7 @@ ALTER TABLE public.tag_definitions
-- Changes 7/26/23 --
ALTER TABLE public.tag_definitions
ADD COLUMN is_archived bool DEFAULT FALSE;

-- Changes 10/24/24
ALTER TABLE public.tag_definitions
ADD COLUMN metadata json NOT NULL DEFAULT '{}';
42 changes: 42 additions & 0 deletions SQL Scripts/utility/add_read_only_groups.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
DO $$
DECLARE
_layer_group_id uuid;
_role_id uuid;
_name varchar;
_description varchar;
_is_admin bool;
_is_default bool;
_is_read_only bool;
_layer_id uuid;
_project_id uuid;
BEGIN
-- Get the read-only default group
FOR _role_id, _name, _description, _is_admin, _is_default, _is_read_only
IN SELECT dg.role_id, dg.name, dg.description, dg.is_admin, dg.is_default, dg.is_read_only
FROM public.default_groups dg
WHERE dg.group_type = 'layer' AND dg.is_read_only IS TRUE
LOOP
-- Loop through all layers
FOR _layer_id IN SELECT l.id FROM public.layers l
LOOP
IF NOT EXISTS(SELECT 1 FROM public.layer_groups lg WHERE lg.layer_id = _layer_id AND lg.is_read_only IS TRUE)
THEN
_layer_group_id = extensions.uuid_generate_v4();
INSERT INTO public.layer_groups
(id, layer_id, role_id, name, description, is_admin, is_default, is_read_only)
VALUES (_layer_group_id, _layer_id, _role_id, _name, _description, _is_admin, _is_default, _is_read_only);
END IF;
END LOOP;
END LOOP;
-- Set the Student role in project groups to read-only
FOR _project_id
IN SELECT p.id
FROM public.projects p
LOOP
-- For each project group set the Student role to read-only
UPDATE public.project_groups pg
SET is_read_only = TRUE
WHERE pg.is_default IS TRUE;
END LOOP;
END
$$
963 changes: 962 additions & 1 deletion config.json

Large diffs are not rendered by default.

Loading

0 comments on commit b354047

Please sign in to comment.