From 429b2b8a213e43ea871bda2329ea924fcf33c0da Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 9 Mar 2017 22:08:47 -0500 Subject: [PATCH] add subvault --- .../organizationSubvaultsAddController.js | 18 ++++++ .../organizationSubvaultsController.js | 38 ++++++++---- .../views/organizationSubvaults.html | 21 +++---- .../views/organizationSubvaultsAdd.html | 28 +++++++++ src/app/services/apiService.js | 18 +++--- src/app/services/cipherService.js | 60 ++++++++++++++----- src/index.html | 1 + 7 files changed, 139 insertions(+), 45 deletions(-) create mode 100644 src/app/organization/organizationSubvaultsAddController.js create mode 100644 src/app/organization/views/organizationSubvaultsAdd.html diff --git a/src/app/organization/organizationSubvaultsAddController.js b/src/app/organization/organizationSubvaultsAddController.js new file mode 100644 index 0000000000..f1255d2135 --- /dev/null +++ b/src/app/organization/organizationSubvaultsAddController.js @@ -0,0 +1,18 @@ +angular + .module('bit.organization') + + .controller('organizationSubvaultsAddController', function ($scope, $state, $uibModalInstance, apiService, cipherService, + $analytics) { + $scope.submit = function (model) { + var subvault = cipherService.encryptSubvault(model, $state.params.orgId); + $scope.submitPromise = apiService.subvaults.post({ orgId: $state.params.orgId }, subvault, function (response) { + $analytics.eventTrack('Created Subvault'); + var decSubvault = cipherService.decryptSubvault(response, $state.params.orgId, true); + $uibModalInstance.close(decSubvault); + }).$promise; + }; + + $scope.close = function () { + $uibModalInstance.dismiss('cancel'); + }; + }); diff --git a/src/app/organization/organizationSubvaultsController.js b/src/app/organization/organizationSubvaultsController.js index 199c9b15bf..4876d90cac 100644 --- a/src/app/organization/organizationSubvaultsController.js +++ b/src/app/organization/organizationSubvaultsController.js @@ -1,25 +1,41 @@ angular .module('bit.organization') - .controller('organizationSubvaultsController', function ($scope, $state, apiService) { + .controller('organizationSubvaultsController', function ($scope, $state, apiService, $uibModal, cipherService) { $scope.subvaults = []; $scope.loading = true; $scope.$on('$viewContentLoaded', function () { loadList(); }); + $scope.add = function () { + var modal = $uibModal.open({ + animation: true, + templateUrl: 'app/organization/views/organizationSubvaultsAdd.html', + controller: 'organizationSubvaultsAddController' + }); + + modal.result.then(function (subvault) { + $scope.subvaults.push(subvault); + }); + }; + + $scope.delete = function (subvault) { + if (!confirm('Are you sure you want to delete this subvault (' + subvault.name + ')?')) { + return; + } + + apiService.subvaults.del({ orgId: $state.params.orgId, id: subvault.id }, function () { + var index = $scope.subvaults.indexOf(subvault); + if (index > -1) { + $scope.subvaults.splice(index, 1); + } + }); + }; + function loadList() { apiService.subvaults.listOrganization({ orgId: $state.params.orgId }, function (list) { - var subvaults = []; - - for (var i = 0; i < list.Data.length; i++) { - subvaults.push({ - id: list.Data[i].Id, - name: list.Data[i].Name - }); - } - - $scope.subvaults = subvaults; + $scope.subvaults = cipherService.decryptSubvaults(list.Data, $state.params.orgId, true); $scope.loading = false; }); } diff --git a/src/app/organization/views/organizationSubvaults.html b/src/app/organization/views/organizationSubvaults.html index b55bfd73dd..d5584ae3e9 100644 --- a/src/app/organization/views/organizationSubvaults.html +++ b/src/app/organization/views/organizationSubvaults.html @@ -9,20 +9,20 @@

Subvaults

-
-
-
+
+
Loading...
-
+

No subvaults.

-
+
@@ -32,13 +32,14 @@ - diff --git a/src/app/organization/views/organizationSubvaultsAdd.html b/src/app/organization/views/organizationSubvaultsAdd.html new file mode 100644 index 0000000000..9ecfc87a07 --- /dev/null +++ b/src/app/organization/views/organizationSubvaultsAdd.html @@ -0,0 +1,28 @@ + + + + + + \ No newline at end of file diff --git a/src/app/services/apiService.js b/src/app/services/apiService.js index 52a61887b7..6366f3804a 100644 --- a/src/app/services/apiService.js +++ b/src/app/services/apiService.js @@ -29,15 +29,6 @@ del: { url: _apiUri + '/ciphers/:id/delete', method: 'POST', params: { id: '@id' } } }); - _service.subvaults = $resource(_apiUri + '/subvaults/:id', {}, { - get: { method: 'GET', params: { id: '@id' } }, - list: { method: 'GET', params: {} }, - listOrganization: { url: _apiUri + '/subvaults/organization/:orgId', method: 'GET', params: { orgId: '@orgId' } }, - post: { method: 'POST', params: {} }, - put: { method: 'POST', params: { id: '@id' } }, - del: { url: _apiUri + '/subvaults/:id/delete', method: 'POST', params: { id: '@id' } } - }); - _service.organizations = $resource(_apiUri + '/organizations/:id', {}, { get: { method: 'GET', params: { id: '@id' } }, getExtended: { url: _apiUri + '/organizations/:id/extended', method: 'GET', params: { id: '@id' } }, @@ -56,6 +47,15 @@ del: { url: _apiUri + '/organizations/:orgId/users/:id/delete', method: 'POST', params: { id: '@id', orgId: '@orgId' } } }); + _service.subvaults = $resource(_apiUri + '/organizations/:orgId/subvaults/:id', {}, { + get: { method: 'GET', params: { id: '@id', orgId: '@orgId' } }, + listMe: { url: _apiUri + '/subvaults', method: 'GET', params: {} }, + listOrganization: { method: 'GET', params: { orgId: '@orgId' } }, + post: { method: 'POST', params: { orgId: '@orgId' } }, + put: { method: 'POST', params: { id: '@id', orgId: '@orgId' } }, + del: { url: _apiUri + '/organizations/:orgId/subvaults/:id/delete', method: 'POST', params: { id: '@id', orgId: '@orgId' } } + }); + _service.accounts = $resource(_apiUri + '/accounts', {}, { register: { url: _apiUri + '/accounts/register', method: 'POST', params: {} }, emailToken: { url: _apiUri + '/accounts/email-token', method: 'POST', params: {} }, diff --git a/src/app/services/cipherService.js b/src/app/services/cipherService.js index 638827f924..dedf481092 100644 --- a/src/app/services/cipherService.js +++ b/src/app/services/cipherService.js @@ -61,21 +61,6 @@ angular username: decryptProperty(encryptedCipher.Data.Username, key, true) }; - try { - login.name = cryptoService.decrypt(encryptedCipher.Data.Name, key); - } - catch (err) { - login.name = '[error: cannot decrypt]'; - } - - try { - login.username = encryptedCipher.Data.Username && encryptedCipher.Data.Username !== '' ? - cryptoService.decrypt(encryptedCipher.Data.Username, key) : null; - } - catch (err) { - login.username = '[error: cannot decrypt]'; - } - return login; }; @@ -109,6 +94,31 @@ angular }; }; + _service.decryptSubvaults = function (encryptedSubvaults, orgId, catchError) { + if (!encryptedSubvaults) throw "encryptedSubvaults is undefined or null"; + + var unencryptedSubvaults = []; + for (var i = 0; i < encryptedSubvaults.length; i++) { + unencryptedSubvaults.push(_service.decryptSubvault(encryptedSubvaults[i], orgId, catchError)); + } + + return unencryptedSubvaults; + }; + + _service.decryptSubvault = function (encryptedSubvault, orgId, catchError) { + if (!encryptedSubvault) throw "encryptedSubvault is undefined or null"; + + catchError = catchError === true ? true : false; + orgId = orgId || encryptedSubvault.OrganizationId; + var key = cryptoService.getOrgKey(orgId); + + return { + id: encryptedSubvault.Id, + name: catchError ? decryptProperty(encryptedSubvault.Name, key, false) : + cryptoService.decrypt(encryptedSubvault.Name, key) + }; + }; + function decryptProperty(property, key, checkEmpty) { if (checkEmpty && (!property || property === '')) { return null; @@ -172,5 +182,25 @@ angular }; }; + _service.encryptSubvaults = function (unencryptedSubvaults, orgId) { + if (!unencryptedSubvaults) throw "unencryptedSubvaults is undefined or null"; + + var encryptedSubvaults = []; + for (var i = 0; i < unencryptedSubvaults.length; i++) { + encryptedSubvaults.push(_service.encryptSubvault(unencryptedSubvaults[i], orgId)); + } + + return encryptedSubvaults; + }; + + _service.encryptSubvault = function (unencryptedSubvault, orgId) { + if (!unencryptedSubvault) throw "unencryptedSubvault is undefined or null"; + + return { + id: unencryptedSubvault.id, + name: cryptoService.encrypt(unencryptedSubvault.name, cryptoService.getOrgKey(orgId)) + }; + }; + return _service; }); diff --git a/src/index.html b/src/index.html index 91c2c140cb..0f366e4370 100644 --- a/src/index.html +++ b/src/index.html @@ -125,6 +125,7 @@ +
- - {{subvault.name}}