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 - Aleš Kůdela #33

Open
wants to merge 1 commit 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: 5 additions & 1 deletion app/Resources/views/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
<a href="{{ path("homepage") }}">Domů</a>
<a href="{{ path("homepage") }}">Domů</a>
</li>
<li>
<a href="{{ path("supersecret") }}">Editace uživatele</a>
</li>

</ul>
<p class="navbar-text navbar-right">
{% if user is defined and user %}
Expand Down
17 changes: 16 additions & 1 deletion app/Resources/views/user/supersecret.html.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
{% extends 'base.html.twig' %}

{% block body %}
Hello {{ user.username }}
Hello {{ user.username }}

{{ form_start(form) }}
{{ form_row(form.username) }}
{{ form_row(form.phone) }}

{{ form_row(form.title) }}
{{ form_row(form.name) }}
{{ form_row(form.street) }}
{{ form_row(form.city) }}
{{ form_row(form.country) }}
{{ form_row(form.zip) }}
<br />
<button class="btn btn-lg btn-primary btn-block" type="submit">Uložit!</button>
{{ form_end(form) }}

{% endblock %}
209 changes: 113 additions & 96 deletions src/AppBundle/Controller/UserController.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php

namespace AppBundle\Controller;

use AppBundle\Entity\User;
use AppBundle\Facade\UserFacade;
use AppBundle\FormType\RegistrationFormType;
use AppBundle\FormType\UserEditFormType;
use Doctrine\ORM\EntityManager;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
Expand All @@ -13,104 +16,118 @@
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;


/**
* @author Vašek Boch <vasek.boch@live.com>
* @author Jan Klat <jenik@klatys.cz>
* @Route(service="app.controller.user_controller")
*/
class UserController
{
private $userFacade;
private $formFactory;
private $passwordEncoder;
private $entityManager;
private $router;

public function __construct(
UserFacade $userFacade,
FormFactory $formFactory,
PasswordEncoderInterface $passwordEncoder,
EntityManager $entityManager,
RouterInterface $router
) {
$this->userFacade = $userFacade;
$this->formFactory = $formFactory;
$this->passwordEncoder = $passwordEncoder;
$this->entityManager = $entityManager;
$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"));
}

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

/**
* @Route("/prihlasit", name="user_login")
* @Template("user/login.html.twig")
*
* @return RedirectResponse|array
*/
public function loginAction()
{
return [
"last_username" => $this->userFacade->getLastUsername(),
"error" => $this->userFacade->getAuthenticationError(),
];
}

/**
* @Route("/odhlasit", name="user_logout")
*/
public function logoutAction()
{}

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

}
class UserController {

private $userFacade;
private $formFactory;
private $passwordEncoder;
private $entityManager;
private $router;

public function __construct(
UserFacade $userFacade, FormFactory $formFactory, PasswordEncoderInterface $passwordEncoder, EntityManager $entityManager, RouterInterface $router
) {
$this->userFacade = $userFacade;
$this->formFactory = $formFactory;
$this->passwordEncoder = $passwordEncoder;
$this->entityManager = $entityManager;
$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"));
}

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

/**
* @Route("/prihlasit", name="user_login")
* @Template("user/login.html.twig")
*
* @return RedirectResponse|array
*/
public function loginAction() {
return [
"last_username" => $this->userFacade->getLastUsername(),
"error" => $this->userFacade->getAuthenticationError(),
];
}

/**
* @Route("/odhlasit", name="user_logout")
*/
public function logoutAction() {

}

/**
* @Route("/uzivatel/super-tajna-stranka", name="supersecret")
* @Template("user/supersecret.html.twig")
* @param Request $request
* @return array
*/
public function superSecretAction(Request $request) {
Copy link
Member

Choose a reason for hiding this comment

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

Tu action bych asi přejmenoval na userEditAction

if (!$this->userFacade->getUser()) {
throw new UnauthorizedHttpException("Přihlašte se");
}

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

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


// 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(),
"user" => $this->userFacade->getUser(),
];
}

}
Loading