Last active
April 7, 2021 15:28
-
-
Save NBoulfroy/d97e4134ee5895259b4b48b3d11dfb40 to your computer and use it in GitHub Desktop.
[Login system] A login system for Symfony with Doctrine without FosUserBundle #PHP #symfony4 #doctrine2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends 'base.html.twig' %} | |
{% block title %}{{ parent() }} - connexion{% endblock %} | |
{% block stylesheets %} | |
{{ parent() }} | |
{% endblock %} | |
{% block content %} | |
<div class="container"> | |
<div class="mt-5"> | |
<div class="col offset col-md-6 offset-md-3"> | |
{# Error message displaying #} | |
{% if error %} | |
<div class="alert alert-danger alert-dismissible fade show text-center"> | |
{{ error.messageKey|trans(error.messageData, 'security') }} | |
<button type="button" class="close" data-dismiss="alert" aria-label="Close"> | |
<span aria-hidden="true">×</span> | |
</button> | |
</div> | |
{% endif %} | |
<div class="card"> | |
<div class="card-body"> | |
<h5 class="card-title text-center">Connexion à la console</h5> | |
<div class="card-text"> | |
<form method="post" action="{{ path('security_login') }}" name="form_login"> | |
<div class="row"> | |
<div class="col-sm-6 col-md-6 col-lg-6 col-xl-6"> | |
<div class="form-group"> | |
<label for="username" class="sr-only">Identifiant</label> | |
<input type="text" value="{{ last_username }}" name="_username" | |
id="username" class="form-control" placeholder="Identifiant" | |
required autofocus /> | |
</div> | |
</div> | |
<div class="col-sm-6 col-md-6 col-lg-6 col-xl-6"> | |
<div class="form-group"> | |
<label for="password" class="sr-only">Password</label> | |
<input type="password" name="_password" id="password" class="form-control" | |
placeholder="Mot de passe" required /> | |
</div> | |
</div> | |
</div> | |
<input type="hidden" name="_csrf_token" value="{{ csrf_token('authenticate') }}" /> | |
<div class="row"> | |
<div class="col-sm-2 offset-sm-5 col-md-4 offset-md-4 col-lg-2 offset-lg-5 col-xl-2 offset-xl-5"> | |
<button class="btn btn-danger" type="submit"> | |
Connexion | |
</button> | |
</div> | |
</div> | |
</form> | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> | |
{% endblock %} | |
{% block javascripts %} | |
{{ parent() }} | |
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
security_login: | |
path: /login | |
controller: App\Controller\SecurityController::securityLoginAction | |
methods: [GET, POST] | |
security_logout: | |
path: /logout | |
controller: App\Controller\SecurityController::securityLogoutAction | |
methods: GET |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
security: | |
encoders: | |
App\Entity\User: | |
algorithm: bcrypt | |
providers: | |
doctrine_provider: | |
entity: | |
class: App\Entity\User | |
property: username # email | |
firewalls: | |
dev: | |
pattern: ^/(_(profiler|wdt)|css|images|js)/ | |
security: false | |
main: | |
anonymous: ~ | |
form_login: | |
login_path: /login | |
check_path: /login | |
provider: doctrine_provider | |
always_use_default_target_path: true | |
default_target_path: / | |
logout: | |
path: /logout | |
target: /login | |
access_control: | |
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY } | |
- { path: ^/, roles: [ROLE_ADMIN, ROLE_USER] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* SecurityController. | |
* | |
* @Project : console_prospeneo | |
* @File : SecurityController.php | |
* @Author : BOULFROY Nicolas | |
* @Create : 2019/09/27 | |
*/ | |
namespace App\Controller; | |
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | |
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; | |
class SecurityController extends AbstractController | |
{ | |
public function securityLoginAction(AuthenticationUtils $authenticationUtils) | |
{ | |
$error = $authenticationUtils->getLastAuthenticationError(); | |
$lastUsername = $authenticationUtils->getLastUsername(); | |
return $this->render('security/login.html.twig', [ | |
'last_username' => $lastUsername, | |
'error' => $error | |
]); | |
} | |
public function securityLogoutAction() {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* User entity. | |
* | |
* @Project : console_prospeneo | |
* @File : User.php | |
* @Author : BOULFROY Nicolas | |
* @Create : 2019/09/27 | |
*/ | |
namespace App\Model\SQLite\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
use Symfony\Component\Security\Core\User\UserInterface; | |
use \Serializable; | |
/** | |
* Class User | |
* | |
* @package App\Entity | |
* @ORM\Entity(repositoryClass="App\Repository\UserRepository") | |
*/ | |
class User implements UserInterface, Serializable | |
{ | |
/** @var array ROLE_ADMIN */ | |
public const ROLE_ADMIN = array('ROLE_ADMIN'); | |
/** @var array ROLE_USER */ | |
public const ROLE_USER = array('ROLE_USER'); | |
/** | |
* @var int $id | |
* | |
* @ORM\Id | |
* @ORM\GeneratedValue | |
* @ORM\Column(type="integer") | |
*/ | |
private $id; | |
/** | |
* @var string $email | |
* | |
* @ORM\Column(name="email", type="string", length=255, nullable=false, unique=true) | |
*/ | |
private $email; | |
/** | |
* @var string $username | |
* | |
* @ORM\Column(name="username", type="string", length=20, nullable=true, unique=true) | |
*/ | |
private $username; | |
/** | |
* @var string $password | |
* | |
* @ORM\Column(name="password", type="string", length=255, nullable=false) | |
*/ | |
private $password; | |
/** | |
* @var array $roles | |
* | |
* @ORM\Column(name="roles", type="array", nullable=false) | |
*/ | |
private $roles; | |
/** | |
* @var bool $active | |
* | |
* @ORM\Column(name="active", type="boolean", nullable=true, options={"default": false}) | |
*/ | |
private $active; | |
/** | |
* @var string|null $token | |
* | |
* @ORM\Column(name="token", type="string", length=255, nullable=true) | |
*/ | |
private $token; | |
/** | |
* @return int | |
*/ | |
public function getId(): int | |
{ | |
return $this->id; | |
} | |
/** | |
* @return string | |
*/ | |
public function getEmail(): string | |
{ | |
return $this->email; | |
} | |
/** | |
* @param string $email | |
* | |
* @return self | |
*/ | |
public function setEmail(string $email): self | |
{ | |
$this->email = $email; | |
return $this; | |
} | |
/** | |
* @return string | |
*/ | |
public function getUsername(): string | |
{ | |
return $this->username; | |
} | |
/** | |
* @param string $username | |
* | |
* @return self | |
*/ | |
public function setUsername(string $username): self | |
{ | |
$this->username = $username; | |
return $this; | |
} | |
/** | |
* @return string | |
*/ | |
public function getPassword(): string | |
{ | |
return $this->password; | |
} | |
/** | |
* @param string $password | |
* | |
* @return self | |
*/ | |
public function setPassword(string $password): self | |
{ | |
$this->password = $password; | |
return $this; | |
} | |
/** | |
* @return array | |
*/ | |
public function getRoles(): array | |
{ | |
return $this->roles; | |
} | |
/** | |
* @param array $roles | |
* | |
* @return self | |
*/ | |
public function setRoles(array $roles): self | |
{ | |
$this->roles = $roles; | |
return $this; | |
} | |
/** | |
* @return bool | |
*/ | |
public function isActive(): bool | |
{ | |
return $this->active; | |
} | |
/** | |
* @param bool $active | |
* | |
* @return self | |
*/ | |
public function setActive(bool $active): self | |
{ | |
$this->active = $active; | |
return $this; | |
} | |
/** | |
* @return string|null | |
*/ | |
public function getToken(): ?string | |
{ | |
return $this->token; | |
} | |
/** | |
* @param string $token | |
* | |
* @return self | |
*/ | |
public function setToken(string $token): self | |
{ | |
$this->token = $token; | |
return $this; | |
} | |
/** | |
* @return string | |
*/ | |
public function serialize(): string | |
{ | |
return serialize([ | |
$this->id, | |
$this->username, | |
$this->email, | |
$this->password, | |
$this->roles, | |
]); | |
} | |
/** | |
* @param string $string | |
*/ | |
public function unserialize($string): void | |
{ | |
list( | |
$this->id, | |
$this->username, | |
$this->email, | |
$this->password | |
) = unserialize($string, [ | |
[ | |
'allowed_classes' => false | |
] | |
]); | |
} | |
public function eraseCredentials() {} | |
public function getSalt() {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment