Remove unnecessary compatibility layers

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2020-07-13 09:22:32 +02:00
parent 2c8fcb6c6a
commit 4a319ef21a
No known key found for this signature in database
GPG Key ID: 7076EA9751AACDDA
5 changed files with 21 additions and 97 deletions

View File

@ -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);
}
}

View File

@ -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());
}
}
}

View File

@ -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);
}
}

View File

@ -1,78 +0,0 @@
<?php
namespace OCA\Registration\Util;
class CoreBridge {
/**
* This function maps an exception class to the available exception class of the core
* in order to provide cross-core and cross-version compatibility.
*
* @param string $className
* @return string
* @throws \LogicException
*/
public static function exceptionClass($className) {
static $classes = [
'OCSException' => [
'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();
}
}

View File

@ -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));