From 4a319ef21ae5527a6ac4bd309d90139084d0d56f Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 13 Jul 2020 09:22:32 +0200 Subject: [PATCH] Remove unnecessary compatibility layers Signed-off-by: Joas Schilling --- lib/AppInfo/Application.php | 14 ++-- lib/Controller/ApiController.php | 10 +-- lib/Db/RegistrationMapper.php | 5 +- lib/Util/CoreBridge.php | 78 --------------------- tests/Unit/Controller/ApiControllerTest.php | 11 +-- 5 files changed, 21 insertions(+), 97 deletions(-) delete mode 100644 lib/Util/CoreBridge.php diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index c5c9309..fd82a0d 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -23,19 +23,19 @@ namespace OCA\Registration\AppInfo; +use OCA\Registration\Capabilities; use OCP\AppFramework\App; +use OC\Authentication\Token\IProvider; class Application extends App { - public function __construct(array $urlParams = []) { - parent::__construct('registration', $urlParams); + public function __construct() { + parent::__construct('registration'); $container = $this->getContainer(); - $container->registerService('OC\Authentication\Token\IProvider', function ($c) { - return \OC::$server->query('OC\Authentication\Token\IProvider'); + $container->registerService(IProvider::class, function ($c) { + return \OC::$server->query(IProvider::class); // TODO needed? }); - if (interface_exists('\OCP\Capabilities\IPublicCapability')) { - $container->registerCapability(\OCA\Registration\Capabilities::class); - } + $container->registerCapability(Capabilities::class); } } diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index c7847e9..f217525 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -27,9 +27,11 @@ use OCA\Registration\Db\Registration; use OCA\Registration\Service\MailService; use OCA\Registration\Service\RegistrationException; use OCA\Registration\Service\RegistrationService; -use OCA\Registration\Util\CoreBridge; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Http; +use OCP\AppFramework\OCS\OCSBadRequestException; +use OCP\AppFramework\OCS\OCSException; +use OCP\AppFramework\OCS\OCSNotFoundException; use OCP\AppFramework\OCSController; use OCP\AppFramework\Http\DataResponse; use OCP\Defaults; @@ -80,7 +82,7 @@ class ApiController extends OCSController { $this->registrationService->validateDisplayname($displayname); $this->registrationService->validateUsername($username); } catch (RegistrationException $e) { - throw CoreBridge::createException('OCSBadRequestException', $e->getMessage()); + throw new OCSBadRequestException($e->getMessage()); } $data = [ 'username' => $username, @@ -103,7 +105,7 @@ class ApiController extends OCSController { /** @var Registration $registration */ $registration = $this->registrationService->getRegistrationForSecret($clientSecret); } catch (DoesNotExistException $e) { - throw CoreBridge::createException('OCSNotFoundException', 'No pending registration.'); + throw new OCSNotFoundException('No pending registration.'); } if (!$registration->getEmailConfirmed()) { @@ -173,7 +175,7 @@ class ApiController extends OCSController { } return new DataResponse($data, Http::STATUS_OK); } catch (RegistrationException $exception) { - throw CoreBridge::createException('OCSException', $exception->getMessage(), $exception->getCode()); + throw new OCSException($exception->getMessage(), $exception->getCode()); } } } diff --git a/lib/Db/RegistrationMapper.php b/lib/Db/RegistrationMapper.php index 56d2a75..6c144a3 100644 --- a/lib/Db/RegistrationMapper.php +++ b/lib/Db/RegistrationMapper.php @@ -111,7 +111,7 @@ class RegistrationMapper extends QBMapper { * @param Registration $registration */ public function generateNewToken(Registration $registration): void { - $token = $this->random->generate(10, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS); + $token = $this->random->generate(10, ISecureRandom::CHAR_HUMAN_READABLE); $registration->setToken($token); } @@ -119,8 +119,7 @@ class RegistrationMapper extends QBMapper { * @param Registration $registration */ public function generateClientSecret(Registration $registration): void { - $token = $this->random->generate(32, 'abcdefgijkmnopqrstwxyzABCDEFGHJKLMNPQRSTWXYZ23456789'); - //FIXME eqivalent to ISecureRandom::CHAR_HUMAN_READABLE introduced in https://github.com/nextcloud/server/commit/f2a2b34e4639e88f8d948a388a51f010212b42a3 but not supported in ownCloud yet. We'll just use the string for now then switch to constants when supported. + $token = $this->random->generate(32, ISecureRandom::CHAR_HUMAN_READABLE); $registration->setClientSecret($token); } } diff --git a/lib/Util/CoreBridge.php b/lib/Util/CoreBridge.php deleted file mode 100644 index 3875dd5..0000000 --- a/lib/Util/CoreBridge.php +++ /dev/null @@ -1,78 +0,0 @@ - [ - 'OCP\AppFramework\OCS\OCSException', - 'OC\OCS\Exception', - ], - 'OCSBadRequestException' => [ - 'OCP\AppFramework\OCS\OCSBadRequestException', - 'OC\OCS\Exception', - ], - 'OCSNotFoundException' => [ - 'OCP\AppFramework\OCS\OCSNotFoundException', - 'OC\OCS\Exception', - ], - 'DoesNotExistException' => [ - 'OCP\AppFramework\Db\DoesNotExistException', - 'OCP\AppFramework\Db\DoesNotExistException', - ], - ]; - - if (!array_key_exists($className, $classes)) { - throw new \LogicException('No valid exception class found'); - } - - foreach ($classes[$className] as $class) { - if (class_exists($class)) { - return $class; - } - } - - throw new \LogicException('No valid exception class found'); - } - - /** - * @param string $className - * @param null|string $message - * @param null|int $code - * @return \Exception - */ - public static function createException($className, $message = null, $code = null) { - $exceptionClassName = self::exceptionClass($className); - - $reflection = new \ReflectionClass($exceptionClassName); - $params = $reflection->getConstructor()->getParameters(); - - if ($params[0]->getClass() && ($params[0]->getClass()->getName() === 'OC\OCS\Result' || $params[0]->getClass()->getName() === 'OC_OCS_Result')) { - $subClass = $params[0]->getClass()->getName(); - return new $exceptionClassName(new $subClass($message, $code)); - } - - if (count($params) >= 2) { - if ($params[1]->getClass() && $params[1]->getClass()->getName() === 'Exception') { - return new $exceptionClassName($message); - } - - return new $exceptionClassName($message, $code); - } - - if ($exceptionClassName === 'OCP\AppFramework\OCS\OCSNotFoundException') { - return new $exceptionClassName($message); - } - - return new $exceptionClassName(); - } -} diff --git a/tests/Unit/Controller/ApiControllerTest.php b/tests/Unit/Controller/ApiControllerTest.php index f4b3351..2701f42 100644 --- a/tests/Unit/Controller/ApiControllerTest.php +++ b/tests/Unit/Controller/ApiControllerTest.php @@ -15,9 +15,10 @@ use OCA\Registration\Controller\ApiController; use OCA\Registration\Db\Registration; use OCA\Registration\Service\MailService; use OCA\Registration\Service\RegistrationService; -use OCA\Registration\Util\CoreBridge; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\OCS\OCSException; +use OCP\AppFramework\OCS\OCSNotFoundException; use OCP\Defaults; use OCP\IL10N; use OCP\IRequest; @@ -80,7 +81,7 @@ class ApiControllerTest extends TestCase { } public function testValidateFailEmail() { - $exception = CoreBridge::createException('OCSException', '', 999); + $exception = new OCSException('', 999); $this->expectException(get_class($exception)); @@ -93,7 +94,7 @@ class ApiControllerTest extends TestCase { } public function testValidateFailDisplayname() { - $exception = CoreBridge::createException('OCSException', '', 999); + $exception = new OCSException('', 999); $this->expectException(get_class($exception)); @@ -106,7 +107,7 @@ class ApiControllerTest extends TestCase { } public function testValidateFailUsername() { - $exception = CoreBridge::createException('OCSException', '', 999); + $exception = new OCSException('', 999); $this->expectException(get_class($exception)); @@ -119,7 +120,7 @@ class ApiControllerTest extends TestCase { } public function testStatusNoRegistration() { - $exception = CoreBridge::createException('OCSNotFoundException', '', 404); + $exception = new OCSNotFoundException(''); $this->expectException(get_class($exception));