Skip to content

Ukol 3 - Tomáš Linhart #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
6 changes: 6 additions & 0 deletions app/Resources/views/address/add.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends 'base.html.twig' %}

{% block body %}
{{ form_start(form) }}
{{ form_end(form) }}
{% endblock %}
3 changes: 2 additions & 1 deletion app/Resources/views/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
</ul>
<p class="navbar-text navbar-right">
{% if user is defined and user %}
<a href="#" class="navbar-link">{{ user.username }}</a>
<a href="{{ path("user_profile") }}" class="navbar-link">{{ user.username }}</a>
| <a href="{{ path("user_password") }}" class="navbar-link">Změna hesla</a>
| <a href="{{ path("user_logout") }}" class="navbar-link">Odhlásit</a>
{% else %}
<a href="{{ path("user_login") }}" class="navbar-link">Přihlásit</a>
Expand Down
4 changes: 2 additions & 2 deletions app/Resources/views/user/login.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@


<form action="{{ path('user_login') }}" method="post" class="form-signin">
<label for="inputEmail" class="sr-only">E-mail</label>
<input type="email" id="inputEmail" class="form-control" placeholder="E-mail" name="_username" value="{{ last_username }}" required autofocus>
<label for="inputUsername" class="sr-only">Uživatelské jméno</label>
<input type="text" id="inputUsername" class="form-control" placeholder="Uživatelské jméno" name="_username" value="{{ last_username }}" required autofocus>
<label for="inputPassword" class="sr-only">Heslo</label>
<input type="password" id="inputPassword" class="form-control" placeholder="Heslo" name="_password" required>
<input type="hidden" name="_csrf_token" value="{{ csrf_token('authenticate') }}"/>
Expand Down
7 changes: 7 additions & 0 deletions app/Resources/views/user/password.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% extends 'base.html.twig' %}

{% block body %}
{{ form_start(form) }}
{{ form_end(form) }}
{% endblock %}

41 changes: 41 additions & 0 deletions app/Resources/views/user/profile.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{% extends 'base.html.twig' %}

{% block body %}
<h1> {{ user.username }}</h1>
<dl class="dl-horizontal">
<dt>Jméno a příjmení</dt>
<dd>{{ user.name }}</dd>
<dt>Email</dt>
<dd>{{ user.email }}</dd>
<dt>Telefoní číslo</dt>
<dd>{{ user.phone }}</dd>
</dl>

<ul class="nav nav-pills">
<li><a href="{{ path("user_profile_edit") }}" class="navbar-link">Úprava profilu</a></li>
<li><a href="{{ path("user_password") }}" class="navbar-link">Změna hesla</a></li>
<li><a href="{{ path("address_add") }}" class="navbar-link">Přidat adresu</a></li>
</ul>

{% if addresses is not empty %}
<h2>Adresy</h2>
<table class="table table-striped">
<tr>
<td></td>
<td>Název</td>
<td>Ulice</td>
<td>Město</td>
<td>PSČ</td>
</tr>
{% for address in addresses %}
<tr>
<td>{{ address.id }}</td>
<td>{{ address.name }}</td>
<td>{{ address.street }}</td>
<td>{{ address.city }}</td>
<td>{{ address.zip }}</td>
</tr>
{% endfor %}
</table>
{% endif %}
{% endblock %}
6 changes: 6 additions & 0 deletions app/Resources/views/user/profile_edit.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends 'base.html.twig' %}

{% block body %}
{{ form_start(form) }}
{{ form_end(form) }}
{% endblock %}
7 changes: 1 addition & 6 deletions app/Resources/views/user/registration.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,5 @@

{% block body %}
{{ form_start(form) }}
{{ form_row(form.username) }}
{{ form_row(form.plainPassword.first) }}
{{ form_row(form.plainPassword.second) }}
<br />
<button class="btn btn-lg btn-primary btn-block" type="submit">Registrovat!</button>
{{ form_end(form) }}
{% endblock %}
{% endblock %}
6 changes: 5 additions & 1 deletion app/config/security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ security:
# The route name the user can go to in order to logout
path: user_logout
# The name of the route to redirect to after logging out
target: homepage
target: homepage

access_control:
- { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/uzivatel/*, roles: IS_AUTHENTICATED_FULLY }
18 changes: 18 additions & 0 deletions app/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ services:
class: AppBundle\Controller\UserController
autowire: true

app.controller.address_controller:
class: AppBundle\Controller\AddressController
autowire: true

app.facade.category_facade:
class: AppBundle\Facade\CategoryFacade
autowire: true
Expand All @@ -32,6 +36,10 @@ services:
class: AppBundle\Facade\UserFacade
autowire: true

app.facade.address_facade:
class: AppBundle\Facade\AddressFacade
autowire: true

app.repository.category_repository:
class: AppBundle\Repository\CategoryRepository
factory: ['@doctrine.orm.default_entity_manager', getRepository]
Expand All @@ -42,6 +50,16 @@ services:
factory: ['@doctrine.orm.default_entity_manager', getRepository]
arguments: ['AppBundle\Entity\Product']

app.repository.address_repository:
class: AppBundle\Repository\AddressRepository
factory: ['@doctrine.orm.default_entity_manager', getRepository]
arguments: ['AppBundle\Entity\Address']

app.repository.user_address_repository:
class: AppBundle\Repository\UserAddressRepository
factory: ['@doctrine.orm.default_entity_manager', getRepository]
arguments: ['AppBundle\Entity\UserAddress']

encoder:
class: Symfony\Component\Security\Core\Encoder\BCryptPasswordEncoder
arguments:
Expand Down
93 changes: 93 additions & 0 deletions src/AppBundle/Controller/AddressController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace AppBundle\Controller;

use AppBundle\Entity\Address;
use AppBundle\Entity\UserAddress;
use AppBundle\Facade\UserFacade;
use AppBundle\FormType\AddressFormType;
use Doctrine\ORM\EntityManager;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Form\FormFactory;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\Routing\RouterInterface;

/**
* @author Tomáš Linhart <[email protected]>
*
* @Route(service="app.controller.address_controller")
*/
class AddressController
{

/**
* @var UserFacade
*/
private $userFacade;

/**
* @var FormFactory
*/
private $formFactory;

/**
* @var EntityManager
*/
private $entityManager;

/**
* @var RouterInterface
*/
private $router;

public function __construct(
RouterInterface $router,
EntityManager $entityManager,
UserFacade $userFacade,
FormFactory $formFactory
) {
$this->userFacade = $userFacade;
$this->formFactory = $formFactory;
$this->entityManager = $entityManager;
$this->router = $router;
}

/**
* @Route("/uzivatel/pridat-adresu", name="address_add")
* @Template("address/add.html.twig")
*/
public function addressAddAction(Request $request)
{
$user = $this->userFacade->getUser();
if (!$user) {
throw new UnauthorizedHttpException("Přihlašte se");
}

$address = new Address();
$form = $this->formFactory->create(AddressFormType::class, $address);

// 2) handle the submit (will only happen on POST)
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {

$userAddress = new UserAddress();
$userAddress->setUser($user)->setAddress($address);

$this->entityManager->persist($address);
$this->entityManager->persist($userAddress);
$this->entityManager->flush();

// TODO: message

return RedirectResponse::create($this->router->generate("user_profile"));
}

return [
"form" => $form->createView(),
];
}

}
102 changes: 95 additions & 7 deletions src/AppBundle/Controller/UserController.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php
namespace AppBundle\Controller;
use AppBundle\Entity\User;
use AppBundle\Facade\AddressFacade;
use AppBundle\Facade\UserFacade;
use AppBundle\FormType\PasswordFormType;
use AppBundle\FormType\ProfileFormType;
use AppBundle\FormType\RegistrationFormType;
use Doctrine\ORM\EntityManager;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
Expand All @@ -22,6 +25,7 @@
class UserController
{
private $userFacade;
private $addressFacade;
private $formFactory;
private $passwordEncoder;
private $entityManager;
Expand All @@ -32,13 +36,15 @@ public function __construct(
FormFactory $formFactory,
PasswordEncoderInterface $passwordEncoder,
EntityManager $entityManager,
RouterInterface $router
RouterInterface $router,
AddressFacade $addressFacade
) {
$this->userFacade = $userFacade;
$this->formFactory = $formFactory;
$this->passwordEncoder = $passwordEncoder;
$this->entityManager = $entityManager;
$this->router = $router;
$this->addressFacade = $addressFacade;
}

/**
Expand Down Expand Up @@ -98,19 +104,101 @@ public function logoutAction()
{}

/**
* @Route("/uzivatel/super-tajna-stranka", name="supersecret")
* @Template("user/supersecret.html.twig")
* @Route("/uzivatel/zmena-hesla", name="user_password")
* @Template("user/password.html.twig")
*
* @return array
*/
public function superSecretAction()
public function passwordAction(Request $request)
{
if (!$this->userFacade->getUser()) {
$user = $this->userFacade->getUser();
if (!$user) {
throw new UnauthorizedHttpException("Přihlašte se");
}

$form = $this->formFactory->create(PasswordFormType::class, $user);

// 2) handle the submit (will only happen on POST)
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if($this->passwordEncoder->isPasswordValid(
$user->getPassword(),
$form->get("password")->getData(),
$user->getSalt()
)) {
$newPassword = $this->passwordEncoder->encodePassword(
$form->get("plainPassword")->getData(),
$user->getSalt()
);
$user->setPassword($newPassword);

$this->entityManager->persist($user);
$this->entityManager->flush();

// TODO: message

return RedirectResponse::create($this->router->generate("user_profile"));
} else {
// TODO: message
}
}

return [
"form" => $form->createView(),
"user" => $user,
];
}

/**
* @Route("/uzivatel/uprava-udaju", name="user_profile_edit")
* @Template("user/profile_edit.html.twig")
*
* @return array
*/
public function profileEditAction(Request $request)
{
$user = $this->userFacade->getUser();
if (!$user) {
throw new UnauthorizedHttpException("Přihlašte se");
}

$form = $this->formFactory->create(ProfileFormType::class, $user);

// 2) handle the submit (will only happen on POST)
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {

$this->entityManager->persist($user);
$this->entityManager->flush();

// TODO: message

return RedirectResponse::create($this->router->generate("user_profile"));
}

return [
"form" => $form->createView(),
"user" => $user,
];
}

/**
* @Route("/uzivatel", name="user_profile")
* @Template("user/profile.html.twig")
*
* @return array
*/
public function profileAction(Request $request)
{
$user = $this->userFacade->getUser();
if (!$user) {
throw new UnauthorizedHttpException("Přihlašte se");
}

return [
"user" => $this->userFacade->getUser(),
"user" => $user,
"addresses" => $this->addressFacade->getAllUserAddresses($user),
];
}

}
}
Loading