mirror of
https://github.com/sigmasternchen/MyTube
synced 2025-03-15 04:48:55 +00:00
adding users now works
This commit is contained in:
parent
012889d531
commit
4b3189d0b1
8 changed files with 138 additions and 4 deletions
|
@ -5,6 +5,7 @@ namespace App\Controller;
|
|||
|
||||
|
||||
use App\Entity\User;
|
||||
use App\Form\UserType;
|
||||
use App\Mapper\CustomUuidMapper;
|
||||
use App\Service\UserService;
|
||||
use Doctrine\DBAL\Types\ConversionException;
|
||||
|
@ -121,4 +122,34 @@ class UserController extends AbstractController
|
|||
|
||||
return $this->redirectToRoute("app_user_list");
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/admin/users/new", name="app_user_add")
|
||||
*/
|
||||
public function userAdd(Request $request): Response
|
||||
{
|
||||
if (!$this->isGranted(User::ROLE_ADMIN)) {
|
||||
throw new AccessDeniedHttpException();
|
||||
}
|
||||
|
||||
$user = new User();
|
||||
$form = $this->createForm(UserType::class, $user);
|
||||
|
||||
$form->handleRequest($request);
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$user = $form->getData();
|
||||
|
||||
if ($user->isSuperAdmin()) {
|
||||
throw new BadRequestHttpException();
|
||||
}
|
||||
|
||||
$this->userService->add($user);
|
||||
|
||||
return $this->redirectToRoute("app_user_list");
|
||||
}
|
||||
|
||||
return $this->render("user/user-new.html.twig", [
|
||||
"form" => $form->createView()
|
||||
]);
|
||||
}
|
||||
}
|
|
@ -24,6 +24,7 @@ class UserFixtures extends Fixture
|
|||
$admin = new User();
|
||||
$admin->setEmail("admin@mytube");
|
||||
$admin->setName("Administrator");
|
||||
$admin->setCreated();
|
||||
$admin->setPassword($this->passwordEncoder->encodePassword($admin, "password"));
|
||||
$admin->setRoles([User::ROLE_SUPER_ADMIN, User::ROLE_ADMIN, User::ROLE_USER]);
|
||||
$manager->persist($admin);
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
namespace App\Entity;
|
||||
|
||||
use App\Repository\UserRepository;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
@ -15,7 +16,7 @@ use Symfony\Component\Security\Core\User\UserInterface;
|
|||
*/
|
||||
class User implements UserInterface
|
||||
{
|
||||
public const ROLE_SUPER_ADMIN = "ROLE_SUPER_ADMIN";
|
||||
public const ROLE_SUPER_ADMIN = "ROLE_SUPER";
|
||||
public const ROLE_ADMIN = "ROLE_ADMIN";
|
||||
public const ROLE_USER = "ROLE_USER";
|
||||
|
||||
|
@ -54,6 +55,16 @@ class User implements UserInterface
|
|||
*/
|
||||
private $videos;
|
||||
|
||||
/**
|
||||
* @ORM\ManyToOne(targetEntity=User::class)
|
||||
*/
|
||||
private $creator;
|
||||
|
||||
/**
|
||||
* @ORM\Column(type="datetime_immutable")
|
||||
*/
|
||||
private $created;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->videos = new ArrayCollection();
|
||||
|
@ -205,4 +216,27 @@ class User implements UserInterface
|
|||
{
|
||||
return in_array(self::ROLE_SUPER_ADMIN, $this->getRoles());
|
||||
}
|
||||
|
||||
public function getCreator(): ?self
|
||||
{
|
||||
return $this->creator;
|
||||
}
|
||||
|
||||
public function setCreator(?self $creator): self
|
||||
{
|
||||
$this->creator = $creator;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCreated(): ?DateTimeImmutable
|
||||
{
|
||||
return $this->created;
|
||||
}
|
||||
|
||||
public function setCreated(): self
|
||||
{
|
||||
$this->created = new DateTimeImmutable();
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
44
src/Form/UserType.php
Normal file
44
src/Form/UserType.php
Normal file
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace App\Form;
|
||||
|
||||
|
||||
use App\Entity\User;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\EmailType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class UserType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, $options): void
|
||||
{
|
||||
$builder
|
||||
->add("name", TextType::class)
|
||||
->add("email", EmailType::class)
|
||||
->add("roles", ChoiceType::class, [
|
||||
"choices" => [
|
||||
"Admin" => User::ROLE_ADMIN
|
||||
],
|
||||
"multiple" => true,
|
||||
"expanded" => true,
|
||||
])
|
||||
->add("password", PasswordType::class, [
|
||||
"always_empty" => true
|
||||
])
|
||||
->add("submit", SubmitType::class);
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
"data_class" => User::class
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
|
@ -67,4 +67,10 @@ class UserRepository extends ServiceEntityRepository implements PasswordUpgrader
|
|||
$this->_em->remove($user);
|
||||
$this->_em->flush();
|
||||
}
|
||||
|
||||
public function save(User $user)
|
||||
{
|
||||
$this->_em->persist($user);
|
||||
$this->_em->flush();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -50,4 +50,12 @@ class UserService
|
|||
{
|
||||
return $this->userRepository->findOneById($userId);
|
||||
}
|
||||
|
||||
public function add(User $user)
|
||||
{
|
||||
$user->setCreated();
|
||||
$user->setCreator($this->getLoggedInUser());
|
||||
|
||||
$this->userRepository->save($user);
|
||||
}
|
||||
}
|
10
templates/user/user-new.html.twig
Normal file
10
templates/user/user-new.html.twig
Normal file
|
@ -0,0 +1,10 @@
|
|||
{% extends 'base.html.twig' %}
|
||||
|
||||
{% block title %}Users{% endblock %}
|
||||
{% block stylesheets %}
|
||||
<link rel="stylesheet" href="{{ asset("css/admin.css") }}">
|
||||
{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
{{ form(form) }}
|
||||
{% endblock %}
|
|
@ -1,6 +1,6 @@
|
|||
{% extends 'base.html.twig' %}
|
||||
|
||||
{% block title %}Links{% endblock %}
|
||||
{% block title %}Users{% endblock %}
|
||||
{% block stylesheets %}
|
||||
<link rel="stylesheet" href="{{ asset("css/admin.css") }}">
|
||||
{% endblock %}
|
||||
|
@ -88,8 +88,8 @@
|
|||
</table>
|
||||
</div>
|
||||
<div class="addButton">
|
||||
<button type="button" class="btn btn-primary btn-floating">
|
||||
<a type="button" class="btn btn-primary btn-floating" href="{{ path("app_user_add") }}">
|
||||
<i class="fas fa-plus"></i>
|
||||
</button>
|
||||
</a>
|
||||
</div>
|
||||
{% endblock %}
|
Loading…
Reference in a new issue