Merge pull request #32587 from nextcloud/bugfix/noid/improve-jsconfighelper

Improve JSConfigHelper code quality a bit
This commit is contained in:
Joas Schilling 2022-05-31 10:29:30 +02:00 committed by GitHub
commit 279e06a80f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 81 additions and 126 deletions

View file

@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
*
@ -33,9 +35,12 @@ namespace OC\Template;
use bantu\IniGetWrapper\IniGetWrapper;
use OC\CapabilitiesManager;
use OC\Share\Share;
use OCP\App\AppPathNotFoundException;
use OCP\App\IAppManager;
use OCP\Constants;
use OCP\Defaults;
use OCP\Files\FileInfo;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IInitialStateService;
@ -44,62 +49,29 @@ use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\User\Backend\IPasswordConfirmationBackend;
use OCP\Util;
class JSConfigHelper {
/** @var IL10N */
private $l;
/** @var Defaults */
private $defaults;
/** @var IAppManager */
private $appManager;
/** @var ISession */
private $session;
/** @var IUser|null */
private $currentUser;
/** @var IConfig */
private $config;
/** @var IGroupManager */
private $groupManager;
/** @var IniGetWrapper */
private $iniWrapper;
/** @var IURLGenerator */
private $urlGenerator;
/** @var CapabilitiesManager */
private $capabilitiesManager;
/** @var IInitialStateService */
private $initialStateService;
protected IL10N $l;
protected Defaults $defaults;
protected IAppManager $appManager;
protected ISession $session;
protected ?IUser $currentUser;
protected IConfig $config;
protected IGroupManager $groupManager;
protected IniGetWrapper $iniWrapper;
protected IURLGenerator $urlGenerator;
protected CapabilitiesManager $capabilitiesManager;
protected IInitialStateService $initialStateService;
/** @var array user back-ends excluded from password verification */
private $excludedUserBackEnds = ['user_saml' => true, 'user_globalsiteselector' => true];
/**
* @param IL10N $l
* @param Defaults $defaults
* @param IAppManager $appManager
* @param ISession $session
* @param IUser|null $currentUser
* @param IConfig $config
* @param IGroupManager $groupManager
* @param IniGetWrapper $iniWrapper
* @param IURLGenerator $urlGenerator
* @param CapabilitiesManager $capabilitiesManager
*/
public function __construct(IL10N $l,
Defaults $defaults,
IAppManager $appManager,
ISession $session,
$currentUser,
?IUser $currentUser,
IConfig $config,
IGroupManager $groupManager,
IniGetWrapper $iniWrapper,
@ -119,7 +91,7 @@ class JSConfigHelper {
$this->initialStateService = $initialStateService;
}
public function getConfig() {
public function getConfig(): string {
$userBackendAllowsPasswordConfirmation = true;
if ($this->currentUser !== null) {
$uid = $this->currentUser->getUID();
@ -144,10 +116,13 @@ class JSConfigHelper {
}
foreach ($apps as $app) {
$apps_paths[$app] = \OC_App::getAppWebPath($app);
try {
$apps_paths[$app] = $this->appManager->getAppWebPath($app);
} catch (AppPathNotFoundException $e) {
$apps_paths[$app] = false;
}
}
$enableLinkPasswordByDefault = $this->config->getAppValue('core', 'shareapi_enable_link_password_by_default', 'no');
$enableLinkPasswordByDefault = $enableLinkPasswordByDefault === 'yes';
$defaultExpireDateEnabled = $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no') === 'yes';
@ -193,14 +168,14 @@ class JSConfigHelper {
'session_lifetime' => min($this->config->getSystemValue('session_lifetime', $this->iniWrapper->getNumeric('session.gc_maxlifetime')), $this->iniWrapper->getNumeric('session.gc_maxlifetime')),
'session_keepalive' => $this->config->getSystemValue('session_keepalive', true),
'auto_logout' => $this->config->getSystemValue('auto_logout', false),
'version' => implode('.', \OCP\Util::getVersion()),
'version' => implode('.', Util::getVersion()),
'versionstring' => \OC_Util::getVersionString(),
'enable_avatars' => true, // here for legacy reasons - to not crash existing code that relies on this value
'lost_password_link' => $this->config->getSystemValue('lost_password_link', null),
'modRewriteWorking' => $this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true',
'sharing.maxAutocompleteResults' => max(0, $this->config->getSystemValueInt('sharing.maxAutocompleteResults', Constants::SHARING_MAX_AUTOCOMPLETE_RESULTS_DEFAULT)),
'sharing.minSearchStringLength' => $this->config->getSystemValueInt('sharing.minSearchStringLength', 0),
'blacklist_files_regex' => \OCP\Files\FileInfo::BLACKLIST_FILES_REGEX,
'blacklist_files_regex' => FileInfo::BLACKLIST_FILES_REGEX,
];
$array = [
@ -275,10 +250,10 @@ class JSConfigHelper {
'defaultExpireDateEnabled' => $defaultExpireDateEnabled,
'defaultExpireDate' => $defaultExpireDate,
'defaultExpireDateEnforced' => $enforceDefaultExpireDate,
'enforcePasswordForPublicLink' => \OCP\Util::isPublicLinkPasswordRequired(),
'enforcePasswordForPublicLink' => Util::isPublicLinkPasswordRequired(),
'enableLinkPasswordByDefault' => $enableLinkPasswordByDefault,
'sharingDisabledForUser' => \OCP\Util::isSharingDisabledForUser(),
'resharingAllowed' => \OC\Share\Share::isResharingAllowed(),
'sharingDisabledForUser' => Util::isSharingDisabledForUser(),
'resharingAllowed' => Share::isResharingAllowed(),
'remoteShareAllowed' => $outgoingServer2serverShareEnabled,
'federatedCloudShareDoc' => $this->urlGenerator->linkToDocs('user-sharing-federated'),
'allowGroupSharing' => \OC::$server->getShareManager()->allowGroupSharing(),

View file

@ -66,25 +66,20 @@ class RateLimitingMiddlewareTest extends TestCase {
public function testBeforeControllerWithoutAnnotation() {
$this->reflector
->expects($this->at(0))
->expects($this->exactly(4))
->method('getAnnotationParameter')
->with('AnonRateThrottle', 'limit')
->willReturn('');
$this->reflector
->expects($this->at(1))
->method('getAnnotationParameter')
->with('AnonRateThrottle', 'period')
->willReturn('');
$this->reflector
->expects($this->at(2))
->method('getAnnotationParameter')
->with('UserRateThrottle', 'limit')
->willReturn('');
$this->reflector
->expects($this->at(3))
->method('getAnnotationParameter')
->with('UserRateThrottle', 'period')
->willReturn('');
->withConsecutive(
['AnonRateThrottle', 'limit'],
['AnonRateThrottle', 'period'],
['UserRateThrottle', 'limit'],
['UserRateThrottle', 'period']
)
->willReturnMap([
['AnonRateThrottle', 'limit', ''],
['AnonRateThrottle', 'period', ''],
['UserRateThrottle', 'limit', ''],
['UserRateThrottle', 'period', ''],
]);
$this->limiter
->expects($this->never())
@ -107,25 +102,20 @@ class RateLimitingMiddlewareTest extends TestCase {
->willReturn('127.0.0.1');
$this->reflector
->expects($this->at(0))
->expects($this->exactly(4))
->method('getAnnotationParameter')
->with('AnonRateThrottle', 'limit')
->willReturn('100');
$this->reflector
->expects($this->at(1))
->method('getAnnotationParameter')
->with('AnonRateThrottle', 'period')
->willReturn('10');
$this->reflector
->expects($this->at(2))
->method('getAnnotationParameter')
->with('UserRateThrottle', 'limit')
->willReturn('');
$this->reflector
->expects($this->at(3))
->method('getAnnotationParameter')
->with('UserRateThrottle', 'period')
->willReturn('');
->withConsecutive(
['AnonRateThrottle', 'limit'],
['AnonRateThrottle', 'period'],
['UserRateThrottle', 'limit'],
['UserRateThrottle', 'period']
)
->willReturnMap([
['AnonRateThrottle', 'limit', '100'],
['AnonRateThrottle', 'period', '10'],
['UserRateThrottle', 'limit', ''],
['UserRateThrottle', 'period', ''],
]);
$this->limiter
->expects($this->never())
@ -155,25 +145,20 @@ class RateLimitingMiddlewareTest extends TestCase {
->willReturn($user);
$this->reflector
->expects($this->at(0))
->expects($this->exactly(4))
->method('getAnnotationParameter')
->with('AnonRateThrottle', 'limit')
->willReturn('');
$this->reflector
->expects($this->at(1))
->method('getAnnotationParameter')
->with('AnonRateThrottle', 'period')
->willReturn('');
$this->reflector
->expects($this->at(2))
->method('getAnnotationParameter')
->with('UserRateThrottle', 'limit')
->willReturn('100');
$this->reflector
->expects($this->at(3))
->method('getAnnotationParameter')
->with('UserRateThrottle', 'period')
->willReturn('10');
->withConsecutive(
['AnonRateThrottle', 'limit'],
['AnonRateThrottle', 'period'],
['UserRateThrottle', 'limit'],
['UserRateThrottle', 'period']
)
->willReturnMap([
['AnonRateThrottle', 'limit', ''],
['AnonRateThrottle', 'period', ''],
['UserRateThrottle', 'limit', '100'],
['UserRateThrottle', 'period', '10'],
]);
$this->limiter
->expects($this->never())
@ -201,25 +186,20 @@ class RateLimitingMiddlewareTest extends TestCase {
->willReturn(false);
$this->reflector
->expects($this->at(0))
->expects($this->exactly(4))
->method('getAnnotationParameter')
->with('AnonRateThrottle', 'limit')
->willReturn('200');
$this->reflector
->expects($this->at(1))
->method('getAnnotationParameter')
->with('AnonRateThrottle', 'period')
->willReturn('20');
$this->reflector
->expects($this->at(2))
->method('getAnnotationParameter')
->with('UserRateThrottle', 'limit')
->willReturn('100');
$this->reflector
->expects($this->at(3))
->method('getAnnotationParameter')
->with('UserRateThrottle', 'period')
->willReturn('10');
->withConsecutive(
['AnonRateThrottle', 'limit'],
['AnonRateThrottle', 'period'],
['UserRateThrottle', 'limit'],
['UserRateThrottle', 'period']
)
->willReturnMap([
['AnonRateThrottle', 'limit', '200'],
['AnonRateThrottle', 'period', '20'],
['UserRateThrottle', 'limit', '100'],
['UserRateThrottle', 'period', '10'],
]);
$this->limiter
->expects($this->never())