Skip to content

Commit

Permalink
add check isFairUseOfFreePushService on login
Browse files Browse the repository at this point in the history
Signed-off-by: Vitor Mattos <vitor@php.rio>
  • Loading branch information
vitormattos authored and nickvergessen committed Oct 22, 2021
1 parent 32b9bee commit b6d1ca8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
18 changes: 17 additions & 1 deletion core/Controller/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@
use OCP\Defaults;
use OCP\IConfig;
use OCP\IInitialStateService;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IRequest;
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Notification\IManager;
use OCP\Util;

class LoginController extends Controller {
Expand Down Expand Up @@ -81,6 +83,10 @@ class LoginController extends Controller {
private $initialStateService;
/** @var WebAuthnManager */
private $webAuthnManager;
/** @var IManager */
private $manager;
/** @var IL10N */
private $l10n;

public function __construct(?string $appName,
IRequest $request,
Expand All @@ -94,7 +100,9 @@ public function __construct(?string $appName,
Throttler $throttler,
Chain $loginChain,
IInitialStateService $initialStateService,
WebAuthnManager $webAuthnManager) {
WebAuthnManager $webAuthnManager,
IManager $manager,
IL10N $l10n) {
parent::__construct($appName, $request);
$this->userManager = $userManager;
$this->config = $config;
Expand All @@ -107,6 +115,8 @@ public function __construct(?string $appName,
$this->loginChain = $loginChain;
$this->initialStateService = $initialStateService;
$this->webAuthnManager = $webAuthnManager;
$this->manager = $manager;
$this->l10n = $l10n;
}

/**
Expand Down Expand Up @@ -153,6 +163,12 @@ public function showLoginForm(string $user = null, string $redirect_url = null):
}

$loginMessages = $this->session->get('loginMessages');
if (!$this->manager->isFairUseOfFreePushService()) {
if (!is_array($loginMessages)) {
$loginMessages = [[], []];
}
$loginMessages[1][] = $this->l10n->t('This community release of Nextcloud is unsupported and instant notifications are unavailable.');
}
if (is_array($loginMessages)) {
[$errors, $messages] = $loginMessages;
$this->initialStateService->provideInitialState('core', 'loginMessages', $messages);
Expand Down
32 changes: 25 additions & 7 deletions tests/Core/Controller/LoginControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@
use OCP\Defaults;
use OCP\IConfig;
use OCP\IInitialStateService;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IRequest;
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Notification\IManager;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

Expand Down Expand Up @@ -86,6 +88,12 @@ class LoginControllerTest extends TestCase {
/** @var \OC\Authentication\WebAuthn\Manager|MockObject */
private $webAuthnManager;

/** @var IManager|MockObject */
private $notificationManager;

/** @var IL10N|MockObject */
private $l;

protected function setUp(): void {
parent::setUp();
$this->request = $this->createMock(IRequest::class);
Expand All @@ -101,6 +109,13 @@ protected function setUp(): void {
$this->chain = $this->createMock(LoginChain::class);
$this->initialStateService = $this->createMock(IInitialStateService::class);
$this->webAuthnManager = $this->createMock(\OC\Authentication\WebAuthn\Manager::class);
$this->notificationManager = $this->createMock(IManager::class);
$this->l = $this->createMock(IL10N::class);
$this->l->expects($this->any())
->method('t')
->willReturnCallback(function ($text, $parameters = []) {
return vsprintf($text, $parameters);
});


$this->request->method('getRemoteAddress')
Expand All @@ -124,7 +139,9 @@ protected function setUp(): void {
$this->throttler,
$this->chain,
$this->initialStateService,
$this->webAuthnManager
$this->webAuthnManager,
$this->notificationManager,
$this->l
);
}

Expand Down Expand Up @@ -249,6 +266,7 @@ public function testShowLoginFormWithErrorsInSession() {
[
'MessageArray1',
'MessageArray2',
'This community release of Nextcloud is unsupported and instant notifications are unavailable.',
]
);
$this->initialStateService->expects($this->at(1))
Expand Down Expand Up @@ -278,7 +296,7 @@ public function testShowLoginFormForFlowAuth() {
->expects($this->once())
->method('isLoggedIn')
->willReturn(false);
$this->initialStateService->expects($this->at(2))
$this->initialStateService->expects($this->at(4))
->method('provideInitialState')
->with(
'core',
Expand Down Expand Up @@ -339,14 +357,14 @@ public function testShowLoginFormWithPasswordResetOption($canChangePassword,
->method('get')
->with('LdapUser')
->willReturn($user);
$this->initialStateService->expects($this->at(0))
$this->initialStateService->expects($this->at(2))
->method('provideInitialState')
->with(
'core',
'loginUsername',
'LdapUser'
);
$this->initialStateService->expects($this->at(4))
$this->initialStateService->expects($this->at(6))
->method('provideInitialState')
->with(
'core',
Expand Down Expand Up @@ -386,21 +404,21 @@ public function testShowLoginFormForUserNamed0() {
->method('get')
->with('0')
->willReturn($user);
$this->initialStateService->expects($this->at(1))
$this->initialStateService->expects($this->at(3))
->method('provideInitialState')
->with(
'core',
'loginAutocomplete',
true
);
$this->initialStateService->expects($this->at(3))
$this->initialStateService->expects($this->at(5))
->method('provideInitialState')
->with(
'core',
'loginResetPasswordLink',
false
);
$this->initialStateService->expects($this->at(4))
$this->initialStateService->expects($this->at(6))
->method('provideInitialState')
->with(
'core',
Expand Down

0 comments on commit b6d1ca8

Please sign in to comment.