Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>pull/16682/head
parent
bd5c455da4
commit
4aba1f1cff
@ -0,0 +1,148 @@ |
||||
<?php |
||||
declare(strict_types=1); |
||||
/** |
||||
* @copyright Copyright (c) 2019 Arthur Schiwon <blizzz@arthur-schiwon.de> |
||||
* |
||||
* @author Arthur Schiwon <blizzz@arthur-schiwon.de> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* This program is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU Affero General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU Affero General Public License |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
* |
||||
*/ |
||||
|
||||
namespace OCA\WorkflowEngine\Controller; |
||||
|
||||
use Doctrine\DBAL\DBALException; |
||||
use OCA\WorkflowEngine\Helper\ScopeContext; |
||||
use OCA\WorkflowEngine\Manager; |
||||
use OCP\AppFramework\Http\DataResponse; |
||||
use OCP\AppFramework\OCS\OCSBadRequestException; |
||||
use OCP\AppFramework\OCS\OCSException; |
||||
use OCP\AppFramework\OCS\OCSForbiddenException; |
||||
use OCP\AppFramework\OCSController; |
||||
use OCP\IRequest; |
||||
|
||||
abstract class AWorkflowController extends OCSController { |
||||
|
||||
/** @var Manager */ |
||||
protected $manager; |
||||
|
||||
public function __construct( |
||||
$appName, |
||||
IRequest $request, |
||||
Manager $manager |
||||
) { |
||||
parent::__construct($appName, $request); |
||||
|
||||
$this->manager = $manager; |
||||
} |
||||
|
||||
/** |
||||
* @throws OCSForbiddenException |
||||
*/ |
||||
abstract protected function getScopeContext(): ScopeContext; |
||||
|
||||
/** |
||||
* Example: curl -u joann -H "OCS-APIREQUEST: true" "http://my.nc.srvr/ocs/v2.php/apps/workflowengine/api/v1/workflows/global?format=json" |
||||
* |
||||
* @throws OCSForbiddenException |
||||
*/ |
||||
public function index(): DataResponse { |
||||
$operationsByClass = $this->manager->getAllOperations($this->getScopeContext()); |
||||
|
||||
foreach ($operationsByClass as &$operations) { |
||||
foreach ($operations as &$operation) { |
||||
$operation = $this->manager->formatOperation($operation); |
||||
} |
||||
} |
||||
|
||||
return new DataResponse($operationsByClass); |
||||
} |
||||
|
||||
/** |
||||
* Example: curl -u joann -H "OCS-APIREQUEST: true" "http://my.nc.srvr/ocs/v2.php/apps/workflowengine/api/v1/workflows/global/OCA\\Workflow_DocToPdf\\Operation?format=json" |
||||
* |
||||
* @throws OCSForbiddenException |
||||
*/ |
||||
public function show(string $id): DataResponse { |
||||
$context = $this->getScopeContext(); |
||||
|
||||
// The ID corresponds to a class name |
||||
$operations = $this->manager->getOperations($id, $context); |
||||
|
||||
foreach ($operations as &$operation) { |
||||
$operation = $this->manager->formatOperation($operation); |
||||
} |
||||
|
||||
return new DataResponse($operations); |
||||
} |
||||
|
||||
/** |
||||
* @throws OCSBadRequestException |
||||
* @throws OCSForbiddenException |
||||
* @throws OCSException |
||||
*/ |
||||
public function create(string $class, string $name, array $checks, string $operation): DataResponse { |
||||
$context = $this->getScopeContext(); |
||||
try { |
||||
$operation = $this->manager->addOperation($class, $name, $checks, $operation, $context); |
||||
$operation = $this->manager->formatOperation($operation); |
||||
return new DataResponse($operation); |
||||
} catch (\UnexpectedValueException $e) { |
||||
throw new OCSBadRequestException($e->getMessage(), $e); |
||||
} catch (\DomainException $e) { |
||||
throw new OCSForbiddenException($e->getMessage(), $e); |
||||
} catch(DBALException $e) { |
||||
throw new OCSException('An internal error occurred', $e); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* @throws OCSBadRequestException |
||||
* @throws OCSForbiddenException |
||||
* @throws OCSException |
||||
*/ |
||||
public function update(int $id, string $name, array $checks, string $operation): DataResponse { |
||||
try { |
||||
$operation = $this->manager->updateOperation($id, $name, $checks, $operation, $this->getScopeContext()); |
||||
$operation = $this->manager->formatOperation($operation); |
||||
return new DataResponse($operation); |
||||
} catch (\UnexpectedValueException $e) { |
||||
throw new OCSBadRequestException($e->getMessage(), $e); |
||||
} catch (\DomainException $e) { |
||||
throw new OCSForbiddenException($e->getMessage(), $e); |
||||
} catch(DBALException $e) { |
||||
throw new OCSException('An internal error occurred', $e); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* @throws OCSBadRequestException |
||||
* @throws OCSForbiddenException |
||||
* @throws OCSException |
||||
*/ |
||||
public function destroy(int $id): DataResponse { |
||||
try { |
||||
$deleted = $this->manager->deleteOperation($id, $this->getScopeContext()); |
||||
return new DataResponse($deleted); |
||||
} catch (\UnexpectedValueException $e) { |
||||
throw new OCSBadRequestException($e->getMessage(), $e); |
||||
} catch (\DomainException $e) { |
||||
throw new OCSForbiddenException($e->getMessage(), $e); |
||||
} catch(DBALException $e) { |
||||
throw new OCSException('An internal error occurred', $e); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,117 @@ |
||||
<?php |
||||
declare(strict_types=1); |
||||
/** |
||||
* @copyright Copyright (c) 2019 Arthur Schiwon <blizzz@arthur-schiwon.de> |
||||
* |
||||
* @author Arthur Schiwon <blizzz@arthur-schiwon.de> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* This program is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU Affero General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU Affero General Public License |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
* |
||||
*/ |
||||
|
||||
namespace OCA\WorkflowEngine\Controller; |
||||
|
||||
use OCA\WorkflowEngine\Helper\ScopeContext; |
||||
use OCA\WorkflowEngine\Manager; |
||||
use OCP\AppFramework\Http\DataResponse; |
||||
use OCP\AppFramework\OCS\OCSBadRequestException; |
||||
use OCP\AppFramework\OCS\OCSForbiddenException; |
||||
use OCP\IRequest; |
||||
use OCP\IUserSession; |
||||
use OCP\WorkflowEngine\IManager; |
||||
|
||||
class UserWorkflowsController extends AWorkflowController { |
||||
|
||||
/** @var IUserSession */ |
||||
private $session; |
||||
|
||||
/** @var ScopeContext */ |
||||
private $scopeContext; |
||||
|
||||
public function __construct( |
||||
$appName, |
||||
IRequest $request, |
||||
Manager $manager, |
||||
IUserSession $session |
||||
) { |
||||
parent::__construct($appName, $request, $manager); |
||||
|
||||
$this->session = $session; |
||||
} |
||||
|
||||
/** |
||||
* Retrieve all configured workflow rules |
||||
* |
||||
* Example: curl -u joann -H "OCS-APIREQUEST: true" "http://my.nc.srvr/ocs/v2.php/apps/workflowengine/api/v1/workflows/user?format=json" |
||||
* |
||||
* @NoAdminRequired |
||||
* @throws OCSForbiddenException |
||||
*/ |
||||
public function index(): DataResponse { |
||||
return parent::index(); |
||||
} |
||||
|
||||
/** |
||||
* @NoAdminRequired |
||||
* |
||||
* Example: curl -u joann -H "OCS-APIREQUEST: true" "http://my.nc.srvr/ocs/v2.php/apps/workflowengine/api/v1/workflows/user/OCA\\Workflow_DocToPdf\\Operation?format=json" |
||||
* @throws OCSForbiddenException |
||||
*/ |
||||
public function show(string $id): DataResponse { |
||||
return parent::show($id); |
||||
} |
||||
|
||||
/** |
||||
* @NoAdminRequired |
||||
* @throws OCSBadRequestException |
||||
* @throws OCSForbiddenException |
||||
*/ |
||||
public function create(string $class, string $name, array $checks, string $operation): DataResponse { |
||||
return parent::create($class, $name, $checks, $operation); |
||||
} |
||||
|
||||
/** |
||||
* @NoAdminRequired |
||||
* @throws OCSBadRequestException |
||||
* @throws OCSForbiddenException |
||||
*/ |
||||
public function update(int $id, string $name, array $checks, string $operation): DataResponse { |
||||
return parent::update($id, $name, $checks, $operation); |
||||
} |
||||
|
||||
/** |
||||
* @NoAdminRequired |
||||
* @throws OCSForbiddenException |
||||
*/ |
||||
public function destroy(int $id): DataResponse { |
||||
return parent::destroy($id); |
||||
} |
||||
|
||||
/** |
||||
* @throws OCSForbiddenException |
||||
*/ |
||||
protected function getScopeContext(): ScopeContext { |
||||
if($this->scopeContext === null) { |
||||
$user = $this->session->getUser(); |
||||
if(!$user) { |
||||
throw new OCSForbiddenException('User not logged in'); |
||||
} |
||||
$this->scopeContext = new ScopeContext(IManager::SCOPE_USER, $user->getUID()); |
||||
} |
||||
return $this->scopeContext; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,78 @@ |
||||
<?php |
||||
declare(strict_types=1); |
||||
/** |
||||
* @copyright Copyright (c) 2019 Arthur Schiwon <blizzz@arthur-schiwon.de> |
||||
* |
||||
* @author Arthur Schiwon <blizzz@arthur-schiwon.de> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* This program is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU Affero General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU Affero General Public License |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
* |
||||
*/ |
||||
|
||||
namespace OCA\WorkflowEngine\Helper; |
||||
|
||||
use OCP\WorkflowEngine\IManager; |
||||
|
||||
class ScopeContext { |
||||
/** @var int */ |
||||
private $scope; |
||||
/** @var string */ |
||||
private $scopeId; |
||||
/** @var string */ |
||||
private $hash; |
||||
|
||||
public function __construct(int $scope, string $scopeId = null) { |
||||
$this->scope = $this->evaluateScope($scope); |
||||
$this->scopeId = $this->evaluateScopeId($scopeId); |
||||
} |
||||
|
||||
private function evaluateScope(int $scope): int { |
||||
if(in_array($scope, [IManager::SCOPE_ADMIN, IManager::SCOPE_USER], true)) { |
||||
return $scope; |
||||
} |
||||
throw new \InvalidArgumentException('Invalid scope'); |
||||
} |
||||
|
||||
private function evaluateScopeId(string $scopeId = null): string { |
||||
if($this->scope === IManager::SCOPE_USER |
||||
&& trim((string)$scopeId) === '') |
||||
{ |
||||
throw new \InvalidArgumentException('user scope requires a user id'); |
||||
} |
||||
return trim((string)$scopeId); |
||||
} |
||||
|
||||
/** |
||||
* @return int |
||||
*/ |
||||
public function getScope(): int { |
||||
return $this->scope; |
||||
} |
||||
|
||||
/** |
||||
* @return string |
||||
*/ |
||||
public function getScopeId(): string { |
||||
return $this->scopeId; |
||||
} |
||||
|
||||
public function getHash(): string { |
||||
if($this->hash === null) { |
||||
$this->hash = \hash('sha256', $this->getScope() . '::' . $this->getScopeId()); |
||||
} |
||||
return $this->hash; |
||||
} |
||||
} |
||||
Loading…
Reference in new issue