From 45658be8a3d96f9fe3c44b41f3cce3209fe337ae Mon Sep 17 00:00:00 2001 From: Rastislav Brandobur Date: Sat, 29 May 2021 21:03:00 +0200 Subject: [PATCH 1/4] added slovak lang --- lang/sk.yml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 lang/sk.yml diff --git a/lang/sk.yml b/lang/sk.yml new file mode 100644 index 0000000..80b3767 --- /dev/null +++ b/lang/sk.yml @@ -0,0 +1,6 @@ +sk: + AuthUsernameOrEmail: + Title: "Používateľské meno" + Description: "Slúži na prihlasovanie." + UsernameOrEmail: "Používateľské meno alebo e-mail" + LoginError: "Poskytnuté detaily vyzerajú byť nesprávne. Prosím skúste opäť." \ No newline at end of file From 3c279a283a0b887a479304aad621c57ab45d49c7 Mon Sep 17 00:00:00 2001 From: Rastislav Brandobur Date: Sat, 29 May 2021 21:17:28 +0200 Subject: [PATCH 2/4] added missing focus on the identity input when the page is loaded --- code/forms/UsernameOrEmailLoginForm.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/code/forms/UsernameOrEmailLoginForm.php b/code/forms/UsernameOrEmailLoginForm.php index 4fafaf7..1ffb58d 100755 --- a/code/forms/UsernameOrEmailLoginForm.php +++ b/code/forms/UsernameOrEmailLoginForm.php @@ -75,6 +75,15 @@ public function __construct($controller, $name, $fields = null, $actions = null, // LoginForm does its magic parent::__construct($controller, $name, $fields, $actions); + // Focus on the identity input when the page is loaded + $js = <<setAttribute("action",$form_action_url); From 7221040127d9f51cef40c5077604640426ef6ee9 Mon Sep 17 00:00:00 2001 From: Rastislav Brandobur Date: Sat, 29 May 2021 22:07:45 +0200 Subject: [PATCH 3/4] clean up (the removed functionality is in the parent) --- code/forms/UsernameOrEmailLoginForm.php | 79 +++---------------- .../security/UsernameOrEmailAuthenticator.php | 57 ------------- 2 files changed, 9 insertions(+), 127 deletions(-) diff --git a/code/forms/UsernameOrEmailLoginForm.php b/code/forms/UsernameOrEmailLoginForm.php index 1ffb58d..8ab674f 100755 --- a/code/forms/UsernameOrEmailLoginForm.php +++ b/code/forms/UsernameOrEmailLoginForm.php @@ -7,73 +7,19 @@ class UsernameOrEmailLoginForm extends MemberLoginForm public function __construct($controller, $name, $fields = null, $actions = null, $checkCurrentUser = true) { - $form_action_url = Controller::join_links( - BASE_URL, - "Security", - $name - ); - - $lost_password_url = Controller::join_links( - BASE_URL, - "Security", - "lostpassword" - ); - - if (isset($_REQUEST['BackURL'])) { - $backURL = $_REQUEST['BackURL']; - } else { - $backURL = Session::get('BackURL'); - } - - $fields = new FieldList( - HiddenField::create( - "AuthenticationMethod", - null, - $this->authenticator_class, - $this - ), - $identity_field = TextField::create( - 'Identity', - _t('AuthUsernameOrEmail.UsernameOrEmail', 'Username or Email') - ), - PasswordField::create( - "Password", - _t('Member.PASSWORD', 'Password') - ) - ); - - if(!Security::config()->remember_username) { - // Some browsers won't respect this attribute unless it's added to the form - $this->setAttribute('autocomplete', 'off'); - $identity_field->setAttribute('autocomplete', 'off'); - } - - if(Security::config()->autologin_enabled) { - $fields->push(new CheckboxField( - "Remember", - _t('Member.REMEMBERME', "Remember me?") - )); - } - - - if (isset($backURL)) { - $fields->push(new HiddenField('BackURL', 'BackURL', $backURL)); - } + parent::__construct($controller, $name, $fields, $actions); - $actions = new FieldList( - FormAction::create('dologin', _t('Member.BUTTONLOGIN', "Log in")), - LiteralField::create( - 'forgotPassword', - '

' - . _t('Member.BUTTONLOSTPASSWORD', "I've lost my password") . '

' - ) + $identity_field = TextField::create( + 'Identity', + _t('AuthUsernameOrEmail.UsernameOrEmail', 'Username or Email'), + null, + null, + $this ); - // Reduce attack surface by enforcing POST requests - $this->setFormMethod('POST', true); + $this->Fields()->replaceField("Email", $identity_field); - // LoginForm does its magic - parent::__construct($controller, $name, $fields, $actions); + $this->setValidator(RequiredFields::create('Identity', 'Password')); // Focus on the identity input when the page is loaded $js = <<setAttribute("action",$form_action_url); - - $this - ->setValidator(RequiredFields::create('Identity', 'Password')); - } /** diff --git a/code/security/UsernameOrEmailAuthenticator.php b/code/security/UsernameOrEmailAuthenticator.php index d09d9de..3828476 100755 --- a/code/security/UsernameOrEmailAuthenticator.php +++ b/code/security/UsernameOrEmailAuthenticator.php @@ -7,17 +7,6 @@ */ class UsernameOrEmailAuthenticator extends MemberAuthenticator { - - /** - * @var Array Contains encryption algorithm identifiers. - * If set, will migrate to new precision-safe password hashing - * upon login. See http://open.silverstripe.org/ticket/3004. - */ - public static $migrate_legacy_hashes = array( - 'md5' => 'md5_v2.4', - 'sha1' => 'sha1_v2.4' - ); - /** * Overwrite standard authentication in order to also look for user ID * (as well as email) @@ -97,52 +86,6 @@ protected static function authenticate_member($data, $form, &$success) return $member; } - /** - * Log login attempt - * TODO We could handle this with an extension - * - * @param array $data - * @param Member $member - * @param bool $success - */ - protected static function record_login_attempt($data, $member, $success) { - if(!Security::config()->login_recording) return; - - // Check email is valid - $identity = isset($data['Identity']) ? $data['Identity'] : null; - - if(is_array($identity)) { - throw new InvalidArgumentException("Bad email passed to MemberAuthenticator::authenticate(): $identity"); - } - - $attempt = new LoginAttempt(); - if($success) { - // successful login (member is existing with matching password) - $attempt->MemberID = $member->ID; - $attempt->Status = 'Success'; - - // Audit logging hook - $member->extend('authenticated'); - - } else { - // Failed login - we're trying to see if a user exists with this email (disregarding wrong passwords) - $attempt->Status = 'Failure'; - if($member) { - // Audit logging hook - $attempt->MemberID = $member->ID; - $member->extend('authenticationFailed'); - } else { - // Audit logging hook - singleton('Member')->extend('authenticationFailedUnknownUser', $data); - } - } - - $attempt->Email = $identity; - $attempt->IP = Controller::curr()->getRequest()->getIP(); - $attempt->write(); - } - - /** * Tell this Authenticator to use your custom login form * The 3rd parameter MUST be 'LoginForm' to fit within the authentication From 7b4e303800bdb2b7864deeea03b6b530534bcc92 Mon Sep 17 00:00:00 2001 From: Rastislav Brandobur Date: Sun, 30 May 2021 00:38:05 +0200 Subject: [PATCH 4/4] replaced Identity for Email --- code/forms/UsernameOrEmailLoginForm.php | 18 +++++------------- code/security/UsernameOrEmailAuthenticator.php | 6 +++--- tests/UsernameAuthenticatorTest.php | 8 ++++---- 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/code/forms/UsernameOrEmailLoginForm.php b/code/forms/UsernameOrEmailLoginForm.php index 8ab674f..f0139f0 100755 --- a/code/forms/UsernameOrEmailLoginForm.php +++ b/code/forms/UsernameOrEmailLoginForm.php @@ -9,22 +9,14 @@ public function __construct($controller, $name, $fields = null, $actions = null, { parent::__construct($controller, $name, $fields, $actions); - $identity_field = TextField::create( - 'Identity', - _t('AuthUsernameOrEmail.UsernameOrEmail', 'Username or Email'), - null, - null, - $this - ); - - $this->Fields()->replaceField("Email", $identity_field); - - $this->setValidator(RequiredFields::create('Identity', 'Password')); + if ($emailField = $this->Fields()->fieldByName("Email")) { + $emailField->setTitle(_t('AuthUsernameOrEmail.UsernameOrEmail', 'Username or Email')); + } - // Focus on the identity input when the page is loaded + // Focus on the Email input when the page is loaded $js = <<filter($filter) ->first(); diff --git a/tests/UsernameAuthenticatorTest.php b/tests/UsernameAuthenticatorTest.php index b8b99db..2eeada7 100644 --- a/tests/UsernameAuthenticatorTest.php +++ b/tests/UsernameAuthenticatorTest.php @@ -93,7 +93,7 @@ public function testDefaultAdmin() // Test correct login $result = UsernameOrEmailAuthenticator::authenticate(array( - 'Identity' => 'admin', + 'Email' => 'admin', 'Password' => 'password' ), $form); $this->assertNotEmpty($result); @@ -103,7 +103,7 @@ public function testDefaultAdmin() // Test incorrect login $form->clearMessage(); $result = UsernameOrEmailAuthenticator::authenticate(array( - 'Identity' => 'admin', + 'Email' => 'admin', 'Password' => 'notmypassword' ), $form); $this->assertEmpty($result); @@ -121,11 +121,11 @@ public function testDefaultAdminLockOut() // Test correct login UsernameOrEmailAuthenticator::authenticate(array( - 'Identity' => 'admin', + 'Email' => 'admin', 'Password' => 'wrongpassword' ), $form); $this->assertTrue(Member::default_admin()->isLockedOut()); $this->assertEquals(Member::default_admin()->LockedOutUntil, '2016-04-18 00:10:00'); } -} \ No newline at end of file +}