1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-10 06:08:34 +02:00
bitwarden-browser/src/app/organization/organizationCollectionsEditController.js

109 lines
3.7 KiB
JavaScript
Raw Normal View History

angular
.module('bit.organization')
2017-04-27 15:33:12 +02:00
.controller('organizationCollectionsEditController', function ($scope, $state, $uibModalInstance, apiService, cipherService,
2017-05-09 20:06:44 +02:00
$analytics, id, authService) {
2017-04-27 15:33:12 +02:00
$analytics.eventTrack('organizationCollectionsEditController', { category: 'Modal' });
$scope.collection = {};
2017-05-09 20:06:44 +02:00
$scope.groups = [];
$scope.selectedGroups = {};
$scope.loading = true;
$scope.useGroups = false;
$uibModalInstance.opened.then(function () {
2017-05-09 20:06:44 +02:00
return apiService.collections.getDetails({ orgId: $state.params.orgId, id: id }).$promise;
}).then(function (collection) {
$scope.collection = cipherService.decryptCollection(collection);
var groups = {};
if (collection.GroupIds) {
for (var i = 0; i < collection.GroupIds.length; i++) {
groups[collection.GroupIds[i]] = true;
}
}
$scope.selectedGroups = groups;
return authService.getUserProfile();
}).then(function (profile) {
if (profile.organizations) {
var org = profile.organizations[$state.params.orgId];
$scope.useGroups = !!org.useGroups;
}
if ($scope.useGroups) {
return apiService.groups.listOrganization({ orgId: $state.params.orgId }).$promise;
}
return null;
}).then(function (groups) {
if (!groups) {
$scope.loading = false;
return;
}
var groupsArr = [];
for (var i = 0; i < groups.Data.length; i++) {
groupsArr.push({
id: groups.Data[i].Id,
2017-05-10 18:17:26 +02:00
name: groups.Data[i].Name,
accessAll: groups.Data[i].AccessAll
2017-05-09 20:06:44 +02:00
});
}
$scope.groups = groupsArr;
$scope.loading = false;
});
2017-05-09 20:06:44 +02:00
$scope.toggleGroupSelectionAll = function ($event) {
var groups = {};
if ($event.target.checked) {
for (var i = 0; i < $scope.groups.length; i++) {
groups[$scope.groups[i].id] = true;
}
}
$scope.selectedGroups = groups;
};
$scope.toggleGroupSelection = function (id) {
if (id in $scope.selectedGroups) {
delete $scope.selectedGroups[id];
}
else {
$scope.selectedGroups[id] = true;
}
};
$scope.groupSelected = function (group) {
2017-05-10 18:17:26 +02:00
return group.id in $scope.selectedGroups || group.accessAll;
2017-05-09 20:06:44 +02:00
};
$scope.allSelected = function () {
return Object.keys($scope.selectedGroups).length === $scope.groups.length;
};
$scope.submit = function (model) {
2017-04-27 15:33:12 +02:00
var collection = cipherService.encryptCollection(model, $state.params.orgId);
2017-05-09 20:06:44 +02:00
if ($scope.useGroups) {
collection.groupIds = [];
for (var id in $scope.selectedGroups) {
if ($scope.selectedGroups.hasOwnProperty(id)) {
collection.groupIds.push(id);
}
}
}
2017-04-27 15:33:12 +02:00
$scope.submitPromise = apiService.collections.put({ orgId: $state.params.orgId }, collection, function (response) {
$analytics.eventTrack('Edited Collection');
var decCollection = cipherService.decryptCollection(response, $state.params.orgId, true);
$uibModalInstance.close(decCollection);
}).$promise;
};
$scope.close = function () {
$uibModalInstance.dismiss('cancel');
};
});