2014-10-08 19:44:37 +02:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
2014-11-03 18:19:35 +01:00
|
|
|
/**
|
|
|
|
* @name Acl
|
|
|
|
* @constructor
|
|
|
|
* @param {String} folderId - the folder ID associated to the ACLs
|
|
|
|
*/
|
|
|
|
function Acl(folderId) {
|
|
|
|
this.folderId = folderId;
|
2014-10-08 19:44:37 +02:00
|
|
|
}
|
|
|
|
|
2014-11-03 18:19:35 +01:00
|
|
|
/**
|
|
|
|
* @memberof Acl
|
|
|
|
* @desc The factory we'll use to register with Angular.
|
|
|
|
* @return the Acl constructor
|
|
|
|
*/
|
2015-05-06 04:06:13 +02:00
|
|
|
Acl.factory = ['$q', '$timeout', 'sgSettings', 'Resource', 'User', function($q, $timeout, Settings, Resource, User) {
|
2014-10-17 17:01:56 +02:00
|
|
|
angular.extend(Acl, {
|
2014-11-03 18:19:35 +01:00
|
|
|
$q: $q,
|
|
|
|
$timeout: $timeout,
|
2015-08-03 17:53:54 +02:00
|
|
|
$$resource: new Resource(Settings.activeUser('folderURL'), Settings.activeUser()),
|
2014-11-03 18:19:35 +01:00
|
|
|
$User: User
|
2014-10-08 19:44:37 +02:00
|
|
|
});
|
|
|
|
|
2014-11-03 18:19:35 +01:00
|
|
|
return Acl;
|
2014-10-08 19:44:37 +02:00
|
|
|
}];
|
|
|
|
|
2014-11-03 18:19:35 +01:00
|
|
|
/**
|
|
|
|
* @module SOGo.Common
|
|
|
|
* @desc Factory registration of User in Angular module.
|
|
|
|
*/
|
2015-05-06 04:06:13 +02:00
|
|
|
angular.module('SOGo.Common').factory('Acl', Acl.factory);
|
2014-10-08 19:44:37 +02:00
|
|
|
|
2014-11-03 18:19:35 +01:00
|
|
|
/**
|
|
|
|
* @function $users
|
|
|
|
* @memberof Acl.prototype
|
2015-10-06 22:38:16 +02:00
|
|
|
* @param {Object} owner - the owner to use when fetching the ACL as it might not be the Settings.activeUser
|
2014-11-03 18:19:35 +01:00
|
|
|
* @desc Fetch the list of users that have specific rights for the current folder.
|
|
|
|
* @return a promise of an array of User objects
|
2014-10-08 19:44:37 +02:00
|
|
|
*/
|
2015-10-06 22:38:16 +02:00
|
|
|
Acl.prototype.$users = function(owner) {
|
2014-11-03 18:19:35 +01:00
|
|
|
var _this = this,
|
|
|
|
deferred = Acl.$q.defer(),
|
|
|
|
user;
|
|
|
|
if (this.users) {
|
|
|
|
deferred.resolve(this.users);
|
|
|
|
}
|
|
|
|
else {
|
2015-10-06 22:38:16 +02:00
|
|
|
var acls;
|
|
|
|
if (angular.isDefined(owner))
|
|
|
|
acls = Acl.$$resource.userResource(owner).fetch(this.folderId, 'acls');
|
|
|
|
else
|
|
|
|
acls = Acl.$$resource.fetch(this.folderId, 'acls');
|
|
|
|
|
|
|
|
return acls.then(function(response) {
|
2014-11-03 18:19:35 +01:00
|
|
|
_this.users = [];
|
2015-08-12 15:43:55 +02:00
|
|
|
//console.debug(JSON.stringify(users, undefined, 2));
|
2015-01-13 17:30:23 +01:00
|
|
|
angular.forEach(response.users, function(data) {
|
2014-11-03 18:19:35 +01:00
|
|
|
user = new Acl.$User(data);
|
|
|
|
user.canSubscribeUser = user.isSubscribed;
|
|
|
|
user.wasSubscribed = user.isSubscribed;
|
2015-10-06 22:38:16 +02:00
|
|
|
user.$rights = angular.bind(user, user.$acl, _this.folderId, owner);
|
2014-11-03 18:19:35 +01:00
|
|
|
_this.users.push(user);
|
|
|
|
});
|
|
|
|
deferred.resolve(_this.users);
|
|
|
|
return _this.users;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return deferred.promise;
|
2014-10-08 19:44:37 +02:00
|
|
|
};
|
|
|
|
|
2014-11-03 18:19:35 +01:00
|
|
|
/**
|
|
|
|
* @function $addUser
|
|
|
|
* @memberof Acl.prototype
|
|
|
|
* @param {Object} user - a User object with minimal set of attributes (uid, isGroup, cn, c_email)
|
2015-10-06 22:38:16 +02:00
|
|
|
* @param {Object} owner - the owner to use when fetching the ACL as it might not be the Settings.activeUser
|
2014-11-03 18:19:35 +01:00
|
|
|
* @see {@link User.$filter}
|
|
|
|
*/
|
2015-10-06 22:38:16 +02:00
|
|
|
Acl.prototype.$addUser = function(user, owner) {
|
2014-11-03 18:19:35 +01:00
|
|
|
var _this = this,
|
|
|
|
deferred = Acl.$q.defer(),
|
|
|
|
param = {uid: user.uid};
|
2016-03-03 19:38:54 +01:00
|
|
|
if (!user.uid || _.indexOf(_.map(this.users, 'uid'), user.uid) > -1) {
|
2014-11-03 18:19:35 +01:00
|
|
|
// No UID specified or user already in ACLs
|
|
|
|
deferred.resolve();
|
|
|
|
}
|
|
|
|
else {
|
2015-10-06 22:38:16 +02:00
|
|
|
var acls;
|
|
|
|
|
|
|
|
if (angular.isDefined(owner))
|
|
|
|
acls = Acl.$$resource.userResource(owner).fetch(this.folderId, 'addUserInAcls', param);
|
|
|
|
else
|
|
|
|
acls = Acl.$$resource.fetch(this.folderId, 'addUserInAcls', param);
|
|
|
|
|
|
|
|
acls.then(function() {
|
2014-11-03 18:19:35 +01:00
|
|
|
user.wasSubscribed = false;
|
2015-10-09 22:03:05 +02:00
|
|
|
user.userClass = user.isGroup ? 'normal-group' : 'normal-user';
|
2016-06-20 21:54:54 +02:00
|
|
|
user.$rights = angular.bind(user, user.$acl, _this.folderId, owner);
|
2014-11-03 18:19:35 +01:00
|
|
|
_this.users.push(user);
|
2015-10-27 16:28:42 +01:00
|
|
|
deferred.resolve(user);
|
2014-11-03 18:19:35 +01:00
|
|
|
}, function(data, status) {
|
2019-02-27 15:51:43 +01:00
|
|
|
deferred.reject(l('An error occured, please try again.'));
|
2014-11-03 18:19:35 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return deferred.promise;
|
2014-10-08 19:44:37 +02:00
|
|
|
};
|
|
|
|
|
2014-11-03 18:19:35 +01:00
|
|
|
/**
|
|
|
|
* @function $removeUser
|
|
|
|
* @memberof Acl.prototype
|
|
|
|
* @desc Remove a user from the folder's ACL
|
|
|
|
* @return a promise of the server call to remove the user from the folder's ACL
|
|
|
|
*/
|
2016-06-03 15:00:34 +02:00
|
|
|
Acl.prototype.$removeUser = function(uid, owner) {
|
2014-11-03 18:19:35 +01:00
|
|
|
var _this = this,
|
2016-06-03 15:00:34 +02:00
|
|
|
param = {uid: uid},
|
|
|
|
acls;
|
|
|
|
|
|
|
|
if (angular.isDefined(owner))
|
|
|
|
acls = Acl.$$resource.userResource(owner).fetch(this.folderId, 'removeUserFromAcls', param);
|
|
|
|
else
|
|
|
|
acls = Acl.$$resource.fetch(this.folderId, 'removeUserFromAcls', param);
|
|
|
|
|
|
|
|
return acls.then(function() {
|
2016-03-03 19:38:54 +01:00
|
|
|
var i = _.indexOf(_.map(_this.users, 'uid'), uid);
|
2014-11-03 18:19:35 +01:00
|
|
|
if (i >= 0) {
|
2016-09-23 22:43:48 +02:00
|
|
|
_this.users[i].$shadowRights = null;
|
2014-11-03 18:19:35 +01:00
|
|
|
_this.users.splice(i, 1);
|
|
|
|
}
|
|
|
|
});
|
2014-10-08 19:44:37 +02:00
|
|
|
};
|
|
|
|
|
2016-06-20 15:10:50 +02:00
|
|
|
/**
|
|
|
|
* @function $selectAllRights
|
|
|
|
* @memberof Acl.prototype
|
|
|
|
* @desc Select all rights of an user
|
|
|
|
*/
|
|
|
|
Acl.prototype.$selectAllRights = function(user) {
|
|
|
|
_.forEach(user.rights, function(value, right) {
|
2016-06-20 22:08:41 +02:00
|
|
|
if (angular.isNumber(user.rights[right]))
|
|
|
|
user.rights[right] = 1;
|
2017-09-21 19:59:55 +02:00
|
|
|
else
|
|
|
|
user.rights[right] = "Modifier";
|
2016-06-20 15:10:50 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-08-28 18:34:27 +02:00
|
|
|
/**
|
|
|
|
* @function $hasNoRight
|
|
|
|
* @memberof Acl.prototype
|
|
|
|
* @desc Check if user has any rights on the resource
|
|
|
|
* @return true if user has no right at all
|
|
|
|
*/
|
|
|
|
Acl.prototype.$hasNoRight = function(user) {
|
|
|
|
var o = _.find(user.rights, function(value, right) {
|
|
|
|
if (angular.isNumber(value))
|
|
|
|
return (value === 1);
|
|
|
|
else
|
|
|
|
return (value !== 'None');
|
|
|
|
});
|
|
|
|
return _.isUndefined(o);
|
|
|
|
};
|
|
|
|
|
2014-11-03 18:19:35 +01:00
|
|
|
/**
|
|
|
|
* @function $resetUsersRights
|
|
|
|
* @memberof Acl.prototype
|
|
|
|
* @desc Restore initial rights of all users.
|
|
|
|
*/
|
|
|
|
Acl.prototype.$resetUsersRights = function() {
|
|
|
|
angular.forEach(this.users, function(user) {
|
|
|
|
user.$resetRights();
|
|
|
|
});
|
2014-10-08 19:44:37 +02:00
|
|
|
};
|
|
|
|
|
2014-11-03 18:19:35 +01:00
|
|
|
/**
|
|
|
|
* @function $saveUsersRights
|
|
|
|
* @memberof Acl.prototype
|
|
|
|
* @desc Save user rights that have changed and subscribe users that have been selected.
|
2015-10-06 22:38:16 +02:00
|
|
|
* @param {Object} owner - the owner to use when fetching the ACL as it might not be the Settings.activeUser
|
2014-11-03 18:19:35 +01:00
|
|
|
* @return a promise that resolved only if the modifications and subscriptions were successful
|
|
|
|
*/
|
2015-10-06 22:38:16 +02:00
|
|
|
Acl.prototype.$saveUsersRights = function(owner) {
|
2014-11-03 18:19:35 +01:00
|
|
|
var _this = this,
|
|
|
|
deferredSave = Acl.$q.defer(),
|
|
|
|
deferredSubscribe = Acl.$q.defer(),
|
|
|
|
param = {action: 'saveUserRights'},
|
|
|
|
users = [];
|
2014-10-08 19:44:37 +02:00
|
|
|
|
2014-11-03 18:19:35 +01:00
|
|
|
// Save user rights
|
|
|
|
angular.forEach(this.users, function(user) {
|
|
|
|
if (user.$rightsAreDirty()) {
|
|
|
|
users.push(user.$omit());
|
|
|
|
// console.debug('save ' + JSON.stringify(user.$omit(), undefined, 2));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (users.length) {
|
2015-10-06 22:38:16 +02:00
|
|
|
var acls;
|
|
|
|
|
|
|
|
if (angular.isDefined(owner))
|
|
|
|
acls = Acl.$$resource.userResource(owner).save(this.folderId, users, param);
|
|
|
|
else
|
|
|
|
acls = Acl.$$resource.save(this.folderId, users, param);
|
|
|
|
|
|
|
|
acls.then(function() {
|
2014-11-03 18:19:35 +01:00
|
|
|
// Save was successful; copy rights to shadow rights
|
|
|
|
angular.forEach(_this.users, function(user) {
|
|
|
|
if (user.$rightsAreDirty()) {
|
|
|
|
user.$shadowRights = angular.copy(user.rights);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
deferredSave.resolve();
|
|
|
|
}, deferredSave.reject);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
deferredSave.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subscribe users
|
|
|
|
users = [];
|
|
|
|
angular.forEach(this.users, function(user) {
|
|
|
|
if (!user.wasSubscribed && user.isSubscribed) {
|
|
|
|
users.push(user.uid);
|
|
|
|
// console.debug('subscribe ' + user.uid);
|
2015-07-24 22:14:53 +02:00
|
|
|
}
|
2014-11-03 18:19:35 +01:00
|
|
|
});
|
|
|
|
if (users.length) {
|
|
|
|
param = {uids: users.join(',')};
|
|
|
|
Acl.$$resource.fetch(this.folderId, 'subscribeUsers', param)
|
|
|
|
.then(function() {
|
|
|
|
// Subscribe was successful; reset "wasSubscribed" attribute
|
|
|
|
angular.forEach(_this.users, function(user) {
|
|
|
|
user.wasSubscribed = user.isSubscribed;
|
|
|
|
});
|
|
|
|
deferredSubscribe.resolve();
|
|
|
|
}, deferredSubscribe.reject);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
deferredSubscribe.resolve();
|
|
|
|
}
|
|
|
|
return Acl.$q.all([deferredSave.promise, deferredSubscribe.promise]);
|
2014-10-17 17:01:56 +02:00
|
|
|
};
|
2014-11-03 18:19:35 +01:00
|
|
|
|
|
|
|
})();
|