UserUsurpationHelper.php 6.75 KB
Newer Older
1
2
3
4
5
<?php

namespace UnicaenAuth\View\Helper;

use UnicaenApp\Form\View\Helper\FormControlGroup;
6
use UnicaenAuth\Entity\Db\AbstractUser;
7
use UnicaenAuth\Options\ModuleOptions;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
8
9
10
11
12
13
14
15
use Laminas\Form\Element\Hidden;
use Laminas\Form\Element\Submit;
use Laminas\Form\Element\Text;
use Laminas\Form\Form;
use Laminas\Form\View\Helper\Form as FormHelper;
use Laminas\Form\View\Helper\FormElement;
use Laminas\View\Helper\HeadScript;
use Laminas\View\Renderer\PhpRenderer;
16
17

/**
18
19
20
21
22
23
24
25
 * Aide de vue consacrée à l'usurpation d'identité.
 *
 * 2 visuels possibles :
 * - formulaire de saisie de l'identifiant de connexion de l'utilsateur à usurper ;
 * - simple bouton pour usurper l'identité de l'utilisateur passer en paramètre ;
 *
 * Si une usurpation est en cours :
 * - bouton pour stopper l'usurpation.
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier@unicaen.fr>
 */
class UserUsurpationHelper extends UserAbstract
{
    /**
     * @var PhpRenderer
     */
    protected $view;

    /**
     * @var ModuleOptions
     */
    protected $moduleOptions;

    /**
     * @var string
     */
    private $url;

    /**
     * @var bool
     */
    private $usurpationEnabled = false;

51
52
53
54
55
    /**
     * @var bool
     */
    private $usurpationEnCours = false;

56
57
58
59
60
    /**
     * @var bool
     */
    private $asButton = false;

61
62
63
64
65
    /**
     * Point d'entrée.
     *
     * @return self
     */
66
    public function __invoke(): self
67
68
69
70
71
72
    {
        return $this;
    }
    
    /**
     * Retourne le code HTML généré par cette aide de vue.
73
     * Equivalent à {@see renderAsTextfield()}.
74
75
76
     * 
     * @return string 
     */
77
    public function __toString(): string
78
    {
79
80
81
82
        return $this->renderAsTextfield();
    }

    /**
83
84
     * @param AbstractUser $user
     * @param string $buttonLabel
85
86
     * @return string
     */
87
    public function renderAsButton(AbstractUser $user, string $buttonLabel = 'Usurper'): string
88
    {
89
90
91
        if ($this->usurpationEnCours) {
            return $this->renderStopButton();
        }
92
        if (!$this->usurpationEnabled) {
93
94
95
            return '';
        }

96
97
98
99
100
101
102
103
104
105
106
107
        $this->asButton = true;

        $form = $this->createForm();

        $identity = $form->get('identity');
        $submit = $form->get('submit');

        $identity->setValue($user->getUsername());
        $submit
            ->setValue($buttonLabel)
            ->setAttribute('title', "Usurper l'identité de " . $user);

108
109
        /** @var FormHelper $formHelper */
        $formHelper = $this->view->plugin('form');
110
111
        /** @var FormElement $formElementHelper */
        $formElementHelper = $this->view->plugin('formElement');
112

113
114
115
116
117
        $html = '';
        $html .= $formHelper->openTag($form);
        $html .= $formElementHelper->__invoke($identity);
        $html .= $formElementHelper->__invoke($submit);
        $html .= $formHelper->closeTag();
118

119
120
        return $html;
    }
121

122
123
124
    /**
     * @return string
     */
125
    public function renderAsTextfield(): string
126
    {
127
128
129
        if ($this->usurpationEnCours) {
            return $this->renderStopButton();
        }
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
        if (!$this->usurpationEnabled) {
            return '';
        }

        $this->asButton = false;

        $form = $this->createForm();

        $identity = $form->get('identity');
        $submit = $form->get('submit');

        /** @var FormHelper $formHelper */
        $formHelper = $this->view->plugin('form');
        /** @var FormControlGroup $formControlGroupHelper */
        $formControlGroupHelper = $this->view->plugin('formControlGroup');
145
146
147
148

        $html = '';
        $html .= $formHelper->openTag($form);
        $html .= "<div><strong>Usurpation d'identité :</strong></div>";
Bertrand Gauthier's avatar
Bertrand Gauthier committed
149
150
151
152
        $html .= '<div class="row">' . PHP_EOL;
        $html .= '<div class="col-sm-9">' . $formControlGroupHelper->__invoke($identity) . '</div>' . PHP_EOL;
        $html .= '<div class="col-sm-3">' . $formControlGroupHelper->__invoke($submit) . '</div>' . PHP_EOL;
        $html .= '</div>' . PHP_EOL;
153
154
        $html .= $formHelper->closeTag();

155
156
        $formId = $form->getAttribute('id');

Bertrand Gauthier's avatar
Bertrand Gauthier committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
        $js = <<<EOS
$(function() {
    $("body")
        .on("input", ".user-usurpation-input", function() {
            const input = $(this);
            const submit = $(".user-usurpation-submit");
            input.val().length > 0 ? 
                submit.prop("disabled", false) : 
                submit.prop("disabled", true);
        })
        .on("submit", "#$formId", function() {
            $("body *").css('cursor', 'wait');
        });
});
171
EOS;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
172
        $this->view->inlineScript(HeadScript::SCRIPT, $js);
173
174
175
176

        return $html;
    }

177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
    /**
     * @return string
     */
    protected function renderStopButton(): string
    {
        $url = $this->view->url('utilisateur/default', ['action' => 'stopper-usurpation']);

        return <<<EOS
<p class="user-usurpation-stop-btn">
    <a class="btn btn-danger" href="$url">Stopper l'usurpation</a>
</p>
EOS;
    }

    protected function createForm(): Form
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
    {
        $form = new Form('user-usurpation-form');
        $form->setAttributes([
            'id' => $formId = uniqid('user-usurpation-form'),
            'class' => 'user-usurpation-form disable-ajax-submit',
            'action' => $this->url,
        ]);

        if ($this->asButton) {
            $identity = null;
            $identity = new Hidden('identity');
            $identity->setAttributes([
                'id' => 'user-usurpation-hidden',
            ]);
        } else {
            $identity = new Text('identity');
            $identity->setAttributes([
                'class' => 'user-usurpation-input',
                'placeholder' => "Identifiant utilisateur",
            ]);
        }

        $submit = new Submit('submit');
        $submit->setValue("Usurper");
        $submit->setAttributes([
Bertrand Gauthier's avatar
Bertrand Gauthier committed
217
            'disabled' => !$this->asButton,
218
219
220
221
222
223
224
225
226
            'class' => 'user-usurpation-submit btn btn-danger',
        ]);

        $form->add($identity);
        $form->add($submit);

        return $form;
    }

227
228
    /**
     * @param string $url
229
     * @return self
230
     */
231
    public function setUrl(string $url): self
232
233
234
235
236
237
238
239
240
241
    {
        $this->url = $url;

        return $this;
    }

    /**
     * @param ModuleOptions $moduleOptions
     * @return self
     */
242
    public function setModuleOptions(ModuleOptions $moduleOptions): self
243
244
245
246
247
248
249
250
251
252
    {
        $this->moduleOptions = $moduleOptions;

        return $this;
    }

    /**
     * @param bool $usurpationEnabled
     * @return self
     */
253
    public function setUsurpationEnabled($usurpationEnabled = true): self
254
255
256
257
258
    {
        $this->usurpationEnabled = $usurpationEnabled;

        return $this;
    }
259
260
261
262
263
264
265
266
267
268
269

    /**
     * @param bool $usurpationEnCours
     * @return self
     */
    public function setUsurpationEnCours(bool $usurpationEnCours): self
    {
        $this->usurpationEnCours = $usurpationEnCours;

        return $this;
    }
270
}