From 4c5c89da546cf94080dd7690a84dd1ddd1aca01a Mon Sep 17 00:00:00 2001 From: Dario Bagatto Date: Thu, 30 May 2024 16:05:51 +0200 Subject: [PATCH] Feature/hellodata 1435 set owners for charts (#69) * HELLODATA-1435 added triggers to manage owners of charts * HELLODATA-1435 adapted handling of owner consistency --- .../db/changelog/changelogs/04_triggers.sql | 50 ++++++++++++++++++- .../06_handle_owners_on_datasets.sql | 6 +-- .../changelogs/07_handle_owners_on_charts.sql | 47 +++++++++++++++++ .../08_handle_owners_on_dashboards.sql | 47 +++++++++++++++++ .../db/changelog/superset-changelog.xml | 9 +++- 5 files changed, 153 insertions(+), 6 deletions(-) create mode 100644 hello-data-subsystems/hello-data-superset/src/main/docker/db/changelog/changelogs/07_handle_owners_on_charts.sql create mode 100644 hello-data-subsystems/hello-data-superset/src/main/docker/db/changelog/changelogs/08_handle_owners_on_dashboards.sql diff --git a/hello-data-subsystems/hello-data-superset/src/main/docker/db/changelog/changelogs/04_triggers.sql b/hello-data-subsystems/hello-data-superset/src/main/docker/db/changelog/changelogs/04_triggers.sql index e9bbdc9f..9bc47db8 100644 --- a/hello-data-subsystems/hello-data-superset/src/main/docker/db/changelog/changelogs/04_triggers.sql +++ b/hello-data-subsystems/hello-data-superset/src/main/docker/db/changelog/changelogs/04_triggers.sql @@ -178,6 +178,11 @@ BEGIN INSERT INTO dashboard_user SELECT nextval('dashboard_user_id_seq'), new.user_id, dashboards.id from dashboards; + --- Handle ownership of charts + DELETE FROM slice_user WHERE user_id = new.user_id; + INSERT INTO slice_user + SELECT nextval('slice_user_id_seq'), new.user_id, slices.id from slices; + --- Handle ownership of datasets DELETE FROM sqlatable_user WHERE user_id = new.user_id; INSERT INTO sqlatable_user @@ -295,6 +300,10 @@ BEGIN IF old.role_id = BI_EDITOR_role_id THEN --- Delete ownership of dashboards DELETE FROM dashboard_user WHERE user_id = old.user_id; + + --- Delete ownership of charts + DELETE FROM slice_user WHERE user_id = old.user_id; + --- Delete ownership of datasets DELETE FROM sqlatable_user WHERE user_id = old.user_id; END IF; @@ -350,4 +359,43 @@ DROP TRIGGER IF EXISTS insert_owners_on_new_dataset on tables; CREATE TRIGGER insert_owners_on_new_dataset AFTER INSERT ON tables FOR EACH ROW -EXECUTE FUNCTION insert_owners_on_new_dataset(); \ No newline at end of file +EXECUTE FUNCTION insert_owners_on_new_dataset(); + + + +-- +-- Trigger function for inserts on slices table that sets all BI_EDITOR users as owner of the corresponding chart +-- +-- DROP FUNCTION insert_owners_on_new_slice; +CREATE OR REPLACE FUNCTION insert_owners_on_new_slice() RETURNS TRIGGER AS +$$ +BEGIN + + INSERT INTO slice_user(id, user_id, slice_id) + + SELECT + nextval('slice_user_id_seq'), + ab_user.id, + new.id + FROM + ab_user + LEFT JOIN ab_user_role ON ab_user.id = ab_user_role.user_id + LEFT JOIN ab_role ON ab_role.id = ab_user_role.role_id + WHERE + ab_role.name = 'BI_EDITOR' + AND ab_user.id <> new.created_by_fk; + + RETURN new; +END; +$$ + LANGUAGE plpgsql; + + +-- +-- After insert Trigger on slices that sets all BI_EDITOR users as owner of the corresponding chart +-- +DROP TRIGGER IF EXISTS insert_owners_on_new_slice on slices; +CREATE TRIGGER insert_owners_on_new_dataset + AFTER INSERT ON slices + FOR EACH ROW +EXECUTE FUNCTION insert_owners_on_new_slice(); \ No newline at end of file diff --git a/hello-data-subsystems/hello-data-superset/src/main/docker/db/changelog/changelogs/06_handle_owners_on_datasets.sql b/hello-data-subsystems/hello-data-superset/src/main/docker/db/changelog/changelogs/06_handle_owners_on_datasets.sql index 5806fb38..1e490787 100644 --- a/hello-data-subsystems/hello-data-superset/src/main/docker/db/changelog/changelogs/06_handle_owners_on_datasets.sql +++ b/hello-data-subsystems/hello-data-superset/src/main/docker/db/changelog/changelogs/06_handle_owners_on_datasets.sql @@ -30,6 +30,8 @@ -- Deletes potential duplicate entries in the sqlatable_user table. -- This script is repeatable. -- +TRUNCATE sqlatable_user RESTART IDENTITY; + WITH editor_user_ids AS( SELECT @@ -42,6 +44,4 @@ WITH role_id = (SELECT id FROM ab_role WHERE name = 'BI_EDITOR') ) -INSERT INTO sqlatable_user (id, user_id, table_id) SELECT nextval('sqlatable_user_id_seq'), editor_user_ids.user_id, tables.id FROM tables, editor_user_ids; - -DELETE FROM sqlatable_user WHERE id NOT IN (SELECT min(id) FROM sqlatable_user GROUP BY user_id, table_id); \ No newline at end of file +INSERT INTO sqlatable_user (id, user_id, table_id) SELECT nextval('sqlatable_user_id_seq'), editor_user_ids.user_id, tables.id FROM tables, editor_user_ids; \ No newline at end of file diff --git a/hello-data-subsystems/hello-data-superset/src/main/docker/db/changelog/changelogs/07_handle_owners_on_charts.sql b/hello-data-subsystems/hello-data-superset/src/main/docker/db/changelog/changelogs/07_handle_owners_on_charts.sql new file mode 100644 index 00000000..6829ae86 --- /dev/null +++ b/hello-data-subsystems/hello-data-superset/src/main/docker/db/changelog/changelogs/07_handle_owners_on_charts.sql @@ -0,0 +1,47 @@ +-- +-- Copyright © 2024, Kanton Bern +-- All rights reserved. +-- +-- Redistribution and use in source and binary forms, with or without +-- modification, are permitted provided that the following conditions are met: +-- * Redistributions of source code must retain the above copyright +-- notice, this list of conditions and the following disclaimer. +-- * Redistributions in binary form must reproduce the above copyright +-- notice, this list of conditions and the following disclaimer in the +-- documentation and/or other materials provided with the distribution. +-- * Neither the name of the nor the +-- names of its contributors may be used to endorse or promote products +-- derived from this software without specific prior written permission. +-- +-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +-- DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY +-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +-- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +-- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +-- + +-- +-- Adds all current BI_EDITORs as owners of all charts. +-- Deletes potential duplicate entries in the slice_user table. +-- This script is repeatable. +-- +TRUNCATE slice_user RESTART IDENTITY; + +WITH + editor_user_ids AS( + SELECT + user_id + + FROM + ab_user_role + + WHERE + role_id = (SELECT id FROM ab_role WHERE name = 'BI_EDITOR') + ) + +INSERT INTO slice_user (id, user_id, slice_id) SELECT nextval('slice_user_id_seq'), editor_user_ids.user_id, slices.id FROM slices, editor_user_ids; \ No newline at end of file diff --git a/hello-data-subsystems/hello-data-superset/src/main/docker/db/changelog/changelogs/08_handle_owners_on_dashboards.sql b/hello-data-subsystems/hello-data-superset/src/main/docker/db/changelog/changelogs/08_handle_owners_on_dashboards.sql new file mode 100644 index 00000000..1733fbae --- /dev/null +++ b/hello-data-subsystems/hello-data-superset/src/main/docker/db/changelog/changelogs/08_handle_owners_on_dashboards.sql @@ -0,0 +1,47 @@ +-- +-- Copyright © 2024, Kanton Bern +-- All rights reserved. +-- +-- Redistribution and use in source and binary forms, with or without +-- modification, are permitted provided that the following conditions are met: +-- * Redistributions of source code must retain the above copyright +-- notice, this list of conditions and the following disclaimer. +-- * Redistributions in binary form must reproduce the above copyright +-- notice, this list of conditions and the following disclaimer in the +-- documentation and/or other materials provided with the distribution. +-- * Neither the name of the nor the +-- names of its contributors may be used to endorse or promote products +-- derived from this software without specific prior written permission. +-- +-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +-- DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY +-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +-- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +-- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +-- + +-- +-- Adds all current BI_EDITORs as owners of all charts. +-- Deletes potential duplicate entries in the dashboard_user table. +-- This script is repeatable. +-- +TRUNCATE dashboard_user RESTART IDENTITY; + +WITH + editor_user_ids AS( + SELECT + user_id + + FROM + ab_user_role + + WHERE + role_id = (SELECT id FROM ab_role WHERE name = 'BI_EDITOR') + ) + +INSERT INTO dashboard_user (id, user_id, dashboard_id) SELECT nextval('dashboard_user_id_seq'), editor_user_ids.user_id, dashboards.id FROM dashboards, editor_user_ids; \ No newline at end of file diff --git a/hello-data-subsystems/hello-data-superset/src/main/docker/db/changelog/superset-changelog.xml b/hello-data-subsystems/hello-data-superset/src/main/docker/db/changelog/superset-changelog.xml index 438c0791..f2bc2799 100644 --- a/hello-data-subsystems/hello-data-superset/src/main/docker/db/changelog/superset-changelog.xml +++ b/hello-data-subsystems/hello-data-superset/src/main/docker/db/changelog/superset-changelog.xml @@ -51,11 +51,16 @@ - + - + + + + + + \ No newline at end of file