passman/lib/Service/CredentialService.php

87 lines
2.5 KiB
PHP
Raw Normal View History

2016-09-12 01:45:20 +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\Service;
2016-10-05 19:46:25 +08:00
use OCA\Passman\Db\Credential;
use OCA\Passman\Db\SharingACL;
use OCA\Passman\Db\SharingACLMapper;
2016-09-12 01:45:20 +08:00
use OCP\IConfig;
use OCP\AppFramework\Db\DoesNotExistException;
use OCA\Passman\Db\CredentialMapper;
class CredentialService {
private $credentialMapper;
private $sharingACL;
2016-09-12 01:45:20 +08:00
public function __construct(CredentialMapper $credentialMapper, SharingACLMapper $sharingACL) {
2016-09-12 01:45:20 +08:00
$this->credentialMapper = $credentialMapper;
$this->sharingACL = $sharingACL;
2016-09-12 01:45:20 +08:00
}
2016-10-05 19:46:25 +08:00
/**
* Create a new credential
* @param $user_id
* @param $item_guid
* @return Credential
*/
2016-09-12 01:45:20 +08:00
public function createCredential($credential) {
return $this->credentialMapper->create($credential);
}
2016-09-12 02:47:29 +08:00
2016-09-15 03:12:10 +08:00
public function updateCredential($credential) {
2016-09-24 00:17:47 +08:00
return $this->credentialMapper->updateCredential($credential);
}
public function upd($credential) {
return $this->credentialMapper->upd($credential);
2016-09-15 03:12:10 +08:00
}
public function deleteCredential($credential){
return $this->credentialMapper->deleteCredential($credential);
}
2016-09-23 22:52:41 +08:00
public function getCredentialsByVaultId($vault_id, $user_id) {
2016-09-12 02:47:29 +08:00
return $this->credentialMapper->getCredentialsByVaultId($vault_id, $user_id);
}
2016-09-23 22:52:41 +08:00
2016-10-01 02:43:20 +08:00
public function getRandomCredentialByVaultId($vault_id, $user_id) {
$credentials = $this->credentialMapper->getRandomCredentialByVaultId($vault_id, $user_id);
return array_pop($credentials);
2016-10-01 02:43:20 +08:00
}
2016-09-23 22:52:41 +08:00
public function getExpiredCredentials($timestamp) {
return $this->credentialMapper->getExpiredCredentials($timestamp);
}
2016-09-23 22:52:41 +08:00
2016-09-24 00:17:47 +08:00
public function getCredentialById($credential_id, $user_id){
$credential = $this->credentialMapper->getCredentialById($credential_id);
if ($credential->getUserId() == $user_id){
return $credential;
}
else {
$acl = $this->sharingACL->getItemACL($user_id, $credential->getGuid());
if ($acl->hasPermission(SharingACL::READ));
return $credential;
}
throw new DoesNotExistException("Did expect one result but found none when executing");
2016-09-23 22:52:41 +08:00
}
public function getCredentialLabelById($credential_id){
return $this->credentialMapper->getCredentialLabelById($credential_id);
}
public function getCredentialByGUID($credential_guid){
return $this->credentialMapper->getCredentialByGUID($credential_guid);
}
2016-09-12 01:45:20 +08:00
}