Passed
Push — master ( e9d973...038fa0 )
by Fran
03:28
created

UserController::showAdminManager()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2.003

Importance

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