Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
65 changes: 58 additions & 7 deletions src/Controller/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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('[email protected]');
$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;
}

}
4 changes: 3 additions & 1 deletion templates/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
<form method="post" action="{{ path('searchRecipe') }}">
<div class="ui computer menu">
<a class="item" href="{{ path('recipeIndex') }}"><i class="home icon"></i> Startseite</a>
<a class="item" href="{{ path('addRecipe') }}"><i class="plus icon"></i> Rezept hinzufügen</a>
{% if app.user.username == 'admin' %}
<a class="item" href="{{ path('addRecipe') }}"><i class="plus icon"></i> Rezept hinzufügen</a>
{% endif %}
<a class="item" href="{{ path('listRecipeTags') }}"><i class="tag icon"></i> Tags</a>
<a class="item" href="{{ path('searchByIngredients') }}"><i class="search icon"></i>
Zutatensuche</a>
Expand Down
6 changes: 4 additions & 2 deletions templates/details.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@
<p>
<a href="{{ recipe.originUrl }}" target="_blank"><i
class="external alternate icon"></i> {{ recipe.originUrl }}</a>
<a href="{{ path('editRecipe', {'id': recipe.id}) }}" class="ui mini button right"><i
class="edit icon"></i> Bearbeiten</a>
{% if app.user.username == 'admin' %}
<a href="{{ path('editRecipe', {'id': recipe.id}) }}" class="ui mini button right"><i
class="edit icon"></i> Bearbeiten</a>
<a href="{{ path('editRecipeImages', {'id': recipe.id}) }}" class="ui mini button right"><i
class="edit icon"></i> Fotos Bearbeiten</a>
{% endif %}
</p>
</div>
</div>
Expand Down
2 changes: 2 additions & 0 deletions templates/security/login.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
</form>

<div class="ui message">
<a href="{{ path('register') }}">Registrieren</a>
<br/>
Passwort vergessen? Schade!
</div>
</div>
Expand Down
71 changes: 71 additions & 0 deletions templates/security/register.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{% extends 'html.html.twig' %}

{% block body %}
<div class="ui middle aligned center aligned grid">
<div class="column">
<h2 class="ui teal image header">
<div class="content">
Registrieren
</div>
</h2>
<form action="{{ path('addUser') }}" method="post" class="ui large form">
<div class="ui stacked segment">
{% if error %}
<div class="ui negative message">
<i class="close icon"></i>
<div class="header">Anmeldung fehlgeschlagen!</div>
<p>{{ error }}</p>
</div>
{% endif %}
<div class="field">
<div class="ui left icon input">
<i class="mail icon"></i>
<input type="text" name="_email" placeholder="E-Mail" />
</div>
</div>
<div class="field">
<div class="ui left icon input">
<i class="user icon"></i>
<input type="text" name="_username" placeholder="Username" />
</div>
</div>
<div class="field">
<div class="ui left icon input">
<i class="lock icon"></i>
<input type="password" name="_password1" placeholder="Passwort">
</div>
</div>
<div class="field">
<div class="ui left icon input">
<i class="lock icon"></i>
<input type="password" name="_password2" placeholder="Passwort wiederholen">
</div>
</div>
<button type="submit" class="ui fluid large teal submit button">Registrieren</button>
</div>

<div class="ui error message"></div>

</form>
</div>
</div>

<style type="text/css">
body {
background-color: #DADADA;
}

body > .grid {
height: 100%;
}

.image {
margin-top: -100px;
}

.column {
max-width: 450px;
}
</style>

{% endblock %}