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;
|
2016-10-04 03:18:28 +08:00
|
|
|
use OCA\Passman\Db\SharingACL;
|
|
|
|
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;
|
2016-10-04 03:18:28 +08:00
|
|
|
use OCA\Passman\Service\ShareService;
|
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,
|
|
|
|
$UserId,
|
2016-09-23 22:52:41 +08:00
|
|
|
CredentialService $credentialService,
|
2016-09-24 17:40:15 +08:00
|
|
|
ActivityService $activityService,
|
2016-10-04 03:18:28 +08:00
|
|
|
CredentialRevisionService $credentialRevisionService,
|
2016-10-05 19:46:25 +08:00
|
|
|
ShareService $sharingService
|
|
|
|
) {
|
2016-09-09 23:36:35 +08:00
|
|
|
parent::__construct($AppName, $request);
|
|
|
|
$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
|
2016-10-05 19:46:25 +08:00
|
|
|
if(!$credential->getHidden()) {
|
|
|
|
$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-09-10 00:46:20 +08:00
|
|
|
public function getCredential($credential_id) {
|
2016-09-24 00:17:47 +08:00
|
|
|
return new JSONResponse($this->credentialService->getCredentialById($credential_id, $this->userId));
|
2016-09-09 23:36:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*/
|
2016-09-15 03:12:10 +08:00
|
|
|
public function updateCredential($changed, $created,
|
|
|
|
$credential_id, $custom_fields, $delete_time,
|
|
|
|
$description, $email, $expire_time, $favicon, $files, $guid,
|
|
|
|
$hidden, $label, $otp, $password, $renew_interval,
|
2016-10-04 02:33:32 +08:00
|
|
|
$tags, $url, $username, $vault_id, $revision_created, $shared_key, $acl) {
|
2016-10-04 03:36:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
$storedCredential = $this->credentialService->getCredentialById($credential_id, $this->userId);
|
|
|
|
|
2016-09-15 03:12:10 +08:00
|
|
|
$credential = array(
|
|
|
|
'credential_id' => $credential_id,
|
|
|
|
'guid' => $guid,
|
|
|
|
'label' => $label,
|
|
|
|
'description' => $description,
|
|
|
|
'created' => $created,
|
|
|
|
'changed' => $changed,
|
2016-09-16 20:01:25 +08:00
|
|
|
'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,
|
2016-09-16 20:01:25 +08:00
|
|
|
'delete_time' => $delete_time,
|
|
|
|
'hidden' => $hidden,
|
2016-09-15 03:12:10 +08:00
|
|
|
'otp' => $otp,
|
2016-10-05 19:46:25 +08:00
|
|
|
'shared_key' => ($shared_key === null) ? '' : $shared_key,
|
2016-09-15 03:12:10 +08:00
|
|
|
);
|
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-10-04 03:36:39 +08:00
|
|
|
//@TODO Add activities for non owned items
|
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) && $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) && $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){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$this->activityService->add(
|
|
|
|
$activity, $params,
|
|
|
|
'', array(),
|
|
|
|
$link, $target_user, Activity::TYPE_ITEM_ACTION);
|
|
|
|
}
|
|
|
|
if ($this->userId != $storedCredential->getUserId()) {
|
|
|
|
$this->activityService->add(
|
|
|
|
$activity, $params,
|
|
|
|
'', array(),
|
|
|
|
$link, $storedCredential->getUserId(), Activity::TYPE_ITEM_ACTION);
|
|
|
|
}
|
|
|
|
}
|
2016-09-23 22:52:41 +08:00
|
|
|
|
2016-10-04 03:36:39 +08:00
|
|
|
$this->credentialRevisionService->createRevision($storedCredential, $storedCredential->getUserId(), $credential_id, $this->userId);
|
2016-10-03 21:47:00 +08:00
|
|
|
$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-09-10 00:46:20 +08:00
|
|
|
public function deleteCredential($credential_id) {
|
2016-09-24 03:30:07 +08:00
|
|
|
$credential = $this->credentialService->getCredentialById($credential_id, $this->userId);
|
2016-09-24 18:59:19 +08:00
|
|
|
if ($credential) {
|
2016-09-24 03:30:07 +08:00
|
|
|
$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);
|
2016-09-10 00:46:20 +08:00
|
|
|
}
|
|
|
|
|
2016-09-23 23:22:47 +08:00
|
|
|
|
2016-09-10 00:46:20 +08:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*/
|
|
|
|
public function getRevision($credential_id) {
|
2016-09-24 17:40:15 +08:00
|
|
|
$result = $this->credentialRevisionService->getRevisions($credential_id, $this->userId);
|
|
|
|
return new JSONResponse($result);
|
2016-09-10 00:46:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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-09-10 00:46:20 +08:00
|
|
|
}
|
2016-09-09 23:36:35 +08:00
|
|
|
}
|