Compare commits

...

3 commits

Author SHA1 Message Date
Benjamin Renard ff9f623f99
composer: fix ext-pdo requirement for PHP 8 compatibility 2023-07-10 18:03:55 +02:00
Benjamin Renard 15c1daabb5
Add implode_with_keys function 2023-07-10 17:58:56 +02:00
Benjamin Renard 43a467b15a
Login form: add remember username feature 2023-04-22 18:56:13 +02:00
9 changed files with 80 additions and 18 deletions

View file

@ -24,7 +24,7 @@
"smarty-gettext/smarty-gettext": "^1.6", "smarty-gettext/smarty-gettext": "^1.6",
"sepia/po-parser": "^6.0", "sepia/po-parser": "^6.0",
"sentry/sdk": "^3.3", "sentry/sdk": "^3.3",
"ext-pdo": "^7.3", "ext-pdo": ">=7.3",
"ext-json": "*", "ext-json": "*",
"ext-yaml": "^2.0", "ext-yaml": "^2.0",
"league/mime-type-detection": "^1.11", "league/mime-type-detection": "^1.11",

View file

@ -172,6 +172,12 @@ auth:
http: "HTTP" http: "HTTP"
cas: "SSO" cas: "SSO"
# Remember username
# Enable the feature (default: true)
# remember_username: true
# Cookie name (default: remember_username)
# remember_username_cookie_name: "remember_username"
# #
# HTTP Authentication Configuration # HTTP Authentication Configuration
# #

View file

@ -1,7 +1,7 @@
msgid "" msgid ""
msgstr "" msgstr ""
"Project-Id-Version: \n" "Project-Id-Version: \n"
"POT-Creation-Date: 2023-03-22 19:13+0100\n" "POT-Creation-Date: 2023-04-22 18:48+0200\n"
"PO-Revision-Date: \n" "PO-Revision-Date: \n"
"Last-Translator: Benjamin Renard <brenard@easter-eggs.com>\n" "Last-Translator: Benjamin Renard <brenard@easter-eggs.com>\n"
"Language-Team: \n" "Language-Team: \n"
@ -28,11 +28,11 @@ msgstr "Inconnu"
msgid "Unable to connect to the database." msgid "Unable to connect to the database."
msgstr "Impossible de se connecter à la base de données." msgstr "Impossible de se connecter à la base de données."
#: Auth/Form.php:41 #: Auth/Form.php:51
msgid "Invalid username or password." msgid "Invalid username or password."
msgstr "Nom d'utilisateur ou mot de passe invalide." msgstr "Nom d'utilisateur ou mot de passe invalide."
#: Auth/Form.php:74 #: Auth/Form.php:92
msgid "Sign in" msgid "Sign in"
msgstr "Connexion" msgstr "Connexion"
@ -581,15 +581,19 @@ msgstr "Merci de patienter pendant le traitement de votre requête."
msgid "Are you sure?" msgid "Are you sure?"
msgstr "Êtes-vous sure ?" msgstr "Êtes-vous sure ?"
#: templates/login.tpl:19 templates/login.tpl:20 #: templates/login.tpl:20 templates/login.tpl:21
msgid "Username" msgid "Username"
msgstr "Nom d'utilisateur" msgstr "Nom d'utilisateur"
#: templates/login.tpl:24 templates/login.tpl:25 #: templates/login.tpl:26 templates/login.tpl:27
msgid "Password" msgid "Password"
msgstr "Mot de passe" msgstr "Mot de passe"
#: templates/login.tpl:29 #: templates/login.tpl:33
msgid "Remember username"
msgstr "Mémoriser nom d'utilisateur"
#: templates/login.tpl:38
msgid "Submit" msgid "Submit"
msgstr "Envoyer" msgstr "Envoyer"

View file

@ -1,7 +1,7 @@
msgid "" msgid ""
msgstr "" msgstr ""
"POT-Creation-Date: 2023-03-22 19:13+0100\n" "POT-Creation-Date: 2023-04-22 18:48+0200\n"
"PO-Revision-Date: 2023-03-22 19:13+0100\n" "PO-Revision-Date: 2023-04-22 18:48+0200\n"
"MIME-Version: 1.0\n" "MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n" "Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
@ -22,11 +22,11 @@ msgstr ""
msgid "Unable to connect to the database." msgid "Unable to connect to the database."
msgstr "" msgstr ""
#: Auth/Form.php:41 #: Auth/Form.php:51
msgid "Invalid username or password." msgid "Invalid username or password."
msgstr "" msgstr ""
#: Auth/Form.php:74 #: Auth/Form.php:92
msgid "Sign in" msgid "Sign in"
msgstr "" msgstr ""
@ -504,15 +504,19 @@ msgstr ""
msgid "Are you sure?" msgid "Are you sure?"
msgstr "" msgstr ""
#: templates/login.tpl:19 templates/login.tpl:20 #: templates/login.tpl:20 templates/login.tpl:21
msgid "Username" msgid "Username"
msgstr "" msgstr ""
#: templates/login.tpl:24 templates/login.tpl:25 #: templates/login.tpl:26 templates/login.tpl:27
msgid "Password" msgid "Password"
msgstr "" msgstr ""
#: templates/login.tpl:29 #: templates/login.tpl:33
msgid "Remember username"
msgstr ""
#: templates/login.tpl:38
msgid "Submit" msgid "Submit"
msgstr "" msgstr ""

View file

@ -172,6 +172,12 @@ auth:
http: "HTTP" http: "HTTP"
cas: "SSO" cas: "SSO"
# Remember username
# Enable the feature (default: true)
# remember_username: true
# Cookie name (default: remember_username)
# remember_username_cookie_name: "remember_username"
# #
# HTTP Authentication Configuration # HTTP Authentication Configuration
# #

View file

@ -22,6 +22,8 @@ class Form extends Method {
array( array(
'display_other_methods' => array(), 'display_other_methods' => array(),
'include_navbar' => true, 'include_navbar' => true,
'remember_username' => true,
'remember_username_cookie_name' => 'remember_username',
) )
); );
Url :: add_url_handler('#^login$#', array('EesyPHP\\Auth\\Form', 'handle_login'), null, false); Url :: add_url_handler('#^login$#', array('EesyPHP\\Auth\\Form', 'handle_login'), null, false);
@ -38,7 +40,15 @@ class Form extends Method {
$user = null; $user = null;
if (isset($_REQUEST['username']) && isset($_REQUEST['password'])) { if (isset($_REQUEST['username']) && isset($_REQUEST['password'])) {
$user = Auth :: authenticate($_REQUEST['username'], $_REQUEST['password']); $user = Auth :: authenticate($_REQUEST['username'], $_REQUEST['password']);
if (!$user) Tpl::add_error(_('Invalid username or password.')); if ($user) {
setcookie(
App::get('auth.login_form.remember_username_cookie_name'),
App::get('auth.login_form.remember_username') && isset($_REQUEST['remember-username'])?
$user->username:null
);
}
else
Tpl::add_error(_('Invalid username or password.'));
} }
if ($force && !$user) { if ($force && !$user) {
if (Url :: get_current_url() != 'login') if (Url :: get_current_url() != 'login')
@ -67,8 +77,15 @@ class Form extends Method {
} }
if ($user) if ($user)
Url :: redirect(isset($_REQUEST['next'])?urldecode($_REQUEST['next']):null); Url :: redirect(isset($_REQUEST['next'])?urldecode($_REQUEST['next']):null);
else
Tpl :: assign('next', (isset($_REQUEST['next'])?urldecode($_REQUEST['next']):'')); $remember_username = App::get('auth.login_form.remember_username', null, 'bool');
Tpl :: assign('remember_username', $remember_username);
if ($remember_username) {
$cookie_name = App::get('auth.login_form.remember_username_cookie_name');
Tpl :: assign('username', isset($_COOKIE[$cookie_name])?$_COOKIE[$cookie_name]:null);
}
Tpl :: assign('next', isset($_REQUEST['next'])?urldecode($_REQUEST['next']):'');
Tpl :: assign('display_other_methods', $display_other_methods); Tpl :: assign('display_other_methods', $display_other_methods);
Tpl :: assign('include_navbar', App::get('auth.login_form.include_navbar', null, 'bool')); Tpl :: assign('include_navbar', App::get('auth.login_form.include_navbar', null, 'bool'));
Tpl :: display('login.tpl', I18n::_('Sign in')); Tpl :: display('login.tpl', I18n::_('Sign in'));

View file

@ -328,4 +328,20 @@ function format_duration($value, $unit=null, $precision=null, $separator=null) {
return implode(is_null($separator)?' ':strval($separator), $result); return implode(is_null($separator)?' ':strval($separator), $result);
} }
/**
* Implode array's keys & values (ex: 'k1=v1, k2=v2, ...')
* @param array<string|int,mixed> $values Array to implode
* @param boolean $quoted Set to false to disable values quotting (optional, default: true)
* @param string $separator Values separator (opional, default: ", ")
* @param string $kv_separator Key/value separator (optional, default: "=")
* @return string Imploded array string
*/
function implode_with_keys($values, $quoted=true, $separator=', ', $kv_separator='=') {
$result = [];
$quoted = $quoted?'"':'';
foreach ($values as $key => $value)
$result[] = "$key$kv_separator$quoted$value$quoted";
return implode($separator, $result);
}
# vim: tabstop=2 shiftwidth=2 softtabstop=2 expandtab # vim: tabstop=2 shiftwidth=2 softtabstop=2 expandtab

View file

@ -16,7 +16,7 @@
<div class="form-floating"> <div class="form-floating">
<input type="text" class="form-control" id="input_username" name="username" <input type="text" class="form-control" id="input_username" name="username"
placeholder="{t domain=$CORE_TEXT_DOMAIN}Username{/t}"/> placeholder="{t domain=$CORE_TEXT_DOMAIN}Username{/t}" value="{if $remember_username && $username}{$username|escape:"quotes"}{/if}"/>
<label for="input_username">{t domain=$CORE_TEXT_DOMAIN}Username{/t}</label> <label for="input_username">{t domain=$CORE_TEXT_DOMAIN}Username{/t}</label>
</div> </div>
<div class="form-floating"> <div class="form-floating">
@ -25,6 +25,15 @@
<label for="input_password">{t domain=$CORE_TEXT_DOMAIN}Password{/t}</label> <label for="input_password">{t domain=$CORE_TEXT_DOMAIN}Password{/t}</label>
</div> </div>
{if $remember_username}
<div class="checkbox mb-3">
<label>
<input type="checkbox" name="remember-username" value="remember" {if $username}checked{/if}>
{t domain=$CORE_TEXT_DOMAIN}Remember username{/t}
</label>
</div>
{/if}
<button class="w-100 btn btn-lg btn-primary" type="submit"> <button class="w-100 btn btn-lg btn-primary" type="submit">
{t domain=$CORE_TEXT_DOMAIN}Submit{/t} {t domain=$CORE_TEXT_DOMAIN}Submit{/t}
</button> </button>