Skip to content

Instantly share code, notes, and snippets.

@dhensby
Created September 1, 2017 10:34
Show Gist options
  • Select an option

  • Save dhensby/c3b0a1f2f54f5074747c21018b2f479f to your computer and use it in GitHub Desktop.

Select an option

Save dhensby/c3b0a1f2f54f5074747c21018b2f479f to your computer and use it in GitHub Desktop.
My custom authenticator
<?php
global $project;
$project = 'mysite';
global $database;
$database = '';
require_once 'conf/ConfigureFromEnv.php';
// Set the site locale
i18n::set_locale('en_US');
// set up My authentication
Authenticator::register_authenticator('MyAuthenticator');
Authenticator::set_default_authenticator('MyAuthenticator');
Authenticator::unregister_authenticator('MemberAuthenticator');
<?php
class MyAuthenticator extends MemberAuthenticator {
/**
* Attempt to find and authenticate member if possible from the given data
*
* @param array $data
* @param Form $form
* @param bool &$success Success flag
* @return Member Found member, regardless of successful login
*/
protected static function authenticate_member($data, $form, &$success)
{
// clearly this is not clever because all the success/failure hooks get called regardless of my custom logic
$member = parent::authenticate_member($data, $form, $success);
// randomly fail one in 3 logins
if ($success && rand(1,3) == 1) {
$success = false;
$form->sessionMessage('Today was not your lucky day', 'bad');
}
if ($success) {
return $member;
}
}
public static function get_login_form(Controller $controller) {
return MyLoginForm::create($controller, "LoginForm");
}
}
<?php
class MyLoginForm extends MemberLoginForm {
protected $authenticator_class = 'MyAuthenticator';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment