mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-22 01:49:05 +02:00
Allow admins to set passwords in users admin page.
This commit is contained in:
parent
694beca825
commit
006cd9c7e5
4 changed files with 80 additions and 6 deletions
|
@ -33,6 +33,7 @@ namespace App\Controller\AdminPages;
|
||||||
|
|
||||||
use App\Entity\Base\NamedDBElement;
|
use App\Entity\Base\NamedDBElement;
|
||||||
use App\Entity\Base\StructuralDBElement;
|
use App\Entity\Base\StructuralDBElement;
|
||||||
|
use App\Entity\UserSystem\User;
|
||||||
use App\Form\AdminPages\ImportType;
|
use App\Form\AdminPages\ImportType;
|
||||||
use App\Form\AdminPages\MassCreationForm;
|
use App\Form\AdminPages\MassCreationForm;
|
||||||
use App\Services\EntityExporter;
|
use App\Services\EntityExporter;
|
||||||
|
@ -42,6 +43,9 @@ use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
|
||||||
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
|
||||||
|
use Symfony\Component\Security\Core\Tests\Encoder\PasswordEncoder;
|
||||||
use Symfony\Component\Validator\ConstraintViolationList;
|
use Symfony\Component\Validator\ConstraintViolationList;
|
||||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||||
|
|
||||||
|
@ -53,15 +57,18 @@ abstract class BaseAdminController extends AbstractController
|
||||||
protected $twig_template = '';
|
protected $twig_template = '';
|
||||||
protected $route_base = '';
|
protected $route_base = '';
|
||||||
|
|
||||||
|
|
||||||
|
protected $passwordEncoder;
|
||||||
protected $translator;
|
protected $translator;
|
||||||
|
|
||||||
public function __construct(TranslatorInterface $translator)
|
public function __construct(TranslatorInterface $translator, UserPasswordEncoderInterface $passwordEncoder)
|
||||||
{
|
{
|
||||||
if ($this->entity_class === '' || $this->form_class === '' || $this->twig_template === '' || $this->route_base === '') {
|
if ($this->entity_class === '' || $this->form_class === '' || $this->twig_template === '' || $this->route_base === '') {
|
||||||
throw new \InvalidArgumentException('You have to override the $entity_class, $form_class, $route_base and $twig_template value in your subclasss!');
|
throw new \InvalidArgumentException('You have to override the $entity_class, $form_class, $route_base and $twig_template value in your subclasss!');
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->translator = $translator;
|
$this->translator = $translator;
|
||||||
|
$this->passwordEncoder = $passwordEncoder;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function _edit(NamedDBElement $entity, Request $request, EntityManagerInterface $em)
|
protected function _edit(NamedDBElement $entity, Request $request, EntityManagerInterface $em)
|
||||||
|
@ -73,6 +80,14 @@ abstract class BaseAdminController extends AbstractController
|
||||||
|
|
||||||
$form->handleRequest($request);
|
$form->handleRequest($request);
|
||||||
if ($form->isSubmitted() && $form->isValid()) {
|
if ($form->isSubmitted() && $form->isValid()) {
|
||||||
|
//Check if we editing a user and if we need to change the password of it
|
||||||
|
if ($entity instanceof User && !empty($form['new_password']->getData())) {
|
||||||
|
$password = $this->passwordEncoder->encodePassword($entity, $form['new_password']->getData());
|
||||||
|
$entity->setPassword($password);
|
||||||
|
//By default the user must change the password afterwards
|
||||||
|
$entity->setNeedPwChange(true);
|
||||||
|
}
|
||||||
|
|
||||||
$em->persist($entity);
|
$em->persist($entity);
|
||||||
$em->flush();
|
$em->flush();
|
||||||
$this->addFlash('success', $this->translator->trans('entity.edit_flash'));
|
$this->addFlash('success', $this->translator->trans('entity.edit_flash'));
|
||||||
|
@ -103,6 +118,12 @@ abstract class BaseAdminController extends AbstractController
|
||||||
$form->handleRequest($request);
|
$form->handleRequest($request);
|
||||||
|
|
||||||
if ($form->isSubmitted() && $form->isValid()) {
|
if ($form->isSubmitted() && $form->isValid()) {
|
||||||
|
if ($new_entity instanceof User && !empty($form['new_password']->getData())) {
|
||||||
|
$password = $this->passwordEncoder->encodePassword($new_entity, $form['new_password']->getData());
|
||||||
|
$new_entity->setPassword($password);
|
||||||
|
//By default the user must change the password afterwards
|
||||||
|
$new_entity->setNeedPwChange(true);
|
||||||
|
}
|
||||||
$em->persist($new_entity);
|
$em->persist($new_entity);
|
||||||
$em->flush();
|
$em->flush();
|
||||||
$this->addFlash('success', $this->translator->trans('entity.created_flash'));
|
$this->addFlash('success', $this->translator->trans('entity.created_flash'));
|
||||||
|
@ -172,6 +193,7 @@ abstract class BaseAdminController extends AbstractController
|
||||||
if ($entity instanceof StructuralDBElement && $request->get('delete_recursive', false)) {
|
if ($entity instanceof StructuralDBElement && $request->get('delete_recursive', false)) {
|
||||||
$recursionHelper->delete($entity, false);
|
$recursionHelper->delete($entity, false);
|
||||||
} else {
|
} else {
|
||||||
|
if ($entity instanceof StructuralDBElement) {
|
||||||
$parent = $entity->getParent();
|
$parent = $entity->getParent();
|
||||||
|
|
||||||
//Move all sub entities to the current parent
|
//Move all sub entities to the current parent
|
||||||
|
@ -179,6 +201,7 @@ abstract class BaseAdminController extends AbstractController
|
||||||
$subelement->setParent($parent);
|
$subelement->setParent($parent);
|
||||||
$entityManager->persist($subelement);
|
$entityManager->persist($subelement);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//Remove current element
|
//Remove current element
|
||||||
$entityManager->remove($entity);
|
$entityManager->remove($entity);
|
||||||
|
|
|
@ -328,10 +328,32 @@ class User extends NamedDBElement implements UserInterface, HasPermissionsInterf
|
||||||
return $this->permissions;
|
return $this->permissions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the user needs a password change
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isNeedPwChange(): bool
|
||||||
|
{
|
||||||
|
return $this->need_pw_change;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the status, if the user needs a password change.
|
||||||
|
* @param bool $need_pw_change
|
||||||
|
* @return User
|
||||||
|
*/
|
||||||
|
public function setNeedPwChange(bool $need_pw_change): User
|
||||||
|
{
|
||||||
|
$this->need_pw_change = $need_pw_change;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/************************************************
|
/************************************************
|
||||||
* Getters
|
* Getters
|
||||||
************************************************/
|
************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the full name in the format FIRSTNAME LASTNAME [(USERNAME)].
|
* Returns the full name in the format FIRSTNAME LASTNAME [(USERNAME)].
|
||||||
* Example: Max Muster (m.muster).
|
* Example: Max Muster (m.muster).
|
||||||
|
|
|
@ -42,14 +42,18 @@ use App\Form\Type\StructuralEntityType;
|
||||||
use FOS\CKEditorBundle\Form\Type\CKEditorType;
|
use FOS\CKEditorBundle\Form\Type\CKEditorType;
|
||||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||||
use Symfony\Component\Form\AbstractType;
|
use Symfony\Component\Form\AbstractType;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
||||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||||
use Symfony\Component\Form\Extension\Core\Type\LanguageType;
|
use Symfony\Component\Form\Extension\Core\Type\LanguageType;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
|
||||||
use Symfony\Component\Form\Extension\Core\Type\ResetType;
|
use Symfony\Component\Form\Extension\Core\Type\ResetType;
|
||||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||||
use Symfony\Component\Form\Extension\Core\Type\TimezoneType;
|
use Symfony\Component\Form\Extension\Core\Type\TimezoneType;
|
||||||
use Symfony\Component\Form\FormBuilderInterface;
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
use Symfony\Component\Security\Core\Security;
|
use Symfony\Component\Security\Core\Security;
|
||||||
|
use Symfony\Component\Validator\Constraints\Length;
|
||||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||||
|
|
||||||
class UserAdminForm extends AbstractType
|
class UserAdminForm extends AbstractType
|
||||||
|
@ -146,6 +150,25 @@ class UserAdminForm extends AbstractType
|
||||||
'disabled' => !$this->security->isGranted('change_user_settings', $entity)
|
'disabled' => !$this->security->isGranted('change_user_settings', $entity)
|
||||||
])
|
])
|
||||||
|
|
||||||
|
->add('new_password', RepeatedType::class, [
|
||||||
|
'type' => PasswordType::class,
|
||||||
|
'first_options' => ['label' => $this->trans->trans('user.settings.pw_new.label')],
|
||||||
|
'second_options' => ['label' => $this->trans->trans('user.settings.pw_confirm.label')],
|
||||||
|
'invalid_message' => 'password_must_match',
|
||||||
|
'required' => false,
|
||||||
|
'mapped' => false,
|
||||||
|
'constraints' => [new Length([
|
||||||
|
'min' => 6,
|
||||||
|
'max' => 128,
|
||||||
|
])]
|
||||||
|
])
|
||||||
|
|
||||||
|
->add('need_pw_change', CheckboxType::class, [
|
||||||
|
'required' => false,
|
||||||
|
'label_attr' => ['class' => 'checkbox-custom'],
|
||||||
|
'label' => $this->trans->trans('user.edit.needs_pw_change')
|
||||||
|
])
|
||||||
|
|
||||||
//Permission section
|
//Permission section
|
||||||
->add('permissions', PermissionsType::class, [
|
->add('permissions', PermissionsType::class, [
|
||||||
'mapped' => false,
|
'mapped' => false,
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
{% block additional_pills %}
|
{% block additional_pills %}
|
||||||
<li class="nav-item"><a data-toggle="tab" class="nav-link link-anchor" href="#configuration">{% trans %}user.edit.configuration{% endtrans %}</a></li>
|
<li class="nav-item"><a data-toggle="tab" class="nav-link link-anchor" href="#configuration">{% trans %}user.edit.configuration{% endtrans %}</a></li>
|
||||||
|
<li class="nav-item"><a data-toggle="tab" class="nav-link link-anchor" href="#password">{% trans %}user.edit.password{% endtrans %}</a></li>
|
||||||
<li class="nav-item"><a data-toggle="tab" class="nav-link link-anchor" href="#permissions">{% trans %}user.edit.permissions{% endtrans %}</a></li>
|
<li class="nav-item"><a data-toggle="tab" class="nav-link link-anchor" href="#permissions">{% trans %}user.edit.permissions{% endtrans %}</a></li>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
|
@ -28,6 +29,11 @@
|
||||||
{{ form_row(form.currency) }}
|
{{ form_row(form.currency) }}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="tab-pane" id="password">
|
||||||
|
{{ form_row(form.new_password) }}
|
||||||
|
{{ form_row(form.need_pw_change) }}
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="tab-pane" id="permissions">
|
<div class="tab-pane" id="permissions">
|
||||||
{{ form_row(form.permissions) }}
|
{{ form_row(form.permissions) }}
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue