UNPKG

identity

Version:

Identity client

57 lines (41 loc) 1.4 kB
module.exports = function(){ this.attribute('id', Number, {primary: true}); this.attribute('name', String); this.attribute('description', String); this.hasMany('permissions', {as: 'owner'}); //dont send client_id and secret... this.actions.index.params = {}; this.actions.show.params = {}; this.hasRole = function(role){ for(var i = 0; i < this.permissions.length; i++){ if(this.permissions[i].role_id == role) return true; } return false; }; this.getValues = function(entity_id, 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 && this.permissions[i].role_id == role)){ 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; }; }