From a7d96426e31d8853a2def37c587b27fe1c1ff3f6 Mon Sep 17 00:00:00 2001 From: dartcafe Date: Tue, 11 Aug 2020 21:56:26 +0200 Subject: user can add own email address --- lib/Controller/PollController.php | 12 ++++++++++-- lib/Controller/ShareController.php | 24 +++++++++++++++++++++-- lib/Model/Acl.php | 40 +++++++++++++++++++++++++++++++------- lib/Service/ShareService.php | 30 +++++++++++++++++----------- 4 files changed, 84 insertions(+), 22 deletions(-) (limited to 'lib') diff --git a/lib/Controller/PollController.php b/lib/Controller/PollController.php index a564fe40..494cb59a 100644 --- a/lib/Controller/PollController.php +++ b/lib/Controller/PollController.php @@ -151,8 +151,15 @@ class PollController extends Controller { } try { - $shares = $this->shareService->list($pollId, $token); + if ($token) { + $share = $this->shareService->get($token); + $shares = []; + } else { + $share = null; + $shares = $this->shareService->list($pollId, $token); + } } catch (Exception $e) { + $share = null; $shares = []; } @@ -161,8 +168,9 @@ class PollController extends Controller { 'poll' => $poll, 'comments' => $comments, 'options' => $options, + 'share' => $share, 'shares' => $shares, - 'votes' => $votes + 'votes' => $votes, ], Http::STATUS_OK); } diff --git a/lib/Controller/ShareController.php b/lib/Controller/ShareController.php index 9cd5f554..0c50b4c7 100644 --- a/lib/Controller/ShareController.php +++ b/lib/Controller/ShareController.php @@ -84,6 +84,26 @@ class ShareController extends Controller { } } + /** + * Add share + * @NoAdminRequired + * @param int $pollId + * @param int $pollId + * @param string $type + * @param string $userId + * @param string $userEmail + * @return DataResponse + */ + public function get($token) { + try { + return new DataResponse(['share' => $this->shareService->get($token)], Http::STATUS_CREATED); + } catch (NotAuthorizedException $e) { + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); + } catch (\Exception $e) { + return new DataResponse($e, Http::STATUS_CONFLICT); + } + } + /** * Add share * @NoAdminRequired @@ -115,10 +135,10 @@ class ShareController extends Controller { * @param string $userName * @return DataResponse */ - public function personal($token, $userName) { + public function personal($token, $userName, $emailAddress = '') { try { - return new DataResponse($this->shareService->personal($token, $userName), Http::STATUS_CREATED); + return new DataResponse($this->shareService->personal($token, $userName, $emailAddress), Http::STATUS_CREATED); } catch (NotAuthorizedException $e) { return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } catch (InvalidUsername $e) { diff --git a/lib/Model/Acl.php b/lib/Model/Acl.php index ee7b627b..86743c67 100644 --- a/lib/Model/Acl.php +++ b/lib/Model/Acl.php @@ -75,6 +75,9 @@ class Acl implements JsonSerializable { /** @var Poll */ private $poll; + /** @var Share */ + private $share; + /** * Acl constructor. * @param string $appName @@ -84,7 +87,8 @@ class Acl implements JsonSerializable { * @param PollMapper $pollMapper * @param VoteMapper $voteMapper * @param ShareMapper $shareMapper - * @param Poll $pollMapper + * @param Poll $poll + * @param Share $share * */ public function __construct( @@ -94,7 +98,8 @@ class Acl implements JsonSerializable { PollMapper $pollMapper, VoteMapper $voteMapper, ShareMapper $shareMapper, - Poll $poll + Poll $poll, + Share $share ) { $this->userId = $userId; $this->userManager = $userManager; @@ -103,6 +108,7 @@ class Acl implements JsonSerializable { $this->voteMapper = $voteMapper; $this->shareMapper = $shareMapper; $this->poll = $poll; + $this->share = $share; } /** @@ -117,26 +123,27 @@ class Acl implements JsonSerializable { $this->token = $token; $this->pollId = 0; $this->userId = null; - $share = $this->shareMapper->findByToken($token); + $this->share = $this->shareMapper->findByToken($token); if (\OC::$server->getUserSession()->isLoggedIn()) { - if ($share->getType() !== 'group' && $share->getType() !== 'public') { + if ($this->share->getType() !== 'group' && $this->share->getType() !== 'public') { throw new NotAuthorizedException; } $this->userId = \OC::$server->getUserSession()->getUser()->getUID(); } else { - if ($share->getType() === 'group' || $share->getType() === 'user') { + if ($this->share->getType() === 'group' || $this->share->getType() === 'user') { throw new NotAuthorizedException; } - $this->userId = $share->getUserId(); + $this->userId = $this->share->getUserId(); } - $this->pollId = $share->getPollId(); + $this->pollId = $this->share->getPollId(); } elseif ($pollId) { $this->user = \OC::$server->getUserSession()->getUser()->getUID(); $this->pollId = $pollId; + $this->share = null; } $this->poll = $this->pollMapper->find($this->pollId); @@ -306,6 +313,16 @@ class Acl implements JsonSerializable { && $this->userId; } + /** + * @NoAdminRequired + * @return bool + */ + public function getAllowSubscribe(): bool { + return ($this->hasEmail()) + && !$this->poll->getDeleted() + && $this->getAllowView(); + } + /** * @NoAdminRequired * @return bool @@ -348,6 +365,14 @@ class Acl implements JsonSerializable { return $this->token; } + private function hasEmail():bool { + if ($this->share) { + return strlen($this->share->getUserEmail()) > 0; + } else { + return \OC::$server->getUserSession()->isLoggedIn(); + } + } + /** * @return array */ @@ -367,6 +392,7 @@ class Acl implements JsonSerializable { 'allowEdit' => $this->getAllowEdit(), 'allowSeeResults' => $this->getAllowSeeResults(), 'allowSeeUsernames' => $this->getAllowSeeUsernames(), + 'allowSubscribe' => $this->getAllowSubscribe(), 'userHasVoted' => $this->getUserHasVoted(), 'groupShare' => $this->getGroupShare(), 'personalShare' => $this->getPersonalShare(), diff --git a/lib/Service/ShareService.php b/lib/Service/ShareService.php index 70881cf9..83f6233f 100644 --- a/lib/Service/ShareService.php +++ b/lib/Service/ShareService.php @@ -120,6 +120,10 @@ class ShareService { throw new NotAuthorizedException; } + if ($type === 'contact') { + $type = 'external'; + } + $this->share = new Share(); $this->share->setType($type); $this->share->setPollId($pollId); @@ -168,19 +172,20 @@ class ShareService { * @throws NotAuthorizedException * @throws InvalidUsername */ - public function personal($token, $userName) { - $publicShare = $this->shareMapper->findByToken($token); + public function personal($token, $userName, $emailAddress) { + $this->share = $this->shareMapper->findByToken($token); // Return of validatePublicUsername is a DataResponse - $checkUsername = $this->systemController->validatePublicUsername($publicShare->getPollId(), $userName, $token); + $checkUsername = $this->systemController->validatePublicUsername($this->share->getPollId(), $userName, $token); // if status is not 200, return DataResponse from validatePublicUsername if ($checkUsername->getStatus() !== 200) { throw new InvalidUsername; } - if ($publicShare->getType() === 'public') { + if ($this->share->getType() === 'public') { + $pollId = $this->share->getPollId(); $this->share = new Share(); $this->share->setToken(\OC::$server->getSecureRandom()->generate( 16, @@ -189,17 +194,20 @@ class ShareService { ISecureRandom::CHAR_UPPER )); $this->share->setType('external'); - $this->share->setPollId($publicShare->getPollId()); + $this->share->setPollId($pollId); $this->share->setUserId($userName); - $this->share->setUserEmail(''); + $this->share->setUserEmail($emailAddress); $this->share->setInvitationSent(time()); - return $this->shareMapper->insert($this->share); + $this->shareMapper->insert($this->share); + $this->mailService->sendInvitationMail($this->share->getToken()); + return $this->share; - } elseif ($publicShare->getType() === 'email') { + } elseif ($this->share->getType() === 'email') { - $publicShare->setType('external'); - $publicShare->setUserId($userName); - return $this->shareMapper->update($publicShare); + $this->share->setType('external'); + $this->share->setUserId($userName); + $this->share->setUserEmail($emailAddress); + return $this->shareMapper->update($this->share); } else { throw new NotAuthorizedException; -- cgit v1.2.3