-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Contexts): API to add and remove a node to a Context
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
- Loading branch information
Showing
11 changed files
with
325 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
|
||
namespace OCA\Tables\Errors; | ||
|
||
class BadRequestError extends \Exception { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace OCA\Tables\Middleware; | ||
|
||
use OCA\Tables\Errors\InternalError; | ||
use OCA\Tables\Errors\PermissionError; | ||
use OCA\Tables\Service\PermissionsService; | ||
use OCP\AppFramework\Controller; | ||
use OCP\AppFramework\Middleware; | ||
use OCP\AppFramework\Utility\IControllerMethodReflector; | ||
use OCP\IRequest; | ||
|
||
class PermissionMiddleware extends Middleware { | ||
private IControllerMethodReflector $reflector; | ||
private PermissionsService $permissionsService; | ||
private ?string $userId; | ||
private IRequest $request; | ||
|
||
public function __construct( | ||
IControllerMethodReflector $reflector, | ||
PermissionsService $permissionsService, | ||
IRequest $request, | ||
?string $userId, | ||
) { | ||
|
||
$this->reflector = $reflector; | ||
$this->permissionsService = $permissionsService; | ||
$this->userId = $userId; | ||
$this->request = $request; | ||
} | ||
|
||
/** | ||
* @throws PermissionError | ||
* @throws InternalError | ||
*/ | ||
public function beforeController(Controller $controller, string $methodName): void { | ||
$this->assertCanManageNode(); | ||
$this->assertCanManageContext(); | ||
} | ||
|
||
/** | ||
* @throws PermissionError | ||
* @throws InternalError | ||
*/ | ||
protected function assertCanManageNode(): void { | ||
if ($this->reflector->hasAnnotation('CanManageNode')) { | ||
$nodeId = $this->request->getParam('nodeId'); | ||
$nodeType = $this->request->getParam('nodeType'); | ||
|
||
if (!is_numeric($nodeId) || !is_numeric($nodeType)) { | ||
throw new InternalError('Cannot identify node'); | ||
} | ||
|
||
if ($this->userId === null) { | ||
throw new PermissionError('User not authenticated'); | ||
} | ||
|
||
if (!$this->permissionsService->canManageNodeById((int)$nodeType, (int)$nodeId, $this->userId)) { | ||
throw new PermissionError(sprintf('User %s cannot manage node %d (type %d)', | ||
$this->userId, (int)$nodeId, (int)$nodeType | ||
)); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @throws PermissionError | ||
* @throws InternalError | ||
*/ | ||
protected function assertCanManageContext(): void { | ||
if ($this->reflector->hasAnnotation('CanManageContext')) { | ||
$contextId = $this->request->getParam('contextId'); | ||
|
||
if (!is_numeric($contextId)) { | ||
throw new InternalError('Cannot identify context'); | ||
} | ||
|
||
if ($this->userId === null) { | ||
throw new PermissionError('User not authenticated'); | ||
} | ||
|
||
if (!$this->permissionsService->canManageContextById((int)$contextId, $this->userId)) { | ||
throw new PermissionError(sprintf('User %s cannot manage context %d', | ||
$this->userId, (int)$contextId | ||
)); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.