Skip to content
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

Ukol 3 - Vojtěch Bartoš #27

Open
wants to merge 2 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
12 changes: 12 additions & 0 deletions app/Resources/views/user/profile.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends 'base.html.twig' %}

{% block body %}
{{ form_start(form) }}
{{ form_row(form.name) }}
{{ form_row(form.phone) }}
{{ form_row(form.username) }}
{{ form_row(form.address) }}
<br />
<button class="btn btn-lg btn-primary btn-block" type="submit">Uložit</button>
{{ form_end(form) }}
{% endblock %}
21 changes: 20 additions & 1 deletion app/config/security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@ security:
entity: { class: AppBundle:User, property: username }

firewalls:
user_area:
pattern: ^/profile

form_login:
check_path: user_login
login_path: user_login
csrf_token_generator: security.csrf.token_manager
default_target_path: /

logout:
# 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

context: primary_auth
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Našel jsem si, že lze sdílet context mezi více firewally. Nelíbí se mi ale, že musím u obou konfigurovat form_login i logout a jediné v čem se vlastně liší je, že tady chybí anonymous. Lze to napsat i úsporněji?


secured_area:
pattern: ^/

Expand All @@ -24,4 +41,6 @@ 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

context: primary_auth
37 changes: 37 additions & 0 deletions src/AppBundle/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace AppBundle\Controller;
use AppBundle\Entity\User;
use AppBundle\Facade\UserFacade;
use AppBundle\FormType\PersonalInfoFormType;
use AppBundle\FormType\RegistrationFormType;
use Doctrine\ORM\EntityManager;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
Expand Down Expand Up @@ -91,6 +92,42 @@ public function loginAction()
];
}

/**
* @Route("/profile", name="user_profile")
* @Template("user/profile.html.twig")
*
* @param Request $request
* @return array|RedirectResponse
*/
public function profileAction(Request $request)
{
// 1) build the form
$user = $this->userFacade->getUser();
$form = $this->formFactory->create(PersonalInfoFormType::class, $user);

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

if ($address = $user->getAddress()) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je dobře volat pokaždé persist & flush ?

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

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

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

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

/**
* @Route("/odhlasit", name="user_logout")
*/
Expand Down
98 changes: 98 additions & 0 deletions src/AppBundle/Entity/Address.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @ORM\Entity
*/
class Address
{
/**
* @var int
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @var int
*/
private $id;

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

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

/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank()
* @var string
*/
private $zip;

/**
* @return int
*/
public function getId()
{
return $this->id;
}

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

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

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

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

/**
* @return string
*/
public function getZip()
{
return $this->zip;
}

/**
* @param string $zip
*/
public function setZip(string $zip)
{
$this->zip = $zip;
}
}
67 changes: 66 additions & 1 deletion src/AppBundle/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,25 @@ class User implements UserInterface
private $password;

/**
* @Assert\NotBlank()
* @ORM\Column(type="string", length=255, name="name", nullable=true)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nejsem si jistý, jak správně řešit tuto validaci když pro registraci chci aby to nebylo null ale pro editaci adresy to null naopak chci..

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

/**
* @ORM\Column(type="string", length=20, name="phone", nullable=true)
* @Assert\Length(max=4096)
*/
private $phone;

/**
* @ORM\OneToOne(targetEntity="Address")
* @ORM\JoinColumn(name="address_id", referencedColumnName="id")
* @var Address
*/
private $address;

/**
* @Assert\Length(max=4096)
*/
private $plainPassword;
Expand Down Expand Up @@ -129,4 +147,51 @@ public function eraseCredentials()
return;
}

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

/**
* @param mixed $name
*/
public function setName($name)
{
$this->name = $name;
}

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

/**
* @param mixed $phone
*/
public function setPhone($phone)
{
$this->phone = $phone;
}

/**
* @return Address
*/
public function getAddress()
{
return $this->address;
}

/**
* @param Address $address
*/
public function setAddress($address)
{
$this->address = $address;
}
}
42 changes: 42 additions & 0 deletions src/AppBundle/FormType/AddressFormType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace AppBundle\FormType;

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

class AddressFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('street', TextType::class, [
"label" => "Ulice",
"attr" => [
"class" => "form-control",
],
])
->add('city', TextType::class, [
"label" => "Město",
"attr" => [
"class" => "form-control",
],
])
->add('zip', TextType::class, [
"label" => "PSČ",
"attr" => [
"class" => "form-control",
],
]);
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
"data_class" => Address::class,
));
}
}
44 changes: 44 additions & 0 deletions src/AppBundle/FormType/PersonalInfoFormType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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 PersonalInfoFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add("name", TextType::class, [
"label" => "Jméno",
"attr" => [
"class" => "form-control",
],
])
->add("phone", TextType::class, [
"label" => "Telefon",
"attr" => [
"class" => "form-control",
],
])
->add("username", EmailType::class, [
"label" => "E-mail",
"attr" => [
"class" => "form-control",
],
])
->add('address', AddressFormType::class);
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
"data_class" => User::class,
));
}
}