Skip to content

Instantly share code, notes, and snippets.

@Schrank
Created June 26, 2011 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Schrank/1047672 to your computer and use it in GitHub Desktop.
Save Schrank/1047672 to your computer and use it in GitHub Desktop.
Implement UserProvider in Symfony2
services:
user.provider:
class: MyApp\MyBundle\DependencyInjection\UserProvider
arguments: [@doctrine.orm.entity_manager]
security:
providers:
main:
id: user.provider
<?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