Skip to content

Commit 0e44983

Browse files
committed
feat(policy-workbench): hydrate persisted group policies on open
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 8ad90d1 commit 0e44983

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

src/views/Settings/PolicyWorkbench/useRealPolicyWorkbench.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ export function createRealPolicyWorkbenchState() {
153153
const groups = ref<PolicyTargetOption[]>([])
154154
const users = ref<PolicyTargetOption[]>([])
155155
const loadingTargets = ref(false)
156+
const hydrateGroupRulesRequestId = ref(0)
156157

157158
const visibleSettingSummaries = computed<PolicySettingSummary[]>(() => {
158159
return Object.values(realDefinitions).map((definition) => {
@@ -251,8 +252,49 @@ export function createRealPolicyWorkbenchState() {
251252
return true
252253
})
253254

255+
async function hydratePersistedGroupRules(policyKey: string) {
256+
const currentRequestId = hydrateGroupRulesRequestId.value + 1
257+
hydrateGroupRulesRequestId.value = currentRequestId
258+
259+
await loadTargets('group')
260+
261+
const hydratedRules: PolicyRuleRecord[] = []
262+
for (const group of groups.value) {
263+
try {
264+
const persistedPolicy = await policiesStore.fetchGroupPolicy(group.id, policyKey)
265+
if (!persistedPolicy || persistedPolicy.value === null || persistedPolicy.value === undefined) {
266+
continue
267+
}
268+
269+
hydratedRules.push({
270+
id: `group-${group.id}-persisted`,
271+
scope: 'group',
272+
targetId: group.id,
273+
allowChildOverride: persistedPolicy.allowChildOverride,
274+
value: persistedPolicy.value,
275+
})
276+
} catch (error) {
277+
logger.debug('Could not load persisted group policy for target', {
278+
error,
279+
policyKey,
280+
groupId: group.id,
281+
})
282+
}
283+
}
284+
285+
if (currentRequestId !== hydrateGroupRulesRequestId.value || activeSettingKey.value !== policyKey) {
286+
return
287+
}
288+
289+
groupRules.value = hydratedRules
290+
nextRuleNumber.value = hydratedRules.length + 1
291+
}
292+
254293
function openSetting(key: string) {
255294
activeSettingKey.value = key
295+
groupRules.value = []
296+
userRules.value = []
297+
void hydratePersistedGroupRules(key)
256298
}
257299

258300
function mergeSelectedTargets(scope: 'group' | 'user', fetchedTargets: PolicyTargetOption[]) {
@@ -363,6 +405,9 @@ export function createRealPolicyWorkbenchState() {
363405
return
364406
}
365407

408+
// Cancel any in-flight hydration result to avoid stale overwrite while editing.
409+
hydrateGroupRulesRequestId.value += 1
410+
366411
const isEdit = !!ruleId
367412
editorMode.value = isEdit ? 'edit' : 'create'
368413
duplicateMessage.value = null

0 commit comments

Comments
 (0)