Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Symfony 2 authentication with (doctrine) Entity
- security:
- encoders:
- PartnersFrontendBundleEntityUser: plaintext
- SymfonyComponentSecurityCoreUserUser: plaintext
- providers:
- main:
- entity: { class: FrontendBundle:User, property: username }
- # in_memory:
- # users:
- # sergi: { password: boo123, roles: [ 'ROLE_USER' ] }
- firewalls:
- main:
- pattern: /.*
- form_login: true
- anonymous: true
- logout: true
- access_control:
- - { path: /docs.*, role: ROLE_USER }
- - { path: /control.*, role: ROLE_USER }
- - { path: /.*, role: IS_AUTHENTICATED_ANONYMOUSLY }
- role_hierarchy:
- ROLE_ADMIN: ROLE_USER
- <?php
- namespace PartnersFrontendBundleEntity;
- use SymfonyComponentSecurityCoreUserUserInterface;
- use PartnersFrontendBundleRepositoryUserRepository;
- /**
- * @orm:Entity(repositoryClass="PartnersFrontendBundleRepository
- UserRepository")
- * @orm:Table(name="users")
- */
- class User implements UserInterface
- {
- /**
- * @orm:Id
- * @orm:Column(type="integer")
- * @orm:GeneratedValue(strategy="IDENTITY")
- */
- protected $id;
- /**
- * @orm:Column(type="string", length="32", unique=true)
- */
- protected $username;
- /**
- * @orm:Column(type="string", length="255", unique=true)
- */
- protected $email;
- /**
- * @orm:Column(type="string", length="128")
- */
- protected $password;
- /**
- * @orm:Column(type="string", length="128")
- */
- protected $organization;
- /**
- * @orm:Column(type="string", length="128")
- */
- protected $contact;
- /**
- * @orm:Column(type="string", length="16")
- */
- protected $phone;
- /**
- * @orm:Column(type="string", length="10")
- */
- protected $cid;
- /**
- * @orm:Column(type="smallint")
- */
- protected $status;
- public function getId()
- {
- return $this->id;
- }
- public function setId($id)
- {
- $this->id = $id;
- }
- public function getUsername()
- {
- return $this->username;
- }
- public function setUsername($username)
- {
- $this->username = $username;
- }
- public function getPassword()
- {
- return $this->password;
- }
- public function setPassword($password)
- {
- $this->password = $password;
- }
- public function setEmail($email)
- {
- $this->email = $email;
- }
- public function getEmail()
- {
- return $this->email;
- }
- public function setPhone($phone)
- {
- $this->phone = $phone;
- }
- public function getPhone()
- {
- return $this->phone;
- }
- public function setContact($contact)
- {
- $this->contact = $contact;
- }
- public function getContact()
- {
- return $this->contact;
- }
- public function setOrganization($org)
- {
- $this->organization = $org;
- }
- public function getOrganization()
- {
- return $this->organization;
- }
- public function setCid($cid)
- {
- $this->cid = $cid;
- }
- public function getCid()
- {
- return $this->cid;
- }
- public function setStatus($status)
- {
- $this->status = $status;
- }
- public function getStatus()
- {
- return $this->status;
- }
- /**
- * Implementing the UserInterface interface
- */
- public function __toString()
- {
- return $this->getUsername();
- }
- public function getRoles()
- {
- return array('ROLE_USER');
- }
- public function eraseCredentials()
- {
- return false;
- }
- public function getSalt()
- {
- return $this->getId();
- }
- /**
- * equals.
- *
- * @param UserInterface $account
- * @return bool
- */
- public function equals(UserInterface $account)
- {
- if ($account->getUsername() != $this->getUsername) {
- return false;
- }
- if ($account->getEmail() != $this->getEmail) {
- return false;
- }
- return true;
- }
- }
- <?php
- namespace PartnersFrontendBundleRepository;
- use DoctrineORMEntityRepository;
- use SymfonyComponentSecurityCoreUserUserProviderInterface;
- use SymfonyComponentSecurityCoreUserUserInterface;
- class UserRepository extends EntityRepository implements
- UserProviderInterface
- {
- const CREATED = 0;
- const ACTIVE = 10;
- const INACTIVE = 20;
- /**
- * loadUserByUsername.
- *
- * @param string $username
- * @return PartnersFrontendBundleEntityUser
- */
- public function loadUserByUsername($username)
- {
- return $this->findOneBy(array('username' => $username));
- }
- function loadUser(UserInterface $user)
- {
- return $user;
- }
- function loadUserByAccount(AccountInterface $account)
- {
- return $this->loadUserByUsername($account->getUsername());
- }
- public function supportsClass($class)
- {
- return true;
- }
- }
- exception 'SymfonyComponentSecurityCoreException
- BadCredentialsException' with message 'Bad credentials' in /var/www/
- inspiring/trunk/Symfony/vendor/symfony/src/Symfony/Component/Security/
- Core/Authentication/Provider/DaoAuthenticationProvider.php:66 Stack
- trace: #0 /var/www/inspiring/trunk/Symfony/vendor/symfony/src/Symfony/
- Component/HttpFoundation/SessionStorage/NativeSessionStorage.php(81):
- session_start() #1 /var/www/inspiring/trunk/Symfony/app/cache/dev/
- classes-53824.php(284): SymfonyComponentHttpFoundationSessionStorage
- NativeSessionStorage->start() #2 /var/www/inspiring/trunk/Symfony/app/
- cache/dev/appDevDebugProjectContainer.php(1151): SymfonyComponent
- HttpFoundationSession->start() #3 /var/www/inspiring/trunk/Symfony/
- app/bootstrap.php.cache(109): appDevDebugProjectContainer-
- >getSessionService() #4 /var/www/inspiring/trunk/Symfony/app/cache/dev/
- classes-53824.php(1553): SymfonyComponentDependencyInjection
- Container->get('session') #5 /var/www/inspiring/trunk/Symfony/app/
- cache/dev/classes-53824.php(1544): SymfonyBundleFrameworkBundle
- RequestListener->initializeSession(Object(SymfonyComponent
- HttpFoundationRequest), true) #6 /var/www/inspiring/trunk/Symfony/
- app/cache/dev/classes-53824.php(1214): SymfonyBundleFrameworkBundle
- RequestListener->onCoreRequest(Object(SymfonyComponentHttpKernel
- EventGetResponseEvent)) #7 /var/www/inspiring/trunk/Symfony/vendor/
- symfony/src/Symfony/Bundle/FrameworkBundle/Debug/
- TraceableEventDispatcher.php(49): SymfonyComponentEventDispatcher
- EventDispatcher->triggerListener(Object(SymfonyBundleFrameworkBundle
- RequestListener), 'onCoreRequest', Object(SymfonyComponentHttpKernel
- EventGetResponseEvent)) #8 /var/www/inspiring/trunk/Symfony/app/
- cache/dev/classes-53824.php(1146): SymfonyBundleFrameworkBundleDebug
- TraceableEventDispatcher->triggerListener(Object(SymfonyBundle
- FrameworkBundleRequestListener), 'onCoreRequest', Object(Symfony
- ComponentHttpKernelEventGetResponseEvent)) #9 /var/www/inspiring/
- trunk/Symfony/app/cache/dev/classes-53824.php(1734): SymfonyComponent
- EventDispatcherEventDispatcher->dispatch('onCoreRequest',
- Object(SymfonyComponentHttpKernelEventGetResponseEvent)) #10 /var/
- www/inspiring/trunk/Symfony/app/bootstrap.php.cache(411): Symfony
- BundleFrameworkBundleContainerAwareEventDispatcher-
- >dispatch('onCoreRequest', Object(SymfonyComponentHttpKernelEvent
- GetResponseEvent)) #11 /var/www/inspiring/trunk/Symfony/app/
- bootstrap.php.cache(400): SymfonyComponentHttpKernelHttpKernel-
- >handleRaw(Object(SymfonyComponentHttpFoundationRequest), 1) #12 /
- var/www/inspiring/trunk/Symfony/vendor/symfony/src/Symfony/Bundle/
- FrameworkBundle/HttpKernel.php(35): SymfonyComponentHttpKernel
- HttpKernel->handle(Object(SymfonyComponentHttpFoundationRequest),
- 1, true) #13 /var/www/inspiring/trunk/Symfony/app/
- bootstrap.php.cache(576): SymfonyBundleFrameworkBundleHttpKernel-
- >handle(Object(SymfonyComponentHttpFoundationRequest), 1, true)
- #14 /var/www/inspiring/trunk/Symfony/web/app_dev.php(15): Symfony
- ComponentHttpKernelKernel->handle(Object(SymfonyComponent
- HttpFoundationRequest)) #15 {main}
- $factory = $this->container->get('security.encoder_factory');
- $encoder = $factory->getEncoder($user);
- $pwd = $encoder->encodePassword('your_random_password', $user->getSalt());
- $user->setPassword($pwd);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement