Created
June 26, 2011 14:55
-
-
Save Schrank/1047672 to your computer and use it in GitHub Desktop.
Implement UserProvider in Symfony2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
user.provider: | |
class: MyApp\MyBundle\DependencyInjection\UserProvider | |
arguments: [@doctrine.orm.entity_manager] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
security: | |
providers: | |
main: | |
id: user.provider |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MyApp\MyBundle\DependencyInjection; | |
use Symfony\Component\Security\Core\User\User; | |
use Symfony\Component\Security\Core\User\UserInterface; | |
use Symfony\Component\Security\Core\User\UserProviderInterface; | |
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; | |
class UserProvider implements UserProviderInterface { | |
private $db = null; | |
public function __construct($db_link) { | |
$this->db = $db_link; | |
} | |
function loadUserByUsername($username) { | |
$optionRep = $this->db->getRepository('MyApp\MyBundle\Entity\Option'); | |
/* | |
There is only a options-database with two users... | |
*/ | |
$admin = $optionRep->find('adminName'); | |
if(!is_null($admin) && $admin->getValue() == $username) { | |
$password = $optionRep->find('adminPassword')->getValue(); | |
return new User($username, $password, array('ROLE_ADMIN')); | |
} | |
$user = $optionRep->find('partnerName'); | |
if(!is_null($user) && $user->getValue() == $username) { | |
$password = $optionRep->find('partnerPassword')->getValue(); | |
return new User($username, $password, array('ROLE_USER')); | |
} | |
throw new UsernameNotFoundException('Benutzername oder Passwort falsch.'); | |
} | |
function loadUser(UserInterface $user) { | |
return $this->loadUserByUsername($user->getUsername()); | |
} | |
function supportsClass($class) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment