2016-10-02 07:56:36 +08:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Created by PhpStorm.
|
|
|
|
* User: wolfi
|
|
|
|
* Date: 1/10/16
|
|
|
|
* Time: 21:54
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Passman\Service;
|
|
|
|
|
|
|
|
|
2016-10-03 00:01:41 +08:00
|
|
|
use Icewind\SMB\Share;
|
2016-10-02 23:32:22 +08:00
|
|
|
use OCA\Passman\Db\CredentialMapper;
|
2016-10-02 07:56:36 +08:00
|
|
|
use OCA\Passman\Db\ShareRequest;
|
|
|
|
use OCA\Passman\Db\ShareRequestMapper;
|
2016-10-02 19:48:23 +08:00
|
|
|
use OCA\Passman\Db\SharingACL;
|
2016-10-02 07:56:36 +08:00
|
|
|
use OCA\Passman\Db\SharingACLMapper;
|
|
|
|
|
|
|
|
class ShareService {
|
|
|
|
private $sharingACL;
|
|
|
|
private $shareRequest;
|
2016-10-02 23:32:22 +08:00
|
|
|
private $credential;
|
2016-10-02 07:56:36 +08:00
|
|
|
|
2016-10-02 23:32:22 +08:00
|
|
|
public function __construct(SharingACLMapper $sharingACL, ShareRequestMapper $shareRequest, CredentialMapper $credentials) {
|
2016-10-02 07:56:36 +08:00
|
|
|
$this->sharingACL = $sharingACL;
|
|
|
|
$this->shareRequest = $shareRequest;
|
2016-10-02 23:32:22 +08:00
|
|
|
$this->credential = $credentials;
|
2016-10-02 07:56:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates requests for all the items on the request array of objects.
|
2016-10-02 19:48:23 +08:00
|
|
|
* This array must follow this spec:
|
|
|
|
* user_id: The target user id
|
|
|
|
* vault_id: The id of the target vault
|
|
|
|
* guid: The guid of the target vault
|
|
|
|
* key: The shared key cyphered with the target vault RSA public key
|
2016-10-02 07:56:36 +08:00
|
|
|
* @param $target_item_id string The shared item ID
|
|
|
|
* @param $target_item_guid string The shared item GUID
|
2016-10-02 17:43:31 +08:00
|
|
|
* @param $request_array array
|
2016-10-02 07:56:36 +08:00
|
|
|
* @param $permissions integer Must be created with a bitmask from options on the ShareRequest class
|
2016-10-02 17:43:31 +08:00
|
|
|
* @return array Array of sharing requests
|
2016-10-02 07:56:36 +08:00
|
|
|
*/
|
2016-10-02 22:10:04 +08:00
|
|
|
public function createBulkRequests($target_item_id, $target_item_guid, $request_array, $permissions, $credential_owner) {
|
2016-10-02 07:56:36 +08:00
|
|
|
$created = (new \DateTime())->getTimestamp();
|
2016-10-02 17:43:31 +08:00
|
|
|
$requests = array();
|
2016-10-02 07:56:36 +08:00
|
|
|
foreach ($request_array as $req){
|
|
|
|
$t = new ShareRequest();
|
|
|
|
$t->setItemId($target_item_id);
|
|
|
|
$t->setItemGuid($target_item_guid);
|
2016-10-02 19:48:23 +08:00
|
|
|
$t->setTargetUserId($req['user_id']);
|
2016-10-02 17:43:31 +08:00
|
|
|
$t->setTargetVaultId($req['vault_id']);
|
|
|
|
$t->setTargetVaultGuid($req['guid']);
|
|
|
|
$t->setSharedKey($req['key']);
|
2016-10-02 07:56:36 +08:00
|
|
|
$t->setPermissions($permissions);
|
|
|
|
$t->setCreated($created);
|
2016-10-02 22:10:04 +08:00
|
|
|
$t->setFromUserId($credential_owner);
|
2016-10-02 17:43:31 +08:00
|
|
|
array_push($requests, $this->shareRequest->createRequest($t));
|
2016-10-02 07:56:36 +08:00
|
|
|
}
|
2016-10-02 17:43:31 +08:00
|
|
|
return $requests;
|
2016-10-02 07:56:36 +08:00
|
|
|
}
|
2016-10-02 19:48:23 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies the given share, defaults to no expire
|
|
|
|
* @param $item_guid
|
|
|
|
* @param $target_vault_guid
|
|
|
|
* @param $final_shared_key
|
|
|
|
*/
|
|
|
|
public function applyShare($item_guid, $target_vault_guid, $final_shared_key){
|
|
|
|
$request = $this->shareRequest->getRequestByGuid($item_guid, $target_vault_guid);
|
|
|
|
$permissions = $request->getPermissions();
|
|
|
|
|
|
|
|
$acl = new SharingACL();
|
|
|
|
$acl->setItemId($request->getItemId());
|
|
|
|
$acl->setItemGuid($request->getItemGuid());
|
|
|
|
$acl->setUserId($request->getTargetUserId());
|
|
|
|
$acl->setCreated($request->getCreated());
|
|
|
|
$acl->setExpire(0);
|
|
|
|
$acl->setPermissions($permissions);
|
|
|
|
$acl->setVaultId($request->getTargetVaultId());
|
2016-10-03 00:22:10 +08:00
|
|
|
$acl->setVaultGuid($request->getTargetVaultGuid());
|
2016-10-02 19:48:23 +08:00
|
|
|
$acl->setSharedKey($final_shared_key);
|
|
|
|
|
|
|
|
$this->sharingACL->createACLEntry($acl);
|
|
|
|
$this->shareRequest->cleanItemRequestsForUser($request->getItemId(), $request->getTargetUserId());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtains pending requests for the given user ID
|
|
|
|
* @param $user_id
|
|
|
|
* @return \OCA\Passman\Db\ShareRequest[]
|
|
|
|
*/
|
|
|
|
public function getUserPendingRequests($user_id){
|
|
|
|
return $this->shareRequest->getUserPendingRequests($user_id);
|
|
|
|
}
|
2016-10-02 23:19:12 +08:00
|
|
|
|
2016-10-03 01:04:19 +08:00
|
|
|
public function getSharedItems($user_id, $vault_guid){
|
|
|
|
$entries = $this->sharingACL->getVaultEntries($user_id, $vault_guid);
|
2016-10-03 01:29:06 +08:00
|
|
|
|
2016-10-02 23:32:22 +08:00
|
|
|
$return = [];
|
|
|
|
foreach ($entries as $entry){
|
2016-10-03 04:03:34 +08:00
|
|
|
// Check if the user can read the credential, probably unnecesary, but just to be sure
|
|
|
|
if (!$entry->hasPermission(SharingACL::READ)) continue;
|
2016-10-03 19:58:53 +08:00
|
|
|
|
2016-10-02 23:32:22 +08:00
|
|
|
$tmp = $entry->jsonSerialize();
|
2016-10-03 01:42:50 +08:00
|
|
|
$tmp['credential_data'] = $this->credential->getCredentialById($entry->getItemId())->jsonSerialize();
|
2016-10-03 21:01:30 +08:00
|
|
|
|
|
|
|
if (!$entry->hasPermission(SharingACL::FILES)) unset($tmp['credential_data']['files']);
|
2016-10-03 01:42:50 +08:00
|
|
|
unset($tmp['credential_data']['shared_key']);
|
2016-10-02 23:32:22 +08:00
|
|
|
$return[] = $tmp;
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2016-10-02 23:37:06 +08:00
|
|
|
|
2016-10-02 23:19:12 +08:00
|
|
|
/**
|
|
|
|
* Deletes an share reuqest by id
|
2016-10-03 00:01:41 +08:00
|
|
|
* @param ShareRequest $request
|
|
|
|
* @return \OCA\Passman\Db\ShareRequest[]
|
2016-10-02 23:19:12 +08:00
|
|
|
*/
|
2016-10-03 00:01:41 +08:00
|
|
|
public function cleanItemRequestsForUser(ShareRequest $request){
|
|
|
|
return $this->shareRequest->cleanItemRequestsForUser($request->getItemId(), $request->getTargetUserId());
|
|
|
|
}
|
2016-10-02 23:19:12 +08:00
|
|
|
|
2016-10-03 00:01:41 +08:00
|
|
|
/**
|
|
|
|
* Get an share request by id
|
|
|
|
*/
|
|
|
|
public function getShareRequestById($id){
|
|
|
|
return $this->shareRequest->getShareRequestById($id);
|
2016-10-02 23:19:12 +08:00
|
|
|
}
|
2016-10-03 00:42:43 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get an share request by $item_guid and $target_vault_guid
|
|
|
|
*
|
|
|
|
*/
|
2016-10-03 21:50:10 +08:00
|
|
|
public function getRequestByGuid($item_guid, $target_vault_guid) {
|
2016-10-03 00:42:43 +08:00
|
|
|
return $this->shareRequest->getRequestByGuid($item_guid, $target_vault_guid);
|
|
|
|
}
|
|
|
|
|
2016-10-03 21:50:10 +08:00
|
|
|
/**
|
|
|
|
* Get the access control list by item guid
|
|
|
|
*
|
|
|
|
* @param string $item_guid
|
|
|
|
* @return \OCA\Passman\Db\SharingACL[]
|
|
|
|
*/
|
|
|
|
public function getCredentialAclList($item_guid) {
|
|
|
|
return $this->sharingACL->getCredentialAclList($item_guid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get pending share requests by guid
|
|
|
|
*
|
|
|
|
* @param string $item_guid
|
|
|
|
* @return \OCA\Passman\Db\ShareRequest[]
|
|
|
|
*/
|
|
|
|
public function getShareRequestsByGuid($item_guid) {
|
|
|
|
return $this->shareRequest->getShareRequestsByGuid($item_guid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get pending share requests by guid
|
|
|
|
*
|
|
|
|
* @param ShareRequest $request
|
|
|
|
* @return \OCA\Passman\Db\ShareRequest[]
|
|
|
|
*/
|
|
|
|
public function deleteShareRequest(ShareRequest $request) {
|
|
|
|
return $this->shareRequest->deleteShareRequest($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get pending share requests by guid
|
|
|
|
*
|
|
|
|
* @param ShareRequest $request
|
|
|
|
* @return \OCA\Passman\Db\ShareRequest[]
|
|
|
|
*/
|
|
|
|
public function deleteShareACL(SharingACL $ACL) {
|
|
|
|
return $this->sharingACL->deleteShareACL($ACL);
|
|
|
|
}
|
2016-10-02 07:56:36 +08:00
|
|
|
}
|