From 62820793d03e27a48cf88b50b7d788404e6b9f09 Mon Sep 17 00:00:00 2001 From: Flavio Copes Date: Thu, 10 Dec 2015 19:34:02 +0100 Subject: [PATCH] Add pattern for username. Drop manual validation --- admin.php | 62 ++--------------------------------------- pages/admin/register.md | 2 ++ 2 files changed, 4 insertions(+), 60 deletions(-) diff --git a/admin.php b/admin.php index c1114129..afa873d0 100644 --- a/admin.php +++ b/admin.php @@ -79,46 +79,6 @@ class AdminPlugin extends Plugin } } - /** - * Validate a value. Currently validates - * - * - 'user' for username format and username availability. - * - 'password1' for password format - * - 'password2' for equality to password1 - * - * @param object $form The form - * @param string $type The field type - * @param string $value The field value - * @param string $extra Any extra value required - * - * @return mixed - */ - protected function validate($type, $value, $extra = '') - { - switch ($type) { - case 'username_format': - if (!preg_match('/^[a-z0-9_-]{3,16}$/', $value)) { - return false; - } - return true; - break; - - case 'password1': - if (!preg_match('/(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}/', $value)) { - return false; - } - return true; - break; - - case 'password2': - if (strcmp($value, $extra)) { - return false; - } - return true; - break; - } - } - /** * Process the admin registration form. * @@ -141,26 +101,7 @@ class AdminPlugin extends Plugin $data = []; $username = $form->value('username'); - if (!$this->validate('username_format', $username)) { - $this->grav->fireEvent('onFormValidationError', - new Event([ - 'form' => $form, - 'message' => $this->grav['language']->translate('PLUGIN_LOGIN.USERNAME_NOT_VALID')])); - $event->stopPropagation(); - return; - } - - - if (!$this->validate('password1', $form->value('password1'))) { - $this->grav->fireEvent('onFormValidationError', - new Event([ - 'form' => $form, - 'message' => $this->grav['language']->translate('PLUGIN_LOGIN.PASSWORD_NOT_VALID') - ])); - $event->stopPropagation(); - return; - } - if (!$this->validate('password2', $form->value('password2'), $form->value('password1'))) { + if ($form->value('password1') != $form->value('password2')) { $this->grav->fireEvent('onFormValidationError', new Event([ 'form' => $form, @@ -169,6 +110,7 @@ class AdminPlugin extends Plugin $event->stopPropagation(); return; } + $data['password'] = $form->value('password1'); $fields = [ diff --git a/pages/admin/register.md b/pages/admin/register.md index 7adb5156..87be06a2 100644 --- a/pages/admin/register.md +++ b/pages/admin/register.md @@ -10,6 +10,8 @@ form: placeholder: PLUGIN_ADMIN.USERNAME validate: required: true + message: PLUGIN_LOGIN.USERNAME_NOT_VALID + pattern: '^[a-z0-9_-]{3,16}$' - name: email type: text