Skip to content

Commit df331ea

Browse files
committed
feat: add more restrictions
1 parent 31664ec commit df331ea

4 files changed

Lines changed: 21 additions & 0 deletions

migrations/tenants/003_20250711_213213_create_permissions.down.surql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ REMOVE FUNCTION IF EXISTS fn::has_permission;
99
REMOVE EVENT IF EXISTS prevent_deletion_of_main_realm ON TABLE realm;
1010
REMOVE EVENT IF EXISTS prevent_deletion_of_internal_definitions ON TABLE definition;
1111
REMOVE EVENT IF EXISTS parent_definition_must_be_internal ON TABLE definition;
12+
REMOVE EVENT IF EXISTS prevent_deletion_of_definitions_in_use ON TABLE definition;
1213
REMOVE TABLE IF EXISTS realm;
1314
REMOVE TABLE IF EXISTS definition;
1415
REMOVE TABLE IF EXISTS role;

migrations/tenants/003_20250711_213213_create_permissions.surql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ DEFINE FIELD _internal ON definition TYPE bool DEFAULT false;
4242
DEFINE FIELD title ON definition TYPE string;
4343
DEFINE FIELD description ON definition TYPE option<string>;
4444
DEFINE FIELD versions ON definition TYPE array<object> DEFAULT [];
45+
DEFINE FIELD show_in_ui ON definition TYPE bool DEFAULT true;
4546
DEFINE FIELD updated_at ON definition TYPE datetime VALUE time::now();
4647
DEFINE FIELD created_at ON definition TYPE datetime VALUE time::now() READONLY;
4748

@@ -72,6 +73,17 @@ WHEN $event = "CREATE" THEN {
7273
} END;
7374
};
7475

76+
DEFINE EVENT prevent_deletion_of_definitions_in_use ON TABLE definition
77+
WHEN $event = "DELETE" THEN {
78+
LET $relevant_tables = ["group"];
79+
FOR $table_name IN $relevant_tables {
80+
LET $records = SELECT VALUE count() FROM ONLY type::table($table_name) WHERE _definition = record::id($value.id);
81+
IF $records > 0 THEN {
82+
THROW "Definition is still in use";
83+
} END;
84+
};
85+
};
86+
7587
--------------------------------------------------------------------------------
7688

7789
DEFINE TABLE role SCHEMALESS;

migrations/tenants/004_20250714_175230_create_groups.down.surql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ BEGIN TRANSACTION;
77
REMOVE TABLE IF EXISTS group;
88
REMOVE TABLE IF EXISTS in_realm;
99
REMOVE TABLE IF EXISTS member_of;
10+
REMOVE EVENT IF EXISTS group_definition_must_exist ON TABLE group;
1011

1112
--------------------------------------------------------------------------------
1213

migrations/tenants/004_20250714_175230_create_groups.surql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ DEFINE FIELD description ON group TYPE option<string>;
1212
DEFINE FIELD updated_at ON group TYPE datetime VALUE time::now();
1313
DEFINE FIELD created_at ON group TYPE datetime VALUE time::now() READONLY;
1414

15+
DEFINE EVENT group_definition_must_exist ON TABLE group WHEN $event = "CREATE" THEN {
16+
LET $definition = type::thing("definition", $value._definition);
17+
IF not(record::exists($definition)) THEN {
18+
THROW "Group definition does not exist";
19+
} END;
20+
};
21+
1522
--------------------------------------------------------------------------------
1623

1724
DEFINE TABLE member_of TYPE RELATION FROM person TO group ENFORCED;

0 commit comments

Comments
 (0)