GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — user-entity ( 127474...5e6bbe )
by Luis Ramón
03:38
created

AdminUserController::genericDeleteAction()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 18

Duplication

Lines 12
Ratio 41.38 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 12
loc 29
rs 8.5806
cc 4
eloc 18
nc 4
nop 2
1
<?php
2
/*
3
  ÁTICA - Aplicación web para la gestión documental de centros educativos
4
5
  Copyright (C) 2015-2016: Luis Ramón López López
6
7
  This program is free software: you can redistribute it and/or modify
8
  it under the terms of the GNU Affero General Public License as published by
9
  the Free Software Foundation, either version 3 of the License, or
10
  (at your option) any later version.
11
12
  This program is distributed in the hope that it will be useful,
13
  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
  GNU Affero General Public License for more details.
16
17
  You should have received a copy of the GNU Affero General Public License
18
  along with this program.  If not, see [http://www.gnu.org/licenses/].
19
*/
20
21
namespace AppBundle\Controller;
22
23
use AppBundle\Entity\User;
24
use AppBundle\Form\Type\UserType;
25
use Doctrine\ORM\EntityManager;
26
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
27
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
28
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
29
use Symfony\Component\Form\SubmitButton;
30
use Symfony\Component\HttpFoundation\Request;
31
32
/**
33
 * @Route("/admin/usuarios")
34
 * @Security("is_granted('ROLE_ADMIN')")
35
 */
36
class AdminUserController extends Controller
37
{
38
    /**
39
     * @Route("/", name="admin_users", methods={"GET"})
40
     */
41
    public function usersIndexAction(Request $request)
42
    {
43
        /** @var EntityManager $em */
44
        $em = $this->getDoctrine()->getManager();
45
46
        $usersQuery = $em->createQuery('SELECT u FROM AppBundle:User u');
47
48
        $paginator  = $this->get('knp_paginator');
49
        $pagination = $paginator->paginate(
50
            $usersQuery,
51
            $request->query->getInt('page', 1),
52
            $this->getParameter('page.size'),
53
            [
54
                'defaultSortFieldName' => 'u.lastName',
55
                'defaultSortDirection' => 'asc'
56
            ]
57
        );
58
59
        return $this->render('admin/manage_users.html.twig',
60
            [
61
                'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_users'),
62
                'title' => null,
63
                'pagination' => $pagination
64
            ]);
65
    }
66
67
    /**
68
     * @Route("/nuevo", name="admin_user_new", methods={"GET", "POST"})
69
     * @Route("/{user}", name="admin_user_form", methods={"GET", "POST"}, requirements={"user": "\d+"})
70
     */
71
    public function formAction(User $user = null, Request $request)
72
    {
73
        $em = $this->getDoctrine()->getManager();
74
75
        $new = (null === $user);
76
        if ($new) {
77
            $user = $em->getRepository('AppBundle:User')->createNewUser();
78
        }
79
80
        $me = ($user->getId() === $this->getUser()->getId());
81
82
        $form = $this->createForm('AppBundle\Form\Type\UserType', $user, [
83
            'admin' => $this->isGranted('ROLE_ADMIN'),
84
            'me' => $me,
85
            'new' => $new
86
        ]);
87
88
89
        $form->handleRequest($request);
90
91
        if ($form->isSubmitted() && $form->isValid()) {
92
93
            // Guardar el usuario en la base de datos
94
95
            // Si es solicitado, cambiar la contraseña
96
            $passwordSubmit = $form->get('changePassword');
97 View Code Duplication
            if (($passwordSubmit instanceof SubmitButton) && $passwordSubmit->isClicked()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
                $password = $this->container->get('security.password_encoder')
99
                    ->encodePassword($user, $form->get('newPassword')->get('first')->getData());
100
                $user->setPassword($password);
101
                $message = $this->get('translator')->trans('alert.password_changed', [], 'user');
102
            } else {
103
                $message = $this->get('translator')->trans('alert.saved', [], 'user');
104
            }
105
106
            // Probar a guardar los cambios
107
            try {
108
                $em->flush();
109
                $this->addFlash('success', $message);
110
                return $this->redirectToRoute('admin_users');
111
            } catch (\Exception $e) {
112
                $this->addFlash('error', $this->get('translator')->trans('alert.not_saved', [], 'user'));
113
            }
114
        }
115
116
        $titulo = ((string) $user) ?: $this->get('translator')->trans('user.new', [], 'user');
117
118
        return $this->render('admin/form_user.html.twig', [
119
            'form' => $form->createView(),
120
            'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_users'),
121
            'breadcrumb' => [
122
                ['fixed' => $titulo]
123
            ],
124
            'title' => $titulo,
125
            'user' => $user
126
        ]);
127
    }
128
129
    /**
130
     * @Route("/eliminar/{id}", name="admin_user_delete", methods={"GET", "POST"}, requirements={"user": "\d+"})
131
     */
132
    public function genericDeleteAction(User $element, Request $request)
133
    {
134 View Code Duplication
        if ('POST' === $request->getMethod() && $request->request->has('delete')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
135
136
            // Eliminar el departamento de la base de datos
137
            $this->getDoctrine()->getManager()->remove($element);
138
            try {
139
                $this->getDoctrine()->getManager()->flush();
140
                $this->addFlash('success', $this->get('translator')->trans('alert.deleted', [], 'user'));
141
            } catch (\Exception $e) {
142
                $this->addFlash('error', $this->get('translator')->trans('alert.not_deleted', [], 'user'));
143
            }
144
            return $this->redirectToRoute('admin_users');
145
        }
146
147
        $title = (string) $element;
148
149
        $breadcrumb = [
150
            ['fixed' => $title],
151
            ['caption' => 'menu.delete']
152
        ];
153
154
        return $this->render('admin/delete_user.html.twig', [
155
            'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_users'),
156
            'breadcrumb' => $breadcrumb,
157
            'title' => $title,
158
            'element' => $element,
159
        ]);
160
    }
161
}
162