d2-ui
Version:
209 lines (178 loc) • 8.52 kB
JavaScript
Object.defineProperty(exports, '__esModule', {
value: true
});
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
var _UserAuthorities = require('./UserAuthorities');
var _UserAuthorities2 = _interopRequireDefault(_UserAuthorities);
var _UserSettings = require('./UserSettings');
var _UserSettings2 = _interopRequireDefault(_UserSettings);
var _defaultConfig = require('../defaultConfig');
var models = Symbol('models');
var propertiesToIgnore = new Set(['userCredentials', 'userGroups', 'userRoles', 'organisationUnits', 'dataViewOrganisationUnits']);
var authTypes = {
READ: ['READ'],
CREATE: ['CREATE', 'CREATE_PUBLIC', 'CREATE_PRIVATE'],
CREATE_PUBLIC: ['CREATE_PUBLIC'],
CREATE_PRIVATE: ['CREATE_PRIVATE'],
DELETE: ['DELETE'],
UPDATE: ['UPDATE'],
EXTERNALIZE: ['EXTERNALIZE']
};
var propertySymbols = Array.from(propertiesToIgnore).reduce(function (result, property) {
result[property] = Symbol(property); // eslint-disable-line no-param-reassign
return result;
}, {});
function getUserPropertiesToCopy(currentUserObject) {
var properties = undefined;
// 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(function (result, property) {
if (propertiesToIgnore.has(property)) {
if (properties[property].map) {
result[propertySymbols[property]] = properties[property] // eslint-disable-line no-param-reassign
.map(function (value) {
return value.id;
});
}
} else {
result[property] = properties[property]; // eslint-disable-line no-param-reassign
}
return result;
}, {});
}
function isInNoCreateAllowedForList(modelDefinition) {
return modelDefinition && _defaultConfig.noCreateAllowedFor.has(modelDefinition.name);
}
var CurrentUser = (function () {
function CurrentUser(userData, userAuthorities, modelDefinitions, settings) {
_classCallCheck(this, CurrentUser);
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;
}
_createClass(CurrentUser, [{
key: 'getUserGroups',
value: function getUserGroups() {
var userGroupIds = this[propertySymbols.userGroups];
return this[models].userGroup.get({ filter: ['id:in:[' + userGroupIds.join(',') + ']'] });
}
}, {
key: 'getUserRoles',
value: function getUserRoles() {
var userRoleIds = this[propertySymbols.userRoles];
return this[models].userRole.get({ filter: ['id:in:[' + userRoleIds.join(',') + ']'] });
}
}, {
key: 'getOrganisationUnits',
value: function getOrganisationUnits() {
var listOptions = arguments.length <= 0 || arguments[0] === undefined ? { fields: ':all,displayName,children[id,displayName,path,children::isNotEmpty]' } : arguments[0];
var organisationUnitsIds = this[propertySymbols.organisationUnits];
return this[models].organisationUnit.list(Object.assign({}, listOptions, { filter: ['id:in:[' + organisationUnitsIds.join(',') + ']'] }));
}
}, {
key: 'getDataViewOrganisationUnits',
value: function getDataViewOrganisationUnits() {
var listOptions = arguments.length <= 0 || arguments[0] === undefined ? { fields: ':all,displayName,children[id,displayName,path,children::isNotEmpty]' } : arguments[0];
var organisationUnitsIds = this[propertySymbols.dataViewOrganisationUnits];
return this[models].organisationUnit.list(Object.assign({}, listOptions, { filter: ['id:in:[' + organisationUnitsIds.join(',') + ']'] }));
}
}, {
key: 'checkAuthorityForType',
value: function checkAuthorityForType(authorityType, modelType) {
var _this = this;
if (!modelType || !Array.isArray(modelType.authorities)) {
return false;
}
return modelType.authorities
// Filter the correct authority to check for from the model
.filter(function (authority) {
return authorityType.some(function (authToHave) {
return authToHave === authority.type;
});
})
// Check the left over schema authority types
.some(function (schemaAuthority) {
return schemaAuthority.authorities.some(function (authorityToCheckFor) {
return _this.authorities.has(authorityToCheckFor);
});
} // Check if one of the schema authorities are available in the users authorities
);
}
}, {
key: 'checkCreateAuthorityForType',
value: function checkCreateAuthorityForType(authType, modelType) {
// When the modelType is mentioned in the the list of modelTypes that are not
// allowed to be created we return false
if (isInNoCreateAllowedForList(modelType)) {
return false;
}
// Otherwise we check using the normal procedure for checking authorities
return this.checkAuthorityForType(authType, modelType);
}
}, {
key: 'canCreate',
value: function canCreate(modelType) {
return this.checkCreateAuthorityForType(authTypes.CREATE, modelType);
}
}, {
key: 'canCreatePublic',
value: function canCreatePublic(modelType) {
return this.checkCreateAuthorityForType(authTypes.CREATE_PUBLIC, modelType);
}
}, {
key: 'canCreatePrivate',
value: function canCreatePrivate(modelType) {
return this.checkCreateAuthorityForType(authTypes.CREATE_PRIVATE, modelType);
}
}, {
key: 'canDelete',
value: function canDelete(modelType) {
return this.checkAuthorityForType(authTypes.DELETE, modelType);
}
}, {
key: 'canUpdate',
value: function canUpdate(modelType) {
if (this.checkAuthorityForType(authTypes.UPDATE, modelType)) {
return true;
}
return this.checkAuthorityForType(authTypes.CREATE, modelType);
}
}, {
key: 'uiLocale',
get: function get() {
if (this.userSettings && this.userSettings.keyUiLocale) {
return this.userSettings.keyUiLocale;
}
return 'en';
}
}], [{
key: 'create',
value: function create(userData, authorities, modelDefinitions, userSettings) {
return new CurrentUser(userData, _UserAuthorities2['default'].create(authorities), modelDefinitions, new _UserSettings2['default'](userSettings));
}
}]);
return CurrentUser;
})();
exports['default'] = CurrentUser;
module.exports = exports['default'];
//# sourceMappingURL=CurrentUser.js.map
;