From ffcc23957e78002609c0e787ebcefdf0d2ed0efd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Sat, 8 Jul 2017 14:27:51 +0200 Subject: [PATCH] Check if username is already used for a pending registration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- db/registrationmapper.php | 12 ++++++++++++ service/registrationservice.php | 6 +++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/db/registrationmapper.php b/db/registrationmapper.php index 8ebd54c..0559af9 100644 --- a/db/registrationmapper.php +++ b/db/registrationmapper.php @@ -23,6 +23,7 @@ namespace OCA\Registration\Db; +use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\Entity; use OCP\AppFramework\Db\Mapper; use OCP\IDBConnection; @@ -48,7 +49,18 @@ class RegistrationMapper extends Mapper { public function findBySecret($secret) { return $this->findEntity('SELECT * FROM `*PREFIX*registration` WHERE `client_secret` = ? ', [$secret]); + } + public function usernameIsPending($username) { + try { + $entity = $this->findEntity( + 'SELECT id FROM `*PREFIX*registration` WHERE `username` = ? ', + [$username] + ); + } catch (DoesNotExistException $e) { + return false; + } + return true; } /** diff --git a/service/registrationservice.php b/service/registrationservice.php index 9da62bb..f9337c7 100644 --- a/service/registrationservice.php +++ b/service/registrationservice.php @@ -192,9 +192,13 @@ class RegistrationService { * @throws RegistrationException */ public function validateUsername($username) { - if($username === "" || $this->userManager->get($username) !== null) { + if($username === "") { throw new RegistrationException($this->l10n->t('Please provide a valid user name.')); } + + if($this->registrationMapper->usernameIsPending($username) || $this->userManager->get($username) !== null) { + throw new RegistrationException($this->l10n->t('The username you have chosen already exists.')); + } } /**