OpenSlides/openslides/users/static/js/users/base.js
Emanuel Schuetze 9af302ce36 Fix 'operator' error in projector view.
Move operator factory from users/site.js to users/base.js.
The Motion factory uses 'operator' in motions/base.js which is also
loaded on projector. So operator is required in users/base.js.
2015-11-23 22:06:54 +01:00

133 lines
4.3 KiB
JavaScript

(function () {
'use strict';
angular.module('OpenSlidesApp.users', [])
.factory('operator', [
'User',
'Group',
'loadGlobalData',
function(User, Group, loadGlobalData) {
var operatorChangeCallbacks = [];
var operator = {
user: null,
perms: [],
isAuthenticated: function () {
return !!this.user;
},
onOperatorChange: function (func) {
operatorChangeCallbacks.push(func);
},
setUser: function(user_id) {
if (user_id) {
User.find(user_id).then(function(user) {
operator.user = user;
// TODO: load only the needed groups
Group.findAll().then(function() {
operator.perms = user.getPerms();
_.forEach(operatorChangeCallbacks, function (callback) {
callback();
});
});
});
} else {
operator.user = null;
Group.find(1).then(function(group) {
operator.perms = group.permissions;
_.forEach(operatorChangeCallbacks, function (callback) {
callback();
});
});
}
},
// Returns true if the operator has at least one perm of the perms-list.
hasPerms: function(perms) {
if (typeof perms == 'string') {
perms = perms.split(' ');
}
return _.intersection(perms, operator.perms).length > 0;
},
};
return operator;
}
])
.factory('User', ['DS', 'Group', 'jsDataModel', function(DS, Group, jsDataModel) {
var name = 'users/user';
return DS.defineResource({
name: name,
useClass: jsDataModel,
computed: {
full_name: function () {
return this.get_full_name();
},
short_name: function () {
return this.get_short_name();
},
},
methods: {
getResourceName: function () {
return name;
},
get_short_name: function() {
// should be the same as in the python user model.
var firstName = _.trim(this.first_name),
lastName = _.trim(this.last_name),
name;
if (firstName && lastName) {
// TODO: check config
name = [firstName, lastName].join(' ');
} else {
name = firstName || lastName || this.username;
}
return name;
},
get_full_name: function() {
// should be the same as in the python user model.
var firstName = _.trim(this.first_name),
lastName = _.trim(this.last_name),
structure_level = _.trim(this.structure_level),
name;
if (firstName && lastName) {
// TODO: check config
name = [firstName, lastName].join(' ');
} else {
name = firstName || lastName || this.username;
}
if (structure_level) {
name = name + " (" + structure_level + ")";
}
return name;
},
getPerms: function() {
var allPerms = [];
var allGroups = this.groups;
// Add registered group
allGroups.push(2);
_.forEach(allGroups, function(groupId) {
var group = Group.get(groupId);
if (group) {
_.forEach(group.permissions, function(perm) {
allPerms.push(perm);
});
}
});
return _.uniq(allPerms);
},
},
});
}])
.factory('Group', ['DS', function(DS) {
return DS.defineResource({
name: 'users/group',
});
}])
.run(['User', 'Group', function(User, Group) {}]);
}());