Skip to content

Úkol 3 - Lubomír Jiřišta #29

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 5 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
/vendor/
/web/bundles/
composer.lock
composer.phar
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
6 changes: 4 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,10 @@


<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="inputEmail" 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
23 changes: 23 additions & 0 deletions app/Resources/views/user/password.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{% extends 'base.html.twig' %}

{% block body %}
{% for flash_message in app.session.flashBag.get('error') %}
<div class="col-md-12">
<div class="alert alert-danger">
{{ flash_message }}
</div>
</div>
{% endfor %}
{% for flash_message in app.session.flashBag.get('success') %}
<div class="col-md-12">
<div class="alert alert-success">
{{ flash_message }}
</div>
</div>
{% endfor %}
{{ form_start(form, {'attr': {'novalidate': 'novalidate'}}) }}
{{ form_widget(form) }}
<br />
<button class="btn btn-lg btn-primary btn-block" type="submit">Změnit heslo</button>
{{ form_end(form) }}
{% endblock %}
9 changes: 9 additions & 0 deletions app/Resources/views/user/profile.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% extends 'base.html.twig' %}

{% block body %}
{{ form_start(form, {'attr': {'novalidate': 'novalidate'}}) }}
{{ form_widget(form) }}
<br />
<button class="btn btn-lg btn-primary btn-block" type="submit">Uložit</button>
{{ form_end(form) }}
{% endblock %}
7 changes: 6 additions & 1 deletion app/Resources/views/user/registration.html.twig
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
{% extends 'base.html.twig' %}

{% block body %}
{{ form_start(form) }}
{{ form_start(form, {'attr': {'novalidate': 'novalidate'}}) }}
{{ form_row(form.username) }}
{{ form_row(form.plainPassword.first) }}
{{ form_row(form.plainPassword.second) }}
<br /><br />
{{ form_row(form.fullName) }}
{{ form_row(form.address) }}
{{ form_row(form.phone) }}
{{ form_row(form.email) }}
<br />
<button class="btn btn-lg btn-primary btn-block" type="submit">Registrovat!</button>
{{ form_end(form) }}
Expand Down
7 changes: 6 additions & 1 deletion app/config/security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ security:

anonymous: true

provider: database_users

form_login:
check_path: user_login
login_path: user_login
Expand All @@ -24,4 +26,7 @@ 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: ^/uzivatel, roles: IS_AUTHENTICATED_REMEMBERED }
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"symfony/polyfill-apcu": "^1.0",
"sensio/distribution-bundle": "^5.0",
"sensio/framework-extra-bundle": "^3.0.2",
"incenteev/composer-parameter-handler": "^2.0"
"incenteev/composer-parameter-handler": "^2.0",
"symfony/intl": "^3.1"
},
"require-dev": {
"sensio/generator-bundle": "^3.0",
Expand Down
90 changes: 90 additions & 0 deletions src/AppBundle/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
namespace AppBundle\Controller;
use AppBundle\Entity\User;
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 @@ -12,6 +14,7 @@
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;


/**
Expand Down Expand Up @@ -91,6 +94,93 @@ public function loginAction()
];
}

/**
* @Route("/uzivatel/profil", name="user_profile")
* @Template("user/profile.html.twig")
*
* @param Request $request
* @return RedirectResponse|array
*/
public function profileAction(Request $request)
{
$user = $this->userFacade->getUser();

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

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

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

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

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

/**
* @Route("/uzivatel/heslo", name="user_password")
* @Template("user/password.html.twig")
*
* @param Request $request
* @return RedirectResponse|array
*/
public function passwordAction(Request $request)
{
$user = $this->userFacade->getUser();

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

$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$oldPasswordPlain = $form->get('oldPassword')->getData();
$newPasswordPlain = $form->get('plainPassword')->getData();

if (empty(trim($oldPasswordPlain)) || empty(trim($newPasswordPlain))) {
$request->getSession()->getFlashBag()->add(
'error',
'Staré nebo nové heslo nebylo vyplněno..'
);
return [
"form" => $form->createView(),
"user" => $this->userFacade->getUser(),
];
}

if($this->passwordEncoder->isPasswordValid(
$user->getPassword(),
$oldPasswordPlain,
$user->getSalt()
)) {
$newPassword = $this->passwordEncoder->encodePassword($newPasswordPlain, $user->getSalt());
$user->setPassword($newPassword);

$this->entityManager->persist($user);
$this->entityManager->flush();
$request->getSession()->getFlashBag()->add(
'success',
'Vaše heslo bylo změněno!'
);
} else {
$request->getSession()->getFlashBag()->add(
'error',
'Vaše staré heslo se neshoduje. Zkuste to prosím znovu!'
);
}

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

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

/**
* @Route("/odhlasit", name="user_logout")
*/
Expand Down
127 changes: 125 additions & 2 deletions src/AppBundle/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ class User implements UserInterface
private $id;

/**
* @ORM\Column(type="string", length=255, unique=true, name="email")
* @ORM\Column(type="string", length=255, unique=true, name="username")
* @Assert\Blank(groups={"onlyPassword"})
* @Assert\NotBlank()
* @Assert\Email()
*/
private $username;

/**
* @ORM\Column(type="string", length=64)
* @Assert\NotBlank(groups={"onlyProfile"})
*/
private $password;

Expand All @@ -41,6 +42,32 @@ class User implements UserInterface
*/
private $plainPassword;

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

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

/**
* @ORM\Column(type="string", length=13, name="phone", nullable=true)
* @Assert\Regex(
* pattern="/^(\+420)? ?[1-9][0-9]{2} ?[0-9]{3} ?[0-9]{3}$/i",
* htmlPattern="^(\+420)? ?[1-9][0-9]{2} ?[0-9]{3} ?[0-9]{3}$",
* message="Zkontrolujte formát telefonního čísla (+420 xxx xxx xxx)"
* )
*/
private $phone;

/**
* @ORM\Column(type="string", length=100, unique=true, name="email", nullable=true)
* @Assert\Email()
*/
private $email;

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


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

return $this;
}

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

/**
* 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 email
*
* @param string $email
*
* @return User
*/
public function setEmail($email)
{
$this->email = $email;

return $this;
}

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

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

return $this;
}

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