/
CurrentUser.js
152 lines (127 loc) · 5.03 KB
/
CurrentUser.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import UserAuthorities from './UserAuthorities';
import UserSettings from './UserSettings';
const models = Symbol('models');
const propertiesToIgnore = new Set([
'userCredentials',
'userGroups',
'userRoles',
'organisationUnits',
'dataViewOrganisationUnits',
]);
const authTypes = {
READ: ['READ'],
CREATE: ['CREATE', 'CREATE_PUBLIC', 'CREATE_PRIVATE'],
CREATE_PUBLIC: ['CREATE_PUBLIC'],
CREATE_PRIVATE: ['CREATE_PRIVATE'],
DELETE: ['DELETE'],
UPDATE: ['UPDATE'],
EXTERNALIZE: ['EXTERNALIZE'],
};
const propertySymbols = Array
.from(propertiesToIgnore)
.reduce((result, property) => {
result[property] = Symbol(property); // eslint-disable-line no-param-reassign
return result;
}, {});
function getUserPropertiesToCopy(currentUserObject) {
let properties;
// The user credentials object is confusing so we set the properties straight onto the current user
if (currentUserObject.userCredentials) {
properties = Object.assign({}, currentUserObject.userCredentials, currentUserObject);
} else {
properties = Object.assign({}, currentUserObject);
}
return Object.keys(properties)
.reduce((result, property) => {
if (propertiesToIgnore.has(property)) {
if (properties[property].map) {
result[propertySymbols[property]] = properties[property] // eslint-disable-line no-param-reassign
.map(value => value.id);
}
} else {
result[property] = properties[property]; // eslint-disable-line no-param-reassign
}
return result;
}, {});
}
export default class CurrentUser {
constructor(userData, userAuthorities, modelDefinitions, settings) {
Object.assign(this, getUserPropertiesToCopy(userData));
this.authorities = userAuthorities;
this[models] = modelDefinitions;
/**
* @property {UserSettings} settings Contains a reference to a `UserSettings` instance that can be used
* to retrieve and save system settings.
*
* @description
* ```js
* d2.currentUser.userSettings.get('keyUiLocale')
* .then(userSettingsValue => {
* console.log('UI Locale: ' + userSettingsValue);
* });
* ```
*/
this.userSettings = settings;
}
getUserGroups() {
const userGroupIds = this[propertySymbols.userGroups];
return this[models].userGroup.get({ filter: [`id:in:[${userGroupIds.join(',')}]`] });
}
getUserRoles() {
const userRoleIds = this[propertySymbols.userRoles];
return this[models].userRole.get({ filter: [`id:in:[${userRoleIds.join(',')}]`] });
}
getOrganisationUnits() {
const organisationUnitsIds = this[propertySymbols.organisationUnits];
return this[models].organisationUnit.list({
fields: ':owner,displayName,children[id,displayName]',
filter: [`id:in:[${organisationUnitsIds.join(',')}]`],
});
}
getDataViewOrganisationUnits() {
const organisationUnitsIds = this[propertySymbols.dataViewOrganisationUnits];
return this[models].organisationUnit.list({
fields: ':owner,displayName,children[id,displayName]',
filter: [`id:in:[${organisationUnitsIds.join(',')}]`],
});
}
checkAuthorityForType(authorityType, modelType) {
if (!modelType || !Array.isArray(modelType.authorities)) {
return false;
}
return modelType.authorities
// Filter the correct authority to check for from the model
.filter(authority => authorityType.some(authToHave => authToHave === authority.type))
// Check the left over schema authority types
.some(schemaAuthority => schemaAuthority.authorities
.some(authorityToCheckFor => this.authorities.has(authorityToCheckFor)) // Check if one of the schema authorities are available in the users authorities
);
}
canCreate(modelType) {
return this.checkAuthorityForType(authTypes.CREATE, modelType);
}
canCreatePublic(modelType) {
return this.checkAuthorityForType(authTypes.CREATE_PUBLIC, modelType);
}
canCreatePrivate(modelType) {
return this.checkAuthorityForType(authTypes.CREATE_PRIVATE, modelType);
}
canDelete(modelType) {
return this.checkAuthorityForType(authTypes.DELETE, modelType);
}
canUpdate(modelType) {
if (this.checkAuthorityForType(authTypes.UPDATE, modelType)) {
return true;
}
return this.checkAuthorityForType(authTypes.CREATE, modelType);
}
get uiLocale() {
if (this.userSettings && this.userSettings.keyUiLocale) {
return this.userSettings.keyUiLocale;
}
return 'en';
}
static create(userData, authorities, modelDefinitions) {
return new CurrentUser(userData, UserAuthorities.create(authorities), modelDefinitions, new UserSettings());
}
}