passman/controller/sharecontroller.php

242 lines
6.6 KiB
PHP
Raw Normal View History

2016-09-23 18:02:41 +08:00
<?php
/**
* Nextcloud - passman
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Sander Brand <brantje@gmail.com>
* @copyright Sander Brand 2016
*/
namespace OCA\Passman\Controller;
2016-10-02 23:19:12 +08:00
use OCA\Passman\Db\ShareRequest;
use OCA\Passman\Db\Vault;
2016-10-02 20:41:38 +08:00
use OCA\Passman\Service\CredentialService;
use OCA\Passman\Service\NotificationService;
use OCA\Passman\Service\ShareService;
2016-09-23 18:02:41 +08:00
use OCP\IRequest;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\ApiController;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IUserManager;
use OCP\IUser;
use OCA\Passman\Service\VaultService;
2016-09-24 21:58:02 +08:00
use OCA\Passman\Service\ActivityService;
2016-10-02 19:35:49 +08:00
use OCA\Passman\Activity;
2016-09-24 21:58:02 +08:00
2016-09-23 18:02:41 +08:00
class ShareController extends ApiController {
private $userId;
2016-09-23 23:03:36 +08:00
private $activityService;
2016-09-23 18:02:41 +08:00
private $groupManager;
private $userManager;
private $vaultService;
2016-10-02 20:41:38 +08:00
private $shareService;
private $credentialService;
private $notificationService;
2016-09-23 18:02:41 +08:00
private $limit = 50;
private $offset = 0;
public function __construct($AppName,
IRequest $request,
2016-09-27 03:23:24 +08:00
$UserId,
2016-09-23 18:02:41 +08:00
IGroupManager $groupManager,
2016-09-23 23:03:36 +08:00
IUserManager $userManager,
ActivityService $activityService,
VaultService $vaultService,
2016-10-02 20:41:38 +08:00
ShareService $shareService,
CredentialService $credentialService,
NotificationService $notificationService
2016-09-23 18:02:41 +08:00
) {
parent::__construct($AppName, $request);
2016-10-02 20:41:38 +08:00
2016-09-23 18:02:41 +08:00
$this->userId = $UserId;
$this->userManager = $userManager;
$this->groupManager = $groupManager;
2016-09-23 23:03:36 +08:00
$this->activityService = $activityService;
$this->vaultService = $vaultService;
2016-10-02 20:41:38 +08:00
$this->shareService = $shareService;
$this->credentialService = $credentialService;
$this->notificationService = $notificationService;
2016-09-23 18:02:41 +08:00
}
/**
* @NoAdminRequired
*/
2016-10-02 20:41:38 +08:00
public function applyIntermediateShare($item_id, $item_guid, $vaults, $permissions) {
/**
* Assemble notification
*/
$credential = $this->credentialService->getCredentialById($item_id, $this->userId->getUID());
$credential_owner = $credential->getUserId();
$result = $this->shareService->createBulkRequests($item_id, $item_guid, $vaults, $permissions, $credential_owner);
2016-10-02 20:41:38 +08:00
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);
}
2016-09-23 18:02:41 +08:00
/**
* @NoAdminRequired
*/
2016-09-23 18:02:41 +08:00
public function searchUsers($search) {
$users = array();
$usersTmp = $this->userManager->searchDisplayName($search, $this->limit, $this->offset);
foreach ($usersTmp as $user) {
2016-10-02 20:41:38 +08:00
if ($this->userId != $user->getUID() && count($this->vaultService->getByUser($user->getUID())) >= 1) {
2016-09-27 01:20:48 +08:00
$users[] = array(
'text' => $user->getDisplayName(),
'uid' => $user->getUID(),
'type' => 'user'
);
}
2016-09-23 18:02:41 +08:00
}
return $users;
2016-09-23 18:02:41 +08:00
}
/**
* @NoAdminRequired
*/
public function search($search) {
$user_search = $this->searchUsers($search);
return new JSONResponse($user_search);
2016-09-23 18:02:41 +08:00
}
/**
* @NoAdminRequired
*/
2016-10-02 20:41:38 +08:00
public function getVaultsByUser($user_id) {
$user_vaults = $this->vaultService->getByUser($user_id);
$result = array();
2016-10-02 20:41:38 +08:00
foreach ($user_vaults as $vault) {
array_push($result,
array(
'vault_id' => $vault->getId(),
'guid' => $vault->getGuid(),
'public_sharing_key' => $vault->getPublicSharingKey(),
'user_id' => $user_id,
));
}
return new JSONResponse($result);
}
2016-10-02 23:32:22 +08:00
/**
* @NoAdminRequired
* @param $credential
*/
2016-10-02 20:41:38 +08:00
public function share($credential) {
2016-09-23 23:03:36 +08:00
$link = '';
$this->activityService->add(
'item_shared', array($credential->label, $this->userId),
'', array(),
$link, $this->userId, Activity::TYPE_ITEM_ACTION);
}
/**
* @NoAdminRequired
*/
public function savePendingRequest($item_guid, $target_vault_guid, $final_shared_key) {
$sr = $this->shareService->getRequestByGuid($item_guid, $target_vault_guid);
$manager = \OC::$server->getNotificationManager();
$notification = $manager->createNotification();
$notification->setApp('passman')
->setObject('passman_share_request', $sr->getId())
->setUser($this->userId->getUID());
$manager->markProcessed($notification);
$notification = array(
'from_user' => ucfirst($this->userId->getDisplayName()),
'credential_label' => $this->credentialService->getCredentialLabelById($sr->getItemId())->getLabel(),
'target_user' => $sr->getFromUserId(),
'req_id' => $sr->getId()
);
$this->notificationService->credentialAcceptedSharedNotification(
$notification
);
2016-10-02 20:41:38 +08:00
$this->shareService->applyShare($item_guid, $target_vault_guid, $final_shared_key);
}
/**
* @NoAdminRequired
*/
public function getPendingRequests() {
$requests = $this->shareService->getUserPendingRequests($this->userId->getUID());
$results = array();
foreach ($requests as $request){
$result = $request->jsonSerialize();
$c = $this->credentialService->getCredentialLabelById($request->getItemId());
$result['credential_label'] = $c->getLabel();
array_push($results, $result);
}
return new JSONResponse($results);
2016-10-02 20:41:38 +08:00
}
2016-10-02 23:32:22 +08:00
/**
* Obtains the list of credentials shared with this vault
* @NoAdminRequired
*/
public function getVaultItems($vault_guid){
2016-10-03 01:18:40 +08:00
$this->shareService->getSharedItems($this->userId->getUID(), $vault_guid);
2016-10-02 23:32:22 +08:00
}
2016-10-02 23:19:12 +08:00
public function deleteShareRequest($share_request_id){
$sr = $this->shareService->getShareRequestById($share_request_id);
$notification = array(
'from_user' => ucfirst($this->userId->getDisplayName()),
'credential_label' => $this->credentialService->getCredentialLabelById($sr->getItemId())->getLabel(),
'target_user' => $sr->getFromUserId(),
'req_id' => $sr->getId()
);
$this->notificationService->credentialDeclinedSharedNotification(
$notification
);
2016-10-02 23:19:12 +08:00
$manager = \OC::$server->getNotificationManager();
$notification = $manager->createNotification();
$notification->setApp('passman')
->setObject('passman_share_request', $share_request_id)
->setUser($this->userId->getUID());
$manager->markProcessed($notification);
//@TODO load other requests and delete them by item id.
2016-10-03 00:01:41 +08:00
$this->shareService->cleanItemRequestsForUser($sr);
return new JSONResponse(array('result'=> true));
2016-10-02 23:19:12 +08:00
}
2016-09-23 18:02:41 +08:00
}