passman/controller/credentialcontroller.php

312 lines
9.5 KiB
PHP
Raw Normal View History

2016-09-09 23:36:35 +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-05 19:46:25 +08:00
use OCA\Files_External\NotFoundException;
use OCA\Passman\Db\SharingACL;
use OCA\Passman\Utility\NotFoundJSONResponse;
2016-10-05 20:30:28 +08:00
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
2016-09-09 23:36:35 +08:00
use OCP\IRequest;
2016-09-12 01:45:20 +08:00
use OCP\AppFramework\Http\JSONResponse;
2016-09-10 00:02:53 +08:00
use OCP\AppFramework\ApiController;
2016-09-12 01:45:20 +08:00
use OCA\Passman\Service\CredentialService;
2016-09-23 22:52:41 +08:00
use OCA\Passman\Activity;
use OCA\Passman\Service\ActivityService;
2016-09-24 17:40:15 +08:00
use OCA\Passman\Service\CredentialRevisionService;
use OCA\Passman\Service\ShareService;
use OCP\IUser;
2016-09-24 18:59:19 +08:00
2016-09-12 01:45:20 +08:00
class CredentialController extends ApiController {
2016-09-09 23:36:35 +08:00
private $userId;
2016-09-12 01:45:20 +08:00
private $credentialService;
2016-09-23 22:52:41 +08:00
private $activityService;
2016-09-24 17:40:15 +08:00
private $credentialRevisionService;
2016-10-05 19:46:25 +08:00
private $sharingService;
2016-09-15 03:12:10 +08:00
2016-09-12 01:45:20 +08:00
public function __construct($AppName,
IRequest $request,
2016-10-06 00:52:14 +08:00
$userId,
2016-09-23 22:52:41 +08:00
CredentialService $credentialService,
2016-09-24 17:40:15 +08:00
ActivityService $activityService,
CredentialRevisionService $credentialRevisionService,
2016-10-05 19:46:25 +08:00
ShareService $sharingService
) {
2016-09-09 23:36:35 +08:00
parent::__construct($AppName, $request);
2016-10-06 00:52:14 +08:00
$this->userId = $userId;
2016-09-12 01:45:20 +08:00
$this->credentialService = $credentialService;
2016-09-23 22:52:41 +08:00
$this->activityService = $activityService;
2016-09-24 17:40:15 +08:00
$this->credentialRevisionService = $credentialRevisionService;
2016-10-05 19:46:25 +08:00
$this->sharingService = $sharingService;
2016-09-09 23:36:35 +08:00
}
/**
* @NoAdminRequired
*/
2016-09-12 01:45:20 +08:00
public function createCredential($changed, $created,
$credential_id, $custom_fields, $delete_time,
$description, $email, $expire_time, $favicon, $files, $guid,
$hidden, $label, $otp, $password, $renew_interval,
$tags, $url, $username, $vault_id) {
$credential = array(
'credential_id' => $credential_id,
'guid' => $guid,
'user_id' => $this->userId,
'vault_id' => $vault_id,
'label' => $label,
'description' => $description,
'created' => $created,
'changed' => $changed,
'tags' => $tags,
'email' => $email,
'username' => $username,
'password' => $password,
'url' => $url,
'favicon' => $favicon,
'renew_interval' => $renew_interval,
'expire_time' => $expire_time,
'delete_time' => $delete_time,
'files' => $files,
'custom_fields' => $custom_fields,
'otp' => $otp,
'hidden' => $hidden,
);
$credential = $this->credentialService->createCredential($credential);
2016-09-23 22:52:41 +08:00
$link = ''; // @TODO create direct link to credential
if (!$credential->getHidden()) {
2016-10-05 19:46:25 +08:00
$this->activityService->add(
Activity::SUBJECT_ITEM_CREATED_SELF, array($label, $this->userId),
'', array(),
$link, $this->userId, Activity::TYPE_ITEM_ACTION);
}
2016-09-12 01:45:20 +08:00
return new JSONResponse($credential);
2016-09-09 23:36:35 +08:00
}
/**
* @NoAdminRequired
*/
2016-10-07 18:43:54 +08:00
public function getCredential($credential_guid) {
return new JSONResponse($this->credentialService->getCredentialByGUID($credential_guid, $this->userId));
2016-09-09 23:36:35 +08:00
}
/**
* @NoAdminRequired
*/
2016-09-15 03:12:10 +08:00
public function updateCredential($changed, $created,
2016-10-07 18:43:54 +08:00
$credential_id, $custom_fields, $delete_time, $credential_guid,
2016-09-15 03:12:10 +08:00
$description, $email, $expire_time, $favicon, $files, $guid,
$hidden, $label, $otp, $password, $renew_interval,
2016-10-08 00:26:53 +08:00
$tags, $url, $username, $vault_id, $revision_created, $shared_key, $acl, $unshare_action, $set_share_key, $skip_revision) {
2016-10-04 03:36:39 +08:00
2016-10-07 18:43:54 +08:00
$storedCredential = $this->credentialService->getCredentialByGUID($credential_guid, $this->userId);
2016-10-04 03:36:39 +08:00
2016-09-15 03:12:10 +08:00
$credential = array(
'credential_id' => $credential_id,
'guid' => $guid,
'label' => $label,
'description' => $description,
'created' => $created,
'changed' => $changed,
'vault_id' => $vault_id,
2016-09-15 03:12:10 +08:00
'tags' => $tags,
'email' => $email,
'username' => $username,
'password' => $password,
'url' => $url,
'favicon' => $favicon,
'renew_interval' => $renew_interval,
'expire_time' => $expire_time,
'files' => $files,
'custom_fields' => $custom_fields,
'delete_time' => $delete_time,
'hidden' => $hidden,
2016-09-15 03:12:10 +08:00
'otp' => $otp,
);
2016-09-23 22:52:41 +08:00
2016-10-04 03:42:16 +08:00
2016-10-05 19:46:25 +08:00
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);
}
}
2016-09-23 22:52:41 +08:00
$link = ''; // @TODO create direct link to credential
2016-10-05 19:46:25 +08:00
$activity = false;
2016-09-24 18:59:19 +08:00
if ($revision_created) {
2016-10-05 19:46:25 +08:00
$activity = 'item_apply_revision';
2016-09-24 18:59:19 +08:00
$this->activityService->add(
2016-10-05 19:46:25 +08:00
$activity . '_self', array($label, $this->userId, $revision_created),
2016-09-24 18:59:19 +08:00
'', array(),
$link, $this->userId, Activity::TYPE_ITEM_ACTION);
} else if (($storedCredential->getDeleteTime() === 0) && (int) $delete_time > 0) {
2016-10-05 19:46:25 +08:00
$activity = 'item_deleted';
2016-09-23 22:52:41 +08:00
$this->activityService->add(
2016-10-05 19:46:25 +08:00
$activity . '_self', array($label, $this->userId),
2016-09-23 22:52:41 +08:00
'', array(),
$link, $this->userId, Activity::TYPE_ITEM_ACTION);
} else if (($storedCredential->getDeleteTime() > 0) && (int) $delete_time === 0) {
2016-10-05 19:46:25 +08:00
$activity = 'item_recovered';
2016-09-23 22:52:41 +08:00
$this->activityService->add(
2016-10-05 19:46:25 +08:00
$activity . '_self', array($label, $this->userId),
2016-09-23 22:52:41 +08:00
'', array(),
$link, $this->userId, Activity::TYPE_ITEM_ACTION);
} else if ($label !== $storedCredential->getLabel()) {
2016-10-05 19:46:25 +08:00
$activity = 'item_renamed';
2016-09-23 22:52:41 +08:00
$this->activityService->add(
2016-10-05 19:46:25 +08:00
$activity . '_self', array($storedCredential->getLabel(), $label, $this->userId),
2016-09-23 22:52:41 +08:00
'', array(),
2016-09-24 18:59:19 +08:00
$link, $this->userId, Activity::TYPE_ITEM_RENAMED);
2016-09-23 22:52:41 +08:00
} else {
2016-10-05 19:46:25 +08:00
$activity = 'item_edited';
2016-09-23 22:52:41 +08:00
$this->activityService->add(
2016-10-05 19:46:25 +08:00
$activity . '_self', array($label, $this->userId),
2016-09-23 22:52:41 +08:00
'', array(),
$link, $this->userId, Activity::TYPE_ITEM_ACTION);
}
2016-10-05 19:46:25 +08:00
$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) {
2016-10-05 19:46:25 +08:00
continue;
}
$this->activityService->add(
$activity, $params,
'', array(),
$link, $target_user, Activity::TYPE_ITEM_ACTION);
}
if ($this->userId !== $storedCredential->getUserId()) {
2016-10-05 19:46:25 +08:00
$this->activityService->add(
$activity, $params,
'', array(),
$link, $storedCredential->getUserId(), Activity::TYPE_ITEM_ACTION);
}
}
if ($set_share_key === true) {
2016-10-06 01:56:46 +08:00
$storedCredential->setSharedKey($shared_key);
2016-10-06 04:34:34 +08:00
$credential['shared_key'] = $shared_key;
2016-10-06 01:56:46 +08:00
}
if ($unshare_action === true) {
2016-10-06 02:07:13 +08:00
$storedCredential->setSharedKey('');
$credential['shared_key'] = '';
}
if (!$skip_revision) {
2016-10-08 00:26:53 +08:00
$this->credentialRevisionService->createRevision($storedCredential, $storedCredential->getUserId(), $credential_id, $this->userId);
}
$credential = $this->credentialService->updateCredential($credential);
2016-09-23 22:52:41 +08:00
2016-09-15 03:12:10 +08:00
return new JSONResponse($credential);
2016-09-09 23:36:35 +08:00
}
/**
* @NoAdminRequired
*/
2016-10-07 18:43:54 +08:00
public function deleteCredential($credential_guid) {
$credential = $this->credentialService->getCredentialByGUID($credential_guid, $this->userId);
2016-09-24 18:59:19 +08:00
if ($credential) {
$result = $this->credentialService->deleteCredential($credential);
$this->activityService->add(
'item_destroyed_self', array($credential->getLabel()),
'', array(),
'', $this->userId, Activity::TYPE_ITEM_ACTION);
} else {
$result = false;
}
return new JSONResponse($result);
}
/**
* @NoAdminRequired
*/
public function getRevision($credential_guid) {
try {
$credential = $this->credentialService->getCredentialByGUID($credential_guid);
}
catch (DoesNotExistException $ex){
return new NotFoundJSONResponse();
}
2016-10-06 01:02:27 +08:00
// If the request was made by the owner of the credential
if ($this->userId === $credential->getUserId()) {
$result = $this->credentialRevisionService->getRevisions($credential->getId(), $this->userId);
}
else {
$acl = $this->sharingService->getACL($this->userId, $credential_guid);
if ($acl->hasPermission(SharingACL::HISTORY)){
$result = $this->credentialRevisionService->getRevisions($credential->getId());
}
else {
return new NotFoundJSONResponse();
}
}
2016-09-24 17:40:15 +08:00
return new JSONResponse($result);
}
/**
* @NoAdminRequired
*/
2016-09-24 18:59:19 +08:00
public function deleteRevision($credential_id, $revision_id) {
$result = $this->credentialRevisionService->deleteRevision($revision_id, $this->userId);
return new JSONResponse($result);
}
2016-10-05 20:30:28 +08:00
/**
* @NoAdminRequired
*/
public function updateRevision($credential_guid, $revision_id, $credential_data) {
2016-10-05 20:30:28 +08:00
$revision = null;
2016-10-06 00:24:36 +08:00
try {
2016-10-07 18:43:54 +08:00
$credential = $this->credentialService->getCredentialByGUID($credential_guid, $this->userId);
2016-10-06 00:24:36 +08:00
} catch (DoesNotExistException $e) {
2016-10-07 18:43:54 +08:00
return new NotFoundJSONResponse();
2016-10-06 00:24:36 +08:00
}
try {
2016-10-05 20:30:28 +08:00
$revision = $this->credentialRevisionService->getRevision($revision_id);
} catch (DoesNotExistException $exception) {
2016-10-07 18:43:54 +08:00
return new NotFoundJSONResponse();
2016-10-05 20:30:28 +08:00
}
$revision->setCredentialData($credential_data);
2016-10-06 00:24:36 +08:00
2016-10-05 20:30:28 +08:00
$this->credentialRevisionService->updateRevision($revision);
2016-10-06 00:24:36 +08:00
return new JSONResponse(array());
2016-10-05 20:30:28 +08:00
}
2016-09-09 23:36:35 +08:00
}