Skip to content
This repository has been archived by the owner on Dec 7, 2020. It is now read-only.

Commit

Permalink
Feature: disable vote
Browse files Browse the repository at this point in the history
  • Loading branch information
hqy2000 committed Sep 30, 2018
1 parent e1e354e commit ccf2334
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 135 deletions.
260 changes: 131 additions & 129 deletions src/Controller/School/VoteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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; $i<count($vote->getOptions()); $i++) {
$result[$i] = array();
for($j=0; $j<count($vote->getOptions()[$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; $i<count($vote->getOptions()); $i++) {
// $result[$i] = array();
// for($j=0; $j<count($vote->getOptions()[$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));
// }
}
2 changes: 2 additions & 0 deletions web/components/Components/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@
<span class='md-list-item-text'>{{ $t('register') }}</span>
</md-list-item>
<md-divider></md-divider>
<!--
<md-list-item @click="lang">>
<md-icon>translate</md-icon>
<span class='md-list-item-text'>{{ language }}</span>
</md-list-item>
-->
</md-list>
</md-menu-content>
</md-menu>
Expand Down
12 changes: 6 additions & 6 deletions web/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand Down Expand Up @@ -66,10 +66,10 @@ export default new Router({
}, {
path: '/media/live/:id',
component: Live
}, {
}/*, {
path: '/school/vote',
component: Vote
}, {
}*/, {
path: '/user/security',
component: Security
}, {
Expand Down Expand Up @@ -102,10 +102,10 @@ export default new Router({
}, {
path: '/admin/notification',
component: Notification
}, {
}/*, {
path: '/admin/vote',
component: VoteAdmin
}, {
}*/, {
path: '/admin/old',
component: Old
}, {
Expand Down

0 comments on commit ccf2334

Please sign in to comment.