Skip to content

Ukol 3 - Lukas Vraný #26

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
2 changes: 1 addition & 1 deletion app/Resources/views/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
</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_logout") }}" class="navbar-link">Odhlásit</a>
{% else %}
<a href="{{ path("user_login") }}" class="navbar-link">Přihlásit</a>
Expand Down
14 changes: 14 additions & 0 deletions app/Resources/views/user/profile.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends 'base.html.twig' %}

{% block body %}
{{ form_start(form) }}
{{ form_row(form.name) }}
{{ form_row(form.street) }}
{{ form_row(form.city) }}
{{ form_row(form.psc) }}
{{ form_row(form.username) }}
{{ form_row(form.phone) }}
<br />
<button class="btn btn-lg btn-primary btn-block" type="submit">Uložit!</button>
{{ form_end(form) }}
{% endblock %}
58 changes: 33 additions & 25 deletions src/AppBundle/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
use AppBundle\Entity\User;
use AppBundle\Facade\UserFacade;
use AppBundle\FormType\RegistrationFormType;
use Doctrine\ORM\EntityManager;
use AppBundle\FormType\UserEditFormType;
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;
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
use AppBundle\Service\UserService;


/**
Expand All @@ -22,23 +22,20 @@
class UserController
{
private $userFacade;
private $formFactory;
private $passwordEncoder;
private $entityManager;
private $formFactory;
private $router;
private $userService;

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

/**
Expand All @@ -56,19 +53,8 @@ public function registrationAction(Request $request)

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

// 3) Encode the password (you could also do this via Doctrine listener)
$user->setPassword(
$this->passwordEncoder->encodePassword($user->getPlainPassword(), null)
);

// 4) save the User!
$this->entityManager->persist($user);
$this->entityManager->flush();

// ... do any other work - like sending them an email, etc
// maybe set a "flash" success message for the user
if ($form->isSubmitted() && $form->isValid()) {
$this->userService->registerUser($user);
return RedirectResponse::create($this->router->generate("homepage"));
}

Expand All @@ -90,6 +76,28 @@ public function loginAction()
"error" => $this->userFacade->getAuthenticationError(),
];
}

/**
* @Route("/profil", name="user_profile")
* @Template("user/profile.html.twig")
*/
public function profileAction(Request $request){

$user = $this->userFacade->getUser();
$form = $this->formFactory->create(UserEditFormType::class, $user);

$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$this->userService->saveUser($user);
return RedirectResponse::create($this->router->generate("homepage"));
}

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

/**
* @Route("/odhlasit", name="user_logout")
Expand Down
153 changes: 151 additions & 2 deletions src/AppBundle/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,41 @@ class User implements UserInterface
*/
private $password;

/**
* @Assert\NotBlank()
/**
* @Assert\Length(max=4096)
*/
private $plainPassword;

/**
* @var string
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $name;

/**
* @var string
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $street;

/**
* @var string
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $city;

/**
* @var string
* @ORM\Column(type="string", length=64, nullable=true)
*/
private $phone;

/**
* @var string
* @ORM\Column(type="string", length=6, nullable=true)
*/
private $psc;

/**
* @return int
*/
Expand Down Expand Up @@ -129,4 +158,124 @@ public function eraseCredentials()
return;
}


/**
* Set name
*
* @param string $name
*
* @return User
*/
public function setName($name)
{
$this->name = $name;

return $this;
}

/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* Set street
*
* @param string $street
*
* @return User
*/
public function setStreet($street)
{
$this->street = $street;

return $this;
}

/**
* Get street
*
* @return string
*/
public function getStreet()
{
return $this->street;
}

/**
* Set city
*
* @param string $city
*
* @return User
*/
public function setCity($city)
{
$this->city = $city;

return $this;
}

/**
* Get city
*
* @return string
*/
public function getCity()
{
return $this->city;
}

/**
* Set phone
*
* @param string $phone
*
* @return User
*/
public function setPhone($phone)
{
$this->phone = $phone;

return $this;
}

/**
* Get phone
*
* @return string
*/
public function getPhone()
{
return $this->phone;
}

/**
* Set psc
*
* @param string $psc
*
* @return User
*/
public function setPsc($psc)
{
$this->psc = $psc;

return $this;
}

/**
* Get psc
*
* @return string
*/
public function getPsc()
{
return $this->psc;
}
}
61 changes: 61 additions & 0 deletions src/AppBundle/FormType/UserEditFormType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
namespace AppBundle\FormType;

use AppBundle\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class UserEditFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add("name", TextType::class, [
"label" => "Jméno",
"attr" => [
"class" => "form-control",
],
])
->add("street", TextType::class, [
"label" => "Ulice",
"attr" => [
"class" => "form-control",
],
])
->add("city", TextType::class, [
"label" => "Město",
"attr" => [
"class" => "form-control",
],
])
->add("psc", TextType::class, [
"label" => "PSČ",
"attr" => [
"class" => "form-control",
],
])
->add("phone", TextType::class, [
"label" => "Telefon",
"attr" => [
"class" => "form-control",
],
])
->add("username", EmailType::class, [
"label" => "Email",
"attr" => [
"class" => "form-control",
],
])
;
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
"data_class" => User::class,
));
}
}
41 changes: 41 additions & 0 deletions src/AppBundle/Service/UserService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace AppBundle\Service;

use Doctrine\ORM\EntityManager;
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;

/**
* @author Vašek Boch <[email protected]>
* @author Jan Klat <[email protected]>
*/
class UserService
{

private $entityManager;
private $passwordEncoder;

public function __construct(EntityManager $entityManager, PasswordEncoderInterface $passwordEncoder)
{
$this->entityManager = $entityManager;
$this->passwordEncoder = $passwordEncoder;
}

public function registerUser($user)
{
$user->setPassword(
$this->passwordEncoder->encodePassword($user->getPlainPassword(), null)
);

$this->saveUser($user);

// ... do any other work - like sending them an email, etc
// maybe set a "flash" success message for the user
}

public function saveUser($user){
$this->entityManager->persist($user);
$this->entityManager->flush();
}

}