mirror of
https://github.com/nextcloud/passman.git
synced 2025-10-10 13:36:55 +08:00
Send notification to user
This commit is contained in:
parent
ee919c1159
commit
e8c60d579f
4 changed files with 96 additions and 16 deletions
|
@ -12,6 +12,8 @@
|
||||||
namespace OCA\Passman\Controller;
|
namespace OCA\Passman\Controller;
|
||||||
|
|
||||||
use OCA\Passman\Db\Vault;
|
use OCA\Passman\Db\Vault;
|
||||||
|
use OCA\Passman\Service\CredentialService;
|
||||||
|
use OCA\Passman\Service\NotificationService;
|
||||||
use OCA\Passman\Service\ShareService;
|
use OCA\Passman\Service\ShareService;
|
||||||
use OCP\IRequest;
|
use OCP\IRequest;
|
||||||
use OCP\AppFramework\Http\JSONResponse;
|
use OCP\AppFramework\Http\JSONResponse;
|
||||||
|
@ -27,7 +29,6 @@ use OCA\Passman\Service\ActivityService;
|
||||||
use OCA\Passman\Activity;
|
use OCA\Passman\Activity;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class ShareController extends ApiController {
|
class ShareController extends ApiController {
|
||||||
private $userId;
|
private $userId;
|
||||||
private $activityService;
|
private $activityService;
|
||||||
|
@ -35,6 +36,8 @@ class ShareController extends ApiController {
|
||||||
private $userManager;
|
private $userManager;
|
||||||
private $vaultService;
|
private $vaultService;
|
||||||
private $shareService;
|
private $shareService;
|
||||||
|
private $credentialService;
|
||||||
|
private $notificationService;
|
||||||
|
|
||||||
private $limit = 50;
|
private $limit = 50;
|
||||||
private $offset = 0;
|
private $offset = 0;
|
||||||
|
@ -46,19 +49,52 @@ class ShareController extends ApiController {
|
||||||
IUserManager $userManager,
|
IUserManager $userManager,
|
||||||
ActivityService $activityService,
|
ActivityService $activityService,
|
||||||
VaultService $vaultService,
|
VaultService $vaultService,
|
||||||
ShareService $shareService
|
ShareService $shareService,
|
||||||
|
CredentialService $credentialService,
|
||||||
|
NotificationService $notificationService
|
||||||
) {
|
) {
|
||||||
parent::__construct($AppName, $request);
|
parent::__construct($AppName, $request);
|
||||||
|
|
||||||
$this->userId = $UserId;
|
$this->userId = $UserId;
|
||||||
$this->userManager = $userManager;
|
$this->userManager = $userManager;
|
||||||
$this->groupManager = $groupManager;
|
$this->groupManager = $groupManager;
|
||||||
$this->activityService = $activityService;
|
$this->activityService = $activityService;
|
||||||
$this->vaultService = $vaultService;
|
$this->vaultService = $vaultService;
|
||||||
$this->shareService = $shareService;
|
$this->shareService = $shareService;
|
||||||
|
$this->credentialService = $credentialService;
|
||||||
|
$this->notificationService = $notificationService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function applyIntermediateShare($item_id, $item_guid, $vaults, $permissions) {
|
public function applyIntermediateShare($item_id, $item_guid, $vaults, $permissions) {
|
||||||
return new JSONResponse($this->shareService->createBulkRequests($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) {
|
public function searchUsers($search) {
|
||||||
|
|
|
@ -54,7 +54,9 @@ class Application extends App {
|
||||||
$server->getUserManager(),
|
$server->getUserManager(),
|
||||||
$c->query('ActivityService'),
|
$c->query('ActivityService'),
|
||||||
$c->query('VaultService'),
|
$c->query('VaultService'),
|
||||||
$c->query('ShareService')
|
$c->query('ShareService'),
|
||||||
|
$c->query('CredentialService'),
|
||||||
|
$c->query('NotificationService')
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -61,6 +61,26 @@ class Notifier implements INotifier {
|
||||||
}
|
}
|
||||||
return $notification;
|
return $notification;
|
||||||
break;
|
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:
|
default:
|
||||||
// Unknown subject => Unknown notification => throw
|
// Unknown subject => Unknown notification => throw
|
||||||
throw new \InvalidArgumentException();
|
throw new \InvalidArgumentException();
|
||||||
|
|
|
@ -50,4 +50,26 @@ class NotificationService {
|
||||||
$this->manager->notify($notification);
|
$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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue