1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-12-13 15:06:51 +01:00
bitwarden-browser/src/app/vault/vaultController.js

450 lines
16 KiB
JavaScript
Raw Normal View History

2015-12-09 04:35:05 +01:00
angular
.module('bit.vault')
2017-02-28 06:18:11 +01:00
.controller('vaultController', function ($scope, $uibModal, apiService, $filter, cryptoService, authService, toastr,
2017-04-26 16:21:06 +02:00
cipherService, $q, $localStorage, $timeout, $rootScope, $state, $analytics) {
$scope.loading = true;
$scope.logins = [];
2017-04-03 20:07:39 +02:00
$scope.favoriteCollapsed = $localStorage.collapsedFolders && 'favorite' in $localStorage.collapsedFolders;
2017-06-05 16:38:37 +02:00
$scope.folderIdFilter = undefined;
2017-03-07 06:36:27 +01:00
2017-04-21 20:24:24 +02:00
if ($state.params.refreshFromServer) {
$rootScope.vaultFolders = $rootScope.vaultLogins = null;
}
2017-03-19 03:55:54 +01:00
$scope.$on('$viewContentLoaded', function () {
2017-04-14 18:35:46 +02:00
if ($rootScope.vaultFolders && $rootScope.vaultLogins) {
$scope.loading = false;
loadFolderData($rootScope.vaultFolders);
loadLoginData($rootScope.vaultLogins);
return;
}
loadDataFromServer();
});
function loadDataFromServer() {
2017-03-19 03:55:54 +01:00
var folderPromise = apiService.folders.list({}, function (folders) {
2017-03-07 06:36:27 +01:00
var decFolders = [{
id: null,
2017-04-14 18:35:46 +02:00
name: 'No Folder'
2017-03-07 06:36:27 +01:00
}];
2017-03-19 03:55:54 +01:00
for (var i = 0; i < folders.Data.length; i++) {
var decFolder = cipherService.decryptFolderPreview(folders.Data[i]);
decFolders.push(decFolder);
}
2017-04-14 18:35:46 +02:00
loadFolderData(decFolders);
2017-03-19 03:55:54 +01:00
}).$promise;
var cipherPromise = apiService.ciphers.list({}, function (ciphers) {
var decLogins = [];
2017-03-07 06:36:27 +01:00
for (var i = 0; i < ciphers.Data.length; i++) {
2017-03-19 03:55:54 +01:00
if (ciphers.Data[i].Type === 1) {
2017-03-07 06:36:27 +01:00
var decLogin = cipherService.decryptLoginPreview(ciphers.Data[i]);
decLogins.push(decLogin);
}
}
2015-12-09 04:35:05 +01:00
$q.when(folderPromise).then(function () {
2017-04-14 18:35:46 +02:00
loadLoginData(decLogins);
});
2017-03-19 03:55:54 +01:00
}).$promise;
2017-04-14 18:35:46 +02:00
$q.all([cipherPromise, folderPromise]).then(function () {
2017-03-19 03:55:54 +01:00
$scope.loading = false;
2017-03-07 06:36:27 +01:00
});
2017-04-14 18:35:46 +02:00
}
function loadFolderData(decFolders) {
$rootScope.vaultFolders = $filter('orderBy')(decFolders, folderSort);
}
function loadLoginData(decLogins) {
angular.forEach($rootScope.vaultFolders, function (folderValue, folderIndex) {
folderValue.collapsed = $localStorage.collapsedFolders &&
(folderValue.id || 'none') in $localStorage.collapsedFolders;
angular.forEach(decLogins, function (loginValue) {
if (loginValue.favorite) {
loginValue.sort = -1;
}
else if (loginValue.folderId == folderValue.id) {
loginValue.sort = folderIndex;
}
});
});
$rootScope.vaultLogins = $scope.logins = $filter('orderBy')(decLogins, ['sort', 'name', 'username']);
2017-04-15 07:02:56 +02:00
var chunks = chunk($rootScope.vaultLogins, 400);
2017-04-19 16:10:27 +02:00
if (chunks.length > 0) {
$scope.logins = chunks[0];
var delay = 200;
angular.forEach(chunks, function (value, index) {
delay += 200;
// skip the first chuck
if (index > 0) {
$timeout(function () {
Array.prototype.push.apply($scope.logins, value);
}, delay);
}
});
}
2017-04-14 18:35:46 +02:00
}
function sortScopedLoginData() {
$rootScope.vaultLogins = $scope.logins = $filter('orderBy')($rootScope.vaultLogins, ['name', 'username']);
}
2015-12-09 04:35:05 +01:00
function chunk(arr, len) {
var chunks = [],
i = 0,
n = arr.length;
while (i < n) {
chunks.push(arr.slice(i, i += len));
}
return chunks;
}
function folderSort(item) {
if (!item.id) {
return '';
}
return item.name.toLowerCase();
2017-04-19 15:03:47 +02:00
}
2017-06-09 04:25:01 +02:00
$scope.clipboardError = function (e) {
alert('Your web browser does not support easy clipboard copying. ' +
'Edit the login and copy it manually instead.');
};
2017-04-03 20:07:39 +02:00
$scope.collapseExpand = function (folder, favorite) {
if (!$localStorage.collapsedFolders) {
$localStorage.collapsedFolders = {};
}
var id = favorite ? 'favorite' : (folder.id || 'none');
if (id in $localStorage.collapsedFolders) {
delete $localStorage.collapsedFolders[id];
}
else {
$localStorage.collapsedFolders[id] = true;
}
};
2017-01-03 04:26:32 +01:00
$scope.editLogin = function (login) {
2015-12-09 04:35:05 +01:00
var editModel = $uibModal.open({
animation: true,
2017-01-03 04:26:32 +01:00
templateUrl: 'app/vault/views/vaultEditLogin.html',
controller: 'vaultEditLoginController',
2015-12-09 04:35:05 +01:00
resolve: {
2017-04-14 18:35:46 +02:00
loginId: function () { return login.id; }
2015-12-09 04:35:05 +01:00
}
});
editModel.result.then(function (returnVal) {
if (returnVal.action === 'edit') {
2017-04-18 16:45:35 +02:00
login.folderId = returnVal.data.folderId;
login.name = returnVal.data.name;
login.username = returnVal.data.username;
2017-06-09 04:25:01 +02:00
login.password = returnVal.data.password;
2017-04-18 16:45:35 +02:00
login.favorite = returnVal.data.favorite;
sortScopedLoginData();
}
2017-03-24 21:09:57 +01:00
else if (returnVal.action === 'partialEdit') {
2017-04-18 16:45:35 +02:00
login.folderId = returnVal.data.folderId;
login.favorite = returnVal.data.favorite;
2017-03-24 21:09:57 +01:00
}
else if (returnVal.action === 'delete') {
2017-04-18 16:45:35 +02:00
removeLoginFromScopes(login);
2015-12-09 04:35:05 +01:00
}
});
};
2017-01-03 04:26:32 +01:00
$scope.$on('vaultAddLogin', function (event, args) {
$scope.addLogin();
2015-12-09 04:35:05 +01:00
});
2017-03-25 21:09:06 +01:00
$scope.addLogin = function (folder, favorite) {
2015-12-09 04:35:05 +01:00
var addModel = $uibModal.open({
animation: true,
2017-01-03 04:26:32 +01:00
templateUrl: 'app/vault/views/vaultAddLogin.html',
controller: 'vaultAddLoginController',
2015-12-09 04:35:05 +01:00
resolve: {
2017-03-25 21:09:06 +01:00
selectedFolder: function () { return folder; },
checkedFavorite: function () { return favorite; }
2015-12-09 04:35:05 +01:00
}
});
2017-01-03 04:26:32 +01:00
addModel.result.then(function (addedLogin) {
2017-04-14 18:35:46 +02:00
$rootScope.vaultLogins.push(addedLogin);
sortScopedLoginData();
2015-12-09 04:35:05 +01:00
});
};
2017-01-03 04:26:32 +01:00
$scope.deleteLogin = function (login) {
if (!confirm('Are you sure you want to delete this login (' + login.name + ')?')) {
2015-12-09 04:35:05 +01:00
return;
}
2017-01-03 04:26:32 +01:00
apiService.logins.del({ id: login.id }, function () {
2017-04-26 16:21:06 +02:00
$analytics.eventTrack('Deleted Login');
2017-04-18 16:45:35 +02:00
removeLoginFromScopes(login);
2015-12-09 04:35:05 +01:00
});
};
2017-06-30 22:22:39 +02:00
$scope.attachments = function (login) {
2017-07-06 22:15:28 +02:00
authService.getUserProfile().then(function (profile) {
return profile.premium;
}).then(function (isPremium) {
if (!isPremium) {
$uibModal.open({
animation: true,
templateUrl: 'app/views/premiumRequired.html',
controller: 'premiumRequiredController'
});
return;
}
2017-07-05 22:27:28 +02:00
2017-07-06 22:15:28 +02:00
if (!cryptoService.getEncKey()) {
toastr.error('You cannot use this feature until you update your encryption key.', 'Feature Unavailable');
return;
2017-06-30 22:22:39 +02:00
}
2017-07-07 16:58:51 +02:00
var attachmentModel = $uibModal.open({
2017-07-06 22:15:28 +02:00
animation: true,
templateUrl: 'app/vault/views/vaultAttachments.html',
controller: 'vaultAttachmentsController',
resolve: {
loginId: function () { return login.id; }
}
});
2017-07-07 16:58:51 +02:00
attachmentModel.result.then(function (hasAttachments) {
login.hasAttachments = hasAttachments;
2017-07-06 22:15:28 +02:00
});
2017-06-30 22:22:39 +02:00
});
};
2015-12-09 04:35:05 +01:00
$scope.editFolder = function (folder) {
var editModel = $uibModal.open({
animation: true,
templateUrl: 'app/vault/views/vaultEditFolder.html',
controller: 'vaultEditFolderController',
size: 'sm',
resolve: {
folderId: function () { return folder.id; }
}
});
editModel.result.then(function (editedFolder) {
2017-04-18 16:45:35 +02:00
folder.name = editedFolder.name;
2015-12-09 04:35:05 +01:00
});
};
$scope.$on('vaultAddFolder', function (event, args) {
$scope.addFolder();
});
$scope.addFolder = function () {
var addModel = $uibModal.open({
animation: true,
templateUrl: 'app/vault/views/vaultAddFolder.html',
controller: 'vaultAddFolderController',
size: 'sm'
});
addModel.result.then(function (addedFolder) {
2017-04-14 18:35:46 +02:00
$rootScope.vaultFolders.push(addedFolder);
loadFolderData($rootScope.vaultFolders);
2015-12-09 04:35:05 +01:00
});
};
$scope.deleteFolder = function (folder) {
if (!confirm('Are you sure you want to delete this folder (' + folder.name + ')?')) {
2015-12-09 04:35:05 +01:00
return;
}
apiService.folders.del({ id: folder.id }, function () {
2017-04-26 16:21:06 +02:00
$analytics.eventTrack('Deleted Folder');
2017-04-14 18:35:46 +02:00
var index = $rootScope.vaultFolders.indexOf(folder);
2016-11-10 03:49:15 +01:00
if (index > -1) {
2017-04-14 18:35:46 +02:00
$rootScope.vaultFolders.splice(index, 1);
2016-11-10 03:49:15 +01:00
}
2015-12-09 04:35:05 +01:00
});
};
$scope.canDeleteFolder = function (folder) {
2017-04-14 18:35:46 +02:00
if (!folder || !folder.id || !$rootScope.vaultLogins) {
2015-12-09 04:35:05 +01:00
return false;
}
2017-04-14 18:35:46 +02:00
var logins = $filter('filter')($rootScope.vaultLogins, { folderId: folder.id });
return logins && logins.length === 0;
2015-12-09 04:35:05 +01:00
};
$scope.share = function (login) {
var modal = $uibModal.open({
animation: true,
templateUrl: 'app/vault/views/vaultShareLogin.html',
controller: 'vaultShareLoginController',
resolve: {
loginId: function () { return login.id; }
}
});
2017-04-15 05:36:11 +02:00
modal.result.then(function (orgId) {
login.organizationId = orgId;
});
};
2017-04-04 23:21:47 +02:00
2017-04-27 15:33:12 +02:00
$scope.collections = function (login) {
2017-04-04 23:21:47 +02:00
var modal = $uibModal.open({
animation: true,
2017-04-27 15:33:12 +02:00
templateUrl: 'app/vault/views/vaultLoginCollections.html',
controller: 'vaultLoginCollectionsController',
2017-04-04 23:21:47 +02:00
resolve: {
loginId: function () { return login.id; }
}
});
2017-04-18 16:45:35 +02:00
modal.result.then(function (response) {
2017-04-27 15:33:12 +02:00
if (response.collectionIds && !response.collectionIds.length) {
2017-04-18 16:45:35 +02:00
removeLoginFromScopes(login);
}
2017-04-04 23:21:47 +02:00
});
};
2017-04-18 16:45:35 +02:00
2017-06-05 16:38:37 +02:00
$scope.filterFolder = function (folder) {
$scope.folderIdFilter = folder.id;
2017-06-06 18:18:43 +02:00
if ($.AdminLTE && $.AdminLTE.layout) {
$timeout(function () {
$.AdminLTE.layout.fix();
}, 0);
}
2017-06-05 16:38:37 +02:00
};
$scope.clearFilters = function () {
$scope.folderIdFilter = undefined;
2017-06-06 18:18:43 +02:00
if ($.AdminLTE && $.AdminLTE.layout) {
$timeout(function () {
$.AdminLTE.layout.fix();
}, 0);
}
2017-06-05 16:38:37 +02:00
};
$scope.folderFilter = function (folder) {
return $scope.folderIdFilter === undefined || folder.id === $scope.folderIdFilter;
2017-06-08 03:19:37 +02:00
};
2017-06-05 16:38:37 +02:00
2017-06-09 18:30:47 +02:00
$scope.unselectAll = function () {
selectAll(false);
};
$scope.selectFolder = function (folder, $event) {
var checkbox = $($event.currentTarget).closest('.box').find('input[name="loginSelection"]');
checkbox.prop('checked', true);
};
$scope.select = function ($event) {
2017-06-09 18:30:47 +02:00
var checkbox = $($event.currentTarget).closest('tr').find('input[name="loginSelection"]');
checkbox.prop('checked', !checkbox.prop('checked'));
};
2017-06-09 06:44:56 +02:00
function distinct(value, index, self) {
return self.indexOf(value) === index;
}
function getSelectedLogins() {
return $('input[name="loginSelection"]:checked').map(function () {
return $(this).val();
}).get().filter(distinct);
}
2017-06-09 18:30:47 +02:00
function selectAll(select) {
$('input[name="loginSelection"]').prop('checked', select);
2017-06-09 06:44:56 +02:00
}
$scope.bulkMove = function () {
var ids = getSelectedLogins();
if (ids.length === 0) {
alert('You have not selected anything.');
return;
}
2017-06-09 15:46:25 +02:00
var modal = $uibModal.open({
animation: true,
templateUrl: 'app/vault/views/vaultMoveLogins.html',
controller: 'vaultMoveLoginsController',
2017-06-09 18:05:22 +02:00
size: 'sm',
2017-06-09 15:46:25 +02:00
resolve: {
ids: function () { return ids; }
}
});
modal.result.then(function (folderId) {
for (var i = 0; i < ids.length; i++) {
var login = $filter('filter')($rootScope.vaultLogins, { id: ids[i] });
if (login.length) {
login[0].folderId = folderId;
}
}
2017-06-09 18:30:47 +02:00
selectAll(false);
2017-06-09 15:46:25 +02:00
sortScopedLoginData();
2017-06-09 18:30:47 +02:00
toastr.success('Items have been moved!');
2017-06-09 15:46:25 +02:00
});
2017-06-09 06:44:56 +02:00
};
$scope.bulkDelete = function () {
var ids = getSelectedLogins();
if (ids.length === 0) {
alert('You have not selected anything.');
return;
}
if (!confirm('Are you sure you want to delete the selected logins (total: ' + ids.length + ')?')) {
return;
}
$scope.bulkActionLoading = true;
apiService.ciphers.delMany({ ids: ids }, function () {
$analytics.eventTrack('Bulk Deleted Logins');
for (var i = 0; i < ids.length; i++) {
var login = $filter('filter')($rootScope.vaultLogins, { id: ids[i] });
if (login.length && login[0].edit) {
removeLoginFromScopes(login[0]);
}
}
2017-06-09 18:30:47 +02:00
selectAll(false);
2017-06-09 06:44:56 +02:00
$scope.bulkActionLoading = false;
2017-06-09 18:30:47 +02:00
toastr.success('Items have been deleted!');
2017-06-09 06:44:56 +02:00
}, function () {
2017-06-09 18:30:47 +02:00
toastr.error('An error occurred.');
2017-06-09 06:44:56 +02:00
$scope.bulkActionLoading = false;
});
};
2017-04-18 16:45:35 +02:00
function removeLoginFromScopes(login) {
var index = $rootScope.vaultLogins.indexOf(login);
if (index > -1) {
$rootScope.vaultLogins.splice(index, 1);
}
index = $scope.logins.indexOf(login);
if (index > -1) {
$scope.logins.splice(index, 1);
}
}
2015-12-09 04:35:05 +01:00
});