Compare commits

..

No commits in common. "86cf4f6c977f9bc45f7085d849f748c113fe089f" and "c1c67b3c7b7bf3589970634683e98319fa2d81f0" have entirely different histories.

3 changed files with 94 additions and 89 deletions

View File

@ -103,10 +103,9 @@ public class HsOfficeRelationshipEntity implements HasUuid, Stringifyable {
.createSubRole(ADMIN, (with) -> {
with.permission(EDIT);
})
.createSubRole(AGENT, (with) -> {
with.incomingSuperRole("holderPerson", ADMIN);
})
.createSubRole(AGENT)
.createSubRole(TENANT, (with) -> {
with.incomingSuperRole("anchorPerson", ADMIN);
with.incomingSuperRole("holderPerson", ADMIN);
with.incomingSuperRole("contact", ADMIN);
with.outgoingSubRole("anchorPerson", REFERRER);

View File

@ -14,14 +14,15 @@ public class RbacViewPostgresGenerator {
private final RbacView rbacDef;
private final String liqibaseTagPrefix;
private final StringWriter plPgSql = new StringWriter();
private final StringBuilder plPgSql = new StringBuilder();
public RbacViewPostgresGenerator(final RbacView forRbacDef) {
rbacDef = forRbacDef;
liqibaseTagPrefix = rbacDef.getRootEntityAlias().entityClass().getSimpleName();
plPgSql.writeLn("""
plPgSql.append("""
--liquibase formatted sql
-- generated at: %{timestamp}
"""
.replace("%{timestamp}", LocalDateTime.now().toString()));

View File

@ -26,33 +26,34 @@ class RolesGrantsAndPermissionsGenerator {
RolesGrantsAndPermissionsGenerator(final RbacView rbacDef, final String liquibaseTagPrefix) {
this.rbacDef = rbacDef;
this.rbacGrants.addAll(rbacDef.getGrantDefs());
this.rbacGrants.addAll(rbacGrants);
this.liquibaseTagPrefix = liquibaseTagPrefix;
entityClass = rbacDef.getRootEntityAlias().entityClass();
simpleEntityVarName = rbacDef.getRootEntityAlias().simpleName();
simpleEntityName = capitalize(simpleEntityVarName);
simpleEntityName = entityClass.getSimpleName();
simpleEntityVarName = uncapitalize(simpleEntityName);
rawTableName = withoutRvSuffix(entityClass.getAnnotation(Table.class).name());
}
void generateTo(final StringWriter plPgSql) {
void generateTo(final StringBuilder plPgSql) {
generateHeader(plPgSql);
generateTriggerFunction(plPgSql);
generageInsertTrigger(plPgSql);
generateFooter(plPgSql);
}
private void generateHeader(final StringWriter plPgSql) {
plPgSql.writeLn("""
private void generateHeader(final StringBuilder plPgSql) {
plPgSql.append("""
-- ============================================================================
--changeset %{liquibaseTagPrefix}-rbac-CREATE-ROLES-GRANTS-PERMISSIONS:1 endDelimiter:--//
-- ----------------------------------------------------------------------------
"""
.replace("%{liquibaseTagPrefix}", liquibaseTagPrefix));
}
private void generateTriggerFunction(final StringWriter plPgSql) {
plPgSql.writeLn("""
private void generateTriggerFunction(final StringBuilder plPgSql) {
plPgSql.append("""
/*
Creates the roles, grants and permission for the AFTER INSERT TRIGGER.
*/
@ -65,30 +66,27 @@ class RolesGrantsAndPermissionsGenerator {
if TG_OP <> 'INSERT' then
raise exception 'invalid usage of TRIGGER AFTER INSERT function';
end if;
%{createRolesWithGrantsSql}
return NEW;
end; $$;
"""
.replace("%{simpleEntityName}", simpleEntityName));
.replace("%{simpleEntityName}", simpleEntityName)
.replace("%{createRolesWithGrantsSql}", createRolesWithGrantsSql())
);
plPgSql.indented(() -> {
}
private String createRolesWithGrantsSql() {
final var plPgSql = new StringBuilder();
createRolesWithGrantsSql(plPgSql, OWNER);
createRolesWithGrantsSql(plPgSql, ADMIN);
createRolesWithGrantsSql(plPgSql, AGENT);
createRolesWithGrantsSql(plPgSql, TENANT);
createRolesWithGrantsSql(plPgSql, REFERRER);
if (!rbacGrants.isEmpty()) {
throw new IllegalStateException("unprocessed grants: " + rbacGrants);
// rbacGrants.forEach(g -> plPgSql.writeLn("-- unprocessed grant: " + g));
return plPgSql.toString();
}
plPgSql.writeLn("return NEW;");
});
plPgSql.writeLn("end; $$;");
plPgSql.writeLn();
}
private void createRolesWithGrantsSql(final StringWriter plPgSql, final RbacView.Role role) {
private void createRolesWithGrantsSql(final StringBuilder plPgSql, final RbacView.Role role) {
final var isToCreate = rbacDef.getRoleDefs().stream()
.filter(roleDef -> rbacDef.isRootEntityAlias(roleDef.getEntityAlias()) && roleDef.getRole() == role )
@ -97,11 +95,12 @@ class RolesGrantsAndPermissionsGenerator {
return;
}
plPgSql.writeLn();
plPgSql.writeLn("perform createRoleWithGrants(");
plPgSql.indented( () -> {
plPgSql.writeLn("%{simpleVarName)%{roleSuffix}(NEW),"
.replace("%{simpleVarName)", simpleEntityVarName)
plPgSql.append("""
perform createRoleWithGrants(
%{simpleEntityVarName)%{roleSuffix}(NEW),
"""
.replace("%{simpleEntityVarName)", simpleEntityVarName)
.replace("%{roleSuffix}", capitalize(role.roleName())));
final var permissionGrantsForRole = findPermissionsGrantsForRole(rbacDef.getRootEntityAlias(), role);
@ -112,8 +111,7 @@ class RolesGrantsAndPermissionsGenerator {
.map(RbacView.Permission::permission)
.map(p -> "'" + p + "'")
.collect(joining(", "));
plPgSql.indented( () ->
plPgSql.writeLn("permissions => array[" + permissionsForRoleInPlPgSql + "],\n"));
plPgSql.append(indent(3) + "permissions => array[" + permissionsForRoleInPlPgSql + "],\n");
rbacGrants.removeAll(permissionGrantsForRole);
}
@ -123,8 +121,7 @@ class RolesGrantsAndPermissionsGenerator {
.map(RbacView.RbacGrantDefinition::getUserDef)
.map(this::toPlPgSqlReference)
.collect(joining(", "));
plPgSql.indented(() ->
plPgSql.writeLn("userUuids => array[" + grantsToUsersPlPgSql + "],\n"));
plPgSql.append(indent(3) + "userUuids => array[" + grantsToUsersPlPgSql + "],\n");
rbacGrants.removeAll(grantsToUsers);
}
@ -134,8 +131,7 @@ class RolesGrantsAndPermissionsGenerator {
.map(RbacView.RbacGrantDefinition::getSuperRoleDef)
.map(r -> toPlPgSqlReference(NEW, r))
.collect(joining(", "));
plPgSql.indented(() ->
plPgSql.writeLn("incomingSuperRoles => array[" + incomingGrantsInPlPgSql + "],\n"));
plPgSql.append(indent(3) + "incomingSuperRoles => array[" + incomingGrantsInPlPgSql + "],\n");
rbacGrants.removeAll(incomingGrants);
}
@ -145,16 +141,16 @@ class RolesGrantsAndPermissionsGenerator {
.map(RbacView.RbacGrantDefinition::getSuperRoleDef)
.map(r -> toPlPgSqlReference(NEW, r))
.collect(joining(", "));
plPgSql.indented(() ->
plPgSql.writeLn("outgoingSubRoles => array[" + outgoingGrantsInPlPgSql + "],\n"));
plPgSql.append(indent(3) + "outgoingSubRoles => array[" + outgoingGrantsInPlPgSql + "],\n");
rbacGrants.removeAll(outgoingGrants);
}
plPgSql.chopTail(",\n");
plPgSql.writeLn();
});
if (!rbacGrants.isEmpty()) {
throw new IllegalStateException("unprocessed grants: " + rbacGrants);
}
plPgSql.writeLn(");");
chopTail(plPgSql, ",\n");
plPgSql.append("\n" + indent(2) + ");\n");
}
private Set<RbacView.RbacGrantDefinition> findPermissionsGrantsForRole(final RbacView.EntityAlias entityAlias, final RbacView.Role role) {
@ -186,8 +182,8 @@ class RolesGrantsAndPermissionsGenerator {
.collect(Collectors.toSet());
}
private void generageInsertTrigger(final StringWriter plPgSql) {
plPgSql.writeLn("""
private void generageInsertTrigger(final StringBuilder plPgSql) {
plPgSql.append("""
/*
An AFTER INSERT TRIGGER which creates the role structure for a new %{simpleEntityName}
*/
@ -204,9 +200,8 @@ class RolesGrantsAndPermissionsGenerator {
);
}
private static void generateFooter(final StringWriter plPgSql) {
plPgSql.writeLn();
plPgSql.writeLn();
private static void generateFooter(final StringBuilder plPgSql) {
plPgSql.append("\n\n");
}
private String withoutRvSuffix(final String tableName) {
@ -234,4 +229,14 @@ class RolesGrantsAndPermissionsGenerator {
private static String toTriggerReference(final PostgresTriggerReference triggerRef, final RbacView.EntityAlias entityAlias) {
return triggerRef.name().toLowerCase() + capitalize(entityAlias.aliasName());
}
private String indent(final int tabs) {
return " ".repeat(4*tabs);
}
private void chopTail(final StringBuilder plPgSql, final String tail) {
if (plPgSql.toString().endsWith(tail)) {
plPgSql.setLength(plPgSql.length() - tail.length());
}
}
}