2007-09-14 05:12:21 +02:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2017-04-22 06:30:10 +02:00
|
|
|
namespace SilverStripe\Security\MemberAuthenticator;
|
2016-06-23 01:37:22 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Control\Session;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\ValidationResult;
|
2016-06-23 01:37:22 +02:00
|
|
|
use InvalidArgumentException;
|
2017-05-30 09:42:00 +02:00
|
|
|
use SilverStripe\Security\Authenticator;
|
2017-04-22 06:30:10 +02:00
|
|
|
use SilverStripe\Security\Security;
|
|
|
|
use SilverStripe\Security\Member;
|
2017-04-23 05:30:33 +02:00
|
|
|
use SilverStripe\Security\LoginAttempt;
|
2016-06-23 01:37:22 +02:00
|
|
|
|
2007-09-14 05:12:21 +02:00
|
|
|
/**
|
|
|
|
* Authenticator for the default "member" method
|
|
|
|
*
|
2017-05-30 09:42:00 +02:00
|
|
|
* @author Sam Minnee <sam@silverstripe.com>
|
|
|
|
* @author Simon Erkelens <simonerkelens@silverstripe.com>
|
2007-09-14 05:12:21 +02:00
|
|
|
*/
|
2017-05-30 09:42:00 +02:00
|
|
|
class MemberAuthenticator implements Authenticator
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
|
|
|
|
2017-04-22 06:30:10 +02:00
|
|
|
public function supportedServices()
|
|
|
|
{
|
2017-05-30 09:42:00 +02:00
|
|
|
// Bitwise-OR of all the supported services in this Authenticator, to make a bitmask
|
|
|
|
return Authenticator::LOGIN | Authenticator::LOGOUT | Authenticator::CHANGE_PASSWORD
|
|
|
|
| Authenticator::RESET_PASSWORD;
|
2017-04-22 06:30:10 +02:00
|
|
|
}
|
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
2017-05-30 09:42:00 +02:00
|
|
|
* @param array $data
|
|
|
|
* @param null|ValidationResult $result
|
|
|
|
* @return null|Member
|
2016-11-23 06:09:10 +01:00
|
|
|
*/
|
2017-05-30 09:42:00 +02:00
|
|
|
public function authenticate($data, &$result = null)
|
2017-04-22 06:30:10 +02:00
|
|
|
{
|
|
|
|
// Find authenticated member
|
2017-05-30 09:42:00 +02:00
|
|
|
$member = $this->authenticateMember($data, $result);
|
2017-04-22 06:30:10 +02:00
|
|
|
|
|
|
|
// Optionally record every login attempt as a {@link LoginAttempt} object
|
2017-05-30 09:42:00 +02:00
|
|
|
$this->recordLoginAttempt($data, $member, $result->isValid());
|
2017-04-22 06:30:10 +02:00
|
|
|
|
|
|
|
if ($member) {
|
|
|
|
Session::clear('BackURL');
|
|
|
|
}
|
|
|
|
|
2017-05-30 09:42:00 +02:00
|
|
|
return $result->isValid() ? $member : null;
|
2017-04-22 06:30:10 +02:00
|
|
|
}
|
2016-11-23 06:09:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt to find and authenticate member if possible from the given data
|
|
|
|
*
|
2017-04-30 05:17:26 +02:00
|
|
|
* @param array $data Form submitted data
|
2017-05-30 09:42:00 +02:00
|
|
|
* @param ValidationResult $result
|
|
|
|
* @param Member|null This third parameter is used in the CMSAuthenticator(s)
|
2016-11-23 06:09:10 +01:00
|
|
|
* @return Member Found member, regardless of successful login
|
|
|
|
*/
|
2017-05-30 09:42:00 +02:00
|
|
|
protected function authenticateMember($data, &$result = null, $member = null)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
// Default success to false
|
2017-05-30 09:42:00 +02:00
|
|
|
$email = !empty($data['Email']) ? $data['Email'] : null;
|
|
|
|
$result = new ValidationResult();
|
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
// Check default login (see Security::setDefaultAdmin())
|
|
|
|
$asDefaultAdmin = $email === Security::default_admin_username();
|
|
|
|
if ($asDefaultAdmin) {
|
|
|
|
// If logging is as default admin, ensure record is setup correctly
|
|
|
|
$member = Member::default_admin();
|
2017-05-30 09:42:00 +02:00
|
|
|
$success = Security::check_default_admin($email, $data['Password']);
|
|
|
|
$result = $member->canLogIn();
|
2016-11-23 06:09:10 +01:00
|
|
|
//protect against failed login
|
2017-05-30 09:42:00 +02:00
|
|
|
if ($success && $result->isValid()) {
|
2016-11-23 06:09:10 +01:00
|
|
|
return $member;
|
2017-05-30 09:42:00 +02:00
|
|
|
} else {
|
|
|
|
$result->addError(_t(
|
|
|
|
'SilverStripe\\Security\\Member.ERRORWRONGCRED',
|
|
|
|
"The provided details don't seem to be correct. Please try again."
|
|
|
|
));
|
2016-11-23 06:09:10 +01:00
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
// Attempt to identify user by email
|
|
|
|
if (!$member && $email) {
|
|
|
|
// Find user by email
|
2017-04-30 05:17:26 +02:00
|
|
|
/** @var Member $member */
|
2016-11-23 06:09:10 +01:00
|
|
|
$member = Member::get()
|
2017-04-30 05:17:26 +02:00
|
|
|
->filter([Member::config()->get('unique_identifier_field') => $email])
|
2016-11-23 06:09:10 +01:00
|
|
|
->first();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate against member if possible
|
|
|
|
if ($member && !$asDefaultAdmin) {
|
|
|
|
$result = $member->checkPassword($data['Password']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit failure to member and form (if available)
|
2017-05-30 09:42:00 +02:00
|
|
|
if (!$result->isValid()) {
|
2016-11-23 06:09:10 +01:00
|
|
|
if ($member) {
|
|
|
|
$member->registerFailedLogin();
|
|
|
|
}
|
|
|
|
} else {
|
2017-05-30 09:42:00 +02:00
|
|
|
if ($member) {
|
2016-11-29 00:31:16 +01:00
|
|
|
$member->registerSuccessfulLogin();
|
2017-05-30 09:42:00 +02:00
|
|
|
} else {
|
|
|
|
// A non-existing member occurred. This will make the result "valid" so let's invalidate
|
|
|
|
$result->addError(_t(
|
|
|
|
'SilverStripe\\Security\\Member.ERRORWRONGCRED',
|
|
|
|
"The provided details don't seem to be correct. Please try again."
|
|
|
|
));
|
|
|
|
$member = null;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2016-11-23 06:09:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $member;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log login attempt
|
|
|
|
* TODO We could handle this with an extension
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @param Member $member
|
2017-05-30 09:42:00 +02:00
|
|
|
* @param boolean $success
|
2016-11-23 06:09:10 +01:00
|
|
|
*/
|
2017-04-23 05:30:33 +02:00
|
|
|
protected function recordLoginAttempt($data, $member, $success)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-04-30 05:17:26 +02:00
|
|
|
if (!Security::config()->get('login_recording')) {
|
2016-11-29 00:31:16 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
// Check email is valid
|
|
|
|
/** @skipUpgrade */
|
|
|
|
$email = isset($data['Email']) ? $data['Email'] : null;
|
|
|
|
if (is_array($email)) {
|
|
|
|
throw new InvalidArgumentException("Bad email passed to MemberAuthenticator::authenticate(): $email");
|
|
|
|
}
|
|
|
|
|
2017-04-30 05:17:26 +02:00
|
|
|
$attempt = LoginAttempt::create();
|
2017-05-30 09:42:00 +02:00
|
|
|
if ($success && $member) {
|
2016-11-23 06:09:10 +01:00
|
|
|
// 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
|
|
|
|
Member::singleton()->extend('authenticationFailedUnknownUser', $data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$attempt->Email = $email;
|
|
|
|
$attempt->IP = Controller::curr()->getRequest()->getIP();
|
|
|
|
$attempt->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-05-30 09:42:00 +02:00
|
|
|
* @param $link
|
|
|
|
* @return LostPasswordHandler
|
2016-11-23 06:09:10 +01:00
|
|
|
*/
|
2017-04-22 06:30:10 +02:00
|
|
|
public function getLostPasswordHandler($link)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-04-22 06:30:10 +02:00
|
|
|
return LostPasswordHandler::create($link, $this);
|
2016-11-23 06:09:10 +01:00
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
2017-05-30 09:42:00 +02:00
|
|
|
* @param string $link
|
|
|
|
* @return ChangePasswordHandler
|
2016-11-23 06:09:10 +01:00
|
|
|
*/
|
2017-04-22 06:30:10 +02:00
|
|
|
public function getChangePasswordHandler($link)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-04-22 06:30:10 +02:00
|
|
|
return ChangePasswordHandler::create($link, $this);
|
2016-11-23 06:09:10 +01:00
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-04-22 06:30:10 +02:00
|
|
|
/**
|
2017-05-30 09:42:00 +02:00
|
|
|
* @param string $link
|
|
|
|
* @return LoginHandler
|
2017-04-22 06:30:10 +02:00
|
|
|
*/
|
|
|
|
public function getLoginHandler($link)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-04-22 06:30:10 +02:00
|
|
|
return LoginHandler::create($link, $this);
|
2016-11-23 06:09:10 +01:00
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-04-30 05:17:26 +02:00
|
|
|
/**
|
2017-05-30 09:42:00 +02:00
|
|
|
* @param string $link
|
|
|
|
* @return LogoutHandler
|
2017-04-30 05:17:26 +02:00
|
|
|
*/
|
2017-05-20 06:32:25 +02:00
|
|
|
public function getLogoutHandler($link)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-05-20 06:32:25 +02:00
|
|
|
return LogoutHandler::create($link, $this);
|
2016-11-23 06:09:10 +01:00
|
|
|
}
|
2007-09-14 05:12:21 +02:00
|
|
|
}
|