Advertisement
Guest User

ayudasymfony

a guest
Feb 24th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.93 KB | None | 0 0
  1. //entidad usuario
  2. <?php
  3.  
  4. namespace Actas\MainBundle\Entity;
  5.  
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  8. use Symfony\Component\Validator\Constraints as Assert;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10.  
  11.  
  12. /**
  13.  * Usuarios
  14.  *
  15.  * @ORM\Table(name="usuarios")
  16.  * @ORM\Entity(repositoryClass="Actas\MainBundle\Repository\UsuariosRepository")
  17.  * @UniqueEntity(fields="usuario", message="Ese Usuario ya existe")
  18.  */
  19. class Usuarios implements UserInterface, \Serializable
  20. {
  21.     /**
  22.      * @var int
  23.      *
  24.      * @ORM\Column(name="id", type="integer")
  25.      * @ORM\Id
  26.      * @ORM\GeneratedValue(strategy="AUTO")
  27.      */
  28.     private $id;
  29.  
  30.     /**
  31.      * @var string
  32.      *
  33.      * @ORM\Column(name="usuario", type="string", length=100, unique=true)
  34.      * @Assert\NotBlank(message="no deje en blanco")
  35.      */
  36.     private $usuario;
  37.  
  38.     /**
  39.      * @var string
  40.      *
  41.      * @ORM\Column(name="contrasea", type="string", length=64)
  42.      */
  43.     private $contrasea;
  44.    
  45.     /**
  46.      * @var bool
  47.      *
  48.      * @ORM\Column(name="is_activo", type="boolean")
  49.      *
  50.      */
  51.     private $is_activo = 0;
  52.  
  53.     /**
  54.      * Get id
  55.      *
  56.      * @return integer
  57.      */
  58.     public function getId()
  59.     {
  60.         return $this->id;
  61.     }
  62.  
  63.     /**
  64.      * Set usuario
  65.      *
  66.      * @param string $usuario
  67.      * @return Usuarios
  68.      */
  69.     public function setUsuario($usuario)
  70.     {
  71.         $this->usuario = $usuario;
  72.  
  73.         return $this;
  74.     }
  75.  
  76.     /**
  77.      * Get usuario
  78.      *
  79.      * @return string
  80.      */
  81.     public function getUsuario()
  82.     {
  83.         return $this->usuario;
  84.     }
  85.  
  86.     /**
  87.      * Set contrasea
  88.      *
  89.      * @param string $contrasea
  90.      * @return Usuarios
  91.      */
  92.     public function setContrasea($contrasea)
  93.     {
  94.         $this->contrasea = $contrasea;
  95.  
  96.         return $this;
  97.     }
  98.  
  99.     /**
  100.      * Get contrasea
  101.      *
  102.      * @return string
  103.      */
  104.     public function getContrasea()
  105.     {
  106.         return $this->contrasea;
  107.     }
  108.     /**
  109.      * Get is_activo
  110.      *
  111.      * @return boolean
  112.      */
  113.     public function getIsactivo()
  114.     {
  115.         return (boolean)$this->is_activo;
  116.     }
  117.    
  118.     /**
  119.      * Set is_activo
  120.      *
  121.      * @param boolean is_activo
  122.      * @return Usuarios
  123.      */
  124.     public function setIsactivo($is_activo)
  125.     {
  126.         (boolean)$this->is_activo = $is_activo;
  127.  
  128.         return $this;
  129.     }
  130.  
  131.     public function eraseCredentials() {
  132.        
  133.     }
  134.  
  135.     public function getPassword() {
  136.        
  137.     }
  138.  
  139.     public function getRoles() {
  140.         return array('ROLE_USER');
  141.        
  142.     }
  143.  
  144.     public function getSalt() {
  145.        
  146.     }
  147.  
  148.     public function getUsername() {
  149.        
  150.     }
  151.  
  152.     public function serialize() {
  153.         return serialize(array(
  154.         $this->id,
  155.         $this->usuario,
  156.         $this->contrasea,
  157.         $this->is_activo
  158.         ));
  159.        
  160.     }
  161.  
  162.     public function unserialize($serialized) {
  163.         list(
  164.         $this->id,  
  165.         $this->usuario,
  166.         $this->contrasea,
  167.         $this->is_activo
  168.                 )=  unserialize($serialized);
  169.     }
  170.  
  171. }
  172.  
  173.  
  174. //login_controller
  175. <?php
  176.  
  177. namespace Actas\MainBundle\Controller;
  178.  
  179. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  180. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  181. use Symfony\Component\HttpFoundation\Request;
  182.  
  183. class LoginController extends Controller
  184. {
  185.     /**
  186.      * @Route("/login", name="login")
  187.      */
  188.     public function loginAction(Request $request)      
  189.     {
  190.         $authenticationUtils = $this->get('security.authentication_utils');
  191.  
  192.     // get the login error if there is one
  193.     $error = $authenticationUtils->getLastAuthenticationError();
  194.  
  195.     // last username entered by the user
  196.     $lastUsername = $authenticationUtils->getLastUsername();
  197.  
  198.     return $this->render(
  199.         'ActasMainBundle:login:login.html.twig',
  200.         array(
  201.             // last username entered by the user
  202.             'last_username' => $lastUsername,
  203.             'error'         => $error,
  204.         )
  205.     );
  206.      
  207.        
  208.      
  209.     }
  210.  
  211.      /**
  212.      * @Route("/logout", name="logout")
  213.      */
  214.     public function logoutAction()
  215.     {
  216.        
  217.     }
  218.  
  219. }
  220.  
  221.  
  222. //security.yml
  223. # To get started with security, check out the documentation:
  224. # http://symfony.com/doc/current/book/security.html
  225. security:
  226.     encoders:
  227.         Actas\MainBundle\Entity\Usuarios:
  228.             algorithm: bcrypt
  229.            
  230.     providers:
  231.         mysql:
  232.             entity:
  233.                 class: ActasMainBundle\Entity\Usuarios
  234.                 property: usuario
  235.     # http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
  236.    
  237.  
  238.     firewalls:
  239.         # disables authentication for assets and the profiler, adapt it according to your needs
  240.        dev:
  241.             pattern: ^/(_(profiler|wdt)|css|images|js)/
  242.             security: false
  243.  
  244.         main:
  245.             anonymous: ~
  246.             form_login:
  247.                 login_path: login
  248.                 check_path: login
  249.                
  250.     access_control:
  251.       - {path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY}
  252.       - {path: ^/usuarios, roles: ROLE_USER}
  253.        
  254. //login twig
  255.  
  256. {% block inputs %}
  257.     {% if error %}
  258.     <div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
  259. {% endif %}
  260.  
  261. <form action="{{ path('login') }}" method="post">
  262.     <label for="username">Username:</label>
  263.     <input type="text" id="username" name="_username" value="{{ last_username }}" />
  264.  
  265.     <label for="password">Password:</label>
  266.     <input type="password" id="password" name="_password" />
  267.  
  268.     {#
  269.        If you want to control the URL the user
  270.         is redirected to on success (more details below)
  271.         <input type="hidden" name="_target_path" value="/account" />
  272.     #}
  273.  
  274.     <button type="submit">login</button>
  275. </form>
  276. {% endblock %}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement