Passed
Push — master ( 2955c9...ee5866 )
by Fran
05:26
created

UserController::setAdminUsers()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 3
nop 0
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
ccs 0
cts 18
cp 0
crap 12
1
<?php
2
namespace PSFS\controller;
3
4
use PSFS\base\config\AdminForm;
5
use PSFS\base\config\LoginForm;
6
use PSFS\base\exception\ConfigException;
7
use PSFS\base\Logger;
8
use PSFS\base\Security;
9
use PSFS\base\Template;
10
use PSFS\controller\base\Admin;
11
12
/**
13
 * Class UserController
14
 * @package PSFS\controller
15
 */
16
class UserController extends Admin
17
{
18
    /**
19
     * Método que gestiona los usuarios administradores de la plataforma
20
     * @GET
21
     * @route /admin/setup
22
     * @return string|null
23
     * @throws \HttpException
24
     */
25
    public function adminers()
26
    {
27
        $admins = $this->srv->getAdmins();
28
        $form = new AdminForm();
29
        $form->build();
30
        return $this->render('admin.html.twig', array(
31
            'admins' => $admins,
32
            'form' => $form,
33
            'profiles' => Security::getProfiles(),
34
        ));
35
    }
36
37
    /**
38
     * Servicio que guarda los usuarios de administración
39
     * @POST
40
     * @route /admin/setup
41
     * @visible false
42
     * @return string|void
43
     * @throws \HttpException
44
     */
45
    public function setAdminUsers()
46
    {
47
        $admins = $this->srv->getAdmins();
48
        $form = new AdminForm();
49
        $form->build();
50
        $form->hydrate();
51
        if ($form->isValid()) {
52
            if (Security::save($form->getData())) {
53
                Logger::log('Configuration saved successful');
54
                Security::getInstance()->setFlash("callback_message", _("Usuario agregado correctamente"));
55
                Security::getInstance()->setFlash("callback_route", $this->getRoute("admin"), true);
0 ignored issues
show
Unused Code introduced by
The call to Security::setFlash() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
56
            } else {
57
                throw new ConfigException(_('Error al guardar los administradores, prueba a cambiar los permisos'));
58
            }
59
        }
60
        return $this->render('admin.html.twig', array(
61
            'admins' => $admins,
62
            'form' => $form,
63
            'profiles' => Security::getProfiles(),
64
        ));
65
    }
66
67
    /**
68
     * Acción que pinta un formulario genérico de login pra la zona restringida
69
     * @param string $route
0 ignored issues
show
Documentation introduced by
Should the type for parameter $route not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
70
     * @GET
71
     * @route /admin/login
72
     * @visible false
73
     * @return string HTML
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
74
     */
75
    public function adminLogin($route = null)
76
    {
77
        return Admin::staticAdminLogon($route);
78
    }
79
80
    /**
81
     * Servicio que valida el login
82
     * @param null $route
83
     * @POST
84
     * @visible false
85
     * @route /admin/login
86
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
87
     * @throws \PSFS\base\exception\FormException
88
     */
89
    public function postLogin($route = null)
90
    {
91
        $form = new LoginForm();
92
        $form->setData(array("route" => $route));
93
        $form->build();
94
        $tpl = Template::getInstance();
95
        $tpl->setPublicZone(true);
96
        $template = "login.html.twig";
97
        $params = array(
98
            'form' => $form,
99
        );
100
        $cookies = array();
101
        $form->hydrate();
102
        if ($form->isValid()) {
103
            if (Security::getInstance()->checkAdmin($form->getFieldValue("user"), $form->getFieldValue("pass"))) {
104
                $cookies = array(
105
                    array(
106
                        "name" => Security::getInstance()->getHash(),
107
                        "value" => base64_encode($form->getFieldValue("user") . ":" . $form->getFieldValue("pass")),
108
                        "expire" => time() + 3600,
109
                        "http" => true,
110
                    )
111
                );
112
                $template = "redirect.html.twig";
113
                $params = array(
114
                    'route' => $form->getFieldValue("route"),
115
                    'status_message' => _("Acceso permitido... redirigiendo!!"),
116
                    'delay' => 1,
117
                );
118
            } else {
119
                $form->setError("user", _("El usuario no tiene acceso a la web"));
120
            }
121
        }
122
        return $tpl->render($template, $params, $cookies);
123
    }
124
}