SHARE
TWEET

Untitled

a guest Sep 3rd, 2018 114 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Symfony 2 authentication with (doctrine) Entity
  2. security:
  3.     encoders:
  4.         PartnersFrontendBundleEntityUser:       plaintext
  5.         SymfonyComponentSecurityCoreUserUser: plaintext
  6.     providers:
  7.         main:
  8.             entity: { class: FrontendBundle:User, property: username }
  9.         #        in_memory:
  10.         #            users:
  11.         #                sergi:        { password: boo123, roles: [ 'ROLE_USER' ] }
  12.     firewalls:
  13.         main:
  14.             pattern:         /.*
  15.             form_login:      true
  16.             anonymous:       true
  17.             logout:          true
  18.     access_control:
  19.         - { path: /docs.*, role: ROLE_USER }
  20.         - { path: /control.*, role: ROLE_USER }
  21.         - { path: /.*, role: IS_AUTHENTICATED_ANONYMOUSLY }
  22.     role_hierarchy:
  23.         ROLE_ADMIN:       ROLE_USER
  24.    
  25. <?php
  26.     namespace PartnersFrontendBundleEntity;
  27.     use SymfonyComponentSecurityCoreUserUserInterface;
  28.     use PartnersFrontendBundleRepositoryUserRepository;
  29.     /**
  30.      * @orm:Entity(repositoryClass="PartnersFrontendBundleRepository
  31.     UserRepository")
  32.      * @orm:Table(name="users")
  33.      */
  34.     class User implements UserInterface
  35.     {
  36.         /**
  37.          * @orm:Id
  38.          * @orm:Column(type="integer")
  39.          * @orm:GeneratedValue(strategy="IDENTITY")
  40.          */
  41.         protected $id;
  42.         /**
  43.          * @orm:Column(type="string", length="32", unique=true)
  44.          */
  45.         protected $username;
  46.         /**
  47.          * @orm:Column(type="string", length="255", unique=true)
  48.          */
  49.         protected $email;
  50.         /**
  51.          * @orm:Column(type="string", length="128")
  52.          */
  53.         protected $password;
  54.         /**
  55.          * @orm:Column(type="string", length="128")
  56.          */
  57.         protected $organization;
  58.         /**
  59.          * @orm:Column(type="string", length="128")
  60.          */
  61.         protected $contact;
  62.         /**
  63.          * @orm:Column(type="string", length="16")
  64.          */
  65.         protected $phone;
  66.         /**
  67.          * @orm:Column(type="string", length="10")
  68.          */
  69.         protected $cid;
  70.         /**
  71.          * @orm:Column(type="smallint")
  72.          */
  73.         protected $status;
  74.         public function getId()
  75.         {
  76.             return $this->id;
  77.         }
  78.         public function setId($id)
  79.         {
  80.             $this->id = $id;
  81.         }
  82.         public function getUsername()
  83.         {
  84.             return $this->username;
  85.         }
  86.         public function setUsername($username)
  87.         {
  88.             $this->username = $username;
  89.         }
  90.         public function getPassword()
  91.         {
  92.             return $this->password;
  93.         }
  94.         public function setPassword($password)
  95.         {
  96.             $this->password = $password;
  97.         }
  98.         public function setEmail($email)
  99.         {
  100.             $this->email = $email;
  101.         }
  102.         public function getEmail()
  103.         {
  104.             return $this->email;
  105.         }
  106.         public function setPhone($phone)
  107.         {
  108.             $this->phone = $phone;
  109.         }
  110.         public function getPhone()
  111.         {
  112.             return $this->phone;
  113.         }
  114.         public function setContact($contact)
  115.         {
  116.             $this->contact = $contact;
  117.         }
  118.         public function getContact()
  119.         {
  120.             return $this->contact;
  121.         }
  122.         public function setOrganization($org)
  123.         {
  124.             $this->organization = $org;
  125.         }
  126.         public function getOrganization()
  127.         {
  128.             return $this->organization;
  129.         }
  130.         public function setCid($cid)
  131.         {
  132.             $this->cid = $cid;
  133.         }
  134.         public function getCid()
  135.         {
  136.             return $this->cid;
  137.         }
  138.         public function setStatus($status)
  139.         {
  140.           $this->status = $status;
  141.         }
  142.         public function getStatus()
  143.         {
  144.           return $this->status;
  145.         }
  146.         /**
  147.          * Implementing the UserInterface interface
  148.          */
  149.         public function __toString()
  150.         {
  151.             return $this->getUsername();
  152.         }
  153.         public function getRoles()
  154.         {
  155.             return array('ROLE_USER');
  156.         }
  157.         public function eraseCredentials()
  158.         {
  159.             return false;
  160.         }
  161.         public function getSalt()
  162.         {
  163.             return $this->getId();
  164.         }
  165.         /**
  166.          * equals.
  167.          *
  168.          * @param UserInterface $account
  169.          * @return bool
  170.          */
  171.         public function equals(UserInterface $account)
  172.         {
  173.             if ($account->getUsername() != $this->getUsername) {
  174.                 return false;
  175.             }
  176.             if ($account->getEmail() != $this->getEmail) {
  177.                 return false;
  178.             }
  179.             return true;
  180.         }
  181.     }
  182.    
  183. <?php
  184.     namespace PartnersFrontendBundleRepository;
  185.     use DoctrineORMEntityRepository;
  186.     use SymfonyComponentSecurityCoreUserUserProviderInterface;
  187.     use SymfonyComponentSecurityCoreUserUserInterface;
  188.     class UserRepository extends EntityRepository implements
  189.     UserProviderInterface
  190.     {
  191.         const CREATED  = 0;
  192.         const ACTIVE   = 10;
  193.         const INACTIVE = 20;
  194.         /**
  195.          * loadUserByUsername.
  196.          *
  197.          * @param string $username
  198.          * @return PartnersFrontendBundleEntityUser
  199.          */
  200.         public function loadUserByUsername($username)
  201.         {
  202.             return $this->findOneBy(array('username' => $username));
  203.         }
  204.         function loadUser(UserInterface $user)
  205.         {
  206.             return $user;
  207.         }
  208.         function loadUserByAccount(AccountInterface $account)
  209.         {
  210.             return $this->loadUserByUsername($account->getUsername());
  211.         }
  212.         public function supportsClass($class)
  213.         {
  214.             return true;
  215.         }
  216.     }
  217.    
  218. exception 'SymfonyComponentSecurityCoreException
  219. BadCredentialsException' with message 'Bad credentials' in /var/www/
  220. inspiring/trunk/Symfony/vendor/symfony/src/Symfony/Component/Security/
  221. Core/Authentication/Provider/DaoAuthenticationProvider.php:66 Stack
  222. trace: #0 /var/www/inspiring/trunk/Symfony/vendor/symfony/src/Symfony/
  223. Component/HttpFoundation/SessionStorage/NativeSessionStorage.php(81):
  224. session_start() #1 /var/www/inspiring/trunk/Symfony/app/cache/dev/
  225. classes-53824.php(284): SymfonyComponentHttpFoundationSessionStorage
  226. NativeSessionStorage->start() #2 /var/www/inspiring/trunk/Symfony/app/
  227. cache/dev/appDevDebugProjectContainer.php(1151): SymfonyComponent
  228. HttpFoundationSession->start() #3 /var/www/inspiring/trunk/Symfony/
  229. app/bootstrap.php.cache(109): appDevDebugProjectContainer-
  230. >getSessionService() #4 /var/www/inspiring/trunk/Symfony/app/cache/dev/
  231.  
  232. classes-53824.php(1553): SymfonyComponentDependencyInjection
  233. Container->get('session') #5 /var/www/inspiring/trunk/Symfony/app/
  234. cache/dev/classes-53824.php(1544): SymfonyBundleFrameworkBundle
  235. RequestListener->initializeSession(Object(SymfonyComponent
  236. HttpFoundationRequest), true) #6 /var/www/inspiring/trunk/Symfony/
  237. app/cache/dev/classes-53824.php(1214): SymfonyBundleFrameworkBundle
  238. RequestListener->onCoreRequest(Object(SymfonyComponentHttpKernel
  239. EventGetResponseEvent)) #7 /var/www/inspiring/trunk/Symfony/vendor/
  240. symfony/src/Symfony/Bundle/FrameworkBundle/Debug/
  241. TraceableEventDispatcher.php(49): SymfonyComponentEventDispatcher
  242. EventDispatcher->triggerListener(Object(SymfonyBundleFrameworkBundle
  243. RequestListener), 'onCoreRequest', Object(SymfonyComponentHttpKernel
  244. EventGetResponseEvent)) #8 /var/www/inspiring/trunk/Symfony/app/
  245. cache/dev/classes-53824.php(1146): SymfonyBundleFrameworkBundleDebug
  246. TraceableEventDispatcher->triggerListener(Object(SymfonyBundle
  247. FrameworkBundleRequestListener), 'onCoreRequest', Object(Symfony
  248. ComponentHttpKernelEventGetResponseEvent)) #9 /var/www/inspiring/
  249. trunk/Symfony/app/cache/dev/classes-53824.php(1734): SymfonyComponent
  250. EventDispatcherEventDispatcher->dispatch('onCoreRequest',
  251. Object(SymfonyComponentHttpKernelEventGetResponseEvent)) #10 /var/
  252. www/inspiring/trunk/Symfony/app/bootstrap.php.cache(411): Symfony
  253. BundleFrameworkBundleContainerAwareEventDispatcher-
  254. >dispatch('onCoreRequest', Object(SymfonyComponentHttpKernelEvent
  255.  
  256. GetResponseEvent)) #11 /var/www/inspiring/trunk/Symfony/app/
  257. bootstrap.php.cache(400): SymfonyComponentHttpKernelHttpKernel-
  258. >handleRaw(Object(SymfonyComponentHttpFoundationRequest), 1) #12 /
  259.  
  260. var/www/inspiring/trunk/Symfony/vendor/symfony/src/Symfony/Bundle/
  261. FrameworkBundle/HttpKernel.php(35): SymfonyComponentHttpKernel
  262. HttpKernel->handle(Object(SymfonyComponentHttpFoundationRequest),
  263. 1, true) #13 /var/www/inspiring/trunk/Symfony/app/
  264. bootstrap.php.cache(576): SymfonyBundleFrameworkBundleHttpKernel-
  265. >handle(Object(SymfonyComponentHttpFoundationRequest), 1, true)
  266.  
  267. #14 /var/www/inspiring/trunk/Symfony/web/app_dev.php(15): Symfony
  268. ComponentHttpKernelKernel->handle(Object(SymfonyComponent
  269. HttpFoundationRequest)) #15 {main}
  270.    
  271. $factory = $this->container->get('security.encoder_factory');
  272. $encoder = $factory->getEncoder($user);
  273. $pwd = $encoder->encodePassword('your_random_password', $user->getSalt());
  274. $user->setPassword($pwd);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top