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

Úkol 3 - Vojtěch Havlíček #30

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
10 changes: 10 additions & 0 deletions app/Resources/views/user/changepassword.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends 'base.html.twig' %}

{% block body %}
{{ form_start(form) }}
{{ form_row(form.plainPassword.first) }}
{{ form_row(form.plainPassword.second) }}
<br />
<button class="btn btn-lg btn-primary btn-block" type="submit">Změnit heslo</button>
{{ form_end(form) }}
{% endblock %}
1 change: 1 addition & 0 deletions app/Resources/views/user/profile.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<div class="row">
<div class="col-sm-6 col-lg-6 col-md-6">
<h3>Osobní údaje</h3>
<p><a href="{{ path("user_changepassword") }}">Změnit heslo</a></p>
{{ form_start(form) }}
{{ form_row(form.username) }}
{{ form_row(form.name) }}
Expand Down
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: /prihlasit, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: /uzivatel/*, roles: IS_AUTHENTICATED_FULLY }
13 changes: 0 additions & 13 deletions app/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ services:
class: AppBundle\Controller\UserController
autowire: true

app.controller.adress_controller:
class: AppBundle\Controller\AdressController
autowire: true

app.facade.adress_facade:
class: AppBundle\Facade\AdressFacade
autowire: true

app.facade.category_facade:
class: AppBundle\Facade\CategoryFacade
autowire: true
Expand All @@ -50,11 +42,6 @@ services:
factory: ['@doctrine.orm.default_entity_manager', getRepository]
arguments: ['AppBundle\Entity\Product']

app.repository.adress_repository:
class: AppBundle\Repository\AdressRepository
factory: ['@doctrine.orm.default_entity_manager', getRepository]
arguments: ['AppBundle\Entity\Adress']

encoder:
class: Symfony\Component\Security\Core\Encoder\BCryptPasswordEncoder
arguments:
Expand Down
106 changes: 69 additions & 37 deletions src/AppBundle/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use AppBundle\FormType\RegistrationFormType;
use AppBundle\FormType\ProfileFormType;
use AppBundle\FormType\AddressFormType;
use AppBundle\FormType\ChangePasswordFormType;
use Doctrine\ORM\EntityManager;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
Expand Down Expand Up @@ -44,43 +45,74 @@ public function __construct(
$this->router = $router;
}

/**
* @Route("/registrovat", name="user_registration")
* @Template("user/registration.html.twig")
*
* @param Request $request
* @return RedirectResponse|array
*/
public function registrationAction(Request $request)
{
// 1) build the form
$user = new User();
$form = $this->formFactory->create(RegistrationFormType::class, $user);

// 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
return RedirectResponse::create($this->router->generate("homepage"));
}
/**
* @Route("/registrovat", name="user_registration")
* @Template("user/registration.html.twig")
*
* @param Request $request
* @return RedirectResponse|array
*/
public function registrationAction(Request $request)
{
// 1) build the form
$user = new User();
$form = $this->formFactory->create(RegistrationFormType::class, $user);

return [
"form" => $form->createView(),
];
}
// 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
return RedirectResponse::create($this->router->generate("homepage"));
}

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

/**
* @Route("/uzivatel/zmenit-heslo", name="user_changepassword")
* @Template("user/changepassword.html.twig")
*
* @param Request $request
* @return RedirectResponse|array
*/
public function changepasswordAction(Request $request)
{
$user = $this->userFacade->getUser();
$form = $this->formFactory->create(ChangePasswordFormType::class, $user);

$form->handleRequest($request);
if ($form->isSubmitted()) {

$user->setPassword(
$this->passwordEncoder->encodePassword($user->getPlainPassword(), null)
);

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

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

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

/**
* @Route("/prihlasit", name="user_login")
* @Template("user/login.html.twig")
*
Expand Down Expand Up @@ -117,7 +149,7 @@ public function superSecretAction()
}

/**
Copy link
Collaborator

Choose a reason for hiding this comment

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

Taky udělal bych na to separátní Controller.

* @Route("/profil", name="user_profile")
* @Route("/uzivatel/profil", name="user_profile")
* @Template("user/profile.html.twig")
*
* @param Request $request
Expand Down Expand Up @@ -146,7 +178,7 @@ public function profileAction(Request $request)
}

/**
* @Route("/pridat-adresu", name="add_address")
* @Route("/uzivatel/pridat-adresu", name="add_address")
* @Template("user/addaddress.html.twig")
*
* @param Request $request
Expand Down
43 changes: 43 additions & 0 deletions src/AppBundle/FormType/ChangePasswordFormType
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
namespace AppBundle\FormType;

use AppBundle\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @author Vašek Boch <vasek.boch@live.com>
* @author Jan Klat <jenik@klatys.cz>
*/
class ChangePasswordFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add("plainPassword", RepeatedType::class, [
"type" => PasswordType::class,
"first_options" => [
"label" => "Heslo",
"attr" => [
"class" => "form-control",
],
],
"second_options" => [
"label" => "Heslo znova",
"attr" => [
"class" => "form-control",
],
],
]);
}

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