140 lines
3.9 KiB
JavaScript
140 lines
3.9 KiB
JavaScript
/**
|
|
* this model contains information about the organization for
|
|
* the current user and the users who are inside the organizacion.
|
|
*
|
|
* Attributes:
|
|
* - users: collection with user instances whithin the organization (see cdb.admin.Organization.Users
|
|
*/
|
|
cdb.admin.Organization = cdb.core.Model.extend({
|
|
|
|
url: '/api/v1/org/',
|
|
|
|
initialize: function(attrs, opts) {
|
|
attrs = attrs || {}
|
|
this.owner = new cdb.admin.User(this.get('owner'));
|
|
|
|
this.display_email = (typeof attrs.admin_email != 'undefined') && attrs.admin_email != null && (attrs.admin_email == '' ? this.owner.email : attrs.admin_email);
|
|
|
|
var collectionOpts = {
|
|
organization: this,
|
|
currentUserId: opts && opts.currentUserId
|
|
};
|
|
this.users = new cdb.admin.Organization.Users(attrs.users, collectionOpts);
|
|
this.groups = new cdb.admin.OrganizationGroups(attrs.groups, collectionOpts);
|
|
this.grantables = new cdb.admin.Grantables(undefined, collectionOpts);
|
|
|
|
// make sure all the users/groups have a reference to this organization
|
|
this.users.each(this._setOrganizationOnModel, this);
|
|
this.groups.each(this._setOrganizationOnModel, this);
|
|
},
|
|
|
|
_setOrganizationOnModel: function(m) {
|
|
m.organization = this;
|
|
},
|
|
|
|
fetch: function() {
|
|
throw new Error("organization should not be fetch, should be static");
|
|
},
|
|
|
|
containsUser: function(user) {
|
|
return !!this.users.find(function(u) {
|
|
return u.id === user.id;
|
|
})
|
|
},
|
|
|
|
isOrgAdmin: function (user) {
|
|
return this.owner.id === user.id || !!_.find(this.get('admins'), function (u) {
|
|
return u.id === user.id;
|
|
});
|
|
},
|
|
|
|
viewUrl: function() {
|
|
return new cdb.common.OrganizationUrl({
|
|
base_url: this.get('base_url')
|
|
})
|
|
}
|
|
|
|
});
|
|
|
|
// helper to manage organization users
|
|
cdb.admin.Organization.Users = Backbone.Collection.extend({
|
|
|
|
model: cdb.admin.User,
|
|
_DEFAULT_EXCLUDE_CURRENT_USER: true,
|
|
|
|
url: function() {
|
|
return '/api/v1/organization/' + this.organization.id + '/users';
|
|
},
|
|
|
|
initialize: function(models, opts) {
|
|
if (!opts.organization) {
|
|
throw new Error('Organization is needed for fetching organization users');
|
|
}
|
|
this.elder('initialize');
|
|
this.organization = opts.organization;
|
|
|
|
this.currentUserId = opts.currentUserId;
|
|
this._excludeCurrentUser = this._DEFAULT_EXCLUDE_CURRENT_USER;
|
|
|
|
// Let's add abort behaviour
|
|
this.sync = Backbone.syncAbort;
|
|
},
|
|
|
|
comparator: function(mdl) {
|
|
return mdl.get('username');
|
|
},
|
|
|
|
excludeCurrentUser: function(exclude) {
|
|
exclude = !!exclude;
|
|
this._excludeCurrentUser = exclude;
|
|
if (exclude && !this.currentUserId) {
|
|
cdb.log.error('set excludeCurrentUser to true, but there is no current user id set to exclude!');
|
|
}
|
|
},
|
|
|
|
restoreExcludeCurrentUser: function() {
|
|
this.excludeCurrentUser(this._DEFAULT_EXCLUDE_CURRENT_USER);
|
|
},
|
|
|
|
parse: function(r) {
|
|
this.total_entries = r.total_entries;
|
|
this.total_user_entries = r.total_user_entries;
|
|
|
|
return _.reduce(r.users, function(memo, user) {
|
|
if (this._excludeCurrentUser && user.id === this.currentUserId) {
|
|
this.total_user_entries--;
|
|
this.total_entries--;
|
|
} else {
|
|
memo.push(user);
|
|
}
|
|
return memo;
|
|
}, [], this);
|
|
},
|
|
|
|
// @return {Number, undefined} may be undefined until a first fetch is done
|
|
totalCount: function() {
|
|
return this.total_user_entries;
|
|
}
|
|
});
|
|
|
|
cdb.admin.Organization.Invites = cdb.core.Model.extend({
|
|
|
|
defaults: {
|
|
users_emails: []
|
|
},
|
|
|
|
url: function() {
|
|
return '/api/v1/organization/' + this.organizationId + '/invitations';
|
|
},
|
|
|
|
initialize: function(attrs, opts) {
|
|
if (!opts.organizationId) {
|
|
throw new Error('Organization id is needed for fetching organization users');
|
|
} else {
|
|
this.organizationId = opts.organizationId;
|
|
this.set('welcome_text', 'I\'d like to invite you to my ' + cdb.config.get('app_name') + ' organization,\nBest regards');
|
|
}
|
|
}
|
|
|
|
});
|