Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Symfony2 : Authentication with Active directory
- class LdapUser implements UserInterface
- {
- private $username;
- private $first_name;
- private $last_name;
- private $password;
- private $salt;
- private $roles;
- public function __construct($username, $first_name, $last_name, $password, $salt, array $roles) {
- $this->username = $username;
- $this->first_name = $first_name;
- $this->last_name = $last_name;
- $this->password = $password;
- $this->salt = $salt;
- $this->roles = $roles;
- }
- ...
- }
- public function loadUserByUsername($username)
- {
- $server = "my_ldap_server";
- $root_dn = "my_root_dn";
- $root_pw = "my_root_pw";
- $ds = ldap_connect($server);
- if ($ds) {
- ldap_bind($ds, $root_dn, $root_pw);
- $search = ldap_search($ds, "my_branch", "(sAMAccountName=".$username.")", array("sn", "givenName"));
- $info = ldap_get_entries($ds, $sr);
- if($info['count'] > 0) {
- $user = $info[0];
- return new LdapUser($username, $user['givenName'][0], $user['sn'][0], '???PASSWORD???', '???SALT???', array('ROLE_USER'));
- } else {
- throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
- }
- ldap_close($ds);
- } else {
- echo "Connexion au serveur LDAP impossible";
- }
- }
Add Comment
Please, Sign In to add comment