From e590022cf89ea46d70220eaa81662aa3089c1e77 Mon Sep 17 00:00:00 2001 From: Timz99 Date: Sat, 19 Aug 2017 21:47:07 +0200 Subject: [PATCH] Validation and (.) support Added validation for the username in the install proces. Added dot (.) to the list of allowed symbols. --- .../Install/Controller/InstallController.php | 1 + src/psm/Util/User/UserValidator.php | 31 +++++++++++++++---- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/psm/Module/Install/Controller/InstallController.php b/src/psm/Module/Install/Controller/InstallController.php index 81ec1f96..d4d93c99 100644 --- a/src/psm/Module/Install/Controller/InstallController.php +++ b/src/psm/Module/Install/Controller/InstallController.php @@ -244,6 +244,7 @@ class InstallController extends AbstractController { } else { // validate the lot try { + $validator->username_new($new_user['user_name']); $validator->email($new_user['email']); $validator->password($new_user['password'], $new_user['password_repeat']); } catch(\InvalidArgumentException $e) { diff --git a/src/psm/Util/User/UserValidator.php b/src/psm/Util/User/UserValidator.php index 138e1801..33243745 100644 --- a/src/psm/Util/User/UserValidator.php +++ b/src/psm/Util/User/UserValidator.php @@ -78,7 +78,7 @@ class UserValidator { if(strlen($username) > 64 || strlen($username) < 2) { throw new \InvalidArgumentException('user_name_bad_length'); } - if (!preg_match('/^[a-zA-Z\d_]{2,64}$/i', $username)) { + if (!preg_match('/^[a-zA-Z\d_\.]{2,64}$/i', $username)) { throw new \InvalidArgumentException('user_name_invalid'); } $user_exists = $this->user->getUserByUsername($username); @@ -107,11 +107,30 @@ class UserValidator { } /** - * Check email - * @param string $email - * @return boolean - * @throws \InvalidArgumentException - */ + * Install only; Check username on: + * + * - Length (2-64 chars) + * - Contents (alphabetic chars and digits only) + * @param string $username + * @return boolean + * @throws \InvalidArgumentException + */ + public function username_new($username) { + if(strlen($username) > 64 || strlen($username) < 2) { + throw new \InvalidArgumentException('user_name_bad_length'); + } + if (!preg_match('/^[a-zA-Z\d_\.]{2,64}$/i', $username)) { + throw new \InvalidArgumentException('user_name_invalid'); + } + return true; + } + + /** + * Check email + * @param string $email + * @return boolean + * @throws \InvalidArgumentException + */ public function email($email) { if(strlen($email) > 255 || strlen($email) < 5) { throw new \InvalidArgumentException('user_email_bad_length');