Commit 58a7ad4a authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

UserUsurpationHelper : ajout de la possibilité de dessiner un simple bouton

parent a9ec1509
Pipeline #9135 passed with stage
in 50 seconds
...@@ -3,11 +3,14 @@ ...@@ -3,11 +3,14 @@
namespace UnicaenAuth\View\Helper; namespace UnicaenAuth\View\Helper;
use UnicaenApp\Form\View\Helper\FormControlGroup; use UnicaenApp\Form\View\Helper\FormControlGroup;
use UnicaenAuth\Entity\Db\AbstractUser;
use UnicaenAuth\Options\ModuleOptions; use UnicaenAuth\Options\ModuleOptions;
use Zend\Form\Element\Hidden;
use Zend\Form\Element\Submit; use Zend\Form\Element\Submit;
use Zend\Form\Element\Text; use Zend\Form\Element\Text;
use Zend\Form\Form; use Zend\Form\Form;
use Zend\Form\View\Helper\Form as FormHelper; use Zend\Form\View\Helper\Form as FormHelper;
use Zend\Form\View\Helper\FormElement;
use Zend\View\Renderer\PhpRenderer; use Zend\View\Renderer\PhpRenderer;
/** /**
...@@ -37,6 +40,11 @@ class UserUsurpationHelper extends UserAbstract ...@@ -37,6 +40,11 @@ class UserUsurpationHelper extends UserAbstract
*/ */
private $usurpationEnabled = false; private $usurpationEnabled = false;
/**
* @var bool
*/
private $asButton = false;
/** /**
* Point d'entrée. * Point d'entrée.
* *
...@@ -49,55 +57,90 @@ class UserUsurpationHelper extends UserAbstract ...@@ -49,55 +57,90 @@ class UserUsurpationHelper extends UserAbstract
/** /**
* Retourne le code HTML généré par cette aide de vue. * Retourne le code HTML généré par cette aide de vue.
* Equivalent à {@see renderAsTextfield()}.
* *
* @return string * @return string
*/ */
public function __toString() public function __toString()
{ {
if (! $this->usurpationEnabled) { return $this->renderAsTextfield();
}
/**
* @return string
*/
public function renderAsButton(AbstractUser $user, string $buttonLabel = 'Usurper')
{
if (!$this->usurpationEnabled) {
return ''; return '';
} }
$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);
/** @var FormHelper $formHelper */ /** @var FormHelper $formHelper */
$formHelper = $this->view->plugin('form'); $formHelper = $this->view->plugin('form');
/** @var FormControlGroup $formControlGroupHelper */ /** @var FormElement $formElementHelper */
$formControlGroupHelper = $this->view->plugin('formControlGroup'); $formElementHelper = $this->view->plugin('formElement');
$form = new Form('user-usurpation-form'); $html = '';
$form->setAttributes([ $html .= $formHelper->openTag($form);
'class' => 'user-usurpation-form disable-ajax-submit', $html .= $formElementHelper->__invoke($identity);
'action' => $this->url, $html .= $formElementHelper->__invoke($submit);
]); $html .= $formHelper->closeTag();
$input = new Text('identity'); return $html;
$input->setAttributes([ }
'id' => 'user-usurpation-input',
'placeholder' => "Identifiant utilisateur",
]);
$submit = new Submit('submit'); /**
$submit->setValue("Usurper"); * @return string
$submit->setAttributes([ */
'class' => 'user-usurpation-submit btn btn-info', public function renderAsTextfield()
]); {
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');
$html = ''; $html = '';
$html .= $formHelper->openTag($form); $html .= $formHelper->openTag($form);
$html .= "<div><strong>Usurpation d'identité :</strong></div>"; $html .= "<div><strong>Usurpation d'identité :</strong></div>";
$html .= $formControlGroupHelper->__invoke($input); $html .= $formControlGroupHelper->__invoke($identity);
$html .= $formControlGroupHelper->__invoke($submit); $html .= $formControlGroupHelper->__invoke($submit);
$html .= $formHelper->closeTag(); $html .= $formHelper->closeTag();
$formId = $form->getAttribute('id');
$html .= <<<EOS $html .= <<<EOS
<script> <script>
$(".user-usurpation-form").submit(function() { var form = $("#$formId").submit(function() {
$("body *").css('cursor', 'wait'); $("body *").css('cursor', 'wait');
}); });
var input = $("#user-usurpation-input").on('input', function() { var input = form.find(".user-usurpation-input").on('input', function() {
updateUsurpationSubmit(); updateUsurpationSubmit();
}); });
function updateUsurpationSubmit() { function updateUsurpationSubmit() {
$(".user-usurpation-submit").prop("disabled", input.val().length === 0); form.find(".user-usurpation-submit").prop("disabled", input.val().length === 0);
} }
updateUsurpationSubmit(); updateUsurpationSubmit();
</script> </script>
...@@ -106,6 +149,41 @@ EOS; ...@@ -106,6 +149,41 @@ EOS;
return $html; return $html;
} }
protected function createForm()
{
$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([
'class' => 'user-usurpation-submit btn btn-danger',
]);
$form->add($identity);
$form->add($submit);
return $form;
}
/** /**
* @param string $url * @param string $url
* @return $this * @return $this
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment