first commit after reset
[mixstore.git] / src / Mixstore / UserBundle / Controller / MyAdminController.php
CommitLineData
929ca066
BA
1<?php
2
3namespace Mixstore\UserBundle\Controller;
4
5use Symfony\Component\HttpFoundation\Response;
6use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7use Mixstore\StoreBundle\Entity\Language;
8
9class MyAdminController extends Controller
10{
11 //helper
12 private function checkSuperAdmin()
13 {
14 $sadmin = $this->getUser();
15 if (is_null($sadmin) || !in_array('ROLE_SUPER_ADMIN', $sadmin->getRoles()))
16 //TODO: nice error page
17 return $this->redirect($this->generateUrl('mixstore_static_home'));
18 }
19
20 //set initial languages
21 public function setLanguageAction()
22 {
23 $this->checkSuperAdmin();
24
25 $em = $this->getDoctrine()->getManager();
26 foreach (array('C', 'C#', 'C++', 'Fortran', 'Java', 'Julia', 'Lua', 'MATLAB', 'Octave', 'Python', 'R', 'Scilab') as $lgName)
27 $em->persist(new Language($lgName));
28 $em->flush();
29
30 return new Response('OK');
31 }
32
33 public function addLanguageAction($lgname)
34 {
35 $this->checkSuperAdmin();
36
37 $em = $this->getDoctrine()->getManager();
38 $em->persist(new Language($lgname));
39 $em->flush();
40
41 return new Response('OK');
42 }
43
44 public function usersAction()
45 {
46 $this->checkSuperAdmin();
47
48 return $this->render('MixstoreUserBundle:Admin:users.html.twig', array(
49 'users' => $this
50 ->getDoctrine()
51 ->getManager()
52 ->getRepository('MixstoreUserBundle:User')
53 ->findAll(),
54 ));
55 }
56
57 public function toggleAction()
58 {
59 $this->checkSuperAdmin();
60
61 //get id of user to toggle
62 $userId = $this->getRequest()->get('id');
63 //get user to toggle
64 $em = $this->getDoctrine()->getManager();
65 $user = $em
66 ->getRepository('MixstoreUserBundle:User')
67 ->findById($userId)[0];
68
69 if (in_array('ROLE_ADMIN', $user->getRoles()))
70 $user->setRoles(array('ROLE_USER'));
71 else
72 $user->setRoles(array('ROLE_ADMIN'));
73
74 //save new state
75 $em->persist($user);
76 $em->flush();
77
78 //dummy unused answer (no error case for the moment)
79 return new Response('OK');
80 }
81
82 public function deleteAction($id)
83 {
84 $this->checkSuperAdmin();
85
86 $em = $this->getDoctrine()->getManager();
87 $user = $em
88 ->getRepository('MixstoreUserBundle:User')
89 ->findById($id)[0];
90 $em->remove($user);
91 $em->flush();
92
93 return new Response('OK');
94 }
95
96 //unique usage: set super admin
97 public function setsaAction()
98 {
99 $em = $this->getDoctrine()->getManager();
100 $sadmin = $em
101 ->getRepository('MixstoreUserBundle:User')
102 ->findById(1)[0]; //HACK: hard-coded ID
103
104 $sadmin->setRoles(array('ROLE_ADMIN', 'ROLE_SUPER_ADMIN'));
105 $em->persist($sadmin);
106 $em->flush();
107
108 return new response('OK');
109 }
110}