From e8c60d579f377baa59056b6f8b39fdf2dffdf40b Mon Sep 17 00:00:00 2001 From: brantje Date: Sun, 2 Oct 2016 14:41:38 +0200 Subject: [PATCH] Send notification to user --- controller/sharecontroller.php | 66 ++++++++++++++++++++++------- lib/AppInfo/Application.php | 4 +- lib/Notifier.php | 20 +++++++++ lib/Service/NotificationService.php | 22 ++++++++++ 4 files changed, 96 insertions(+), 16 deletions(-) diff --git a/controller/sharecontroller.php b/controller/sharecontroller.php index fb122702..ef1aaec2 100644 --- a/controller/sharecontroller.php +++ b/controller/sharecontroller.php @@ -12,6 +12,8 @@ namespace OCA\Passman\Controller; use OCA\Passman\Db\Vault; +use OCA\Passman\Service\CredentialService; +use OCA\Passman\Service\NotificationService; use OCA\Passman\Service\ShareService; use OCP\IRequest; use OCP\AppFramework\Http\JSONResponse; @@ -27,14 +29,15 @@ use OCA\Passman\Service\ActivityService; use OCA\Passman\Activity; - class ShareController extends ApiController { private $userId; private $activityService; private $groupManager; private $userManager; private $vaultService; - private $shareService; + private $shareService; + private $credentialService; + private $notificationService; private $limit = 50; private $offset = 0; @@ -46,27 +49,60 @@ class ShareController extends ApiController { IUserManager $userManager, ActivityService $activityService, VaultService $vaultService, - ShareService $shareService + ShareService $shareService, + CredentialService $credentialService, + NotificationService $notificationService ) { parent::__construct($AppName, $request); + $this->userId = $UserId; $this->userManager = $userManager; $this->groupManager = $groupManager; $this->activityService = $activityService; $this->vaultService = $vaultService; - $this->shareService = $shareService; + $this->shareService = $shareService; + $this->credentialService = $credentialService; + $this->notificationService = $notificationService; } - public function applyIntermediateShare($item_id, $item_guid, $vaults, $permissions){ - return new JSONResponse($this->shareService->createBulkRequests($item_id, $item_guid, $vaults, $permissions)); - } + public function applyIntermediateShare($item_id, $item_guid, $vaults, $permissions) { + /** + * Assemble notification + */ + $credential = $this->credentialService->getCredentialById($item_id, $this->userId->getUID()); + $result = $this->shareService->createBulkRequests($item_id, $item_guid, $vaults, $permissions); + if ($credential) { + $processed_users = array(); + + foreach ($result as $vault){ + if(!in_array($vault->getTargetUserId(), $processed_users)){ + $target_user = $vault->getTargetUserId(); + $notification = array( + 'from_user' => ucfirst($this->userId->getDisplayName()), + 'credential_label' => $credential->getLabel(), + 'credential_id' => $credential->getId(), + 'item_id' => $credential->getId(), + 'target_user' => $target_user, + 'req_id' => $vault->getId() + ); + $this->notificationService->credentialSharedNotification( + $notification + ); + array_push($processed_users, $target_user); + } + } + + + } + return new JSONResponse($result); + } public function searchUsers($search) { $users = array(); $usersTmp = $this->userManager->searchDisplayName($search, $this->limit, $this->offset); foreach ($usersTmp as $user) { - if($this->userId != $user->getUID() && count($this->vaultService->getByUser($user->getUID())) >= 1) { + if ($this->userId != $user->getUID() && count($this->vaultService->getByUser($user->getUID())) >= 1) { $users[] = array( 'text' => $user->getDisplayName(), 'uid' => $user->getUID(), @@ -90,10 +126,10 @@ class ShareController extends ApiController { /** * @NoAdminRequired */ - public function getVaultsByUser($user_id){ + public function getVaultsByUser($user_id) { $user_vaults = $this->vaultService->getByUser($user_id); $result = array(); - foreach($user_vaults as $vault){ + foreach ($user_vaults as $vault) { array_push($result, array( 'vault_id' => $vault->getId(), @@ -105,7 +141,7 @@ class ShareController extends ApiController { return new JSONResponse($result); } - public function share($credential){ + public function share($credential) { $link = ''; $this->activityService->add( @@ -115,11 +151,11 @@ class ShareController extends ApiController { } public function savePendingRequest($item_guid, $target_vault_guid, $final_shared_key) { - $this->shareService->applyShare($item_guid, $target_vault_guid, $final_shared_key); - } + $this->shareService->applyShare($item_guid, $target_vault_guid, $final_shared_key); + } public function getPendingRequests() { - return new JSONResponse($this->shareService->getUserPendingRequests()); - } + return new JSONResponse($this->shareService->getUserPendingRequests()); + } } \ No newline at end of file diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index bd8a5d05..76cf20b6 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -54,7 +54,9 @@ class Application extends App { $server->getUserManager(), $c->query('ActivityService'), $c->query('VaultService'), - $c->query('ShareService') + $c->query('ShareService'), + $c->query('CredentialService'), + $c->query('NotificationService') ); }); diff --git a/lib/Notifier.php b/lib/Notifier.php index ce63d76e..8bab59af 100644 --- a/lib/Notifier.php +++ b/lib/Notifier.php @@ -61,6 +61,26 @@ class Notifier implements INotifier { } return $notification; break; + + case 'credential_shared': + $notification->setParsedSubject( + (string) $l->t('%s shared "%s" credential with you. Click here to accept', $notification->getSubjectParameters()) + ); + + // Deal with the actions for a known subject + foreach ($notification->getActions() as $action) { + switch ($action->getLabel()) { + case 'decline': + $action->setParsedLabel( + (string) $l->t('Decline') + ); + break; + } + + $notification->addParsedAction($action); + } + return $notification; + break; default: // Unknown subject => Unknown notification => throw throw new \InvalidArgumentException(); diff --git a/lib/Service/NotificationService.php b/lib/Service/NotificationService.php index 089c312d..e4568f50 100644 --- a/lib/Service/NotificationService.php +++ b/lib/Service/NotificationService.php @@ -50,4 +50,26 @@ class NotificationService { $this->manager->notify($notification); } + + function credentialSharedNotification($data){ + $urlGenerator = \OC::$server->getURLGenerator(); + $link = $urlGenerator->getAbsoluteURL($urlGenerator->linkTo('','index.php/apps/passman/#/')); + $api = $urlGenerator->getAbsoluteURL($urlGenerator->linkTo('', 'index.php/apps/passman')); + $notification = $this->manager->createNotification(); + + $declineAction = $notification->createAction(); + $declineAction->setLabel('decline') + ->setLink($api . '/api/v2/sharing/decline/'. $data['req_id'], 'DELETE'); + + $notification->setApp('passman') + ->setUser($data['target_user']) + ->setDateTime(new \DateTime()) + ->setObject('credential', $data['item_id']) // $type and $id + ->setSubject('credential_shared', [$data['from_user'], $data['credential_label']]) // $subject and $parameters + ->setLink($link) + ->addAction($declineAction); + + $this->manager->notify($notification); + } + } \ No newline at end of file