Merge pull request #5 from gnieark/master

user form
This commit is contained in:
Gnieark 2020-01-02 22:31:38 +01:00 committed by GitHub
commit 4c37fbad58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 95 additions and 4 deletions

View File

@ -21,4 +21,4 @@ register:
users:
path: /users
controller: App\Controller\SecurityController::showUserManagePage
methods: GET
methods: GET|POST

View File

@ -8,11 +8,14 @@ use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use App\Entity\User;
use App\Service\Menus;
use Doctrine\ORM\EntityManagerInterface;
class SecurityController extends AbstractController
@ -92,10 +95,61 @@ class SecurityController extends AbstractController
}
}
private function getformCreateUser()
{
return $this->createFormBuilder()
->add('email', EmailType::class,[])
->add('password', PasswordType::class, [])
->add('display_name', TextType::class,[])
->add('roles', ChoiceType::class, [
'choices' => [
'USER' => false,
'RESOURCE_MANAGER' => 'RESOURCE_MANAGER',
'SUPER ADMIN' => 'SUPER_ADMIN',
],
'multiple' => false
])
->add('save', SubmitType::class,[])
->getForm();
}
public function showUserManagePage()
public function showUserManagePage(Request $request, UserPasswordEncoderInterface $passwordEncoder)
{
$formCreateUser = $this->getformCreateUser();
$formCreateUser->handleRequest($request);
if ($formCreateUser->isSubmitted() && $formCreateUser->isValid()) {
$userValues = $formCreateUser->getData();
$user = new User();
$user ->setEmail($userValues ["email"])
->setPassword( $passwordEncoder->encodePassword($user,$userValues ["password"]) )
->setDisplayName( $userValues ["display_name"] )
->setActive(true)
->setRoles(array( $userValues["roles"] ));
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($user);
$entityManager->flush();
//empty the form
$formCreateUser = $this->getformCreateUser();
}
$users = $this->getDoctrine()
->getRepository(User::class)
->findAll();
$menus = new Menus();
return $this->render('security/listUsers.html.twig', [
"menus" => $menus->getMenus( $this->getUser() ),
"users" => $users,
"formCreateUser" => $formCreateUser->createView()
]);
}
}

View File

@ -0,0 +1,37 @@
{% extends 'base.html.twig' %}
{% block title %}Manage users{% endblock %}
{% block header %}<h1>Manage users</h1>{% endblock %}
{% block nav %}{% include 'menus.html.twig' %}{% endblock %}
{% block content %}
<article id="FormCreateUser">
<h2>Create a new user</h2>
{{ form(formCreateUser) }}
<p>{{ form_errors(formCreateUser) }}</p>
</article>
<article>
<h2>Users list</h2>
<table>
<thead>
<tr>
<th>Displayed Name</th>
<th>Login (email)</th>
<th>Roles</th>
<th>Active</th>
</tr>
</thead>
<tbody>
{% for u in users %}
<tr>
<td>{{ u.displayName }} </td>
<td>{{ u.email }}</td>
<td>{{ u.roles|join(',') }}</td>
<td>{{ u.active? 'Yes':'No' }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</article>
{% endblock %}