Skip to content
Open
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
19 changes: 18 additions & 1 deletion src/controllers/MagicLoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
namespace creode\magiclogin\controllers;

use Craft;
use DateTime;
use craft\elements\User;
use craft\web\Controller;
use creode\magiclogin\MagicLogin;
use DateTime;
use yii\web\NotFoundHttpException;

/**
* MagicLogin Controller
Expand Down Expand Up @@ -162,6 +163,11 @@ public function actionRegisterForm()
$this->redirect($generalConfig->postLoginRedirect);
}

$userConfig = Craft::$app->getProjectConfig()->get('users') ?? [];
if (! $userConfig['allowPublicRegistration']) {
throw new NotFoundHttpException();
}

return $this->renderTemplate('magic-login/_register-form');
}

Expand All @@ -181,6 +187,11 @@ public function actionRegister()
)
);

$userSettings = Craft::$app->getProjectConfig()->get('users');
if (!$userSettings['allowPublicRegistration']) {
throw new NotFoundHttpException();
}

if (Craft::$app->getUser()->getIdentity()) {
$generalConfig = Craft::$app->getConfig()->getGeneral();
$this->setSuccessFlash(\Craft::t('magic-login', 'You are already logged in.'));
Expand All @@ -191,6 +202,12 @@ public function actionRegister()
->getRequest()
->getRequiredParam('email');

$generalConfig = Craft::$app->getConfig()->getGeneral();
if (! $generalConfig->useEmailAsUsername) {
$this->setFailFlash(\Craft::t('magic-login', 'Please enter a valid username.'));
return;
}

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
// TODO: Maybe set this to be configurable in future.
$this->setFailFlash(\Craft::t('magic-login', 'Please enter a valid email address.'));
Expand Down
1 change: 1 addition & 0 deletions test-config/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

return [
'postLoginRedirect' => '',
'useEmailAsUsername' => true,
];
1 change: 1 addition & 0 deletions tests/_craft/config/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
'requireUserAgentAndIpForSession' => false,
'securityKey' => App::env('SECURITY_KEY'),
'enableCsrfProtection' => false,
'useEmailAsUsername' => true,
];
2 changes: 1 addition & 1 deletion tests/functional/LoginFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function testUnregisteredUserSignup()
$this->tester->submitForm(
'#magic-login-form',
[
'email' => 'test@example.com',
'email' => 'test-2@example.com',
],
'submitButton'
);
Expand Down
31 changes: 31 additions & 0 deletions tests/functional/RegistrationFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,37 @@ public function testWhenRegistrationErrorOccursUserIsNotCreated()
$this->assertEquals($userCount, count(User::find()->all()));
}

/**
* Test that we return a 404, not the user registration page when public registration is disabled.
*/
public function testWhenPublicRegistrationDisabledUserCannotRegister()
{
$userSettings = Craft::$app->getProjectConfig()->get('users') ?? [];
$userSettings['allowPublicRegistration'] = false;
Craft::$app->projectConfig->set('users', $userSettings);

$this->tester->amOnPage('/magic-login/register');
$this->tester->seeResponseCodeIs(404);
}

public function testWhenPublicRegistrationDisabledUserCannotRegisterWithMagicLink()
{
$this->tester->amOnPage('/magic-login/register');

$userSettings = Craft::$app->getProjectConfig()->get('users') ?? [];
$userSettings['allowPublicRegistration'] = false;

Craft::$app->projectConfig->set('users', $userSettings);
$this->tester->submitForm(
'#magic-login-register',
[
'email' => 'creode-test@example.com',
],
);

$this->tester->seeResponseCodeIs(404);
}

/**
* Tests that when a user is successfully registered the magic
* login group is attached.
Expand Down
36 changes: 36 additions & 0 deletions tests/functional/SignupFormCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace creode\magiclogintests\acceptance;

use Craft;
use FunctionalTester;

class SignupFormCest
{
public function _before(FunctionalTester $I)
{
// Ensure useEmailAsUsername is set to true.
$generalConfig = Craft::$app->getConfig()->getGeneral();
$generalConfig->useEmailAsUsername = false;
}

public function testSignupFormWithoutRequiredUsername(FunctionalTester $I)
{
$I->amOnPage('/magic-login/register');

// Attempt to submit the form with only an email address
$I->fillField('email', 'test@example.com');
$I->click('Submit');

// Expect error messages and no redirection
$I->dontSeeInCurrentUrl('/confirmation');
$I->see('Please enter a valid username'); // Adjust according to the actual error message expected
}

public function _after(FunctionalTester $I)
{
// Reset useEmailAsUsername to true
$generalConfig = Craft::$app->getConfig()->getGeneral();
$generalConfig->useEmailAsUsername = true;
}
}