From fdee3f07fe8c831fb1679f07b88439db03d6798a Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Fri, 17 Apr 2020 16:02:23 +0200 Subject: [PATCH 1/8] add call to check for uniq slug --- src/Controller/PollController.php | 34 +++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/Controller/PollController.php b/src/Controller/PollController.php index 751498c..c7665b2 100644 --- a/src/Controller/PollController.php +++ b/src/Controller/PollController.php @@ -5,7 +5,6 @@ namespace App\Controller; use App\Entity\Choice; use App\Entity\Owner; use App\Entity\Poll; -use App\Service\MailService; use FOS\RestBundle\Controller\Annotations\Delete; use FOS\RestBundle\Controller\Annotations\Get; use FOS\RestBundle\Controller\Annotations\Post; @@ -268,7 +267,7 @@ class PollController extends FramadateController { $sent = $this->sendOwnerPollsAction( $foundOwner, $poll ); if ( $sent ) { - return $this->json( [ "message" => "test email sent to ".$foundOwner->getEmail()."!" ], 200 ); + return $this->json( [ "message" => "test email sent to " . $foundOwner->getEmail() . "!" ], 200 ); } } @@ -347,4 +346,35 @@ class PollController extends FramadateController { 200 ); } + /** + * Delete all expired polls and their children + * @Get( + * path = "/check-slug-is-unique/{slug}", + * name = "check_slug_is_unique", + * ) + */ + public function checkSlugIsUnique( $slug ) { + $emPoll = $this->getDoctrine()->getRepository( Poll::class ); + $found = $emPoll->findOneBySlug( $slug ); + if ( $found ) { + // we should not find an other poll + return $this->json( [ + 'message' => ' NO, this slug is already taken on this Framadate instance ', + 'data' => [ + 'slug' => $slug, + ], + ], + 403 ); + } + + return $this->json( [ + 'message' => ' yes this slug is available on this Framadate instance ', + 'data' => [ + 'slug' => $slug, + ], + ], + 200 ); + + } + } From 33a71d05e4d312d2d15437966b876b0340811f78 Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Fri, 17 Apr 2020 16:12:35 +0200 Subject: [PATCH 2/8] call to update a poll with a token --- src/Controller/PollController.php | 65 +++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/src/Controller/PollController.php b/src/Controller/PollController.php index c7665b2..b4b8df5 100644 --- a/src/Controller/PollController.php +++ b/src/Controller/PollController.php @@ -106,7 +106,7 @@ class PollController extends FramadateController { /** * @Put( - * path = "/{id}", + * path = "/{id}/{token}", * name = "update_poll", * requirements = {"content"="\w+", "poll_id"="\d+"} * ) @@ -114,8 +114,15 @@ class PollController extends FramadateController { public function updatePollConfig( Poll $poll, + string $token, Request $request ) { + if ( $poll->getAdminKey() !== $token ) { + return $this->json( [ + 'message' => 'you are NOT allowed to update the poll ' . $poll->getTitle(), + ], + 403 ); + } // TODO check validity of request // update only if we have the admin key @@ -347,13 +354,13 @@ class PollController extends FramadateController { } /** - * Delete all expired polls and their children + * Check is a slug is already taken by a poll * @Get( * path = "/check-slug-is-unique/{slug}", * name = "check_slug_is_unique", * ) */ - public function checkSlugIsUnique( $slug ) { + public function checkSlugIsUniqueAction( $slug ) { $emPoll = $this->getDoctrine()->getRepository( Poll::class ); $found = $emPoll->findOneBySlug( $slug ); if ( $found ) { @@ -377,4 +384,56 @@ class PollController extends FramadateController { } + + /** + * Delete all expired polls and their children + * @Get( + * path = "/admin/{token}", + * name = "check_slug_is_unique", + * ) + */ + public function getAdministrationConfig( $token ) { + $emPoll = $this->getDoctrine()->getRepository( Poll::class ); + $pollFound = $emPoll->findOneByAdminKey( $token ); + if ( $pollFound ) { + + $poll = $pollFound; + $comments = []; + $stacks = []; + $choices = []; + foreach ( $poll->getComments() as $c ) { + $comments[] = $c->display(); + } + foreach ( $poll->getStacksOfVotes() as $c ) { + $stacks[] = $c->display(); + } + foreach ( $poll->getChoices() as $c ) { + $choices[] = $c->display(); + } + $returnedPoll = [ + 'message' => 'your poll config', + 'poll' => $poll, + 'stacks_count' => count( $poll->getStacksOfVotes() ), + 'stacks' => $stacks, + 'choices_count' => $poll->computeAnswers(), + 'choices' => $choices, + 'comments' => $comments, + 'comments_count' => count( $comments ), + 'token' => $token, + ]; + + return $this->json( $returnedPoll, + 200 ); + } + + return $this->json( [ + 'message' => 'You are not allowed to do anything with this token', + 'data' => [ + 'token' => $token, + ], + ], + 403 ); + + } + } From c251eac2fbe39a12785f186d2c623df4f8350853 Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Fri, 17 Apr 2020 16:12:59 +0200 Subject: [PATCH 3/8] success for updating a poll --- src/Controller/PollController.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Controller/PollController.php b/src/Controller/PollController.php index b4b8df5..8a43b03 100644 --- a/src/Controller/PollController.php +++ b/src/Controller/PollController.php @@ -132,7 +132,8 @@ class PollController extends FramadateController { return $this->json( [ 'message' => 'you updated the poll ' . $poll->getTitle(), - ] ); + ], + 200 ); } /** From 2836ccb137ac193a382d8a4439205d09a30192f5 Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Tue, 21 Apr 2020 17:59:22 +0200 Subject: [PATCH 4/8] create owner controller, change some route to be more RESTly --- .env | 1 + src/Controller/AdminController.php | 71 +++++++++++++++++++++++ src/Controller/DefaultController.php | 58 ------------------- src/Controller/OwnerController.php | 85 ++++++++++++++++++++++++++++ src/Controller/PollController.php | 45 ++------------- 5 files changed, 161 insertions(+), 99 deletions(-) create mode 100755 src/Controller/AdminController.php create mode 100755 src/Controller/OwnerController.php diff --git a/.env b/.env index 524daeb..0b9b4b5 100755 --- a/.env +++ b/.env @@ -15,6 +15,7 @@ ###> symfony/framework-bundle ### APP_ENV=dev +ADMIN_TOKEN=erfd456ref4ety4h56jy4i5opuoipm564iyuyn312b1s6er78g897ryjt7thsb32d1gfb APP_SECRET=597b0529ac702d27dcb9089f7e69c362 # Base website url, should contain https:// and having no trailing slash. example: BASE_URL=https://framadate.org BASE_URL=https://framadate-api.cipherbliss.com diff --git a/src/Controller/AdminController.php b/src/Controller/AdminController.php new file mode 100755 index 0000000..d52425f --- /dev/null +++ b/src/Controller/AdminController.php @@ -0,0 +1,71 @@ +json( [ "message" => "welcome to the framadate admin api, ask /api/v1/doc.json for endpoints" ], + 200 ); + } + + /** + * Delete all expired polls and their children + * @Get( + * path = "/polls/clean/{token}", + * name = "_clean_expired_polls", + * ) + * token is set up in the main env file + */ + public + function cleanExpiredPolls( + string $token + ) { + if ( $this->getParameter( 'ADMIN_TOKEN' ) !== $token ) { + return $this->json( [ + 'message' => 'clean routine can NOT be done, your admin token is bad, and you should feel bad.', + ], + 403 ); + } + $em = $this->getDoctrine()->getManager(); + $emPoll = $this->getDoctrine()->getRepository( Poll::class ); + + $queryFind = $em->createQuery( + 'SELECT p + FROM App\Entity\Poll p + WHERE p.expiracyDate < CURRENT_DATE()' + ); + $queryDelete = $em->createQuery( + 'DELETE + FROM App\Entity\Poll p + WHERE p.expiracyDate < CURRENT_DATE()' + ); + + $foundPolls = $queryFind->getResult(); + + $em->flush(); + + + return $this->json( [ + 'message' => 'clean routine has been done, here are the numbers of polls deleted: ' . count( $foundPolls ), + 'data' => [ + 'count' => count( $foundPolls ), + ], + ], + 200 ); + } + +} diff --git a/src/Controller/DefaultController.php b/src/Controller/DefaultController.php index 40278c2..7c04835 100755 --- a/src/Controller/DefaultController.php +++ b/src/Controller/DefaultController.php @@ -16,65 +16,7 @@ use Symfony\Component\Mailer\Exception\TransportExceptionInterface; * @Route("/api/v1",name="api_") */ class DefaultController extends FramadateController { - /** - * @var MailService - */ - protected $mail_service; - /** - * Send a mail with all the data to one user - * @Get( - * path = "/send-polls-to-user/{email}", - * name = "send_user_polls" - * ) - * - * @param string $email - * - * @return JsonResponse - */ - public function sendPollsToUserAction( string $email ) { - $repository = $this->getDoctrine()->getRepository( Owner::class ); - - - // find user by email - $owner = $repository->findOneByEmail($email); - - if ( $owner ) { - $templateVars = [ - 'owner' => $owner, - 'polls' => $owner->getPolls(), - 'title' => 'Mes sondages - ' . $owner->getEmail(), - ]; - - // send email - $mailSent = 0; - try { - $mailSent = $this->sendOwnerPollsAction( $owner ); - } catch ( Exception $e ) { - } catch ( TransportExceptionInterface $e ) { - } - - if ( $mailSent ) { - return $this->json( [ - 'message' => 'mail succefully sent to user ' . $owner->getEmail(), - 'data' => '', - ], - 200 ); - } - return $this->json( [ - 'message' => 'no sucess sending email ' . $owner->getEmail(), - 'data' => '', - ], - 400 ); - } - return $this->json( [ - 'message' => 'no user found for email ' . $email, - 'data' => '', - ], - 400 ); - - } - } diff --git a/src/Controller/OwnerController.php b/src/Controller/OwnerController.php new file mode 100755 index 0000000..4423221 --- /dev/null +++ b/src/Controller/OwnerController.php @@ -0,0 +1,85 @@ +json( [ "message" => "welcome to the framadate user api, ask /api/v1/doc.json for endpoints" ], + 200 ); + } + + + /** + * Send a mail with all the data to one user + * @Get( + * path = "/{email}/polls/send-by-email", + * name = "_polls_send_by_email" + * ) + * + * @param string $email + * + * @return JsonResponse + */ + public function sendPollsToUserAction( string $email ) { + $repository = $this->getDoctrine()->getRepository( Owner::class ); + + + // find user by email + $owner = $repository->findOneByEmail( $email ); + + if ( $owner ) { + $templateVars = [ + 'owner' => $owner, + 'polls' => $owner->getPolls(), + 'title' => 'Mes sondages - ' . $owner->getEmail(), + ]; + + // send email + $mailSent = 0; + try { + $mailSent = $this->sendOwnerPollsAction( $owner ); + } catch ( Exception $e ) { + } catch ( TransportExceptionInterface $e ) { + } + + if ( $mailSent ) { + return $this->json( [ + 'message' => 'mail succefully sent to user ' . $owner->getEmail(), + 'data' => '', + ], + 200 ); + } + + return $this->json( [ + 'message' => 'no sucess sending email ' . $owner->getEmail(), + 'data' => '', + ], + 400 ); + } + + return $this->json( [ + 'message' => 'no user found for email ' . $email, + 'data' => '', + ], + 400 ); + + } + +} diff --git a/src/Controller/PollController.php b/src/Controller/PollController.php index 8a43b03..6752cb6 100644 --- a/src/Controller/PollController.php +++ b/src/Controller/PollController.php @@ -317,51 +317,14 @@ class PollController extends FramadateController { } - /** - * Delete all expired polls and their children - * @Get( - * path = "/clean-polls", - * name = "clean_expired_polls", - * ) - */ - public - function cleanExpiredPolls() { - $em = $this->getDoctrine()->getManager(); - $emPoll = $this->getDoctrine()->getRepository( Poll::class ); - - $queryFind = $em->createQuery( - 'SELECT p - FROM App\Entity\Poll p - WHERE p.expiracyDate < CURRENT_DATE()' - ); - $queryDelete = $em->createQuery( - 'DELETE - FROM App\Entity\Poll p - WHERE p.expiracyDate < CURRENT_DATE()' - ); - - $foundPolls = $queryFind->getResult(); - - $em->flush(); - - - return $this->json( [ - 'message' => 'clean routine has been done, here are the numbers of polls deleted: ' . count( $foundPolls ), - 'data' => [ - 'count' => count( $foundPolls ), - ], - ], - 200 ); - } - /** * Check is a slug is already taken by a poll * @Get( - * path = "/check-slug-is-unique/{slug}", + * path = "/poll/slug/{slug}", * name = "check_slug_is_unique", * ) */ - public function checkSlugIsUniqueAction( $slug ) { + public function checkSlugIsUniqueAction( string $slug ) { $emPoll = $this->getDoctrine()->getRepository( Poll::class ); $found = $emPoll->findOneBySlug( $slug ); if ( $found ) { @@ -387,10 +350,10 @@ class PollController extends FramadateController { /** - * Delete all expired polls and their children + * Get Admin poll config * @Get( * path = "/admin/{token}", - * name = "check_slug_is_unique", + * name = "get_admin_config", * ) */ public function getAdministrationConfig( $token ) { From ed59c30961326db7de9c0c38e00d5c1479d7875c Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Tue, 21 Apr 2020 18:03:18 +0200 Subject: [PATCH 5/8] up slug path --- src/Controller/PollController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Controller/PollController.php b/src/Controller/PollController.php index 6752cb6..d296287 100644 --- a/src/Controller/PollController.php +++ b/src/Controller/PollController.php @@ -320,13 +320,13 @@ class PollController extends FramadateController { /** * Check is a slug is already taken by a poll * @Get( - * path = "/poll/slug/{slug}", + * path = "/slug/{slug}", * name = "check_slug_is_unique", * ) */ public function checkSlugIsUniqueAction( string $slug ) { $emPoll = $this->getDoctrine()->getRepository( Poll::class ); - $found = $emPoll->findOneBySlug( $slug ); + $found = $emPoll->findOneByCustomUrl( $slug ); if ( $found ) { // we should not find an other poll return $this->json( [ From 2c609e2f09a5142b5ac6512eadff24e26aeeec17 Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Tue, 21 Apr 2020 18:11:39 +0200 Subject: [PATCH 6/8] change slug answers --- src/Controller/PollController.php | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/Controller/PollController.php b/src/Controller/PollController.php index d296287..4cf9ee5 100644 --- a/src/Controller/PollController.php +++ b/src/Controller/PollController.php @@ -325,17 +325,28 @@ class PollController extends FramadateController { * ) */ public function checkSlugIsUniqueAction( string $slug ) { - $emPoll = $this->getDoctrine()->getRepository( Poll::class ); - $found = $emPoll->findOneByCustomUrl( $slug ); + $emPoll = $this->getDoctrine()->getRepository( Poll::class ); + $found = $emPoll->findOneByCustomUrl( $slug ); + $elaborated_message_version = false; + if ( $found ) { - // we should not find an other poll + if ( ! $elaborated_message_version ) { + return $this->json( true, + 200 ); + } + + // we should use an other slug return $this->json( [ 'message' => ' NO, this slug is already taken on this Framadate instance ', 'data' => [ 'slug' => $slug, ], ], - 403 ); + 200 ); + } + if ( ! $elaborated_message_version ) { + return $this->json( false, + 404 ); } return $this->json( [ @@ -344,7 +355,7 @@ class PollController extends FramadateController { 'slug' => $slug, ], ], - 200 ); + 404 ); } From 124439ee3c3d9795895ffbe1f2db7dc27249c07f Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Tue, 21 Apr 2020 18:21:03 +0200 Subject: [PATCH 7/8] slug codes and null content --- src/Controller/PollController.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Controller/PollController.php b/src/Controller/PollController.php index 4cf9ee5..2f55e54 100644 --- a/src/Controller/PollController.php +++ b/src/Controller/PollController.php @@ -331,8 +331,8 @@ class PollController extends FramadateController { if ( $found ) { if ( ! $elaborated_message_version ) { - return $this->json( true, - 200 ); + return $this->json( 'no-content', + 204 ); } // we should use an other slug @@ -342,10 +342,10 @@ class PollController extends FramadateController { 'slug' => $slug, ], ], - 200 ); + 204 ); } if ( ! $elaborated_message_version ) { - return $this->json( false, + return $this->json( 'NOT_found', 404 ); } From ba0f803f2d172b1f230e1c0451c6fde269078799 Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Tue, 21 Apr 2020 18:26:38 +0200 Subject: [PATCH 8/8] slug null content answers, just code --- src/Controller/PollController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Controller/PollController.php b/src/Controller/PollController.php index 2f55e54..f3f53c2 100644 --- a/src/Controller/PollController.php +++ b/src/Controller/PollController.php @@ -331,7 +331,7 @@ class PollController extends FramadateController { if ( $found ) { if ( ! $elaborated_message_version ) { - return $this->json( 'no-content', + return $this->json( null, 204 ); } @@ -345,7 +345,7 @@ class PollController extends FramadateController { 204 ); } if ( ! $elaborated_message_version ) { - return $this->json( 'NOT_found', + return $this->json( null, 404 ); }