Refactor acl object and applied comments

This commit is contained in:
Alexandre Cloutier
2014-10-17 11:01:56 -04:00
committed by Francis Lachapelle
parent b4128f97a6
commit bc19f910f6
9 changed files with 259 additions and 125 deletions

View File

@@ -1,50 +1,51 @@
(function() {
'use strict';
function AclUsers(folder) {
this.folder_id = folder.id;
function Acl(folder_id) {
this.folder_id = folder_id;
}
/* The factory we'll use to register with Angular */
AclUsers.factory = ['$q', '$timeout', 'sgSettings', 'sgResource', function($q, $timeout, Settings, Resource) {
angular.extend(AclUsers, {
$q: $q,
$timeout: $timeout,
Acl.factory = ['sgSettings', 'sgResource', function(Settings, Resource) {
angular.extend(Acl, {
$$resource: new Resource(Settings.baseURL)
});
return AclUsers; // return constructor
return Acl; // return constructor
}];
/* Factory registration in Angular module */
angular.module('SOGo.Common').factory('sgAclUsers', AclUsers.factory);
angular.module('SOGo.Common').factory('sgAcl', Acl.factory);
/* Instance methods
* Public method, assigned to prototype
*/
AclUsers.prototype.userRights = function(uid) {
Acl.prototype.$userRights = function(uid) {
var param = {"uid": uid};
return AclUsers.$$resource.fetch(this.folder_id, "userRights", param);
return Acl.$$resource.fetch(this.folder_id, "userRights", param);
};
AclUsers.prototype.addUser = function(uid) {
Acl.prototype.$addUser = function(uid) {
var param = {"uid": uid};
AclUsers.$$resource.fetch(this.folder_id, "addUserInAcls", param);
Acl.$$resource.fetch(this.folder_id, "addUserInAcls", param);
};
AclUsers.prototype.removeUser = function(uid) {
Acl.prototype.$removeUser = function(uid) {
var param = {"uid": uid};
AclUsers.$$resource.fetch(this.folder_id, "removeUserFromAcls", param);
Acl.$$resource.fetch(this.folder_id, "removeUserFromAcls", param);
};
AclUsers.prototype.saveUsersRights = function(dirtyObjects) {
Acl.prototype.$saveUsersRights = function(dirtyObjects) {
var param = {"action": "saveUserRights"};
AclUsers.$$resource.save(this.folder_id, dirtyObjects, param);
Acl.$$resource.save(this.folder_id, dirtyObjects, param);
};
AclUsers.prototype.subscribeUsers = function(uids) {
Acl.prototype.$subscribeUsers = function(uids) {
var param = {"uids": uids};
AclUsers.$$resource.fetch(this.folder_id, "subscribeUsers", param);
Acl.$$resource.fetch(this.folder_id, "subscribeUsers", param);
};
Acl.prototype.$users = function() {
return Acl.$$resource.fetch(this.folder_id, "acls");
};
})();

View File

@@ -117,11 +117,9 @@
* @param {Object} params - Object parameters injected through the $http protocol
*/
Resource.prototype.fetch = function(folder_id, action, params) {
var deferred = this._q.defer();
var folder_id_path = folder_id ? ("/" + folder_id) : "";
var action_path = action ? ("/" + action) : "";
var path = this._path + folder_id_path + action_path;
var deferred = this._q.defer(),
path = [this._path, (folder_id ? folder_id : ""), (action ? action : "")];
path = _.compact(path).join("/");
this._http({
method: 'GET',

View File

@@ -17,6 +17,9 @@
var newAddressBookData = AddressBook.$$resource.create('createFolder', this.name);
this.$unwrap(newAddressBookData);
}
else if (this.id){
this.$acl = new AddressBook.$$acl(this.id);
}
}
else {
// The promise will be unwrapped first
@@ -29,12 +32,13 @@
* @desc The factory we'll use to register with Angular
* @returns the AddressBook constructor
*/
AddressBook.$factory = ['$q', '$timeout', 'sgSettings', 'sgResource', 'sgCard', function($q, $timeout, Settings, Resource, Card) {
AddressBook.$factory = ['$q', '$timeout', 'sgSettings', 'sgResource', 'sgCard', 'sgAcl', function($q, $timeout, Settings, Resource, Card, Acl) {
angular.extend(AddressBook, {
$q: $q,
$timeout: $timeout,
$$resource: new Resource(Settings.baseURL),
$Card: Card
$Card: Card,
$$acl: Acl
});
return AddressBook; // return constructor
@@ -131,6 +135,7 @@
angular.forEach(cards, function(o, i) {
cards[i] = new AddressBook.$Card(o);
});
return cards;
});
});
@@ -198,6 +203,8 @@
angular.forEach(_this.cards, function(o, i) {
_this.cards[i] = new AddressBook.$Card(o);
});
// Instanciate Acl object
_this.$acl = new AddressBook.$$acl(_this.id);
});
}, function(data) {
AddressBook.$timeout(function() {

View File

@@ -138,7 +138,7 @@
i = _.indexOf(_.pluck($scope.addressbooks, 'id'), $rootScope.addressbook.id);
}
$scope.editMode = $rootScope.addressbook.id;
$scope.originalAddressbook = angular.extend({}, $scope.addressbook.$omit());
$scope.originalAddressbook = angular.extend({}, $scope.addressbook.$omit());
focus('addressBookName_' + i);
}
};
@@ -182,44 +182,53 @@
$scope.share = function() {
var modal = $modal.open({
templateUrl: 'addressbookSharing.html',
controller: function($scope, $http, $modalInstance, sgAclUsers, sgUser) {
controller: function($scope, $modalInstance, sgUser) {
/* Variables for the scope */
$scope.AclUsers = new sgAclUsers($rootScope.addressbook);
$scope.User = new sgUser($rootScope.addressbook);
var dirtyObjects = {};
$scope.User.$acls().then(function(users) {
stateAddressbook.$acl.$users().then(function(users) {
$scope.users = [];
angular.forEach(users, function(user){
user.canSubscribeUser = (user.isSubscribed) ? false : true;
user.canSubscribeUser = user.isSubscribed;
$scope.users.push(user);
})
});
$scope.User = new sgUser();
/* Functions */
$scope.closeModal = function() {
$modalInstance.close();
};
$scope.saveModal = function() {
if(!_.isEmpty(dirtyObjects)) {
if(dirtyObjects["anonymous"])
{
Dialog.confirm(l("Warning"), l("Any user with an account on this system will be able to access your folder. Are you certain you trust them all?")).then(function(res){
if(res){
$scope.AclUsers.saveUsersRights(dirtyObjects);
$modalInstance.close();
};
})
if(dirtyObjects["anonymous"]) {
if($scope.validateChanges(dirtyObjects["anonymous"])) {
Dialog.confirm(l("Warning"), l("Potentially anyone on the Internet will be able to access your folder, even if they do not have an account on this system. Is this information suitable for the public Internet?")).then(function(res){
if(res){
stateAddressbook.$acl.$saveUsersRights(dirtyObjects);
$modalInstance.close();
};
})
}
else{
stateAddressbook.$acl.$saveUsersRights(dirtyObjects);
$modalInstance.close();
}
}
else if (dirtyObjects["<default>"]) {
Dialog.confirm(l("Warning"), l("Potentially anyone on the Internet will be able to access your folder, even if they do not have an account on this system. Is this information suitable for the public Internet?")).then(function(res){
if(res){
$scope.AclUsers.saveUsersRights(dirtyObjects);
$modalInstance.close();
};
})
if($scope.validateChanges(dirtyObjects["<default>"])) {
Dialog.confirm(l("Warning"), l("Any user with an account on this system will be able to access your folder. Are you certain you trust them all?")).then(function(res){
if(res){
stateAddressbook.$acl.$saveUsersRights(dirtyObjects);
$modalInstance.close();
};
})
}
else{
stateAddressbook.$acl.$saveUsersRights(dirtyObjects);
$modalInstance.close();
}
}
else {
$scope.AclUsers.saveUsersRights(dirtyObjects);
stateAddressbook.$acl.$saveUsersRights(dirtyObjects);
var usersToSubscribe = [];
angular.forEach(dirtyObjects, function(dirtyObject){
if(dirtyObject.canSubscribeUser && dirtyObject.isSubscribed){
@@ -227,19 +236,25 @@
}
})
if(!_.isEmpty(usersToSubscribe))
$scope.AclUsers.subscribeUsers(usersToSubscribe);
stateAddressbook.$acl.$subscribeUsers(usersToSubscribe);
$modalInstance.close();
}
}
else
$scope.$aclEditorModal.remove();
$modalInstance.close();
};
$scope.validateChanges = function(object) {
if (object.aclOptions.canViewObjects || object.aclOptions.canCreateObjects || object.aclOptions.canEditObjects || object.aclOptions.canEraseObjects)
return true;
else
return false;
};
$scope.removeUser = function() {
if (!_.isEmpty($scope.userSelected)) {
if(dirtyObjects[$scope.userSelected.uid])
delete dirtyObjects[$scope.userSelected.uid];
$scope.AclUsers.removeUser($scope.userSelected.uid);
stateAddressbook.$acl.$removeUser($scope.userSelected.uid);
// Remove from the users list
$scope.users = _.reject($scope.users, function(o) {
return o.uid == $scope.userSelected.uid;
@@ -251,11 +266,11 @@
if (user.uid) {
// Looks through the list and returns the first value that matches all of the key-value pairs listed
if(!_.findWhere($scope.users, {uid: user.uid})) {
$scope.AclUsers.addUser(user.uid);
$scope.User.$acls().then(function(users) {
stateAddressbook.$acl.$addUser(user.uid);
stateAddressbook.$acl.$users().then(function(users) {
$scope.users = [];
angular.forEach(users, function(user){
user.canSubscribeUser = (user.isSubscribed) ? false : true;
user.canSubscribeUser = user.isSubscribed;
$scope.users.push(user);
})
});
@@ -277,7 +292,7 @@
$scope.userSelected.aclOptions = dirtyObjects[$scope.userSelected.uid].aclOptions;
}
else {
$scope.AclUsers.userRights($scope.userSelected.uid).then(function(userRights) {
stateAddressbook.$acl.$userRights($scope.userSelected.uid).then(function(userRights) {
$scope.userSelected.aclOptions = userRights;
});
}

View File

@@ -159,7 +159,7 @@
// };
}])
.controller('AddressBooksCtrl', ['$scope', '$rootScope', '$ionicModal', '$ionicListDelegate', '$ionicActionSheet', 'sgDialog', 'sgAddressBook', 'sgAclUsers', 'sgUser', function($scope, $rootScope, $ionicModal, $ionicListDelegate, $ionicActionSheet, Dialog, AddressBook, sgAclUsers, sgUser) {
.controller('AddressBooksCtrl', ['$scope', '$state', '$rootScope', '$ionicModal', '$ionicListDelegate', '$ionicActionSheet', 'sgDialog', 'sgAddressBook', 'sgUser', function($scope, $state, $rootScope, $ionicModal, $ionicListDelegate, $ionicActionSheet, Dialog, AddressBook, sgUser) {
// Initialize with data from template
$scope.addressbooks = AddressBook.$findAll(contactFolders);
$scope.newAddressbook = function() {
@@ -205,11 +205,9 @@
}
// Variables in scope
$scope.$aclEditorModal = modal;
$scope.addressbook = addressbook;
$scope.AclUsers = new sgAclUsers(addressbook);
$scope.User = new sgUser(addressbook);
$scope.User = new sgUser();
var aclUsers = {};
$scope.User.$acls().then(function(users) {
addressbook.$acl.$users().then(function(users) {
$scope.refreshUsers(users);
});
$scope.showDelete = false;
@@ -238,23 +236,35 @@
if(!_.isEmpty(dirtyObjects)) {
if(dirtyObjects["anonymous"])
{
Dialog.confirm(l("Warning"), l("Any user with an account on this system will be able to access your folder. Are you certain you trust them all?")).then(function(res){
if(res){
$scope.AclUsers.saveUsersRights(dirtyObjects);
$scope.$aclEditorModal.remove();
};
})
if($scope.validateChanges(dirtyObjects["anonymous"])) {
Dialog.confirm(l("Warning"), l("Potentially anyone on the Internet will be able to access your folder, even if they do not have an account on this system. Is this information suitable for the public Internet?")).then(function(res){
if(res){
addressbook.$acl.$saveUsersRights(dirtyObjects);
$scope.$aclEditorModal.remove();
};
})
}
else {
addressbook.$acl.$saveUsersRights(dirtyObjects);
$scope.$aclEditorModal.remove();
}
}
else if (dirtyObjects["<default>"]) {
Dialog.confirm(l("Warning"), l("Potentially anyone on the Internet will be able to access your folder, even if they do not have an account on this system. Is this information suitable for the public Internet?")).then(function(res){
if(res){
$scope.AclUsers.saveUsersRights(dirtyObjects);
$scope.$aclEditorModal.remove();
};
})
if($scope.validateChanges(dirtyObjects["<default>"])) {
Dialog.confirm(l("Warning"), l("Any user with an account on this system will be able to access your folder. Are you certain you trust them all?")).then(function(res){
if(res){
addressbook.$acl.$saveUsersRights(dirtyObjects);
$scope.$aclEditorModal.remove();
};
})
}
else {
addressbook.$acl.$saveUsersRights(dirtyObjects);
$scope.$aclEditorModal.remove();
}
}
else {
$scope.AclUsers.saveUsersRights(dirtyObjects);
addressbook.$acl.$saveUsersRights(dirtyObjects);
var usersToSubscribe = [];
angular.forEach(dirtyObjects, function(dirtyObject){
if(dirtyObject.canSubscribeUser && dirtyObject.isSubscribed){
@@ -262,7 +272,7 @@
}
})
if(!_.isEmpty(usersToSubscribe))
$scope.AclUsers.subscribeUsers(usersToSubscribe);
addressbook.$acl.$subscribeUsers(usersToSubscribe);
$scope.$aclEditorModal.remove();
}
@@ -270,8 +280,14 @@
else
$scope.$aclEditorModal.remove();
};
$scope.validateChanges = function(object) {
if (object.aclOptions.canViewObjects || object.aclOptions.canCreateObjects || object.aclOptions.canEditObjects || object.aclOptions.canEraseObjects)
return true;
else
return false;
};
$scope.cancelSearch = function() {
$scope.User.$acls().then(function(users) {
addressbook.$acl.$users().then(function(users) {
$scope.refreshUsers(users);
});
};
@@ -283,7 +299,7 @@
if(dirtyObjects[user.uid])
delete dirtyObjects[user.uid];
delete aclUsers[user.uid];
$scope.AclUsers.removeUser(user.uid);
addressbook.$acl.$removeUser(user.uid);
// Remove from the users list
$scope.users = _.reject($scope.users, function(o) {
return o.uid == user.uid;
@@ -294,7 +310,7 @@
$scope.addUser = function (user) {
if (user.uid) {
if(!aclUsers[user.uid]) {
$scope.AclUsers.addUser(user.uid);
addressbook.$acl.$addUser(user.uid);
user.inAclList = true;
user.canSubscribeUser = (user.isSubscribed) ? false : true;
aclUsers[user.uid] = user;
@@ -316,7 +332,7 @@
}
else {
// Otherwise, if it's the first time the user consult the user rights; fetch from server
$scope.AclUsers.userRights($scope.userSelected.uid).then(function(userRights) {
addressbook.$acl.$userRights($scope.userSelected.uid).then(function(userRights) {
$scope.userSelected.aclOptions = userRights;
});
}
@@ -328,6 +344,7 @@
return $scope.User.$filter(search).then(function(results) {
angular.forEach(results, function(userFound){
userFound.inAclList = (aclUsers[userFound.uid]) ? true : false;
userFound["displayName"] = userFound.cn + " <" + userFound.c_email + ">";
$scope.users.push(userFound);
})
});