Test Failed
Push — master ( cc821f...fdc4d2 )
by Fran
09:36 queued 04:02
created

UserController::showAdminManager()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.864

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 14
ccs 4
cts 10
cp 0.4
crap 2.864
rs 9.4285
c 0
b 0
f 0
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\Request;
9
use PSFS\base\Router;
10
use PSFS\base\Security;
11
use PSFS\base\Template;
12
use PSFS\controller\base\Admin;
13
use PSFS\services\AdminServices;
14
15
/**
16
 * Class UserController
17
 * @package PSFS\controller
18
 */
19
class UserController extends Admin
20
{
21
    /**
22
     * @return string
23
     */
24 1
    public static function showAdminManager()
25
    {
26 1
        if (Request::getInstance()->getMethod() != 'GET') {
27
            return self::updateAdminUsers();
28
        }
29 1
        $admins = AdminServices::getInstance()->getAdmins();
30 1
        $form = new AdminForm();
31
        $form->build();
32
        return Template::getInstance()->render('admin.html.twig', array(
33
            'admins' => $admins,
34
            'form' => $form,
35
            'profiles' => Security::getProfiles(),
36
        ));
37
    }
38
39
    /**
40
     * Método que gestiona los usuarios administradores de la plataforma
41
     * @GET
42
     * @route /admin/setup
43
     * @label Gestor de usuarios PSFS
44
     * @return string|null
45
     * @throws \HttpException
46
     */
47
    public function adminers()
48
    {
49
        return self::showAdminManager();
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    public static function updateAdminUsers()
56
    {
57
        $admins = AdminServices::getInstance()->getAdmins();
58
        $form = new AdminForm();
59
        $form->build();
60
        $form->hydrate();
61
        if ($form->isValid()) {
62
            if (Security::save($form->getData())) {
63
                Logger::log('Configuration saved successful');
64
                Security::getInstance()->setFlash("callback_message", _("Usuario agregado correctamente"));
65
                Security::getInstance()->setFlash("callback_route", Router::getInstance()->getRoute("admin", true));
66
            } else {
67
                throw new ConfigException(_('Error al guardar los administradores, prueba a cambiar los permisos'));
68
            }
69
        }
70
        return Template::getInstance()->render('admin.html.twig', array(
71
            'admins' => $admins,
72
            'form' => $form,
73
            'profiles' => Security::getProfiles(),
74
        ));
75
    }
76
77
    /**
78
     * Servicio que guarda los usuarios de administración
79
     * @POST
80
     * @route /admin/setup
81
     * @visible false
82
     * @return string|void
83
     * @throws \HttpException
84
     */
85
    public function setAdminUsers()
86
    {
87
        return self::updateAdminUsers();
88
    }
89
90
    /**
91
     * Acción que pinta un formulario genérico de login pra la zona restringida
92
     * @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...
93
     * @GET
94
     * @route /admin/login
95
     * @visible false
96
     * @return string HTML
97
     */
98
    public function adminLogin($route = null)
99
    {
100
        if ($this->isAdmin()) {
101
            return $this->redirect('admin');
102
        } else {
103
            return Admin::staticAdminLogon($route);
104
        }
105
    }
106
107
    /**
108
     * Servicio que valida el login
109
     * @param null $route
110
     * @POST
111
     * @visible false
112
     * @route /admin/login
113
     * @return string
114
     * @throws \PSFS\base\exception\FormException
115
     */
116
    public function postLogin($route = null)
117
    {
118
        $form = new LoginForm();
119
        $form->setData(array("route" => $route));
120
        $form->build();
121
        $tpl = Template::getInstance();
122
        $tpl->setPublicZone(true);
123
        $template = "login.html.twig";
124
        $params = array(
125
            'form' => $form,
126
        );
127
        $cookies = array();
128
        $form->hydrate();
129
        if ($form->isValid()) {
130
            if (Security::getInstance()->checkAdmin($form->getFieldValue("user"), $form->getFieldValue("pass"))) {
131
                $cookies = array(
132
                    array(
133
                        "name" => Security::getInstance()->getHash(),
134
                        "value" => base64_encode($form->getFieldValue("user") . ":" . $form->getFieldValue("pass")),
135
                        "expire" => time() + 3600,
136
                        "http" => true,
137
                    )
138
                );
139
                $template = "redirect.html.twig";
140
                $params = array(
141
                    'route' => $form->getFieldValue("route"),
142
                    'status_message' => _("Acceso permitido... redirigiendo!!"),
143
                    'delay' => 1,
144
                );
145
            } else {
146
                $form->setError("user", _("El usuario no tiene acceso a la web"));
147
            }
148
        }
149
        return $tpl->render($template, $params, $cookies);
150
    }
151
}