From fb8862c37e286dcacd324e7fa022f28f8ac39164 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Thu, 28 Jul 2022 17:17:22 +0200 Subject: [PATCH] convert rbac-statistics and rbac-role-builder*.sql files to Liquibase changesets --- .../db/changelog/2022-07-28-005-rbac-base.sql | 9 --- ...l => 2022-07-28-020-rbac-role-builder.sql} | 58 ++++++++++++------- ...sql => 2022-07-28-030-rbac-statistics.sql} | 26 +++++---- .../db/changelog/db.changelog-master.yaml | 4 ++ 4 files changed, 58 insertions(+), 39 deletions(-) rename src/main/resources/db/changelog/{12-rbac-role-builder.sql => 2022-07-28-020-rbac-role-builder.sql} (81%) rename src/main/resources/db/changelog/{18-rbac-statistics.sql => 2022-07-28-030-rbac-statistics.sql} (58%) diff --git a/src/main/resources/db/changelog/2022-07-28-005-rbac-base.sql b/src/main/resources/db/changelog/2022-07-28-005-rbac-base.sql index e879852f..83e954ee 100644 --- a/src/main/resources/db/changelog/2022-07-28-005-rbac-base.sql +++ b/src/main/resources/db/changelog/2022-07-28-005-rbac-base.sql @@ -1,12 +1,3 @@ - --- ======================================================== --- RBAC --- -------------------------------------------------------- - -SET SESSION SESSION AUTHORIZATION DEFAULT; - --- https://arctype.com/blog/postgres-uuid/#creating-a-uuid-primary-key-using-uuid-osp-postgresql-example -CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; --liquibase formatted sql --changeset rbac-base-reference:1 endDelimiter:--// diff --git a/src/main/resources/db/changelog/12-rbac-role-builder.sql b/src/main/resources/db/changelog/2022-07-28-020-rbac-role-builder.sql similarity index 81% rename from src/main/resources/db/changelog/12-rbac-role-builder.sql rename to src/main/resources/db/changelog/2022-07-28-020-rbac-role-builder.sql index 410a97e8..48127945 100644 --- a/src/main/resources/db/changelog/12-rbac-role-builder.sql +++ b/src/main/resources/db/changelog/2022-07-28-020-rbac-role-builder.sql @@ -1,12 +1,14 @@ +--liquibase formatted sql +-- ================================================================== +-- PERMISSIONS +--changeset rbac-role-builder-permissions:1 endDelimiter:--// +-- ------------------------------------------------------------------ --- ======================================================== --- Role-Hierarcy helper functions --- -------------------------------------------------------- +/* --- PERMISSIONS -------------------------------------------- + */ --- drop type RbacPermissions; CREATE TYPE RbacPermissions AS ( permissionUuids uuid[] @@ -19,15 +21,18 @@ BEGIN RETURN ROW(createPermissions(forObjectUuid, permitOps))::RbacPermissions; END; $$; --- SUPER ROLES -------------------------------------------- +--// --- drop type RbacSuperRoles; +--changeset rbac-role-builder-super-roles:1 endDelimiter:--// + +/* + + */ CREATE TYPE RbacSuperRoles AS ( roleUuids uuid[] ); --- drop function beneathRoles(roleDescriptors RbacRoleDescriptor[]) CREATE OR REPLACE FUNCTION beneathRoles(roleDescriptors RbacRoleDescriptor[]) RETURNS RbacSuperRoles LANGUAGE plpgsql STRICT AS $$ @@ -42,7 +47,6 @@ BEGIN RETURN ROW(superRoleUuids)::RbacSuperRoles; END; $$; --- drop function beneathRole(roleDescriptor RbacRoleDescriptor) CREATE OR REPLACE FUNCTION beneathRole(roleDescriptor RbacRoleDescriptor) RETURNS RbacSuperRoles LANGUAGE plpgsql STRICT AS $$ @@ -50,7 +54,6 @@ BEGIN RETURN beneathRoles(ARRAY[roleDescriptor]); END; $$; --- drop function beneathRole(roleUuid uuid); CREATE OR REPLACE FUNCTION beneathRole(roleUuid uuid) RETURNS RbacSuperRoles LANGUAGE plpgsql STRICT AS $$ @@ -58,7 +61,6 @@ BEGIN RETURN ROW(ARRAY[roleUuid]::uuid[])::RbacSuperRoles; END; $$; --- drop function asTopLevelRole(roleName varchar); CREATE OR REPLACE FUNCTION asTopLevelRole() RETURNS RbacSuperRoles LANGUAGE plpgsql STRICT AS $$ @@ -66,8 +68,16 @@ BEGIN RETURN ROW(ARRAY[]::uuid[])::RbacSuperRoles; END; $$; --- SUB ROLES ---------------------------------------------- +--// +-- ================================================================= +-- SUB ROLES +--changeset rbac-role-builder-sub-roles:1 endDelimiter:--// +-- ----------------------------------------------------------------- + +/* + + */ CREATE TYPE RbacSubRoles AS ( roleUuids uuid[] @@ -89,15 +99,20 @@ BEGIN RETURN beingItselfA(getRoleId(roleDescriptor, 'fail')); END; $$; --- USERS -------------------------------------------------- +--// --- drop type RbacUsers; +-- ================================================================= +-- USERS +--changeset rbac-role-builder-users:1 endDelimiter:--// +-- ----------------------------------------------------------------- + +/* +*/ CREATE TYPE RbacUsers AS ( userUuids uuid[] ); --- drop function withUsers(userNames varchar); CREATE OR REPLACE FUNCTION withUsers(userNames varchar[]) RETURNS RbacUsers LANGUAGE plpgsql STRICT AS $$ @@ -113,7 +128,6 @@ BEGIN END; $$; --- DROP FUNCTION withUser(userName varchar, whenNotExists RbacWhenNotExists); CREATE OR REPLACE FUNCTION withUser(userName varchar, whenNotExists RbacWhenNotExists = 'fail') RETURNS RbacUsers RETURNS NULL ON NULL INPUT @@ -122,11 +136,15 @@ BEGIN RETURN ROW(ARRAY[getRbacUserId(userName, whenNotExists )]); END; $$; --- ROLE NAME BUILDER -------------------------------------- +--// +-- ================================================================= +-- CREATE ROLE +--changeset rbac-role-builder-create-role:1 endDelimiter:--// +-- ----------------------------------------------------------------- --- CREATE ROLE MAIN FUNCTION ------------------------------ - +/* +*/ CREATE OR REPLACE FUNCTION createRole( roleDescriptor RbacRoleDescriptor, permissions RbacPermissions, @@ -195,4 +213,4 @@ BEGIN RETURN createRole(roleDescriptor, permissions, null, subRoles, users); END; $$; - +--// diff --git a/src/main/resources/db/changelog/18-rbac-statistics.sql b/src/main/resources/db/changelog/2022-07-28-030-rbac-statistics.sql similarity index 58% rename from src/main/resources/db/changelog/18-rbac-statistics.sql rename to src/main/resources/db/changelog/2022-07-28-030-rbac-statistics.sql index 7bb5dac7..94d8a2d3 100644 --- a/src/main/resources/db/changelog/18-rbac-statistics.sql +++ b/src/main/resources/db/changelog/2022-07-28-030-rbac-statistics.sql @@ -1,18 +1,24 @@ +--liquibase formatted sql -DROP VIEW IF EXISTS "RbacStatisticsV"; -CREATE VIEW "RbacStatisticsV" AS - SELECT no, to_char("count", '9 999 999 999') as "count", "table" - FROM ( +--changeset rbac-statistics:1 endDelimiter:--// + +/* + Creates a view which presents some statistics about the RBAC tables. + */ +create view RbacStatisticsView AS + select no, to_char("count", '9 999 999 999') as "count", "table" + from ( select 1 as no, count(*) as "count", 'login users' as "table" from RbacUser - UNION + union select 2 as no, count(*) as "count", 'roles' as "table" from RbacRole - UNION + union select 3 as no, count(*) as "count", 'permissions' as "table" from RbacPermission - UNION + union select 4 as no, count(*) as "count", 'references' as "table" from RbacReference - UNION + union select 5 as no, count(*) as "count", 'grants' as "table" from RbacGrants - UNION + union select 6 as no, count(*) as "count", 'objects' as "table" from RbacObject ) as totals - ORDER BY totals.no; + order by totals.no; +--// diff --git a/src/main/resources/db/changelog/db.changelog-master.yaml b/src/main/resources/db/changelog/db.changelog-master.yaml index 0ba409d2..ae24da75 100644 --- a/src/main/resources/db/changelog/db.changelog-master.yaml +++ b/src/main/resources/db/changelog/db.changelog-master.yaml @@ -9,4 +9,8 @@ databaseChangeLog: file: db/changelog/2022-07-28-004-uuid-ossp-extension.sql - include: file: db/changelog/2022-07-28-005-rbac-base.sql + - include: + file: db/changelog/2022-07-28-020-rbac-role-builder.sql + - include: + file: db/changelog/2022-07-28-030-rbac-statistics.sql