Skip to content

Commit

Permalink
Merge branch 'release/2.8.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Dec 13, 2018
2 parents c0c1aff + 8ddf0fe commit ba36ee8
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 18 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# v2.8.1
## 12/13/2018

1. [](#bugfix)
* Fix various redirects to use `lang-safe` variety for better multi-language support [#186]((https://github.com/getgrav/grav-plugin-login/issues/186))
* Ensure only defined `user_registration.fields` are allowed in registration and profile forms

# v2.8.0
## 11/12/2018

Expand Down
2 changes: 1 addition & 1 deletion blueprints.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Login
version: 2.8.0
version: 2.8.1
description: Enables user authentication and login screen.
icon: sign-in
author:
Expand Down
14 changes: 10 additions & 4 deletions classes/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,13 @@ protected function taskForgot()
$author = $this->grav['config']->get('site.author.name', '');
$fullname = $user->fullname ?: $user->username;

$reset_link = $this->grav['base_url_absolute'] . $this->grav['config']->get('plugins.login.route_reset') . '/task:login.reset/token' . $param_sep . $token . '/user' . $param_sep . $user->username . '/nonce' . $param_sep . Utils::getNonce('reset-form');
if ($this->grav['language']->getDefault() != $this->grav['language']->getLanguage()) {
$lang = '/'.$this->grav['language']->getLanguage();
} else {
$lang = '';
}

$reset_link = $this->grav['base_url_absolute'] . $lang . $this->grav['config']->get('plugins.login.route_reset') . '/task:login.reset/token' . $param_sep . $token . '/user' . $param_sep . $user->username . '/nonce' . $param_sep . Utils::getNonce('reset-form');

$sitename = $this->grav['config']->get('site.title', 'Website');

Expand Down Expand Up @@ -381,7 +387,7 @@ public function taskReset()
if ($good_token === $token) {
if (time() > $expire) {
$messages->add($language->translate('PLUGIN_LOGIN.RESET_LINK_EXPIRED'), 'error');
$this->grav->redirect($this->grav['config']->get('plugins.login.route_forgot', '/'));
$this->grav->redirectLangSafe($this->grav['config']->get('plugins.login.route_forgot', '/'));

return true;
}
Expand All @@ -401,7 +407,7 @@ public function taskReset()
}

$messages->add($language->translate('PLUGIN_LOGIN.RESET_INVALID_LINK'), 'error');
$this->grav->redirect($this->grav['config']->get('plugins.login.route_forgot'));
$this->grav->redirectLangSafe($this->grav['config']->get('plugins.login.route_forgot'));

return true;

Expand All @@ -412,7 +418,7 @@ public function taskReset()

if (!$user || !$token) {
$messages->add($language->translate('PLUGIN_LOGIN.RESET_INVALID_LINK'), 'error');
$this->grav->redirect($this->grav['config']->get('plugins.login.route_forgot'));
$this->grav->redirectLangSafe($this->grav['config']->get('plugins.login.route_forgot'));

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion classes/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public function authenticate($credentials, $options = ['remember_me' => true])
}

if ($redirect) {
$this->grav->redirect($redirect, $event->getRedirectCode());
$this->grav->redirectLangSafe($redirect, $event->getRedirectCode());
}

return $user->authenticated && $user->authorized;
Expand Down
45 changes: 33 additions & 12 deletions login.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
namespace Grav\Plugin;

use Grav\Common\Data\Data;
use Grav\Common\Debugger;
use Grav\Common\Grav;
use Grav\Common\Language\Language;
Expand Down Expand Up @@ -407,7 +408,7 @@ public function handleUserActivation()
}
}

$this->grav->redirect($redirect_route ?: '/', $redirect_code);
$this->grav->redirectLangSafe($redirect_route ?: '/', $redirect_code);
}

/**
Expand Down Expand Up @@ -537,7 +538,7 @@ public function authorizePage()

// User is not logged in; redirect to login page.
if ($this->redirect_to_login && $this->route && !$authorized) {
$this->grav->redirect($this->route, 302);
$this->grav->redirectLangSafe($this->route, 302);
}

/** @var Twig $twig */
Expand Down Expand Up @@ -653,8 +654,14 @@ private function processUserRegistration($form, Event $event)
throw new \RuntimeException($language->translate('PLUGIN_LOGIN.USER_REGISTRATION_DISABLED'));
}

$form->validate();
$form->filter();

/** @var Data $form_data */
$form_data = $form->getData();

// Check for existing username
$username = $form->value('username');
$username = $form_data->get('username');
$existing_username = User::find($username,['username']);
if ($existing_username->exists()) {
$this->grav->fireEvent('onFormValidationError', new Event([
Expand All @@ -669,7 +676,7 @@ private function processUserRegistration($form, Event $event)
}

// Check for existing email
$email = $form->value('email');
$email = $form_data->get('email');
$existing_email = User::find($email,['email']);
if ($existing_email->exists()) {
$this->grav->fireEvent('onFormValidationError', new Event([
Expand All @@ -691,7 +698,7 @@ private function processUserRegistration($form, Event $event)
if ($this->config->get('plugins.login.user_registration.options.validate_password1_and_password2',
false)
) {
if ($form->value('password1') !== $form->value('password2')) {
if ($form_data->get('password1') !== $form_data->get('password2')) {
$this->grav->fireEvent('onFormValidationError', new Event([
'form' => $form,
'message' => $language->translate('PLUGIN_LOGIN.PASSWORDS_DO_NOT_MATCH')
Expand All @@ -700,7 +707,7 @@ private function processUserRegistration($form, Event $event)

return;
}
$data['password'] = $form->value('password1');
$data['password'] = $form_data->get('password1');
}

$fields = (array)$this->config->get('plugins.login.user_registration.fields', []);
Expand All @@ -722,8 +729,8 @@ private function processUserRegistration($form, Event $event)
}
}

if (!isset($data[$field]) && $form->value($field)) {
$data[$field] = $form->value($field);
if (!isset($data[$field]) && $form_data->get($field)) {
$data[$field] = $form_data->get($field);
}
}

Expand Down Expand Up @@ -774,7 +781,7 @@ private function processUserRegistration($form, Event $event)
}

if ($redirect) {
$this->grav->redirect($redirect, $redirect_code);
$this->grav->redirectLangSafe($redirect, $redirect_code);
}
}

Expand All @@ -791,6 +798,12 @@ private function processUserProfile($form, Event $event)
$user = $this->grav['user'];
$language = $this->grav['language'];

$form->validate();
$form->filter();

/** @var Data $form_data */
$form_data = $form->getData();

// Don't save if user doesn't exist
if (!$user->exists()) {
$this->grav->fireEvent('onFormValidationError', new Event([
Expand All @@ -802,7 +815,7 @@ private function processUserProfile($form, Event $event)
}

// Stop overloading of username
$username = $form->value('username');
$username = $form->data('username');
if (isset($username)) {
$this->grav->fireEvent('onFormValidationError', new Event([
'form' => $form,
Expand All @@ -816,7 +829,7 @@ private function processUserProfile($form, Event $event)
}

// Check for existing email
$email = $form->value('email');
$email = $form->data('email');
$existing_email = User::find($email,['email']);
if ($user->username != $existing_email->username && $existing_email->exists()) {
$this->grav->fireEvent('onFormValidationError', new Event([
Expand All @@ -830,7 +843,15 @@ private function processUserProfile($form, Event $event)
return;
}

$user->merge($form->getData()->toArray());
$fields = (array)$this->config->get('plugins.login.user_registration.fields', []);

$data = [];
foreach ($fields as $field) {
if (!isset($data[$field]) && $form_data->get($field)) {
$data[$field] = $form_data->get($field);
}
}
$user->merge($data);

try {
$user->save();
Expand Down

0 comments on commit ba36ee8

Please sign in to comment.