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

112 lines
3.9 KiB
JavaScript
Raw Normal View History

2017-04-04 23:21:47 +02:00
angular
.module('bit.vault')
2017-04-27 15:33:12 +02:00
.controller('vaultLoginCollectionsController', function ($scope, apiService, $uibModalInstance, cipherService,
2017-04-04 23:21:47 +02:00
loginId, $analytics) {
2017-04-27 15:33:12 +02:00
$analytics.eventTrack('vaultLoginCollectionsController', { category: 'Modal' });
2017-04-04 23:21:47 +02:00
$scope.login = {};
$scope.readOnly = false;
$scope.loadingLogin = true;
2017-04-27 15:33:12 +02:00
$scope.loadingCollections = true;
$scope.selectedCollections = {};
$scope.collections = [];
2017-04-04 23:21:47 +02:00
$uibModalInstance.opened.then(function () {
apiService.ciphers.getFullDetails({ id: loginId }).$promise.then(function (cipher) {
$scope.loadingLogin = false;
$scope.readOnly = !cipher.Edit;
if (cipher.Edit && cipher.OrganizationId) {
if (cipher.Type === 1) {
$scope.login = cipherService.decryptLoginPreview(cipher);
}
2017-04-27 15:33:12 +02:00
var collections = {};
if (cipher.CollectionIds) {
for (var i = 0; i < cipher.CollectionIds.length; i++) {
collections[cipher.CollectionIds[i]] = true;
2017-04-04 23:21:47 +02:00
}
}
2017-04-27 15:33:12 +02:00
$scope.selectedCollections = collections;
2017-04-04 23:21:47 +02:00
return cipher;
}
2017-04-12 17:11:01 +02:00
return null;
2017-04-04 23:21:47 +02:00
}).then(function (cipher) {
2017-04-12 17:11:01 +02:00
if (!cipher) {
2017-04-27 15:33:12 +02:00
$scope.loadingCollections = false;
2017-04-04 23:21:47 +02:00
return;
}
2017-04-27 15:33:12 +02:00
apiService.collections.listMe(function (response) {
var collections = [];
2017-04-04 23:21:47 +02:00
for (var i = 0; i < response.Data.length; i++) {
if (response.Data[i].OrganizationId !== cipher.OrganizationId) {
continue;
}
2017-04-27 15:33:12 +02:00
var decCollection = cipherService.decryptCollection(response.Data[i]);
collections.push(decCollection);
2017-04-04 23:21:47 +02:00
}
2017-04-27 15:33:12 +02:00
$scope.loadingCollections = false;
$scope.collections = collections;
2017-04-04 23:21:47 +02:00
});
});
});
2017-04-27 15:33:12 +02:00
$scope.toggleCollectionSelectionAll = function ($event) {
var collections = {};
2017-04-04 23:21:47 +02:00
if ($event.target.checked) {
2017-04-27 15:33:12 +02:00
for (var i = 0; i < $scope.collections.length; i++) {
collections[$scope.collections[i].id] = true;
2017-04-04 23:21:47 +02:00
}
}
2017-04-27 15:33:12 +02:00
$scope.selectedCollections = collections;
2017-04-04 23:21:47 +02:00
};
2017-04-27 15:33:12 +02:00
$scope.toggleCollectionSelection = function (id) {
if (id in $scope.selectedCollections) {
delete $scope.selectedCollections[id];
2017-04-04 23:21:47 +02:00
}
else {
2017-04-27 15:33:12 +02:00
$scope.selectedCollections[id] = true;
2017-04-04 23:21:47 +02:00
}
};
2017-04-27 15:33:12 +02:00
$scope.collectionSelected = function (collection) {
return collection.id in $scope.selectedCollections;
2017-04-04 23:21:47 +02:00
};
$scope.allSelected = function () {
2017-04-27 15:33:12 +02:00
return Object.keys($scope.selectedCollections).length === $scope.collections.length;
2017-04-04 23:21:47 +02:00
};
2017-04-12 18:41:43 +02:00
$scope.submit = function () {
2017-04-04 23:21:47 +02:00
var request = {
2017-04-27 15:33:12 +02:00
collectionIds: []
2017-04-04 23:21:47 +02:00
};
2017-04-27 15:33:12 +02:00
for (var id in $scope.selectedCollections) {
if ($scope.selectedCollections.hasOwnProperty(id)) {
request.collectionIds.push(id);
2017-04-12 18:41:43 +02:00
}
}
2017-04-27 15:33:12 +02:00
$scope.submitPromise = apiService.ciphers.putCollections({ id: loginId }, request)
2017-04-12 18:41:43 +02:00
.$promise.then(function (response) {
2017-04-27 15:33:12 +02:00
$analytics.eventTrack('Edited Login Collections');
2017-04-12 18:41:43 +02:00
$uibModalInstance.close({
2017-04-27 15:33:12 +02:00
action: 'collectionsEdit',
collectionIds: request.collectionIds
2017-04-12 18:41:43 +02:00
});
2017-04-04 23:21:47 +02:00
});
};
$scope.close = function () {
$uibModalInstance.dismiss('cancel');
};
});