Advertisement
Guest User

Untitled

a guest
Aug 15th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.11 KB | None | 0 0
  1. <?php
  2.  
  3. //login.html.twig -------------------------------------
  4.  
  5.         <form action=" {{ path ( '_login_check' ) }} " method="post" class="form_login">
  6.             <ul>
  7.                 <li class="user"><input type="text" name="_username" class="input_login" value="{{ last_username }}" /></li>
  8.                 <li class="password"><input type="password" name="_password" class="input_login" /></li>
  9.                 <li><input class="sub_log" name="login" type="submit" /></li>
  10.             </ul>
  11.         </form>
  12.  
  13. //config.yml -------------------------------------
  14.  
  15.     encoders:
  16.         Bundle\SecurityBundle\Entity\User: sha1
  17.  
  18.     providers:
  19.         main:
  20.             password_encoder: sha1
  21.             entity: { class: SecurityBundle:User, property: username }
  22.  
  23.     firewalls:
  24.         profiler:
  25.             pattern:    /_profiler/.*
  26.             security:  false
  27.  
  28.         main:
  29.             pattern:    /.*
  30.             anonymous: true
  31.             form_login:
  32.                 always_use_default_target_path: true
  33.                 login_path: /
  34.             logout:     { path: /logout, target: / }
  35.  
  36.     access_control:
  37.         - { path: /top, role: ROLE_ADMIN }
  38.         - { path: /.*, role: IS_AUTHENTICATED_ANONYMOUSLY }
  39.  
  40.  
  41.  
  42. /entity/User.php -------------------------------------
  43.  
  44. use Symfony\Component\Security\User\AccountInterface;
  45.  
  46. /**
  47.  * @orm:Entity
  48.  */
  49. class User implements AccountInterface
  50. {
  51.     /**
  52.      * @orm:Id
  53.      * @orm:Column(type="integer")
  54.      * @orm:GeneratedValue(strategy="IDENTITY")
  55.      */
  56.     protected $id;
  57.  
  58.  
  59.      /**
  60.      * @orm:Column(type="string", length="255")
  61.      */
  62.     protected $password;
  63.    
  64.     /**
  65.      * @orm:Column(type="string", length="255")
  66.      */
  67.     protected $username;
  68.  
  69.  
  70.     /**
  71.      * @orm:Column(type="string", length="255")
  72.      */
  73.     protected $name;
  74.  
  75.    
  76.  
  77.     public function getId()
  78.     {
  79.         return $this->id;
  80.     }
  81.  
  82.     public function setName($name)
  83.     {
  84.         $this->name = $name;
  85.     }
  86.  
  87.     public function getName()
  88.     {
  89.         return $this->name;
  90.     }
  91.  
  92.     public function setUsername($username)
  93.     {
  94.         $this->username = $username;
  95.     }
  96.  
  97.     public function getUsername()
  98.     {
  99.         return $this->username;
  100.     }
  101.  
  102.  
  103.    
  104.     // Acount interface
  105.  
  106.     public function __toString(){
  107.  
  108.         return $this->getUsername();
  109.  
  110.     }
  111.  
  112.     public function getRoles(){
  113.  
  114.         return array('ROLE_ADMIN');
  115.  
  116.     }
  117.  
  118.     public function eraseCredentials(){
  119.  
  120.     }
  121.  
  122.     public function getSalt(){
  123.  
  124.         return '';
  125.  
  126.     }
  127.  
  128.     public function getPassword(){
  129.        
  130.         return $this->password;
  131.  
  132.     }
  133.  
  134.     public function setPassword($password){
  135.  
  136.         $this->password = $password;
  137.  
  138.     }
  139.  
  140.     public function equals(AccountInterface $account){
  141.  
  142.         if($this->getUsername() == $account->getUsername()){
  143.  
  144.             return true;
  145.         }
  146.  
  147.         return false;
  148.        
  149.     }
  150.  
  151. }
  152.  
  153. /controller/SecurityController.php -------------------------------------
  154.  
  155.  
  156. namespace Bundle\SecurityBundle\Controller;
  157.  
  158. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  159. use Symfony\Component\Security\SecurityContext;
  160. use Bundle\SecurityBundle\Document;
  161.  
  162. use Bundle\SecurityBundle\Entity\User;
  163. use Bundle\SecurityBundle\Entity\UserRepository;
  164.  
  165. class SecurityController extends Controller {
  166.  
  167.      public function loginAction (){
  168.          
  169.          // get the error if any (works with forward and redirect -- see below)
  170.          if ( $this -> get ( 'request' ) -> attributes -> has ( SecurityContext :: AUTHENTICATION_ERROR )) {
  171.              $error = $this -> get ( 'request' ) -> attributes -> get ( SecurityContext :: AUTHENTICATION_ERROR );
  172.          } else {
  173.              $error = $this -> get ( 'request' ) -> getSession () -> get ( SecurityContext :: AUTHENTICATION_ERROR );
  174.          }
  175.          
  176.  
  177.          return $this -> render ( 'SecurityBundle:Security:login.html.twig' , array (
  178.              // last username entered by the user
  179.              'last_username' => $this -> get ( 'request' ) -> getSession () -> get ( SecurityContext :: LAST_USERNAME ),
  180.              'error' => $error ,
  181.          ));
  182.      }
  183.  
  184. }
  185.  
  186. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement