Merge pull request #226 from nextcloud/techdebt/noid/remove-unnecessary-compatibility-layers

Remove unnecessary compatibility layers
This commit is contained in:
Joas Schilling 2020-07-14 10:34:16 +02:00 committed by GitHub
commit 84627e8e09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 95 additions and 161 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

@ -13,7 +13,6 @@
namespace OCA\Registration\Controller;
use \OCP\IRequest;
use \OCP\AppFramework\Http\TemplateResponse;
use \OCP\AppFramework\Http\DataResponse;
use \OCP\AppFramework\Http;
use \OCP\AppFramework\Controller;
@ -40,8 +39,6 @@ class SettingsController extends Controller {
$this->appName = $appName;
}
/**
* @AdminRequired
*
@ -71,53 +68,25 @@ class SettingsController extends Controller {
$this->config->deleteAppValue($this->appName, 'registered_user_group');
return new DataResponse([
'data' => [
'message' => (string) $this->l10n->t('Saved'),
'message' => $this->l10n->t('Saved'),
],
'status' => 'success'
'status' => 'success',
]);
} elseif (in_array($registered_user_group, $group_id_list)) {
$this->config->setAppValue($this->appName, 'registered_user_group', $registered_user_group);
return new DataResponse([
'data' => [
'message' => (string) $this->l10n->t('Saved'),
'message' => $this->l10n->t('Saved'),
],
'status' => 'success'
'status' => 'success',
]);
} else {
return new DataResponse([
'data' => [
'message' => (string) $this->l10n->t('No such group'),
'message' => $this->l10n->t('No such group'),
],
'status' => 'error'
'status' => 'error',
], Http::STATUS_NOT_FOUND);
}
}
/**
* @AdminRequired
*
* @return TemplateResponse
*/
public function displayPanel() {
// handle groups
$groups = $this->groupmanager->search('');
$group_id_list = [];
foreach ($groups as $group) {
$group_id_list[] = $group->getGid();
}
$current_value = $this->config->getAppValue($this->appName, 'registered_user_group', 'none');
// handle domains
$allowed_domains = $this->config->getAppValue($this->appName, 'allowed_domains', '');
// handle admin validation
$admin_approval_required = $this->config->getAppValue($this->appName, 'admin_approval_required', "no");
return new TemplateResponse('registration', 'admin', [
'groups' => $group_id_list,
'current' => $current_value,
'allowed' => $allowed_domains,
'approval_required' => $admin_approval_required
], '');
}
}

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,33 +1,80 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2017 Pellaeon Lin <pellaeon@hs.ntnu.edu.tw>
*
* @author Pellaeon Lin <pellaeon@hs.ntnu.edu.tw>
*
* @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\Registration\Settings;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\Settings\ISettings;
use OCA\Registration\Controller\SettingsController;
class RegistrationSettings implements ISettings {
public function getForm() {
$controller = \OC::$server->query(SettingsController::class);
return $controller->displayPanel();
/** @var IConfig */
private $config;
/** @var IGroupManager */
private $groupManager;
/** @var string */
protected $appName;
public function __construct(string $appName,
IConfig $config,
IGroupManager $groupManager) {
$this->appName = $appName;
$this->config = $config;
$this->groupManager = $groupManager;
$this->appName = $appName;
}
public function getSection() {
public function getForm(): TemplateResponse {
// handle groups
$groups = $this->groupManager->search('');
$groupIds = [];
foreach ($groups as $group) {
$groupIds[] = $group->getGid();
}
$assignedGroups = $this->config->getAppValue($this->appName, 'registered_user_group', 'none');
// handle domains
$allowedDomains = $this->config->getAppValue($this->appName, 'allowed_domains', '');
// handle admin validation
$adminApprovalRequired = $this->config->getAppValue($this->appName, 'admin_approval_required', "no");
return new TemplateResponse('registration', 'admin', [
'groups' => $groupIds,
'current' => $assignedGroups,
'allowed' => $allowedDomains,
'approval_required' => $adminApprovalRequired
], '');
}
public function getSection(): string {
return 'additional';
}
public function getPriority() {
public function getPriority(): int {
return 50;
}
/*
* Below for ownCloud
*/
public function getPanel() {
return $this->getForm();
}
public function getSectionID() {
return $this->getSection();
}
}

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

@ -1,9 +1,7 @@
<?php
\OCP\Util::addStyle('registration', 'style');
\OCP\Util::addScript('registration', 'form');
if (\OCP\Util::getVersion()[0] >= 12) {
\OCP\Util::addStyle('core', 'guest');
}
?><form action="<?php print_unescaped(\OC::$server->getURLGenerator()->linkToRoute('registration.register.createAccount', ['token'=>$_['token']])) ?>" method="post">
<input type="hidden" name="requesttoken" value="<?php p($_['requesttoken']) ?>" />
<fieldset>

View File

@ -1,8 +1,6 @@
<?php
\OCP\Util::addStyle('registration', 'style');
if (\OCP\Util::getVersion()[0] >= 12) {
\OCP\Util::addStyle('core', 'guest');
}
?>
<ul class="msg error-wide nc-theming-main-text">
<li><?php print_unescaped($_['msg'])?></li>

View File

@ -1,8 +1,6 @@
<?php
\OCP\Util::addStyle('registration', 'style');
if (\OCP\Util::getVersion()[0] >= 12) {
\OCP\Util::addStyle('core', 'guest');
}
if ($_['entered']): ?>
<?php if (empty($_['errormsg'])): ?>
<ul class="success">

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