Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

require_once './vendor/autoload.php';
require_once './vendor-bin/csfixer/vendor/autoload.php';

use Nextcloud\CodingStandard\Config;

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"config": {
"platform": {
"php": "8.0"
"php": "8.1"
},
"allow-plugins": {
"bamarni/composer-bin-plugin": true
Expand Down
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions lib/Command/Generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ class Generate extends Command {
/** @var IUserManager */
private $userManager;

public function __construct(private CodeStorage $codeStorage, IUserManager $userManager) {
public function __construct(
private CodeStorage $codeStorage,
IUserManager $userManager,
) {
parent::__construct();
$this->userManager = $userManager;
}
Expand All @@ -38,14 +41,14 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$user = $this->userManager->get($userId);

if (is_null($user)) {
$output->writeln("<error>Invalid UID</error>");
$output->writeln('<error>Invalid UID</error>');
return 1;
}

if ($this->codeStorage->hasCode($user)) {
// TODO: abort? Ask for confirmattion? Add `-f|--force` flag?
$output->writeln("<info>There is an existing code that will be overwritten.</info>");
$output->writeln("");
$output->writeln('<info>There is an existing code that will be overwritten.</info>');
$output->writeln('');
}

$code = $this->codeStorage->generateCode($user);
Expand Down
7 changes: 4 additions & 3 deletions lib/Controller/AdminCodeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ class AdminCodeController extends Controller {
/** @var IUserSession */
private $userSession;

public function __construct(IRequest $request,
public function __construct(
IRequest $request,
IUserManager $userManager,
IGroupManager $groupManager,
ISubAdmin $subAdmin,
private CodeStorage $codeStorage,
IUserSession $userSession) {
IUserSession $userSession,
) {
parent::__construct(Application::APP_ID, $request);
$this->userManager = $userManager;
$this->groupManager = $groupManager;
Expand All @@ -50,7 +52,6 @@ public function __construct(IRequest $request,
/**
*
* @SubAdminRequired
* @return JSONResponse
*/
public function create(string $uid): JSONResponse {
$currentUser = $this->userSession->getUser();
Expand Down
8 changes: 4 additions & 4 deletions lib/Event/StateChanged.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ class StateChanged extends Event {
/** @var IUser */
private $user;

public function __construct(IUser $user, private bool $enabled) {
public function __construct(
IUser $user,
private bool $enabled,
) {
parent::__construct();

$this->user = $user;
}

/**
* @return IUser
*/
public function getUser(): IUser {
return $this->user;
}
Expand Down
5 changes: 4 additions & 1 deletion lib/Listener/StateChangeRegistryUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ class StateChangeRegistryUpdater implements IEventListener {
/** @var IRegistry */
private $registry;

public function __construct(IRegistry $registry, private AdminProvider $provider) {
public function __construct(
IRegistry $registry,
private AdminProvider $provider,
) {
$this->registry = $registry;
}

Expand Down
2 changes: 0 additions & 2 deletions lib/Migration/Version0Date20180907071626.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
class Version0Date20180907071626 extends SimpleMigrationStep {

/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
*
* @return ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
Expand Down
2 changes: 0 additions & 2 deletions lib/Migration/Version1Date20180907092152.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
class Version1Date20180907092152 extends SimpleMigrationStep {

/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
*
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
Expand Down
3 changes: 0 additions & 3 deletions lib/Migration/Version2Date20180926053333.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ public function postSchemaChange(IOutput $output, Closure $schemaClosure, array
$this->fillExpireDates($qb, $twoDaysAgo);
}

/**
* @param IQueryBuilder $qb
*/
private function fillExpireDates(IQueryBuilder $qb, int $expires) {
$updateQuery = $qb->update('twofactor_admin_codes')
->set('expires', $qb->createNamedParameter($expires))
Expand Down
2 changes: 0 additions & 2 deletions lib/Migration/Version2Date20180926055748.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
class Version2Date20180926055748 extends SimpleMigrationStep {

/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
*
* @return ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
Expand Down
3 changes: 0 additions & 3 deletions lib/Migration/Version3Date20200821134736.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ public function __construct(IDBConnection $db) {
}

/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
*
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
Expand Down Expand Up @@ -61,7 +59,6 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
}

/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
*/
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
Expand Down
2 changes: 0 additions & 2 deletions lib/Migration/Version3Date20200821134738.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
class Version3Date20200821134738 extends SimpleMigrationStep {

/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
*
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
Expand Down
11 changes: 4 additions & 7 deletions lib/Provider/AdminProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@

class AdminProvider implements IProvider, IProvidesIcons, IDeactivatableByAdmin {

public function __construct(private string $appName,
public function __construct(
private string $appName,
private IL10N $l10n,
private IURLGenerator $urlGenerator,
private CodeStorage $codeStorage) {
private CodeStorage $codeStorage,
) {
}

/**
Expand All @@ -50,9 +52,7 @@ public function getDescription(): string {
/**
* Get the template for rending the 2FA provider view
*
* @param IUser $user
*
* @return Template
*/
public function getTemplate(IUser $user): Template {
return new Template($this->appName, 'challenge');
Expand All @@ -61,7 +61,6 @@ public function getTemplate(IUser $user): Template {
/**
* Verify the given challenge
*
* @param IUser $user
*
*/
public function verifyChallenge(IUser $user, string $challenge): bool {
Expand All @@ -70,8 +69,6 @@ public function verifyChallenge(IUser $user, string $challenge): bool {

/**
* Decides whether 2FA is enabled for the given user
*
* @param IUser $user
*/
public function isTwoFactorAuthEnabledForUser(IUser $user): bool {
return $this->codeStorage->hasCode($user);
Expand Down
6 changes: 4 additions & 2 deletions lib/Service/CodeStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ class CodeStorage {
/** @var ITimeFactory */
private $timeFactory;

public function __construct(private CodeMapper $codeMapper,
public function __construct(
private CodeMapper $codeMapper,
ISecureRandom $random,
IEventDispatcher $eventDispatcher,
ITimeFactory $timeFactory) {
ITimeFactory $timeFactory,
) {
$this->random = $random;
$this->eventDispatcher = $eventDispatcher;
$this->timeFactory = $timeFactory;
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/Service/CodeStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function setUp(): void {

public function testValidateInexistentCode() {
$user = $this->createTestUser();
$code = "123456";
$code = '123456';

$valid = $this->codeStorage->validateCode($user, $code);

Expand All @@ -42,7 +42,7 @@ public function testValidateWrongCode() {
$user = $this->createTestUser();
$this->codeStorage->generateCode($user);

$valid = $this->codeStorage->validateCode($user, "123456");
$valid = $this->codeStorage->validateCode($user, '123456');

$this->assertFalse($valid);
}
Expand Down
42 changes: 21 additions & 21 deletions tests/Unit/Command/GenerateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,67 +43,67 @@ protected function setUp(): void {

public function testGenerateCodeInvalidUID() {
$this->userManager->expects($this->once())
->method("get")
->with("user1")
->method('get')
->with('user1')
->willReturn(null);

$ec = $this->command->execute([
"uid" => "user1",
'uid' => 'user1',
]);

$output = $this->command->getDisplay();
$this->assertStringContainsString("Invalid UID", $output);
$this->assertStringContainsString('Invalid UID', $output);
$this->assertEquals(1, $ec);
}

public function testGenerateNewCode() {
$user = $this->createMock(IUser::class);
$this->userManager->expects($this->once())
->method("get")
->with("user1")
->method('get')
->with('user1')
->willReturn($user);
$this->codeStorage->expects($this->once())
->method("hasCode")
->method('hasCode')
->with($user)
->willReturn(false);
$this->codeStorage->expects($this->once())
->method("generateCode")
->method('generateCode')
->with($user)
->willReturn("123456");
->willReturn('123456');

$ec = $this->command->execute([
"uid" => "user1",
'uid' => 'user1',
]);

$output = $this->command->getDisplay();
$this->assertStringContainsString("Generated new one-time code for user1: 123456", $output);
$this->assertStringContainsString("This code is valid for", $output);
$this->assertStringContainsString('Generated new one-time code for user1: 123456', $output);
$this->assertStringContainsString('This code is valid for', $output);
$this->assertEquals(0, $ec);
}

public function testReGenerateCode() {
$user = $this->createMock(IUser::class);
$this->userManager->expects($this->once())
->method("get")
->with("user1")
->method('get')
->with('user1')
->willReturn($user);
$this->codeStorage->expects($this->once())
->method("hasCode")
->method('hasCode')
->with($user)
->willReturn(true);
$this->codeStorage->expects($this->once())
->method("generateCode")
->method('generateCode')
->with($user)
->willReturn("123456");
->willReturn('123456');

$ec = $this->command->execute([
"uid" => "user1",
'uid' => 'user1',
]);

$output = $this->command->getDisplay();
$this->assertStringContainsString("There is an existing code that will be overwritten.", $output);
$this->assertStringContainsString("Generated new one-time code for user1: 123456", $output);
$this->assertStringContainsString("This code is valid for", $output);
$this->assertStringContainsString('There is an existing code that will be overwritten.', $output);
$this->assertStringContainsString('Generated new one-time code for user1: 123456', $output);
$this->assertStringContainsString('This code is valid for', $output);
$this->assertEquals(0, $ec);
}

Expand Down
Loading