mirror of
https://github.com/nextcloud/passman.git
synced 2025-10-09 21:16:18 +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,14 +29,15 @@ 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;
|
||||||
private $groupManager;
|
private $groupManager;
|
||||||
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,27 +49,60 @@ 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) {
|
||||||
$users = array();
|
$users = array();
|
||||||
$usersTmp = $this->userManager->searchDisplayName($search, $this->limit, $this->offset);
|
$usersTmp = $this->userManager->searchDisplayName($search, $this->limit, $this->offset);
|
||||||
|
|
||||||
foreach ($usersTmp as $user) {
|
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(
|
$users[] = array(
|
||||||
'text' => $user->getDisplayName(),
|
'text' => $user->getDisplayName(),
|
||||||
'uid' => $user->getUID(),
|
'uid' => $user->getUID(),
|
||||||
|
@ -90,10 +126,10 @@ class ShareController extends ApiController {
|
||||||
/**
|
/**
|
||||||
* @NoAdminRequired
|
* @NoAdminRequired
|
||||||
*/
|
*/
|
||||||
public function getVaultsByUser($user_id){
|
public function getVaultsByUser($user_id) {
|
||||||
$user_vaults = $this->vaultService->getByUser($user_id);
|
$user_vaults = $this->vaultService->getByUser($user_id);
|
||||||
$result = array();
|
$result = array();
|
||||||
foreach($user_vaults as $vault){
|
foreach ($user_vaults as $vault) {
|
||||||
array_push($result,
|
array_push($result,
|
||||||
array(
|
array(
|
||||||
'vault_id' => $vault->getId(),
|
'vault_id' => $vault->getId(),
|
||||||
|
@ -105,7 +141,7 @@ class ShareController extends ApiController {
|
||||||
return new JSONResponse($result);
|
return new JSONResponse($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function share($credential){
|
public function share($credential) {
|
||||||
|
|
||||||
$link = '';
|
$link = '';
|
||||||
$this->activityService->add(
|
$this->activityService->add(
|
||||||
|
@ -115,11 +151,11 @@ class ShareController extends ApiController {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function savePendingRequest($item_guid, $target_vault_guid, $final_shared_key) {
|
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() {
|
public function getPendingRequests() {
|
||||||
return new JSONResponse($this->shareService->getUserPendingRequests());
|
return new JSONResponse($this->shareService->getUserPendingRequests());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -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