Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //login.html.twig -------------------------------------
- <form action=" {{ path ( '_login_check' ) }} " method="post" class="form_login">
- <ul>
- <li class="user"><input type="text" name="_username" class="input_login" value="{{ last_username }}" /></li>
- <li class="password"><input type="password" name="_password" class="input_login" /></li>
- <li><input class="sub_log" name="login" type="submit" /></li>
- </ul>
- </form>
- //config.yml -------------------------------------
- encoders:
- Bundle\SecurityBundle\Entity\User: sha1
- providers:
- main:
- password_encoder: sha1
- entity: { class: SecurityBundle:User, property: username }
- firewalls:
- profiler:
- pattern: /_profiler/.*
- security: false
- main:
- pattern: /.*
- anonymous: true
- form_login:
- always_use_default_target_path: true
- login_path: /
- logout: { path: /logout, target: / }
- access_control:
- - { path: /top, role: ROLE_ADMIN }
- - { path: /.*, role: IS_AUTHENTICATED_ANONYMOUSLY }
- /entity/User.php -------------------------------------
- use Symfony\Component\Security\User\AccountInterface;
- /**
- * @orm:Entity
- */
- class User implements AccountInterface
- {
- /**
- * @orm:Id
- * @orm:Column(type="integer")
- * @orm:GeneratedValue(strategy="IDENTITY")
- */
- protected $id;
- /**
- * @orm:Column(type="string", length="255")
- */
- protected $password;
- /**
- * @orm:Column(type="string", length="255")
- */
- protected $username;
- /**
- * @orm:Column(type="string", length="255")
- */
- protected $name;
- public function getId()
- {
- return $this->id;
- }
- public function setName($name)
- {
- $this->name = $name;
- }
- public function getName()
- {
- return $this->name;
- }
- public function setUsername($username)
- {
- $this->username = $username;
- }
- public function getUsername()
- {
- return $this->username;
- }
- // Acount interface
- public function __toString(){
- return $this->getUsername();
- }
- public function getRoles(){
- return array('ROLE_ADMIN');
- }
- public function eraseCredentials(){
- }
- public function getSalt(){
- return '';
- }
- public function getPassword(){
- return $this->password;
- }
- public function setPassword($password){
- $this->password = $password;
- }
- public function equals(AccountInterface $account){
- if($this->getUsername() == $account->getUsername()){
- return true;
- }
- return false;
- }
- }
- /controller/SecurityController.php -------------------------------------
- namespace Bundle\SecurityBundle\Controller;
- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
- use Symfony\Component\Security\SecurityContext;
- use Bundle\SecurityBundle\Document;
- use Bundle\SecurityBundle\Entity\User;
- use Bundle\SecurityBundle\Entity\UserRepository;
- class SecurityController extends Controller {
- public function loginAction (){
- // get the error if any (works with forward and redirect -- see below)
- if ( $this -> get ( 'request' ) -> attributes -> has ( SecurityContext :: AUTHENTICATION_ERROR )) {
- $error = $this -> get ( 'request' ) -> attributes -> get ( SecurityContext :: AUTHENTICATION_ERROR );
- } else {
- $error = $this -> get ( 'request' ) -> getSession () -> get ( SecurityContext :: AUTHENTICATION_ERROR );
- }
- return $this -> render ( 'SecurityBundle:Security:login.html.twig' , array (
- // last username entered by the user
- 'last_username' => $this -> get ( 'request' ) -> getSession () -> get ( SecurityContext :: LAST_USERNAME ),
- 'error' => $error ,
- ));
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement