Created
September 1, 2017 10:34
-
-
Save dhensby/c3b0a1f2f54f5074747c21018b2f479f to your computer and use it in GitHub Desktop.
My custom authenticator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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