2008-09-26 04:22:51 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Log-in form for the "member" authentication method
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage security
|
|
|
|
*/
|
|
|
|
class MemberLoginForm extends LoginForm {
|
|
|
|
|
2010-10-19 07:05:23 +02:00
|
|
|
/**
|
|
|
|
* This field is used in the "You are logged in as %s" message
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $loggedInAsField = 'FirstName';
|
|
|
|
|
2008-10-08 04:00:12 +02:00
|
|
|
protected $authenticator_class = 'MemberAuthenticator';
|
|
|
|
|
2008-09-26 04:22:51 +02:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param Controller $controller The parent controller, necessary to
|
|
|
|
* create the appropriate form action tag.
|
|
|
|
* @param string $name The method on the controller that will return this
|
|
|
|
* form object.
|
2011-10-28 03:37:27 +02:00
|
|
|
* @param FieldList|FormField $fields All of the fields in the form - a
|
|
|
|
* {@link FieldList} of {@link FormField}
|
2008-09-26 04:22:51 +02:00
|
|
|
* objects.
|
2011-10-28 03:37:27 +02:00
|
|
|
* @param FieldList|FormAction $actions All of the action buttons in the
|
|
|
|
* form - a {@link FieldList} of
|
2008-09-26 04:22:51 +02:00
|
|
|
* {@link FormAction} objects
|
|
|
|
* @param bool $checkCurrentUser If set to TRUE, it will be checked if a
|
|
|
|
* the user is currently logged in, and if
|
|
|
|
* so, only a logout button will be rendered
|
2008-10-08 04:00:12 +02:00
|
|
|
* @param string $authenticatorClassName Name of the authenticator class that this form uses.
|
2008-09-26 04:22:51 +02:00
|
|
|
*/
|
|
|
|
function __construct($controller, $name, $fields = null, $actions = null,
|
|
|
|
$checkCurrentUser = true) {
|
|
|
|
|
2008-10-08 04:00:12 +02:00
|
|
|
// This is now set on the class directly to make it easier to create subclasses
|
|
|
|
// $this->authenticator_class = $authenticatorClassName;
|
2008-09-26 04:22:51 +02:00
|
|
|
|
|
|
|
$customCSS = project() . '/css/member_login.css';
|
|
|
|
if(Director::fileExists($customCSS)) {
|
|
|
|
Requirements::css($customCSS);
|
|
|
|
}
|
2009-02-02 00:49:53 +01:00
|
|
|
|
2008-09-26 04:22:51 +02:00
|
|
|
if(isset($_REQUEST['BackURL'])) {
|
|
|
|
$backURL = $_REQUEST['BackURL'];
|
|
|
|
} else {
|
|
|
|
$backURL = Session::get('BackURL');
|
|
|
|
}
|
|
|
|
|
2010-04-13 05:18:26 +02:00
|
|
|
if($checkCurrentUser && Member::currentUser() && Member::logged_in_session_exists()) {
|
2011-05-11 09:51:54 +02:00
|
|
|
$fields = new FieldList(
|
2009-04-29 02:07:39 +02:00
|
|
|
new HiddenField("AuthenticationMethod", null, $this->authenticator_class, $this)
|
|
|
|
);
|
2011-05-11 09:51:54 +02:00
|
|
|
$actions = new FieldList(
|
2009-04-29 02:07:39 +02:00
|
|
|
new FormAction("logout", _t('Member.BUTTONLOGINOTHER', "Log in as someone else"))
|
|
|
|
);
|
2008-09-26 04:22:51 +02:00
|
|
|
} else {
|
|
|
|
if(!$fields) {
|
2010-04-12 07:00:05 +02:00
|
|
|
$label=singleton('Member')->fieldLabel(Member::get_unique_identifier_field());
|
2011-05-11 09:51:54 +02:00
|
|
|
$fields = new FieldList(
|
2008-09-26 04:22:51 +02:00
|
|
|
new HiddenField("AuthenticationMethod", null, $this->authenticator_class, $this),
|
2010-04-12 07:00:05 +02:00
|
|
|
//Regardless of what the unique identifer field is (usually 'Email'), it will be held in the 'Email' value, below:
|
|
|
|
new TextField("Email", $label, Session::get('SessionForms.MemberLoginForm.Email'), null, $this),
|
2009-02-02 00:49:53 +01:00
|
|
|
new PasswordField("Password", _t('Member.PASSWORD', 'Password'))
|
2008-09-26 04:22:51 +02:00
|
|
|
);
|
2008-10-08 04:00:12 +02:00
|
|
|
if(Security::$autologin_enabled) {
|
|
|
|
$fields->push(new CheckboxField(
|
|
|
|
"Remember",
|
2009-01-10 12:35:50 +01:00
|
|
|
_t('Member.REMEMBERME', "Remember me next time?")
|
2008-10-08 04:00:12 +02:00
|
|
|
));
|
|
|
|
}
|
2008-09-26 04:22:51 +02:00
|
|
|
}
|
|
|
|
if(!$actions) {
|
2011-05-11 09:51:54 +02:00
|
|
|
$actions = new FieldList(
|
2008-10-16 13:10:56 +02:00
|
|
|
new FormAction('dologin', _t('Member.BUTTONLOGIN', "Log in")),
|
2009-01-10 12:35:50 +01:00
|
|
|
new LiteralField(
|
|
|
|
'forgotPassword',
|
|
|
|
'<p id="ForgotPassword"><a href="Security/lostpassword">' . _t('Member.BUTTONLOSTPASSWORD', "I've lost my password") . '</a></p>'
|
|
|
|
)
|
2008-09-26 04:22:51 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($backURL)) {
|
|
|
|
$fields->push(new HiddenField('BackURL', 'BackURL', $backURL));
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::__construct($controller, $name, $fields, $actions);
|
2009-03-10 23:08:52 +01:00
|
|
|
|
|
|
|
// Focus on the email input when the page is loaded
|
|
|
|
// Only include this if other form JS validation is enabled
|
|
|
|
if($this->getValidator()->getJavascriptValidationHandler() != 'none') {
|
|
|
|
Requirements::customScript(<<<JS
|
|
|
|
(function() {
|
|
|
|
var el = document.getElementById("MemberLoginForm_LoginForm_Email");
|
|
|
|
if(el && el.focus) el.focus();
|
|
|
|
})();
|
|
|
|
JS
|
|
|
|
);
|
|
|
|
}
|
2008-09-26 04:22:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get message from session
|
|
|
|
*/
|
|
|
|
protected function getMessageFromSession() {
|
|
|
|
parent::getMessageFromSession();
|
2010-10-19 07:05:23 +02:00
|
|
|
if(($member = Member::currentUser()) && !Session::get('MemberLoginForm.force_message')) {
|
|
|
|
$this->message = sprintf(_t('Member.LOGGEDINAS', "You're logged in as %s."), $member->{$this->loggedInAsField});
|
2008-09-26 04:22:51 +02:00
|
|
|
}
|
|
|
|
Session::set('MemberLoginForm.force_message', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Login form handler method
|
|
|
|
*
|
|
|
|
* This method is called when the user clicks on "Log in"
|
|
|
|
*
|
|
|
|
* @param array $data Submitted data
|
|
|
|
*/
|
|
|
|
public function dologin($data) {
|
|
|
|
if($this->performLogin($data)) {
|
2011-10-27 22:38:29 +02:00
|
|
|
$this->logInUserAndRedirect($data);
|
2008-09-26 04:22:51 +02:00
|
|
|
} else {
|
2011-10-27 22:38:29 +02:00
|
|
|
if(array_key_exists('Email', $data)){
|
|
|
|
Session::set('SessionForms.MemberLoginForm.Email', $data['Email']);
|
|
|
|
Session::set('SessionForms.MemberLoginForm.Remember', isset($data['Remember']));
|
|
|
|
}
|
2008-11-10 04:51:35 +01:00
|
|
|
|
|
|
|
if(isset($_REQUEST['BackURL'])) $backURL = $_REQUEST['BackURL'];
|
|
|
|
else $backURL = null;
|
|
|
|
|
|
|
|
if($backURL) Session::set('BackURL', $backURL);
|
2008-09-26 04:22:51 +02:00
|
|
|
|
|
|
|
if($badLoginURL = Session::get("BadLoginURL")) {
|
2011-10-29 06:06:19 +02:00
|
|
|
$this->controller->redirect($badLoginURL);
|
2008-09-26 04:22:51 +02:00
|
|
|
} else {
|
|
|
|
// Show the right tab on failed login
|
2008-11-10 04:51:35 +01:00
|
|
|
$loginLink = Director::absoluteURL(Security::Link("login"));
|
|
|
|
if($backURL) $loginLink .= '?BackURL=' . urlencode($backURL);
|
2011-10-29 06:06:19 +02:00
|
|
|
$this->controller->redirect($loginLink . '#' . $this->FormName() .'_tab');
|
2008-09-26 04:22:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-27 22:38:29 +02:00
|
|
|
/**
|
|
|
|
* Login in the user and figure out where to redirect the browser.
|
|
|
|
*
|
|
|
|
* The $data has this format
|
|
|
|
* array(
|
|
|
|
* 'AuthenticationMethod' => 'MemberAuthenticator',
|
|
|
|
* 'Email' => 'sam@silverstripe.com',
|
|
|
|
* 'Password' => '1nitialPassword',
|
|
|
|
* 'BackURL' => 'test/link',
|
|
|
|
* [Optional: 'Remember' => 1 ]
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function logInUserAndRedirect($data) {
|
|
|
|
Session::clear('SessionForms.MemberLoginForm.Email');
|
|
|
|
Session::clear('SessionForms.MemberLoginForm.Remember');
|
|
|
|
|
|
|
|
if(Member::currentUser()->isPasswordExpired()) {
|
|
|
|
if(isset($_REQUEST['BackURL']) && $backURL = $_REQUEST['BackURL']) {
|
|
|
|
Session::set('BackURL', $backURL);
|
|
|
|
}
|
|
|
|
$cp = new ChangePasswordForm($this->controller, 'ChangePasswordForm');
|
|
|
|
$cp->sessionMessage('Your password has expired. Please choose a new one.', 'good');
|
2011-10-29 06:06:19 +02:00
|
|
|
return $this->controller->redirect('Security/changepassword');
|
2011-10-27 22:38:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Absolute redirection URLs may cause spoofing
|
|
|
|
if(isset($_REQUEST['BackURL']) && $_REQUEST['BackURL'] && Director::is_site_url($_REQUEST['BackURL']) ) {
|
2011-10-29 06:06:19 +02:00
|
|
|
return $this->controller->redirect($_REQUEST['BackURL']);
|
2011-10-27 22:38:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Spoofing attack, redirect to homepage instead of spoofing url
|
|
|
|
if(isset($_REQUEST['BackURL']) && $_REQUEST['BackURL'] && !Director::is_site_url($_REQUEST['BackURL'])) {
|
2011-10-29 06:06:19 +02:00
|
|
|
return $this->controller->redirect(Director::absoluteBaseURL());
|
2011-10-27 22:38:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If a default login dest has been set, redirect to that.
|
|
|
|
if (Security::default_login_dest()) {
|
2011-10-29 06:06:19 +02:00
|
|
|
return $this->controller->redirect(Director::absoluteBaseURL() . Security::default_login_dest());
|
2011-10-27 22:38:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect the user to the page where he came from
|
|
|
|
$member = Member::currentUser();
|
|
|
|
if($member) {
|
|
|
|
$firstname = Convert::raw2xml($member->FirstName);
|
|
|
|
if(!empty($data['Remember'])) {
|
|
|
|
Session::set('SessionForms.MemberLoginForm.Remember', '1');
|
|
|
|
$member->logIn(true);
|
|
|
|
} else {
|
|
|
|
$member->logIn();
|
|
|
|
}
|
|
|
|
|
|
|
|
Session::set('Security.Message.message',
|
|
|
|
sprintf(_t('Member.WELCOMEBACK', "Welcome Back, %s"), $firstname)
|
|
|
|
);
|
|
|
|
Session::set("Security.Message.type", "good");
|
|
|
|
}
|
|
|
|
Controller::curr()->redirectBack();
|
|
|
|
}
|
|
|
|
|
2008-09-26 04:22:51 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Log out form handler method
|
|
|
|
*
|
|
|
|
* This method is called when the user clicks on "logout" on the form
|
|
|
|
* created when the parameter <i>$checkCurrentUser</i> of the
|
|
|
|
* {@link __construct constructor} was set to TRUE and the user was
|
|
|
|
* currently logged in.
|
|
|
|
*/
|
|
|
|
public function logout() {
|
|
|
|
$s = new Security();
|
|
|
|
$s->logout();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to authenticate the user
|
|
|
|
*
|
|
|
|
* @param array Submitted data
|
|
|
|
* @return Member Returns the member object on successful authentication
|
|
|
|
* or NULL on failure.
|
|
|
|
*/
|
|
|
|
public function performLogin($data) {
|
2010-10-13 06:04:32 +02:00
|
|
|
$member = call_user_func_array(array($this->authenticator_class, 'authenticate'), array($data, $this));
|
|
|
|
if($member) {
|
2008-09-26 04:22:51 +02:00
|
|
|
$member->LogIn(isset($data['Remember']));
|
|
|
|
return $member;
|
|
|
|
} else {
|
|
|
|
$this->extend('authenticationFailed', $data);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Forgot password form handler method
|
|
|
|
*
|
|
|
|
* This method is called when the user clicks on "I've lost my password"
|
|
|
|
*
|
|
|
|
* @param array $data Submitted data
|
|
|
|
*/
|
|
|
|
function forgotPassword($data) {
|
|
|
|
$SQL_data = Convert::raw2sql($data);
|
2008-10-16 13:29:43 +02:00
|
|
|
$SQL_email = $SQL_data['Email'];
|
2008-11-24 10:31:14 +01:00
|
|
|
$member = DataObject::get_one('Member', "\"Email\" = '{$SQL_email}'");
|
2008-09-26 04:22:51 +02:00
|
|
|
|
2008-10-16 13:29:43 +02:00
|
|
|
if($member) {
|
2008-09-26 04:22:51 +02:00
|
|
|
$member->generateAutologinHash();
|
|
|
|
|
2008-10-16 13:29:43 +02:00
|
|
|
$member->sendInfo(
|
|
|
|
'forgotPassword',
|
|
|
|
array(
|
|
|
|
'PasswordResetLink' => Security::getPasswordResetLink($member->AutoLoginHash)
|
|
|
|
)
|
|
|
|
);
|
2008-09-26 04:22:51 +02:00
|
|
|
|
2011-10-29 06:06:19 +02:00
|
|
|
$this->controller->redirect('Security/passwordsent/' . urlencode($data['Email']));
|
2008-10-16 13:29:43 +02:00
|
|
|
} elseif($data['Email']) {
|
2009-09-10 05:01:46 +02:00
|
|
|
// Avoid information disclosure by displaying the same status,
|
|
|
|
// regardless wether the email address actually exists
|
2011-10-29 06:06:19 +02:00
|
|
|
$this->controller->redirect('Security/passwordsent/' . urlencode($data['Email']));
|
2008-09-26 04:22:51 +02:00
|
|
|
} else {
|
2008-10-16 13:29:43 +02:00
|
|
|
$this->sessionMessage(
|
|
|
|
_t('Member.ENTEREMAIL', 'Please enter an email address to get a password reset link.'),
|
|
|
|
'bad'
|
|
|
|
);
|
|
|
|
|
2011-10-29 06:06:19 +02:00
|
|
|
$this->controller->redirect('Security/lostpassword');
|
2008-09-26 04:22:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2009-02-02 00:49:53 +01:00
|
|
|
?>
|