Skip to content

Commit c4328a6

Browse files
authored
Refactor global perm query to handle deletion (#4597)
1 parent 8f65ae4 commit c4328a6

File tree

1 file changed

+69
-15
lines changed

1 file changed

+69
-15
lines changed

backend/infrahub/core/account.py

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,25 +69,79 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None:
6969
WITH account, r1 as r
7070
WHERE r.status = "active"
7171
WITH account
72-
MATCH group_path = (account)-[]->(:Relationship {name: "group_member"})
73-
<-[]-(:%(group_node)s)
74-
-[]->(:Relationship {name: "role__accountgroups"})
75-
<-[]-(:%(account_role_node)s)
76-
-[]->(:Relationship {name: "role__permissions"})
77-
<-[]-(global_permission:%(global_permission_node)s)
78-
-[:HAS_ATTRIBUTE]->(:Attribute {name: "name"})
79-
-[:HAS_VALUE]->(global_permission_name:AttributeValue)
80-
WITH global_permission, global_permission_name
81-
WHERE all(r IN relationships(group_path) WHERE (%(branch_filter)s) AND r.status = "active")
82-
MATCH action_path = (global_permission)-[:HAS_ATTRIBUTE]->(:Attribute {name: "action"})-[:HAS_VALUE]->(global_permission_action:AttributeValue)
83-
WHERE all(r IN relationships(action_path) WHERE (%(branch_filter)s) AND r.status = "active")
84-
MATCH decision_path = (global_permission)-[:HAS_ATTRIBUTE]->(:Attribute {name: "decision"})-[:HAS_VALUE]->(global_permission_decision:AttributeValue)
85-
WHERE all(r IN relationships(decision_path) WHERE (%(branch_filter)s) AND r.status = "active")
72+
CALL {
73+
WITH account
74+
MATCH (account)-[r1:IS_RELATED]->(:Relationship {name: "group_member"})<-[r2:IS_RELATED]-(account_group:%(account_group_node)s)
75+
WHERE all(r IN [r1, r2] WHERE (%(branch_filter)s))
76+
WITH account_group, r1, r2, (r1.status = "active" AND r2.status = "active") AS is_active
77+
ORDER BY account_group.uuid, r2.branch_level DESC, r2.from DESC, r1.branch_level DESC, r1.from DESC
78+
WITH account_group, head(collect(is_active)) as latest_is_active
79+
WHERE latest_is_active = TRUE
80+
RETURN account_group
81+
}
82+
WITH account_group
83+
84+
CALL {
85+
WITH account_group
86+
MATCH (account_group)-[r1:IS_RELATED]->(:Relationship {name: "role__accountgroups"})<-[r2:IS_RELATED]-(account_role:%(account_role_node)s)
87+
WHERE all(r IN [r1, r2] WHERE (%(branch_filter)s))
88+
WITH account_role, r1, r2, (r1.status = "active" AND r2.status = "active") AS is_active
89+
ORDER BY account_role.uuid, r2.branch_level DESC, r2.from DESC, r1.branch_level DESC, r1.from DESC
90+
WITH account_role, head(collect(is_active)) as latest_is_active
91+
WHERE latest_is_active = TRUE
92+
RETURN account_role
93+
}
94+
WITH account_role
95+
96+
CALL {
97+
WITH account_role
98+
MATCH (account_role)-[r1:IS_RELATED]->(:Relationship {name: "role__permissions"})<-[r2:IS_RELATED]-(global_permission:%(global_permission_node)s)
99+
WHERE all(r IN [r1, r2] WHERE (%(branch_filter)s))
100+
WITH global_permission, r1, r2, (r1.status = "active" AND r2.status = "active") AS is_active
101+
ORDER BY global_permission.uuid, r2.branch_level DESC, r2.from DESC, r1.branch_level DESC, r1.from DESC
102+
WITH global_permission, head(collect(is_active)) as latest_is_active
103+
WHERE latest_is_active = TRUE
104+
RETURN global_permission
105+
}
106+
WITH global_permission
107+
108+
CALL {
109+
WITH global_permission
110+
MATCH (global_permission)-[r1:HAS_ATTRIBUTE]->(:Attribute {name: "name"})-[r2:HAS_VALUE]->(global_permission_name:AttributeValue)
111+
WHERE all(r IN [r1, r2] WHERE (%(branch_filter)s))
112+
RETURN global_permission_name, (r1.status = "active" AND r2.status = "active") AS is_active
113+
ORDER BY r2.branch_level DESC, r2.from DESC, r1.branch_level DESC, r1.from DESC
114+
LIMIT 1
115+
}
116+
WITH global_permission, global_permission_name, is_active AS gpn_is_active
117+
WHERE gpn_is_active = TRUE
118+
119+
CALL {
120+
WITH global_permission
121+
MATCH (global_permission)-[r1:HAS_ATTRIBUTE]->(:Attribute {name: "action"})-[r2:HAS_VALUE]->(global_permission_action:AttributeValue)
122+
WHERE all(r IN [r1, r2] WHERE (%(branch_filter)s))
123+
RETURN global_permission_action, (r1.status = "active" AND r2.status = "active") AS is_active
124+
ORDER BY r2.branch_level DESC, r2.from DESC, r1.branch_level DESC, r1.from DESC
125+
LIMIT 1
126+
}
127+
WITH global_permission, global_permission_name, global_permission_action, is_active AS gpa_is_active
128+
WHERE gpa_is_active = TRUE
129+
130+
CALL {
131+
WITH global_permission
132+
MATCH (global_permission)-[r1:HAS_ATTRIBUTE]->(:Attribute {name: "decision"})-[r2:HAS_VALUE]->(global_permission_decision:AttributeValue)
133+
WHERE all(r IN [r1, r2] WHERE (%(branch_filter)s))
134+
RETURN global_permission_decision, (r1.status = "active" AND r2.status = "active") AS is_active
135+
ORDER BY r2.branch_level DESC, r2.from DESC, r1.branch_level DESC, r1.from DESC
136+
LIMIT 1
137+
}
138+
WITH global_permission, global_permission_name, global_permission_action, global_permission_decision, is_active AS gpd_is_active
139+
WHERE gpd_is_active = TRUE
86140
""" % {
87141
"branch_filter": branch_filter,
88142
"generic_account_node": InfrahubKind.GENERICACCOUNT,
143+
"account_group_node": InfrahubKind.ACCOUNTGROUP,
89144
"account_role_node": InfrahubKind.ACCOUNTROLE,
90-
"group_node": InfrahubKind.ACCOUNTGROUP,
91145
"global_permission_node": InfrahubKind.GLOBALPERMISSION,
92146
}
93147

0 commit comments

Comments
 (0)