identity
Version:
Identity client
119 lines (83 loc) • 3.36 kB
JavaScript
module.exports = function(){
this.attribute('id', Number, {primary: true});
this.attribute('first_name', String);
this.attribute('last_name', String);
this.attribute('external_id', String);
this.attribute('email', String);
this.attribute('active', Boolean);
this.attribute('properties', Object);
this.hasMany('permissions', {as: 'owner'});
this.getter('name', function(){
return this.first_name + ' ' + this.last_name;
});
//dont send client_id and secret...
this.actions.index.params = {};
this.actions.show.params = {};
this.scope('getMe', function(){
if(this.context && this.context.session){
var access_token = this.context.session.access_token;
this.limit(1);
this.temporaryDefinition().beforeFind(function(options){
options.path = '/api/user';
options.params.access_token = access_token;
});
}
});
this.hasRole = function(role){
if(role === 'admin' && this.definition.store.config.simplifyAdminRole === true) role = ['admin', 'application_admin'];
if(!(role instanceof Array)) role = [role]
for(var i = 0; i < this.permissions.length; i++){
if(role.indexOf(this.permissions[i].role) !== -1) return true;
}
return false;
};
this.getValues = function(entity_id, role){
if(role === 'admin' && this.definition.store.config.simplifyAdminRole === true) role = ['admin', 'application_admin'];
if(role && !(role instanceof Array)) role = [role]
if(!entity_id) return false;
entity_id = entity_id.toString();
var tmp = [];
for(var i = 0; i < this.permissions.length; i++){
if(!role || (role.length > 0 && role.indexOf(this.permissions[i].role) !== -1)){
if(this.permissions[i].values && this.permissions[i].values[entity_id]){
tmp = tmp.concat(this.permissions[i].values[entity_id]);
}
}
}
return tmp.filter(function (e, i, arr) {
return arr.lastIndexOf(e) === i;
});
};
this.hasValue = function(entity_id, id, role){
if(!entity_id) return false;
if(!id) return false;
entity_id = entity_id.toString();
id = id.toString();
var tmp = this.getValues(entity_id, role);
for(var i = 0; i < tmp.length; i++){
if(tmp[i].toString() === id.toString()) return true;
}
return false;
};
this.fromGroup = function(entity_id, id, role){
if(role === 'admin' && this.definition.store.config.simplifyAdminRole === true) role = ['admin', 'application_admin'];
if(role && !(role instanceof Array)) role = [role]
if(!entity_id) return null;
if(!id) return null;
if(!role) return null;
entity_id = entity_id.toString();
id = id.toString();
var tmp = [];
for(var i = 0; i < this.permissions.length; i++){
if(role.indexOf(this.permissions[i].role) !== -1 && this.permissions[i].group_id){
if(this.permissions[i].values && this.permissions[i].values[entity_id]){
var values = this.permissions[i].values[entity_id];
for(var x = 0; x < values.length; x++){
if(values[x].toString() === id.toString()) return {name: this.permissions[i].group_name, id:this.permissions[i].group_id};
}
}
}
}
return null;
}
}