-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathaclUtils.ts
More file actions
107 lines (97 loc) · 3.27 KB
/
aclUtils.ts
File metadata and controls
107 lines (97 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* This File contains functions that are needed in several access right views*/
import { FormikProps } from "formik";
import { TransformedAcl } from "../slices/aclDetailsSlice";
import { checkAcls, fetchAclTemplateById, Role } from "../slices/aclSlice";
import { UserInfoState } from "../slices/userInfoSlice";
import { fetchUsersForTemplate } from "../slices/userSlice";
import { AppDispatch } from "../store";
export const getAclTemplateText = (
aclTemplates: {
id: string
value: string
}[],
formikTemplate: string,
) => {
if (!!aclTemplates && aclTemplates.length > 0) {
const template = aclTemplates.find(
template => formikTemplate === template.id,
);
return template ? template.value : "";
} else {
return "";
}
};
// Get all policies that have user information, or all policies that do not have user information
export const policiesFiltered = (
policies: TransformedAcl[],
byUser: boolean,
) => {
if (byUser) {
return policies.filter(policy => policy.user !== undefined);
} else {
return policies.filter(policy => policy.user === undefined);
}
};
// Get all roles that have user information, or all policies that do not have user information
export const rolesFiltered = (
roles: Role[],
byUser: boolean,
) => {
if (byUser) {
return roles.filter(role => role.user !== undefined);
} else {
return roles.filter(role => role.user === undefined);
}
};
/* fetches the policies for the chosen template and sets the policies in the formik form to those policies */
export const handleTemplateChange = async <T extends { policies: TransformedAcl[], aclTemplate: string }>(
templateId: string,
formik: FormikProps<T>,
dispatch: AppDispatch,
aclDefaults: { [key: string]: string } | undefined,
defaultUser?: UserInfoState,
) => {
// fetch information about chosen template from backend
const template = await fetchAclTemplateById(templateId);
// fetch user info
const users = await fetchUsersForTemplate(template.acl.map(role => role.role));
// Add user info to applicable roles
template.acl = template.acl.map(acl => {
if (users && users[acl.role]) {
acl.user = {
username: users[acl.role].username,
name: users[acl.role].name,
email: users[acl.role].email,
};
}
return acl;
});
// always add current user to acl since template could lock the user out
if (defaultUser) {
template.acl = template.acl.concat({
role: defaultUser.userRole,
read: true,
write: true,
actions: aclDefaults && aclDefaults["default_actions"] ? aclDefaults["default_actions"].split(",") : [],
user: {
username: defaultUser.user.username,
name: defaultUser.user.name,
email: defaultUser.user.email,
},
});
}
// If configured, keep roles that match the configured prefix
if (aclDefaults && aclDefaults["keep_on_template_switch_role_prefixes"]) {
const prefixString = aclDefaults["keep_on_template_switch_role_prefixes"];
const prefixes = prefixString.split(",");
for (const policy of formik.values.policies) {
if (prefixes.some(prefix => policy.role.startsWith(prefix)) && !template.acl.find(acl => acl.role === policy.role)) {
template.acl.push(policy);
}
}
}
formik.setFieldValue("policies", template.acl);
formik.setFieldValue("aclTemplate", templateId);
// Is this necessary?
dispatch(checkAcls(formik.values.policies));
};