diff options
author | dartcafe <github@dartcafe.de> | 2020-09-13 13:45:46 +0300 |
---|---|---|
committer | dartcafe <github@dartcafe.de> | 2020-09-13 13:45:46 +0300 |
commit | d0ed62807b380868e97c31265e60d887788fa08a (patch) | |
tree | 1891dc5829205d50043b0f9905966f8da2996d1f /lib/Service | |
parent | c0ebb54966d3b334145d5075e1da8757b90018b8 (diff) |
Validate email address and improve register dialog
Diffstat (limited to 'lib/Service')
-rw-r--r-- | lib/Service/ShareService.php | 28 | ||||
-rw-r--r-- | lib/Service/SystemService.php | 31 |
2 files changed, 37 insertions, 22 deletions
diff --git a/lib/Service/ShareService.php b/lib/Service/ShareService.php index ae14935a..cba983d6 100644 --- a/lib/Service/ShareService.php +++ b/lib/Service/ShareService.php @@ -24,20 +24,19 @@ namespace OCA\Polls\Service; use OCA\Polls\Exceptions\NotAuthorizedException; -use OCA\Polls\Exceptions\InvalidUsername; use OCA\Polls\Exceptions\InvalidShareType; use OCP\Security\ISecureRandom; -use OCA\Polls\Controller\SystemController; +use OCA\Polls\Service\SystemService; use OCA\Polls\Db\ShareMapper; use OCA\Polls\Db\Share; use OCA\Polls\Model\Acl; class ShareService { - /** @var SystemController */ - private $systemController; + /** @var SystemService */ + private $systemService; /** @var ShareMapper */ private $shareMapper; @@ -53,20 +52,20 @@ class ShareService { /** * ShareController constructor. - * @param SystemController $systemController + * @param SystemService $systemService * @param ShareMapper $shareMapper * @param Share $share * @param MailService $mailService * @param Acl $acl */ public function __construct( - SystemController $systemController, + SystemService $systemService, ShareMapper $shareMapper, Share $share, MailService $mailService, Acl $acl ) { - $this->systemController = $systemController; + $this->systemService = $systemService; $this->shareMapper = $shareMapper; $this->share = $share; $this->mailService = $mailService; @@ -142,12 +141,12 @@ class ShareService { * @param string $token * @param string $emailAddress * @return Share - * @throws NotAuthorizedException + * @throws InvalidShareType */ public function setEmailAddress($token, $emailAddress) { $this->share = $this->shareMapper->findByToken($token); if ($this->share->getType() === 'external') { - // TODO: Simple validate email address + $this->systemService->validateEmailAddress($emailAddress); $this->share->setUserEmail($emailAddress); // TODO: Send confirmation return $this->shareMapper->update($this->share); @@ -164,17 +163,14 @@ class ShareService { * @param string $userName * @return Share * @throws NotAuthorizedException - * @throws InvalidUsername */ - public function personal($token, $userName, $emailAddress) { + public function personal($token, $userName, $emailAddress = '') { $this->share = $this->shareMapper->findByToken($token); - // Return of validatePublicUsername is a DataResponse - $checkUsername = $this->systemController->validatePublicUsername($this->share->getPollId(), $userName, $token); + $this->systemService->validatePublicUsername($this->share->getPollId(), $userName, $token); - // if status is not 200, return DataResponse from validatePublicUsername - if ($checkUsername->getStatus() !== 200) { - throw new InvalidUsername; + if ($emailAddress) { + $this->systemService->validateEmailAddress($emailAddress); } if ($this->share->getType() === 'public') { diff --git a/lib/Service/SystemService.php b/lib/Service/SystemService.php index 7ac14284..84355cb5 100644 --- a/lib/Service/SystemService.php +++ b/lib/Service/SystemService.php @@ -25,7 +25,8 @@ namespace OCA\Polls\Service; use OCA\Polls\Exceptions\NotAuthorizedException; use OCA\Polls\Exceptions\TooShortException; -use OCA\Polls\Exceptions\UsernameInvalidException; +use OCA\Polls\Exceptions\InvalidUsernameException; +use OCA\Polls\Exceptions\InvalidEmailAddress; use OCP\IGroupManager; use OCP\IUserManager; @@ -69,11 +70,11 @@ class SystemService { /** * Validate string as email address * @NoAdminRequired - * @param string $query + * @param string $emailAddress * @return bool */ - private function isValidEmail($email) { - return (!preg_match('/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/', $email)) ? false : true; + private function isValidEmail($emailAddress) { + return (!preg_match('/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/', $emailAddress)) ? false : true; } @@ -335,7 +336,25 @@ class SystemService { * @return Boolean * @throws NotAuthorizedException * @throws TooShortException - * @throws UsernameInvalidException + * @throws InvalidEmailAddress + */ + public function validateEmailAddress($emailAddress) { + if (!$this->isValidEmail($emailAddress)) { + throw new InvalidEmailAddress; + } + return true; + } + + + /** + * Validate it the user name is reservrd + * return false, if this username already exists as a user or as + * a participant of the poll + * @NoAdminRequired + * @return Boolean + * @throws NotAuthorizedException + * @throws TooShortException + * @throws InvalidUsernameException */ public function validatePublicUsername($pollId, $userName, $token) { @@ -403,7 +422,7 @@ class SystemService { // return forbidden, if list contains requested username foreach ($list as $element) { if (strtolower(trim($userName)) === strtolower(trim($element['id'])) || strtolower(trim($userName)) === strtolower(trim($element['displayName']))) { - throw new UsernameInvalidException; + throw new InvalidUsernameException; } } |