-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathmfa-login-enforcement.js
45 lines (42 loc) · 1.56 KB
/
mfa-login-enforcement.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
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import ApplicationSerializer from './application';
export default class MfaLoginEnforcementSerializer extends ApplicationSerializer {
primaryKey = 'name';
// Return data with updated keys for hasMany relationships with ids in the name
transformHasManyKeys(data, destination) {
const keys = {
model: ['mfa_methods', 'identity_entities', 'identity_groups'],
server: ['mfa_method_ids', 'identity_entity_ids', 'identity_group_ids'],
};
keys[destination].forEach((newKey, index) => {
const oldKey = destination === 'model' ? keys.server[index] : keys.model[index];
delete Object.assign(data, { [newKey]: data[oldKey] })[oldKey];
});
return data;
}
normalize(model, data) {
this.transformHasManyKeys(data, 'model');
return super.normalize(model, data);
}
normalizeItems(payload) {
if (payload.data) {
if (payload.data?.keys && Array.isArray(payload.data.keys)) {
return payload.data.keys.map((key) => payload.data.key_info[key]);
}
Object.assign(payload, payload.data);
delete payload.data;
}
return payload;
}
serialize() {
const json = super.serialize(...arguments);
// empty arrays are being removed from serialized json
// ensure that they are sent to the server, otherwise removing items will not be persisted
json.auth_method_accessors = json.auth_method_accessors || [];
json.auth_method_types = json.auth_method_types || [];
return this.transformHasManyKeys(json, 'server');
}
}