cartodb/lib/assets/javascripts/builder/data/acl-item-model.js

35 lines
896 B
JavaScript
Raw Normal View History

2020-06-15 10:58:47 +08:00
var Backbone = require('backbone');
var _ = require('underscore');
var PermissionModel = require('./permission-model');
var OWN_ATTR_NAMES = ['id', 'username', 'avatar_url', 'name'];
module.exports = Backbone.Model.extend({
defaults: {
access: 'r'
},
isOwn: function (model) {
return model.id === this.get('entity').id;
},
validate: function (attrs, options) {
if (attrs.access !== PermissionModel.READ_ONLY && attrs.access !== PermissionModel.READ_WRITE) {
return "access can't take 'r' or 'rw' values";
}
},
toJSON: function () {
var entity = _.pick(this.get('entity').toJSON(), OWN_ATTR_NAMES);
// translate name to username
if (!entity.username) {
entity.username = entity.name;
delete entity.name;
}
return {
type: this.get('type') || 'user',
entity: entity,
access: this.get('access')
};
}
});