From 4bc90d904eb63e08d96372c7bbc97ff06297a434 Mon Sep 17 00:00:00 2001 From: brantje Date: Wed, 5 Oct 2016 12:48:59 +0200 Subject: [PATCH] Setting permission on share requests works --- controller/sharecontroller.php | 40 +++++++++++++--- js/app/controllers/share.js | 83 ++++++++++++++++++++-------------- lib/Service/ShareService.php | 9 +++- 3 files changed, 92 insertions(+), 40 deletions(-) diff --git a/controller/sharecontroller.php b/controller/sharecontroller.php index 57d3b630..77c008a3 100644 --- a/controller/sharecontroller.php +++ b/controller/sharecontroller.php @@ -11,6 +11,7 @@ namespace OCA\Passman\Controller; +use OCA\Files_External\NotFoundException; use OCA\Passman\Db\ShareRequest; use OCA\Passman\Db\SharingACL; use OCA\Passman\Db\Vault; @@ -112,9 +113,24 @@ class ShareController extends ApiController { $credential_owner = $credential->getUserId(); $first_vault = $vaults[0]; - $shareRequests = $this->shareService->getPendingShareRequests($item_guid, $first_vault['user_id']); - if(count($shareRequests) > 0){ - return new JSONResponse(array('error'=> 'User got already pending requests')); + try { + $shareRequests = $this->shareService->getPendingShareRequests($item_guid, $first_vault['user_id']); + if (count($shareRequests) > 0) { + return new JSONResponse(array('error' => 'User got already pending requests')); + } + } catch (DoesNotExistException $exception){ + + } + + $acl = null; + try { + $acl = $this->shareService->getCredentialAclForUser($first_vault['user_id'], $item_guid); + } catch (DoesNotExistException $exception){ + + } + + if($acl){ + return new JSONResponse(array('error'=> 'User got already this credential')); } $result = $this->shareService->createBulkRequests($item_id, $item_guid, $vaults, $permissions, $credential_owner); @@ -379,9 +395,21 @@ class ShareController extends ApiController { return new NotFoundResponse(); } if($this->userId->getUID() == $credential->getUserId()){ - $acl = $this->shareService->getACL($user_id, $item_guid); - $acl->setPermissions($permission); - $this->shareService->updateCredentialACL($acl); + $acl = null; + try { + $acl = $this->shareService->getACL($user_id, $item_guid); + $acl->setPermissions($permission); + return $this->shareService->updateCredentialACL($acl); + } catch (DoesNotExistException $exception){ + + } + if($acl === null){ + $sr = $this->shareService->getPendingShareRequestsForCredential($item_guid, $user_id); + foreach ($sr as $shareRequest){ + $shareRequest->setPermissions($permission); + $this->shareService->updateCredentialShareRequest($shareRequest); + } + } } } diff --git a/js/app/controllers/share.js b/js/app/controllers/share.js index dd7e350b..a73818d2 100644 --- a/js/app/controllers/share.js +++ b/js/app/controllers/share.js @@ -94,44 +94,55 @@ angular.module('passmanApp') } }; - - ShareService.getSharedCredentialACL($scope.storedCredential).then(function (aclList) { - - var enc_key = ($scope.storedCredential.shared_key) ? EncryptService.decryptString(angular.copy($scope.storedCredential.shared_key)) : false; - - for(var i = 0; i < aclList.length; i++){ - var acl = aclList[i]; - if(acl.user_id === null){ - $scope.share_settings.linkSharing ={ - enabled: true, - settings: { - expire_time: new Date(acl.expire * 1000), - expire_views: acl.expire_views, - acl: new SharingACL(acl.permissions) + var getAcl = function() { + ShareService.getSharedCredentialACL($scope.storedCredential).then(function (aclList) { + var _list = [] + var enc_key = ($scope.storedCredential.shared_key) ? EncryptService.decryptString(angular.copy($scope.storedCredential.shared_key)) : false; + for (var i = 0; i < aclList.length; i++) { + var acl = aclList[i]; + if (acl.user_id === null) { + $scope.share_settings.linkSharing = { + enabled: true, + settings: { + expire_time: new Date(acl.expire * 1000), + expire_views: acl.expire_views, + acl: new SharingACL(acl.permissions) + } + }; + if (enc_key) { + var hash = window.btoa($scope.storedCredential.guid + '<::>' + enc_key) + $scope.share_link = $location.$$protocol + '://' + $location.$$host + OC.generateUrl('apps/passman/share/public#') + hash; } - }; - if(enc_key) { - var hash = window.btoa($scope.storedCredential.guid + '<::>' + enc_key) - $scope.share_link = $location.$$protocol + '://' + $location.$$host + OC.generateUrl('apps/passman/share/public#') + hash; + } else { + var obj = { + userId: acl.user_id, + displayName: acl.user_id, + type: 'user', + acl: new SharingACL(acl.permissions), + acl_id: acl.acl_id, + pending: acl.pending, + credential_guid: acl.item_guid, + created: acl.created + }; + + _list.push(obj); } - } else { - var obj = { - userId: acl.user_id, - displayName: acl.user_id, - type: 'user', - acl: new SharingACL(acl.permissions), - acl_id: acl.acl_id - }; - $scope.share_settings.credentialSharedWithUserAndGroup.push(obj); } - - } - }); - + $scope.share_settings.credentialSharedWithUserAndGroup = _list; + }); + }; + getAcl(); var acl = new SharingACL(0); + $scope.$watch('share_settings.upload_progress.done', function () { + console.log(); + if($scope.share_settings.upload_progress.done == $scope.share_settings.upload_progress.total){ + getAcl() + } + }); + $scope.inputSharedWith = []; $scope.selectedAccessLevel = '1'; @@ -156,7 +167,9 @@ angular.module('passmanApp') userId: shareWith[i].uid, displayName: shareWith[i].text, type: shareWith[i].type, - acl: angular.copy($scope.default_permissions) + acl: angular.copy($scope.default_permissions), + pending: true, + credential_guid: $scope.selectedCredential.guid }; if ($scope.share_settings.credentialSharedWithUserAndGroup.indexOf(obj) === -1) { $scope.share_settings.credentialSharedWithUserAndGroup.push(obj) @@ -240,13 +253,17 @@ angular.module('passmanApp') for (var i = 0; i < list.length; i++) { var iterator = i; var target_user = list[i]; - if(target_user.hasOwnProperty('acl_id')){ + console.log(target_user) + if(target_user.hasOwnProperty('created')){ + console.log('Updating permissions') + var acl = { user_id: target_user.userId, permission: target_user.acl.getAccessLevel() }; ShareService.updateCredentialAcl($scope.storedCredential, acl); } else { + console.log('Creating new share') $scope.applyShareToUser(list[iterator], enc_key); } } diff --git a/lib/Service/ShareService.php b/lib/Service/ShareService.php index 4fb9346c..2f9eeadb 100644 --- a/lib/Service/ShareService.php +++ b/lib/Service/ShareService.php @@ -257,7 +257,14 @@ class ShareService { return $this->shareRequest->updateShareRequest($shareRequest); } - public function getPendingShareRequests($item_guid, $user_id){ + + /** + * Get pending share requests by guid and uid + * + * @param ShareRequest $request + * @return \OCA\Passman\Db\ShareRequest[] + */ + public function getPendingShareRequestsForCredential($item_guid, $user_id){ return $this->shareRequest->getPendingShareRequests($item_guid, $user_id); } } \ No newline at end of file