diff options
author | dartcafe <github@dartcafe.de> | 2020-07-05 16:30:05 +0300 |
---|---|---|
committer | dartcafe <github@dartcafe.de> | 2020-07-05 16:30:05 +0300 |
commit | f12db4606555121fc912a3185ad616c5dbdb057d (patch) | |
tree | 1a2b550af930b94031a368805a7229916ac28e7f /lib | |
parent | e515d58b6c7115950e01e0ada93d605c6a5b7dcb (diff) |
extend controllers for indiviual invitation sending
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Controller/ShareApiController.php | 22 | ||||
-rw-r--r-- | lib/Controller/ShareController.php | 37 | ||||
-rw-r--r-- | lib/Service/MailService.php | 10 | ||||
-rw-r--r-- | lib/Service/ShareService.php | 40 |
4 files changed, 54 insertions, 55 deletions
diff --git a/lib/Controller/ShareApiController.php b/lib/Controller/ShareApiController.php index cceb7aa6..3d7912da 100644 --- a/lib/Controller/ShareApiController.php +++ b/lib/Controller/ShareApiController.php @@ -35,21 +35,25 @@ use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; use OCA\Polls\Service\ShareService; +use OCA\Polls\Service\MailService; class ShareApiController extends ApiController { private $shareService; + private $mailService; /** * ShareController constructor. * @param string $appName * @param string $userId * @param IRequest $request + * @param MailService $mailService * @param ShareService $shareService */ public function __construct( string $appName, IRequest $request, + MailService $mailService, ShareService $shareService ) { parent::__construct($appName, @@ -58,6 +62,7 @@ class ShareApiController extends ApiController { 'Authorization, Content-Type, Accept', 1728000); $this->shareService = $shareService; + $this->mailService = $mailService; } /** @@ -121,6 +126,23 @@ class ShareApiController extends ApiController { } /** + * SendInvitation + * Sent invitation mails for a share + * @NoAdminRequired + * @CORS + * @NoCSRFRequired + * @param string $token + * @return DataResponse + */ + public function sendInvitation($token) { + try { + return new DataResponse($this->mailService->sendInvitationMail($token), Http::STATUS_OK); + } catch (Exception $e) { + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); + } + } + + /** * delete share * @NoAdminRequired * @CORS diff --git a/lib/Controller/ShareController.php b/lib/Controller/ShareController.php index aa80cc3d..bd3dd187 100644 --- a/lib/Controller/ShareController.php +++ b/lib/Controller/ShareController.php @@ -39,11 +39,13 @@ use OCP\AppFramework\Http\DataResponse; use OCA\Polls\Model\Acl; use OCA\Polls\Service\ShareService; +use OCA\Polls\Service\MailService; class ShareController extends Controller { private $logger; private $shareService; + private $mailService; private $userId; /** @@ -52,6 +54,7 @@ class ShareController extends Controller { * @param string $userId * @param IRequest $request * @param ILogger $logger + * @param MailService $mailService * @param ShareService $shareService */ public function __construct( @@ -59,12 +62,14 @@ class ShareController extends Controller { $userId, IRequest $request, ILogger $logger, + MailService $mailService, ShareService $shareService ) { parent::__construct($appName, $request); $this->logger = $logger; $this->userId = $userId; $this->shareService = $shareService; + $this->mailService = $mailService; } /** @@ -75,21 +80,14 @@ class ShareController extends Controller { * @param Array $share * @return DataResponse */ - public function add($pollId, $share) { - try { - $return = $this->shareService->write( - $pollId, - $share['type'], - $share['userId'], - isset($share['userEmail']) ? $share['userEmail'] : '' - ); - return new DataResponse($return, Http::STATUS_CREATED); + public function add($pollId, $type, $userId = '', $userEmail = '') { + try { + return new DataResponse(['share' => $this->shareService->add($pollId, $type, $userId, $userEmail)], Http::STATUS_CREATED); } catch (NotAuthorizedException $e) { return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); } catch (\Exception $e) { return new DataResponse($e, Http::STATUS_CONFLICT); } - } /** @@ -117,6 +115,25 @@ class ShareController extends Controller { } /** + * SendInvitation + * Sent invitation mails for a share + * @NoAdminRequired + * @PublicPage + * @NoCSRFRequired + * @param string $token + * @return DataResponse + */ + public function sendInvitation($token) { + try { + $sentResult = $this->mailService->sendInvitationMail($token); + $share = $this->shareService->get($token); + return new DataResponse(['share' => $share, 'sentResult' => $sentResult], Http::STATUS_OK); + } catch (Exception $e) { + return new DataResponse(['error' => $e->getMessage()], $e->getStatus()); + } + } + + /** * remove * remove share * @NoAdminRequired diff --git a/lib/Service/MailService.php b/lib/Service/MailService.php index d1a73c3a..c0edc085 100644 --- a/lib/Service/MailService.php +++ b/lib/Service/MailService.php @@ -155,8 +155,8 @@ class MailService { $recipients[] = array( 'userId' => $share->getUserId(), - 'eMailAddress' => null, - 'displayName' => null, + 'eMailAddress' => \OC::$server->getConfig()->getUserValue($share->getUserId(), 'settings', 'email'), + 'displayName' => $this->userManager->get($share->getUserId())->getDisplayName(), 'language' => $this->config->getUserValue( $share->getUserId(), 'core', 'lang' @@ -229,9 +229,9 @@ class MailService { $recipients[] = array( 'userId' => $member, - 'eMailAddress' => null, - 'displayName' => null, - 'language' => $this->config->getUserValue($share->getUserId(), 'core', 'lang'), + 'eMailAddress' => \OC::$server->getConfig()->getUserValue($member, 'settings', 'email'), + 'displayName' => $this->userManager->get($member)->getDisplayName(), + 'language' => $this->config->getUserValue($member, 'core', 'lang'), 'link' => $this->urlGenerator->getAbsoluteURL( $this->urlGenerator->linkToRoute( 'polls.page.indexvote', ['id' => $share->getPollId()] diff --git a/lib/Service/ShareService.php b/lib/Service/ShareService.php index bdc4177c..5d8b47c4 100644 --- a/lib/Service/ShareService.php +++ b/lib/Service/ShareService.php @@ -79,7 +79,6 @@ class ShareService { } return $this->shareMapper->findByPoll($pollId); - } /** @@ -96,43 +95,6 @@ class ShareService { /** * Write a new share to the db and returns the new share as array * @NoAdminRequired - * @depricated - * @param int $pollId - * @param string $share - * @return array - */ - public function write($pollId, $type, $userId, $userEmail = '') { - - if (!$this->acl->setPollId($pollId)->getAllowEdit()) { - throw new NotAuthorizedException; - } - - $this->share = new Share(); - $this->share->setType($type); - $this->share->setPollId($pollId); - $this->share->setUserId($userId); - $this->share->setUserEmail($userEmail); - $this->share->setInvitationSent(0); - $this->share->setToken(\OC::$server->getSecureRandom()->generate( - 16, - ISecureRandom::CHAR_DIGITS . - ISecureRandom::CHAR_LOWER . - ISecureRandom::CHAR_UPPER - )); - - $this->share = $this->shareMapper->insert($this->share); - // TODO: Replace with $this->add and separate sending invitations - $sendResult = $this->mailService->sendInvitationMail($this->share->getToken()); - - return [ - 'share' => $this->share, - 'sendResult' => $sendResult - ]; - } - - /** - * Write a new share to the db and returns the new share as array - * @NoAdminRequired * @param int $pollId * @param string $share * @return array @@ -157,7 +119,6 @@ class ShareService { )); return $this->shareMapper->insert($this->share); - } /** @@ -224,6 +185,5 @@ class ShareService { $this->shareMapper->delete($this->share); return $this->share; - } } |