passman/controller/vaultcontroller.php

126 lines
3.4 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;
use OCP\IRequest;
2016-09-11 05:30:17 +08:00
use OCP\AppFramework\Http\JSONResponse;
2016-09-10 00:02:53 +08:00
use OCP\AppFramework\ApiController;
use OCA\Passman\Service\VaultService;
2016-09-12 02:47:29 +08:00
use OCA\Passman\Service\CredentialService;
2016-09-12 01:45:20 +08:00
2016-09-15 00:57:38 +08:00
2016-09-10 00:02:53 +08:00
class VaultController extends ApiController {
2016-09-09 23:36:35 +08:00
private $userId;
private $vaultService;
2016-09-12 02:47:29 +08:00
private $credentialService;
2016-09-09 23:36:35 +08:00
public function __construct($AppName,
IRequest $request,
$UserId,
2016-09-12 02:47:29 +08:00
VaultService $vaultService,
CredentialService $credentialService) {
2016-09-09 23:36:35 +08:00
parent::__construct($AppName, $request);
$this->userId = $UserId;
$this->vaultService = $vaultService;
2016-09-12 02:47:29 +08:00
$this->credentialService = $credentialService;
2016-09-09 23:36:35 +08:00
}
2016-09-11 05:30:17 +08:00
/**
* @NoAdminRequired
*/
public function listVaults() {
2016-10-01 02:43:20 +08:00
$result = array();
$vaults = $this->vaultService->getByUser($this->userId);
2016-10-01 02:43:20 +08:00
$protected_credential_fields = array('getDescription','getEmail','getUsername','getPassword');
foreach($vaults as $vault){
$credential = $this->credentialService->getRandomCredentialByVaultId($vault->getId(), $this->userId);
$secret_field = $protected_credential_fields[array_rand($protected_credential_fields)];
2016-10-01 02:49:35 +08:00
array_push($result, array(
2016-10-01 02:43:20 +08:00
'vault_id' => $vault->getId(),
'guid' => $vault->getGuid(),
'name' => $vault->getName(),
'created' => $vault->getCreated(),
'public_sharing_key' => $vault->getPublicSharingKey(),
'last_access' => $vault->getlastAccess(),
2016-10-01 02:49:35 +08:00
'challenge_password' => $credential->{$secret_field}()
));
2016-10-01 02:43:20 +08:00
}
return new JSONResponse($result);
2016-09-11 05:30:17 +08:00
}
2016-09-09 23:36:35 +08:00
/**
* @NoAdminRequired
*/
public function create($vault_name) {
$vault = $this->vaultService->createVault($vault_name, $this->userId);
return new JSONResponse($vault);
2016-09-09 23:36:35 +08:00
}
/**
* @NoAdminRequired
*/
2016-09-09 23:41:02 +08:00
public function get($vault_id) {
2016-09-12 02:47:29 +08:00
$credentials = $this->credentialService->getCredentialsByVaultId($vault_id, $this->userId);
2016-09-29 03:22:49 +08:00
$vault = $this->vaultService->getById($vault_id, $this->userId);
$vault = $vault[0];
if($vault) {
$result = array(
'vault_id' => $vault->getId(),
'guid' => $vault->getGuid(),
'name' => $vault->getName(),
'created' => $vault->getCreated(),
'private_sharing_key' => $vault->getPrivateSharingKey(),
'public_sharing_key' => $vault->getPublicSharingKey(),
'sharing_keys_generated' => $vault->getSharingKeysGenerated(),
2016-09-29 04:05:09 +08:00
'vault_settings' => $vault->getVaultSettings(),
2016-09-29 03:22:49 +08:00
'last_access' => $vault->getlastAccess()
);
$result['credentials'] = $credentials;
$this->vaultService->setLastAccess($vault_id, $this->userId);
} else {
$result = array();
}
return new JSONResponse($result);
2016-09-09 23:36:35 +08:00
}
/**
* @NoAdminRequired
*/
2016-09-29 04:05:09 +08:00
public function update($vault_id, $name, $vault_settings) {
$vault = array_pop($this->vaultService->getById($vault_id, $this->userId));
if($name) {
$vault->setName($name);
}
if($vault_settings) {
$vault->setVaultSettings($vault_settings);
}
$this->vaultService->updateVault($vault);
2016-09-09 23:36:35 +08:00
}
2016-09-26 00:34:33 +08:00
/**
* @NoAdminRequired
*/
public function updateSharingKeys($vault_id, $private_sharing_key, $public_sharing_key) {
$this->vaultService->updateSharingKeys($vault_id, $private_sharing_key, $public_sharing_key);
return;
}
2016-09-09 23:36:35 +08:00
/**
* @NoAdminRequired
*/
2016-09-09 23:41:02 +08:00
public function delete($vault_id) {
2016-09-09 23:36:35 +08:00
return;
}
}