From ccf2334b69cfb5953d3d6c74af0d53a24009b1ef Mon Sep 17 00:00:00 2001 From: Qingyang Hu Date: Sun, 30 Sep 2018 17:49:45 +0800 Subject: [PATCH] Feature: disable vote --- src/Controller/School/VoteController.php | 260 ++++++++++++----------- web/components/Components/App.vue | 2 + web/router/index.js | 12 +- 3 files changed, 139 insertions(+), 135 deletions(-) diff --git a/src/Controller/School/VoteController.php b/src/Controller/School/VoteController.php index 1e9dca7..f26a54e 100644 --- a/src/Controller/School/VoteController.php +++ b/src/Controller/School/VoteController.php @@ -25,135 +25,137 @@ * Class VoteController * @deprecated No longer used. */ +// Ctrl + Slash to recover class VoteController extends AbstractController { - /** - * @Route("/school/vote/list", methods="GET") - */ - public function list() { - $this->denyAccessUnlessGranted(Permission::IS_LOGIN); - //if($this->getUser()->hasRole(Permission::IS_ADMIN)) - return $this->response()->responseJsonEntity($this - ->getDoctrine() - ->getManager() - ->getRepository(Vote::class) - ->findAll()); - /*else - return $this->response()->responseJsonEntity($this - ->getDoctrine() - ->getManager() - ->getRepository(Vote::class) - ->findByEnabled(true)); - */ - } - - /** - * @Route("/school/vote/detail", methods="GET") - */ - public function detail(Request $request) { - $this->denyAccessUnlessGranted(Permission::IS_LOGIN); - $id = $request->query->get("id"); - $vote = $this->getDoctrine()->getManager()->getRepository(Vote::class)->find($id); - return $this->response()->responseEntity($vote); - } - - /** - * @Route("/school/vote/vote", methods="POST") - */ - public function vote(Request $request, TranslatorInterface $translator, UserPasswordEncoderInterface $passwordEncoder) { - $this->denyAccessUnlessGranted(Permission::IS_LOGIN); - if(!$this->getUser()->hasRole(Permission::IS_STUDENT)) - return $this->response()->response($translator->trans("not-eligible-to-vote"), Response::HTTP_UNAUTHORIZED); - - $auth = $this->getUser()->getValidAuth(); - if($auth->getSeniorSchool() !== 2 and $auth->getSeniorSchool() !== 3) - return $this->response()->response($translator->trans("not-eligible-to-vote"), Response::HTTP_UNAUTHORIZED); - if($auth->getSeniorRegistration() < 2019 || $auth->getSeniorRegistration() > 2021) - return $this->response()->response($translator->trans("not-eligible-to-vote"), Response::HTTP_UNAUTHORIZED); - $id = $request->request->get("id"); - /** @var Vote $vote */ - $vote = $this->getDoctrine()->getManager()->getRepository(Vote::class)->find($id); - if(is_null($vote)) - return $this->response()->response($translator->trans("vote-not-enabled"), Response::HTTP_UNAUTHORIZED); - if(!$vote->isEnabled()) - return $this->response()->response($translator->trans("vote-not-enabled"), Response::HTTP_UNAUTHORIZED); - $em = $this->getDoctrine()->getManager(); - if(!is_null($em->getRepository(Ticket::class)->findOneByUserAndVote($this->getUser(), $vote))) - return $this->response()->response($translator->trans("already-submit"), Response::HTTP_FORBIDDEN); - try { - if(!$passwordEncoder->isPasswordValid($this->getUser(), $request->request->get("password"))) { - return $this->response()->response($translator->trans("incorrect-password"), Response::HTTP_BAD_REQUEST); - } - if($this->getUser()->isOAuth && !$request->request->has("clientId")) { - return $this->response()->response($translator->trans("invalid-client"). Response::HTTP_BAD_REQUEST); - } - $ticket = new Ticket($vote, $this->getUser(), $request->request->get("choices"), ($request->headers->get("X-Forwarded-For") ?? "") . "|" . $request->getClientIp() , $request->headers->get("user-agent"), $request->request->get("clientId") ?? ""); - $em->persist($ticket); - $em->flush(); - $info = json_encode([ - "request" => $request->request->all(), - "query" => $request->query->all(), - "cookies" => $request->cookies->all(), - "server" => $request->server->all(), - "file" => $request->files->all(), - "user" => $this->getUser()->getInfoArray() - ]); - file_put_contents("/var/log/vote.log", $info, FILE_APPEND); - $this->writeLog("UserVoted", json_encode($request->request->get("choices"))); - return $this->response()->responseEntity($ticket, Response::HTTP_OK); - } catch(\Exception $e) { - return $this->response()->response($translator->trans("invalid-ticket"), Response::HTTP_BAD_REQUEST); - } - } - - /** - * @Route("/school/vote/edit") - */ - public function edit(Request $request) { - $this->denyAccessUnlessGranted(Permission::IS_ADMIN); - $id = $request->query->get("id"); - - $em = $this->getDoctrine()->getManager(); - if($id == "new") - $vote = new Vote(); - else - $vote = $em->getRepository(Vote::class)->find($id) ?? new Vote(); - if($request->isMethod("POST")) { - $vote->setTitle($request->request->get("title")); - $vote->setContent($request->request->get("content")); - $vote->setOptions($request->request->get('options')); - $vote->setEnabled($request->request->getBoolean('enabled')); - $em->persist($vote); - $em->flush(); - } - return $this->response()->responseEntity($vote); - } - - /** - * @Route("/school/vote/result", methods="GET") - */ - public function result(Request $request) { - $this->denyAccessUnlessGranted(Permission::IS_ADMIN); - $id = $request->query->get("id"); - if($id == 'new') - return $this->response()->response(null); - $em = $this->getDoctrine()->getManager(); - /** @var Vote $vote */ - $vote = $em->getRepository(Vote::class)->find($id); - $tickets = $em->getRepository(Ticket::class)->findBy(["vote" => $vote]); - $result = array(); - for($i=0; $igetOptions()); $i++) { - $result[$i] = array(); - for($j=0; $jgetOptions()[$i]["options"]); $j++) { - $result[$i][$j] = 0; - } - } - foreach ($tickets as $ticket) { - /** @var Ticket $ticket*/ - foreach ($ticket->getChoices() as $key => $choice) { - $result[$key][$choice] ++; - } - } - return $this->response()->response(array("total" => count($tickets), "detail" => $result)); - } +// /** +// * @Route("/school/vote/list", methods="GET") +// */ +// public function list() { +// $this->denyAccessUnlessGranted(Permission::IS_LOGIN); +// //if($this->getUser()->hasRole(Permission::IS_ADMIN)) +// return $this->response()->responseJsonEntity($this +// ->getDoctrine() +// ->getManager() +// ->getRepository(Vote::class) +// ->findAll()); +// /*else +// return $this->response()->responseJsonEntity($this +// ->getDoctrine() +// ->getManager() +// ->getRepository(Vote::class) +// ->findByEnabled(true)); +// */ +// } +// +// /** +// * @Route("/school/vote/detail", methods="GET") +// */ +// public function detail(Request $request) { +// $this->denyAccessUnlessGranted(Permission::IS_LOGIN); +// $id = $request->query->get("id"); +// $vote = $this->getDoctrine()->getManager()->getRepository(Vote::class)->find($id); +// return $this->response()->responseEntity($vote); +// } +// +// /** +// * @Route("/school/vote/vote", methods="POST") +// */ +// public function vote(Request $request, TranslatorInterface $translator, UserPasswordEncoderInterface $passwordEncoder) { +// $this->denyAccessUnlessGranted(Permission::IS_LOGIN); +// if(!$this->getUser()->hasRole(Permission::IS_STUDENT)) +// return $this->response()->response($translator->trans("not-eligible-to-vote"), Response::HTTP_UNAUTHORIZED); +// +// $auth = $this->getUser()->getValidAuth(); +// if($auth->getSeniorSchool() !== 2 and $auth->getSeniorSchool() !== 3) +// return $this->response()->response($translator->trans("not-eligible-to-vote"), Response::HTTP_UNAUTHORIZED); +// if($auth->getSeniorRegistration() < 2019 || $auth->getSeniorRegistration() > 2021) +// return $this->response()->response($translator->trans("not-eligible-to-vote"), Response::HTTP_UNAUTHORIZED); +// $id = $request->request->get("id"); +// /** @var Vote $vote */ +// $vote = $this->getDoctrine()->getManager()->getRepository(Vote::class)->find($id); +// if(is_null($vote)) +// return $this->response()->response($translator->trans("vote-not-enabled"), Response::HTTP_UNAUTHORIZED); +// if(!$vote->isEnabled()) +// return $this->response()->response($translator->trans("vote-not-enabled"), Response::HTTP_UNAUTHORIZED); +// $em = $this->getDoctrine()->getManager(); +// if(!is_null($em->getRepository(Ticket::class)->findOneByUserAndVote($this->getUser(), $vote))) +// return $this->response()->response($translator->trans("already-submit"), Response::HTTP_FORBIDDEN); +// try { +// if(!$passwordEncoder->isPasswordValid($this->getUser(), $request->request->get("password"))) { +// return $this->response()->response($translator->trans("incorrect-password"), Response::HTTP_BAD_REQUEST); +// } +// if($this->getUser()->isOAuth && !$request->request->has("clientId")) { +// return $this->response()->response($translator->trans("invalid-client"). Response::HTTP_BAD_REQUEST); +// } +// $ticket = new Ticket($vote, $this->getUser(), $request->request->get("choices"), ($request->headers->get("X-Forwarded-For") ?? "") . "|" . $request->getClientIp() , $request->headers->get("user-agent"), $request->request->get("clientId") ?? ""); +// $em->persist($ticket); +// $em->flush(); +// $info = json_encode([ +// "request" => $request->request->all(), +// "query" => $request->query->all(), +// "cookies" => $request->cookies->all(), +// "server" => $request->server->all(), +// "file" => $request->files->all(), +// "user" => $this->getUser()->getInfoArray() +// ]); +// file_put_contents("/var/log/vote.log", $info, FILE_APPEND); +// $this->writeLog("UserVoted", json_encode($request->request->get("choices"))); +// return $this->response()->responseEntity($ticket, Response::HTTP_OK); +// } catch(\Exception $e) { +// return $this->response()->response($translator->trans("invalid-ticket"), Response::HTTP_BAD_REQUEST); +// } +// } +// +// /** +// * @Route("/school/vote/edit") +// */ +// public function edit(Request $request) { +// $this->denyAccessUnlessGranted(Permission::IS_ADMIN); +// $id = $request->query->get("id"); +// +// $em = $this->getDoctrine()->getManager(); +// if($id == "new") +// $vote = new Vote(); +// else +// $vote = $em->getRepository(Vote::class)->find($id) ?? new Vote(); +// if($request->isMethod("POST")) { +// $vote->setTitle($request->request->get("title")); +// $vote->setContent($request->request->get("content")); +// $vote->setOptions($request->request->get('options')); +// $vote->setEnabled($request->request->getBoolean('enabled')); +// $em->persist($vote); +// $em->flush(); +// } +// return $this->response()->responseEntity($vote); +// } +// +// /** +// * @Route("/school/vote/result", methods="GET") +// */ +// public function result(Request $request) { +// +// $this->denyAccessUnlessGranted(Permission::IS_ADMIN); +// $id = $request->query->get("id"); +// if($id == 'new') +// return $this->response()->response(null); +// $em = $this->getDoctrine()->getManager(); +// /** @var Vote $vote */ +// $vote = $em->getRepository(Vote::class)->find($id); +// $tickets = $em->getRepository(Ticket::class)->findBy(["vote" => $vote]); +// $result = array(); +// for($i=0; $igetOptions()); $i++) { +// $result[$i] = array(); +// for($j=0; $jgetOptions()[$i]["options"]); $j++) { +// $result[$i][$j] = 0; +// } +// } +// foreach ($tickets as $ticket) { +// /** @var Ticket $ticket*/ +// foreach ($ticket->getChoices() as $key => $choice) { +// $result[$key][$choice] ++; +// } +// } +// return $this->response()->response(array("total" => count($tickets), "detail" => $result)); +// } } \ No newline at end of file diff --git a/web/components/Components/App.vue b/web/components/Components/App.vue index 178e762..f496b7f 100644 --- a/web/components/Components/App.vue +++ b/web/components/Components/App.vue @@ -60,10 +60,12 @@ {{ $t('register') }} + diff --git a/web/router/index.js b/web/router/index.js index b1a9162..2090189 100644 --- a/web/router/index.js +++ b/web/router/index.js @@ -12,7 +12,7 @@ import Game from '../components/Media/Game' import Gallery from '../components/Media/Gallery' import Album from '../components/Media/Album' import Live from '../components/Media/Live' -import Vote from '../components/School/Vote' +//import Vote from '../components/School/Vote' import Login from '../components/User/Login' import Register from '../components/User/Register' @@ -25,7 +25,7 @@ import Public from '../components/User/Public' import Preference from '../components/Admin/Preference' import Upload from '../components/Admin/Upload' import Notification from '../components/Admin/Notification' -import VoteAdmin from '../components/Admin/Vote' +//import VoteAdmin from '../components/Admin/Vote' import Old from '../components/Admin/Old' import User from '../components/Admin/User' import Overview from '../components/Admin/Overview' @@ -66,10 +66,10 @@ export default new Router({ }, { path: '/media/live/:id', component: Live - }, { + }/*, { path: '/school/vote', component: Vote - }, { + }*/, { path: '/user/security', component: Security }, { @@ -102,10 +102,10 @@ export default new Router({ }, { path: '/admin/notification', component: Notification - }, { + }/*, { path: '/admin/vote', component: VoteAdmin - }, { + }*/, { path: '/admin/old', component: Old }, {