From d058028c54b66f4dc4672516da0ef8168fc8055f Mon Sep 17 00:00:00 2001 From: brantje Date: Tue, 4 Oct 2016 16:20:55 +0200 Subject: [PATCH] Fix updating shared credential --- appinfo/routes.php | 1 + controller/sharecontroller.php | 29 ++++++++- js/app/controllers/share.js | 109 ++++++++++++++++++++++++-------- js/app/services/shareservice.js | 7 +- 4 files changed, 117 insertions(+), 29 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index 4fadef94..1147f2ad 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -60,6 +60,7 @@ return [ ['name' => 'share#unshareCredential', 'url' => '/api/v2/sharing/credential/{item_guid}', 'verb' => 'DELETE'], ['name' => 'share#getRevisions', 'url' => '/api/v2/sharing/credential/{item_guid}/revisions', 'verb' => 'GET'], ['name' => 'share#getItemAcl', 'url' => '/api/v2/sharing/credential/{item_guid}/acl', 'verb' => 'GET'], + ['name' => 'share#updateSharedCredentialACL', 'url' => '/api/v2/sharing/credential/{item_guid}/acl', 'verb' => 'PATCH'], //Internal API ['name' => 'internal#remind', 'url' => '/api/internal/notifications/remind/{credential_id}', 'verb' => 'POST'], diff --git a/controller/sharecontroller.php b/controller/sharecontroller.php index 86820b57..38b93a15 100644 --- a/controller/sharecontroller.php +++ b/controller/sharecontroller.php @@ -77,15 +77,26 @@ class ShareController extends ApiController { * @NoAdminRequired */ public function createPublicShare($item_id, $item_guid, $permissions, $expire_timestamp, $expire_views) { - $acl = new SharingACL(); + + try{ + $acl = $this->shareService->getACL(null, $item_guid); + } catch (DoesNotExistException $exception){ + $acl = new SharingACL(); + } + + $acl->setItemId($item_id); $acl->setItemGuid($item_guid); $acl->setPermissions($permissions); $acl->setExpire($expire_timestamp); $acl->setExpireViews($expire_views); + if(!$acl->getId()){ + $this->shareService->createACLEntry($acl); + } else { + $this->shareService->updateCredentialACL($acl); + } - $this->shareService->createACLEntry($acl); } /** @@ -102,7 +113,6 @@ class ShareController extends ApiController { $result = $this->shareService->createBulkRequests($item_id, $item_guid, $vaults, $permissions, $credential_owner); if ($credential) { $processed_users = array(); - foreach ($result as $vault){ if(!in_array($vault->getTargetUserId(), $processed_users)){ $target_user = $vault->getTargetUserId(); @@ -351,4 +361,17 @@ class ShareController extends ApiController { return new NotFoundResponse(); } } + + public function updateSharedCredentialACL($item_guid, $user_id, $permission){ + try{ + $credential = $this->credentialService->getCredentialByGUID($item_guid); + } catch (DoesNotExistException $exception){ + return new NotFoundResponse(); + } + if($this->userId->getUID() == $credential->getUserId()){ + $acl = $this->shareService->getACL($user_id, $item_guid); + $acl->setPermissions($permission); + $this->shareService->updateCredentialACL($acl); + } + } } \ No newline at end of file diff --git a/js/app/controllers/share.js b/js/app/controllers/share.js index af778e20..147b2fae 100644 --- a/js/app/controllers/share.js +++ b/js/app/controllers/share.js @@ -163,7 +163,7 @@ angular.module('passmanApp') NotificationService.showNotification('Credential unshared', 4000) }) }; - + console.log($scope.storedCredential); $scope.applyShare = function () { $scope.share_settings.cypher_progress.percent = 0; $scope.share_settings.cypher_progress.done = 0; @@ -171,17 +171,35 @@ angular.module('passmanApp') $scope.share_settings.cypher_progress.times = []; $scope.share_settings.cypher_progress.times_total = []; - ShareService.generateSharedKey(20).then(function (key) { - - var encryptedSharedCredential = ShareService.encryptSharedCredential($scope.storedCredential, key); - CredentialService.updateCredential(encryptedSharedCredential, true); + //Credential is already shared + if($scope.storedCredential.shared_key !== null){ + console.log('Shared key found'); + if($scope.share_settings.linkSharing.enabled){ + var expire_time = new Date(angular.copy( $scope.share_settings.linkSharing.settings.expire_time)).getTime()/1000; + var shareObj = { + item_id: $scope.storedCredential.credential_id, + item_guid: $scope.storedCredential.guid, + permissions: $scope.share_settings.linkSharing.settings.acl.getAccessLevel(), + expire_timestamp: expire_time, + expire_views: $scope.share_settings.linkSharing.settings.expire_views + }; + //ShareService.createPublicSharedCredential(shareObj); + } var list = $scope.share_settings.credentialSharedWithUserAndGroup; console.log(list); + var enc_key = EncryptService.decryptString(angular.copy($scope.storedCredential.shared_key)); for (var i = 0; i < list.length; i++) { - var iterator = i; // Keeps it available inside the promises callback - - if (list[i].type == "user" && !list[i].hasOwnProperty('acl_id')) { + var iterator = i; + var target_user = list[i]; + console.log(target_user) + if(target_user.hasOwnProperty('acl_id')){ + var acl = { + user_id: target_user.userId, + permission: target_user.acl.getAccessLevel() + }; + ShareService.updateCredentialAcl($scope.storedCredential, acl); + } else { ShareService.getVaultsByUser(list[i].userId).then(function (data) { $scope.share_settings.cypher_progress.total += data.length; @@ -189,7 +207,7 @@ angular.module('passmanApp') console.log(data); var start = new Date().getTime() / 1000; - ShareService.cypherRSAStringWithPublicKeyBulkAsync(list[iterator].vaults, key) + ShareService.cypherRSAStringWithPublicKeyBulkAsync(list[iterator].vaults, enc_key) .progress(function (data) { $scope.share_settings.cypher_progress.done++; $scope.share_settings.cypher_progress.percent = $scope.share_settings.cypher_progress.done / $scope.share_settings.cypher_progress.total * 100; @@ -210,23 +228,64 @@ angular.module('passmanApp') } } - if($scope.share_settings.linkSharing.enabled){ - var expire_time = new Date(angular.copy( $scope.share_settings.linkSharing.settings.expire_time)).getTime()/1000; - var shareObj = { - item_id: $scope.storedCredential.credential_id, - item_guid: $scope.storedCredential.guid, - permissions: $scope.share_settings.linkSharing.settings.acl.getAccessLevel(), - expire_timestamp: expire_time, - expire_views: $scope.share_settings.linkSharing.settings.expire_views - }; - ShareService.createPublicSharedCredential(shareObj).then(function(){ - var hash = window.btoa($scope.storedCredential.guid + '<::>'+ key) - $scope.share_link = $location.$$protocol + '://' + $location.$$host + OC.generateUrl('apps/passman/share/public#') + hash; + } else { - }); - } - NotificationService.showNotification('Credential shared', 4000) - }) + ShareService.generateSharedKey(20).then(function (key) { + + var encryptedSharedCredential = ShareService.encryptSharedCredential($scope.storedCredential, key); + CredentialService.updateCredential(encryptedSharedCredential, true); + + var list = $scope.share_settings.credentialSharedWithUserAndGroup; + console.log(list); + for (var i = 0; i < list.length; i++) { + var iterator = i; // Keeps it available inside the promises callback + if (list[i].type == "user") { + ShareService.getVaultsByUser(list[i].userId).then(function (data) { + $scope.share_settings.cypher_progress.total += data.length; + + list[iterator].vaults = data; + console.log(data); + var start = new Date().getTime() / 1000; + + ShareService.cypherRSAStringWithPublicKeyBulkAsync(list[iterator].vaults, key) + .progress(function (data) { + $scope.share_settings.cypher_progress.done++; + $scope.share_settings.cypher_progress.percent = $scope.share_settings.cypher_progress.done / $scope.share_settings.cypher_progress.total * 100; + $scope.$digest(); + }) + .then(function (result) { + console.log(result); + console.log("Took: " + ((new Date().getTime() / 1000) - start) + "s to cypher the string for user [" + data[0].user_id + "]"); + $scope.share_settings.cypher_progress.times.push({ + time: ((new Date().getTime() / 1000) - start), + user: data[0].user_id + }); + list[iterator].vaults = result; + $scope.uploadChanges(list[iterator]); + $scope.$digest(); + }); + }); + } + } + + if($scope.share_settings.linkSharing.enabled){ + var expire_time = new Date(angular.copy( $scope.share_settings.linkSharing.settings.expire_time)).getTime()/1000; + var shareObj = { + item_id: $scope.storedCredential.credential_id, + item_guid: $scope.storedCredential.guid, + permissions: $scope.share_settings.linkSharing.settings.acl.getAccessLevel(), + expire_timestamp: expire_time, + expire_views: $scope.share_settings.linkSharing.settings.expire_views + }; + ShareService.createPublicSharedCredential(shareObj).then(function(){ + var hash = window.btoa($scope.storedCredential.guid + '<::>'+ key) + $scope.share_link = $location.$$protocol + '://' + $location.$$host + OC.generateUrl('apps/passman/share/public#') + hash; + + }); + } + NotificationService.showNotification('Credential shared', 4000) + }) + } }; $scope.uploadChanges = function (user) { diff --git a/js/app/services/shareservice.js b/js/app/services/shareservice.js index 7193d81e..fb066678 100644 --- a/js/app/services/shareservice.js +++ b/js/app/services/shareservice.js @@ -83,7 +83,6 @@ angular.module('passmanApp') } }); }, - createPublicSharedCredential: function (shareObj) { var queryUrl = OC.generateUrl('apps/passman/api/v2/sharing/public'); return $http.post(queryUrl, shareObj).then(function (response) { @@ -116,6 +115,12 @@ angular.module('passmanApp') return result; }) }, + updateCredentialAcl: function(credential, acl){ + var queryUrl = OC.generateUrl('apps/passman/api/v2/sharing/credential/'+ credential.guid +'/acl'); + return $http.patch(queryUrl, acl).then(function (response) { + return response.data; + }) + }, getCredendialsSharedWithUs: function (vault_guid) { var queryUrl = OC.generateUrl('apps/passman/api/v2/sharing/vault/' + vault_guid + '/get'); return $http.get(queryUrl).then(function (response) {