Skip to content

Commit

Permalink
Implement remember login mode choice (#312)
Browse files Browse the repository at this point in the history
Adds a new option to allow front-end user sessions to be persistent or not, or optionally ask the user themselves if they want the session to be persistent or not. By default, all sessions are persistent.

Credit to @KGE.
  • Loading branch information
KGE authored and bennothommo committed Jul 3, 2019
1 parent d090069 commit d484799
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 3 deletions.
26 changes: 25 additions & 1 deletion components/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function prepareVars()
$this->page['canRegister'] = $this->canRegister();
$this->page['loginAttribute'] = $this->loginAttribute();
$this->page['loginAttributeLabel'] = $this->loginAttributeLabel();
$this->page['rememberLoginMode'] = $this->rememberLoginMode();
}

/**
Expand Down Expand Up @@ -137,6 +138,14 @@ public function loginAttributeLabel()
);
}

/**
* Returns the login remember mode.
*/
public function rememberLoginMode()
{
return UserSettings::get('remember_login', UserSettings::REMEMBER_ALWAYS);
}

/**
* Looks for the activation code from the URL parameter. If nothing
* is found, the GET parameter 'activate' is used instead.
Expand Down Expand Up @@ -192,9 +201,24 @@ public function onSignin()
'password' => array_get($data, 'password')
];

/*
* Login remember mode
*/
switch ($this->rememberLoginMode()) {
case UserSettings::REMEMBER_ALWAYS:
$remember = true;
break;
case UserSettings::REMEMBER_NEVER:
$remember = false;
break;
case UserSettings::REMEMBER_ASK:
$remember = (bool) array_get($data, 'remember', false);
break;
}

Event::fire('rainlab.user.beforeAuthenticate', [$this, $credentials]);

$user = Auth::authenticate($credentials, true);
$user = Auth::authenticate($credentials, $remember);
if ($user->isBanned()) {
Auth::logout();
throw new AuthException(/*Sorry, this user is currently not activated. Please contact us for further assistance.*/'rainlab.user::lang.account.banned');
Expand Down
8 changes: 8 additions & 0 deletions components/account/signin.htm
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
placeholder="Enter your password" />
</div>

{% if rememberLoginMode == 'ask' %}
<div class="form-group">
<div class="checkbox">
<label><input name="remember" type="checkbox" value="1">Stay logged in</label>
</div>
</div>
{% endif %}

<button type="submit" class="btn btn-default">Sign in</button>

{{ form_close() }}
5 changes: 5 additions & 0 deletions lang/en/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@
'block_persistence_comment' => 'When enabled users cannot sign in to multiple devices at the same time.',
'login_attribute' => 'Login attribute',
'login_attribute_comment' => 'Select what primary user detail should be used for signing in.',
'remember_login' => 'Remember login mode',
'remember_login_comment' => 'Select if the user session should be persistent.',
'remember_always' => 'Always',
'remember_never' => 'Never',
'remember_ask' => 'Ask the user on login',
],
'user' => [
'label' => 'User',
Expand Down
32 changes: 30 additions & 2 deletions models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class Settings extends Model
const LOGIN_EMAIL = 'email';
const LOGIN_USERNAME = 'username';

const REMEMBER_ALWAYS = 'always';
const REMEMBER_NEVER = 'never';
const REMEMBER_ASK = 'ask';
const MIN_PASSWORD_LENGTH_DEFAULT = 8;

public function initSettingsData()
Expand All @@ -32,6 +35,7 @@ public function initSettingsData()
$this->block_persistence = false;
$this->allow_registration = true;
$this->login_attribute = self::LOGIN_EMAIL;
$this->remember_login = self::REMEMBER_ALWAYS;
$this->min_password_length = self::MIN_PASSWORD_LENGTH_DEFAULT;
}

Expand All @@ -53,6 +57,15 @@ public function getActivateModeOptions()
];
}

public function getActivateModeAttribute($value)
{
if (!$value) {
return self::ACTIVATE_AUTO;
}

return $value;
}

public function getLoginAttributeOptions()
{
return [
Expand All @@ -61,10 +74,25 @@ public function getLoginAttributeOptions()
];
}

public function getActivateModeAttribute($value)
public function getRememberLoginOptions()
{
return [
self::REMEMBER_ALWAYS => [
'rainlab.user::lang.settings.remember_always',
],
self::REMEMBER_NEVER => [
'rainlab.user::lang.settings.remember_never',
],
self::REMEMBER_ASK => [
'rainlab.user::lang.settings.remember_ask',
]
];
}

public function getRememberLoginAttribute($value)
{
if (!$value) {
return self::ACTIVATE_AUTO;
return self::REMEMBER_ALWAYS;
}

return $value;
Expand Down
8 changes: 8 additions & 0 deletions models/settings/fields.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ tabs:
type: radio
tab: rainlab.user::lang.settings.signin_tab

# Remeber Login Mode
remember_login:
span: left
label: rainlab.user::lang.settings.remember_login
commentAbove: rainlab.user::lang.settings.remember_login_comment
type: radio
tab: rainlab.user::lang.settings.signin_tab

# Require Activation
allow_registration:
span: left
Expand Down

0 comments on commit d484799

Please sign in to comment.