split trigger function from the procedure which actually generates the groles and grants

This commit is contained in:
Michael Hoennig 2024-02-28 14:54:58 +01:00
parent dff9803dc3
commit fef6e1c01c

View File

@ -40,7 +40,7 @@ class RolesGrantsAndPermissionsGenerator {
void generateTo(final StringWriter plPgSql) { void generateTo(final StringWriter plPgSql) {
generateHeader(plPgSql); generateHeader(plPgSql);
generateTriggerFunction(plPgSql); generateTriggerFunction(plPgSql);
generageInsertTrigger(plPgSql); generateInsertTrigger(plPgSql);
generateFooter(plPgSql); generateFooter(plPgSql);
} }
@ -56,16 +56,19 @@ class RolesGrantsAndPermissionsGenerator {
private void generateTriggerFunction(final StringWriter plPgSql) { private void generateTriggerFunction(final StringWriter plPgSql) {
plPgSql.writeLn(""" plPgSql.writeLn("""
/* /*
Creates the roles, grants and permission for the AFTER INSERT TRIGGER. A Creates the roles, grants and permission for the AFTER INSERT TRIGGER.
*/ */
create or replace function createRbacRolesFor${simpleEntityName}() create or replace procedure createRbacRolesFor${simpleEntityName}(
returns trigger TG_OP text,
language plpgsql OLD ${rawTableName},
strict as $$ NEW ${rawTableName}
)
language plpgsql as $$
declare declare
""" """
.replace("${simpleEntityName}", simpleEntityName)); .replace("${simpleEntityName}", simpleEntityName)
.replace("${rawTableName}", rawTableName));
plPgSql.indented(() -> { plPgSql.indented(() -> {
rbacDef.getEntityAliases().values().stream() rbacDef.getEntityAliases().values().stream()
@ -101,8 +104,6 @@ class RolesGrantsAndPermissionsGenerator {
generateGrants(plPgSql, ROLE_TO_USER); generateGrants(plPgSql, ROLE_TO_USER);
generateGrants(plPgSql, ROLE_TO_ROLE); generateGrants(plPgSql, ROLE_TO_ROLE);
generateGrants(plPgSql, PERM_TO_ROLE); generateGrants(plPgSql, PERM_TO_ROLE);
plPgSql.writeLn("return NEW;");
}); });
plPgSql.writeLn("end; $$;"); plPgSql.writeLn("end; $$;");
@ -280,17 +281,26 @@ class RolesGrantsAndPermissionsGenerator {
.collect(toSet()); .collect(toSet());
} }
private void generageInsertTrigger(final StringWriter plPgSql) { private void generateInsertTrigger(final StringWriter plPgSql) {
plPgSql.writeLn(""" plPgSql.writeLn("""
/* /*
An AFTER INSERT TRIGGER which creates the role structure for a new ${simpleEntityName} An AFTER INSERT TRIGGER which creates the role structure for a new ${simpleEntityName}
*/ */
create trigger createRbacRolesFor${simpleEntityName}_Trigger create or replace function createRbacRolesFor${simpleEntityName}_tf()
returns trigger
language plpgsql
strict as $$
begin
call createRbacRolesFor${simpleEntityName}(TG_OP, OLD, NEW);
return NEW;
end; $$;
create trigger createRbacRolesFor${simpleEntityName}_tg
after insert after insert
on ${rawTableName} on ${rawTableName}
for each row for each row
execute procedure createRbacRolesFor${simpleEntityName}(); execute procedure createRbacRolesFor${simpleEntityName}_tf();
--// --//
""" """
.replace("${simpleEntityName}", simpleEntityName) .replace("${simpleEntityName}", simpleEntityName)