From 35d6d3777cca1561f2baa61a93745ba1d9af2201 Mon Sep 17 00:00:00 2001 From: brantje Date: Wed, 5 Oct 2016 13:46:25 +0200 Subject: [PATCH] Add activity support for sharing --- controller/credentialcontroller.php | 95 +++++++++++++++++++++-------- controller/sharecontroller.php | 26 +++++++- js/app/controllers/share.js | 4 +- lib/Activity.php | 9 ++- lib/Service/CredentialService.php | 7 +++ lib/Service/ShareService.php | 2 +- 6 files changed, 114 insertions(+), 29 deletions(-) diff --git a/controller/credentialcontroller.php b/controller/credentialcontroller.php index 8c2bdf71..397b3793 100644 --- a/controller/credentialcontroller.php +++ b/controller/credentialcontroller.php @@ -11,6 +11,7 @@ namespace OCA\Passman\Controller; +use OCA\Files_External\NotFoundException; use OCA\Passman\Db\SharingACL; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; @@ -28,7 +29,7 @@ class CredentialController extends ApiController { private $credentialService; private $activityService; private $credentialRevisionService; - private $sharingService; + private $sharingService; public function __construct($AppName, IRequest $request, @@ -36,14 +37,14 @@ class CredentialController extends ApiController { CredentialService $credentialService, ActivityService $activityService, CredentialRevisionService $credentialRevisionService, - ShareService $sharingService - ) { + ShareService $sharingService + ) { parent::__construct($AppName, $request); $this->userId = $UserId; $this->credentialService = $credentialService; $this->activityService = $activityService; $this->credentialRevisionService = $credentialRevisionService; - $this->sharingService = $sharingService; + $this->sharingService = $sharingService; } /** @@ -80,10 +81,12 @@ class CredentialController extends ApiController { ); $credential = $this->credentialService->createCredential($credential); $link = ''; // @TODO create direct link to credential - $this->activityService->add( - Activity::SUBJECT_ITEM_CREATED_SELF, array($label, $this->userId), - '', array(), - $link, $this->userId, Activity::TYPE_ITEM_ACTION); + if(!$credential->getHidden()) { + $this->activityService->add( + Activity::SUBJECT_ITEM_CREATED_SELF, array($label, $this->userId), + '', array(), + $link, $this->userId, Activity::TYPE_ITEM_ACTION); + } return new JSONResponse($credential); } @@ -127,48 +130,92 @@ class CredentialController extends ApiController { 'delete_time' => $delete_time, 'hidden' => $hidden, 'otp' => $otp, - 'shared_key' => ($shared_key === NULL) ? '' : $shared_key, + 'shared_key' => ($shared_key === null) ? '' : $shared_key, ); - - if ($storedCredential->getUserId() !== $this->userId){ - $acl = $this->sharingService->getCredentialAclForUser($this->userId, $storedCredential->getGuid()); - if ($acl->hasPermission(SharingACL::WRITE)) { - $credential['shared_key'] = $storedCredential->getSharedKey(); - } - else { - return new DataResponse(['msg' => 'Not authorized'], Http::STATUS_UNAUTHORIZED); - } - } + if ($storedCredential->getUserId() !== $this->userId) { + $acl = $this->sharingService->getCredentialAclForUser($this->userId, $storedCredential->getGuid()); + if ($acl->hasPermission(SharingACL::WRITE)) { + $credential['shared_key'] = $storedCredential->getSharedKey(); + } else { + return new DataResponse(['msg' => 'Not authorized'], Http::STATUS_UNAUTHORIZED); + } + } //@TODO Add activities for non owned items $link = ''; // @TODO create direct link to credential + $activity = false; if ($revision_created) { + $activity = 'item_apply_revision'; $this->activityService->add( - 'item_apply_revision_self', array($label, $this->userId, $revision_created), + $activity . '_self', array($label, $this->userId, $revision_created), '', array(), $link, $this->userId, Activity::TYPE_ITEM_ACTION); } else if (($storedCredential->getDeleteTime() == 0) && $delete_time > 0) { + $activity = 'item_deleted'; $this->activityService->add( - 'item_deleted_self', array($label, $this->userId), + $activity . '_self', array($label, $this->userId), '', array(), $link, $this->userId, Activity::TYPE_ITEM_ACTION); } else if (($storedCredential->getDeleteTime() > 0) && $delete_time == 0) { + $activity = 'item_recovered'; $this->activityService->add( - 'item_recovered_self', array($label, $this->userId), + $activity . '_self', array($label, $this->userId), '', array(), $link, $this->userId, Activity::TYPE_ITEM_ACTION); } else if ($label != $storedCredential->getLabel()) { + $activity = 'item_renamed'; $this->activityService->add( - 'item_renamed_self', array($storedCredential->getLabel(), $label, $this->userId), + $activity . '_self', array($storedCredential->getLabel(), $label, $this->userId), '', array(), $link, $this->userId, Activity::TYPE_ITEM_RENAMED); } else { + $activity = 'item_edited'; $this->activityService->add( - 'item_edited_self', array($label, $this->userId), + $activity . '_self', array($label, $this->userId), '', array(), $link, $this->userId, Activity::TYPE_ITEM_ACTION); } + $acl_list = null; + + try { + $acl_list = $this->sharingService->getCredentialAclList($storedCredential->getGuid()); + } catch (DoesNotExistException $exception) { + + } + if ($acl_list) { + $params = array(); + switch ($activity) { + case 'item_recovered': + case 'item_deleted': + case 'item_edited': + $params = array($credential['label'], $this->userId); + break; + case 'item_apply_revision': + $params = array($credential['label'], $this->userId, $revision_created); + break; + case 'item_renamed': + $params = array($storedCredential->getLabel(), $label, $this->userId); + break; + } + + foreach ($acl_list as $sharingACL) { + $target_user = $sharingACL->getUserId(); + if($target_user == $this->userId){ + continue; + } + $this->activityService->add( + $activity, $params, + '', array(), + $link, $target_user, Activity::TYPE_ITEM_ACTION); + } + if ($this->userId != $storedCredential->getUserId()) { + $this->activityService->add( + $activity, $params, + '', array(), + $link, $storedCredential->getUserId(), Activity::TYPE_ITEM_ACTION); + } + } $this->credentialRevisionService->createRevision($storedCredential, $storedCredential->getUserId(), $credential_id, $this->userId); $credential = $this->credentialService->updateCredential($credential); diff --git a/controller/sharecontroller.php b/controller/sharecontroller.php index bd0c737b..839bffc8 100644 --- a/controller/sharecontroller.php +++ b/controller/sharecontroller.php @@ -79,6 +79,12 @@ class ShareController extends ApiController { */ public function createPublicShare($item_id, $item_guid, $permissions, $expire_timestamp, $expire_views) { + try{ + $credential = $this->credentialService->getCredentialByGUID($item_guid); + } catch (DoesNotExistException $exception){ + return new NotFoundResponse(); + } + try { $acl = $this->shareService->getACL(null, $item_guid); } catch (DoesNotExistException $exception) { @@ -93,6 +99,11 @@ class ShareController extends ApiController { $acl->setExpireViews($expire_views); if (!$acl->getId()) { $this->shareService->createACLEntry($acl); + + $this->activityService->add( + 'item_shared_publicly', [$credential->getLabel()], + '', array(), + '', $this->userId->getUID(), Activity::TYPE_ITEM_SHARED); } else { $this->shareService->updateCredentialACL($acl); } @@ -113,7 +124,7 @@ class ShareController extends ApiController { $first_vault = $vaults[0]; try { - $shareRequests = $this->shareService->getPendingShareRequests($item_guid, $first_vault['user_id']); + $shareRequests = $this->shareService->getPendingShareRequestsForCredential($item_guid, $first_vault['user_id']); if (count($shareRequests) > 0) { return new JSONResponse(array('error' => 'User got already pending requests')); } @@ -150,9 +161,22 @@ class ShareController extends ApiController { $notification ); array_push($processed_users, $target_user); + + $this->activityService->add( + 'item_shared', [$credential->getLabel(), $target_user], + '', array(), + '', $this->userId->getUID(), Activity::TYPE_ITEM_SHARED); + + + $this->activityService->add( + 'item_share_received', [$credential->getLabel(), $this->userId->getUID()], + '', array(), + '', $target_user, Activity::TYPE_ITEM_SHARED); } } } + + return new JSONResponse($result); } diff --git a/js/app/controllers/share.js b/js/app/controllers/share.js index a73818d2..bc2ca8e1 100644 --- a/js/app/controllers/share.js +++ b/js/app/controllers/share.js @@ -157,7 +157,7 @@ angular.module('passmanApp') $scope.setPermission = function(acl, permission){ acl.togglePermission(permission); }; - + console.log($scope.storedCredential) $scope.shareWith = function (shareWith, selectedAccessLevel) { //@TODO Improve this so we can add, edit and remove users and permissions. $scope.inputSharedWith = []; @@ -169,7 +169,7 @@ angular.module('passmanApp') type: shareWith[i].type, acl: angular.copy($scope.default_permissions), pending: true, - credential_guid: $scope.selectedCredential.guid + credential_guid: $scope.storedCredential.guid }; if ($scope.share_settings.credentialSharedWithUserAndGroup.indexOf(obj) === -1) { $scope.share_settings.credentialSharedWithUserAndGroup.push(obj) diff --git a/lib/Activity.php b/lib/Activity.php index da358943..4ec7d66d 100644 --- a/lib/Activity.php +++ b/lib/Activity.php @@ -30,6 +30,8 @@ class Activity implements \OCP\Activity\IExtension { const SUBJECT_ITEM_DESTROYED_SELF = 'item_destroyed_self'; const SUBJECT_ITEM_EXPIRED = 'item_expired'; const SUBJECT_ITEM_SHARED = 'item_shared'; + const SUBJECT_ITEM_SHARE_RECEIVED = 'item_share_received'; + const SUBJECT_ITEM_SHARED_PUBLICLY = 'item_shared_publicly'; const SUBJECT_ITEM_RENAMED = 'item_renamed'; const SUBJECT_ITEM_RENAMED_SELF = 'item_renamed_self'; @@ -135,7 +137,11 @@ class Activity implements \OCP\Activity\IExtension { case self::SUBJECT_ITEM_EXPIRED: return $l->t('The password of %1$s has expired, renew it now.', $params)->__toString(); case self::SUBJECT_ITEM_SHARED: - return $l->t('%s has been shared', $params)->__toString(); + return $l->t('%1$s has been shared with %2$s', $params)->__toString(); + case self::SUBJECT_ITEM_SHARE_RECEIVED: + return $l->t('You received a share request for %1$s from %2$s', $params)->__toString(); + case self::SUBJECT_ITEM_SHARED_PUBLICLY: + return $l->t('%s has been shared with a link', $params)->__toString(); } } return false; @@ -180,6 +186,7 @@ class Activity implements \OCP\Activity\IExtension { case self::SUBJECT_ITEM_RENAMED_SELF: case self::SUBJECT_ITEM_RENAMED: case self::SUBJECT_ITEM_SHARED: + case self::SUBJECT_ITEM_SHARED_PUBLICLY: return array( 0 => 'passman', ); diff --git a/lib/Service/CredentialService.php b/lib/Service/CredentialService.php index e6868f9a..d0c920b7 100644 --- a/lib/Service/CredentialService.php +++ b/lib/Service/CredentialService.php @@ -11,6 +11,7 @@ namespace OCA\Passman\Service; +use OCA\Passman\Db\Credential; use OCA\Passman\Db\SharingACL; use OCA\Passman\Db\SharingACLMapper; use OCP\IConfig; @@ -29,6 +30,12 @@ class CredentialService { $this->sharingACL = $sharingACL; } + /** + * Create a new credential + * @param $user_id + * @param $item_guid + * @return Credential + */ public function createCredential($credential) { return $this->credentialMapper->create($credential); } diff --git a/lib/Service/ShareService.php b/lib/Service/ShareService.php index 455bc673..1ea81674 100644 --- a/lib/Service/ShareService.php +++ b/lib/Service/ShareService.php @@ -135,7 +135,7 @@ class ShareService { return $return; } - /* + /** * Gets the acl for a given item guid * @param $user_id * @param $item_guid