diff --git a/config/packages/security.yaml b/config/packages/security.yaml index 7408d96..9234a49 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -32,6 +32,9 @@ security: access_control: - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/register, roles: IS_AUTHENTICATED_ANONYMOUSLY } + - { path: ^/addUser, roles: IS_AUTHENTICATED_ANONYMOUSLY } + - { path: ^/recipes/edit, allow_if: "user.getUsername() == 'admin'" } + - { path: ^/recipes/add, allow_if: "user.getUsername() == 'admin'" } - { path: ^/, roles: ROLE_USER } # - { path: ^/admin, roles: ROLE_ADMIN } # - { path: ^/profile, roles: ROLE_USER } diff --git a/src/Controller/LoginController.php b/src/Controller/LoginController.php index cb47f46..cff87ac 100644 --- a/src/Controller/LoginController.php +++ b/src/Controller/LoginController.php @@ -4,6 +4,8 @@ namespace App\Controller; use App\Entity\User; +use PhpParser\Node\Scalar\String_; +use PHPUnit\Runner\Exception; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; @@ -27,13 +29,62 @@ public function login(Request $request, AuthenticationUtils $authenticationUtils /** * @Route("/register", name="register") */ - public function register(UserPasswordEncoderInterface $encoder) { - $user = $this->getDoctrine()->getRepository(User::class)->findOneBy(['username' => 'admin']) ?? new User(); - $user->setUsername('admin'); - $user->setPassword($encoder->encodePassword($user, 'admin')); - $user->setEmail('admin@admin.com'); - $this->getDoctrine()->getManager()->persist($user); - $this->getDoctrine()->getManager()->flush(); + public function register() { + return $this->render('security/register.html.twig', array('error' => null)); } + /** + * @param Request $request + * @param UserPasswordEncoderInterface $encoder + * @return \Symfony\Component\HttpFoundation\Response + * + * @Route("/addUser", name="addUser") + */ + public function addUser(Request $request, UserPasswordEncoderInterface $encoder) { + $email = $request->get('_email'); + $username = $request->get('_username'); + $password1 = $request->get('_password1'); + $password2 = $request->get('_password2'); + $error = $this->checkRequest($email, $username, $password1, $password2); + if($error) + return $this->render('security/register.html.twig', array('error' => $error)); + $user = $this->buildUser($encoder, $username, $password1, $email); + $this->getDoctrine()->getManager()->persist($user); + $this->getDoctrine()->getManager()->flush(); + return $this->redirectToRoute('login'); + } + + /** + * @param String $email + * @param String $username + * @param String $password1 + * @param String $password2 + * @return String|null + */ + private function checkRequest(String $email, String $username, String $password1, String $password2) { + if(empty(trim($email)) || empty(trim($username)) || empty(trim($password1)) || empty(trim($password2))) + return "Bitte alle Felder ausfüllen!"; + if($password1 !== $password2) + return "Passwörter stimmen nicht überein!"; + if($this->getDoctrine()->getRepository(User::class)->findOneBy(['username' => $username])) + return "Nutzername schon vergeben!"; + return null; + } + + /** + * @param UserPasswordEncoderInterface $encoder + * @param $username + * @param $password + * @param $email + * @return User + */ + protected function buildUser(UserPasswordEncoderInterface $encoder, $username, $password, $email): User + { + $user = new User(); + $user->setUsername($username); + $user->setPassword($encoder->encodePassword($user, $password)); + $user->setEmail($email); + return $user; + } + } \ No newline at end of file diff --git a/templates/base.html.twig b/templates/base.html.twig index fb6ae37..4075f12 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -6,7 +6,9 @@
diff --git a/templates/security/register.html.twig b/templates/security/register.html.twig new file mode 100644 index 0000000..102bb45 --- /dev/null +++ b/templates/security/register.html.twig @@ -0,0 +1,71 @@ +{% extends 'html.html.twig' %} + +{% block body %} +