Skip to content
Yunai39 edited this page Oct 31, 2013 · 2 revisions

This is a full Example of an User for the LdapBundle

<?php

namespace Acme\DemoBundle\Security\User;

use Symfony\Component\Security\Core\User\UserInterface;

class CustomLdapUser implements UserInterface
{
/* displayname*/
    private $username;
    private $password;
    private $salt;
    private $roles;
    private $email;
    private $fullname;
    private $displayname;


    public function __construct($username, $password, array $roles)
    {
        $this->username = $username;
        $this->password = $password;
        $this->salt = '';
        $this->roles = $roles;
    }

    public function getDisplayName(){
        return $this->displayname;
    }
    
    public function setDisplayName($displayname){
        $this->displayname = $displayname;
    }
    
    public Function getEmail(){
        return $this->email;
    }
    
    public function setEmail($email){
        $this->email = $email;
    }
    
    public function getFullName(){
        return $this->fullname;
    }

    public function setFullName($fullname){
        $this->fullname = $fullname; 
    }
    /**
     * Returns the password used to authenticate the user.
     *
     * This should be the encoded password. On authentication, a plain-text
     * password will be salted, encoded, and then compared to this value.
     *
     * @return string The password
     */
    public function getPassword()
    {
        return $this->password;
    }



    
    /**
     * Returns the salt that was originally used to encode the password.
     *
     * This can return null if the password was not encoded using a salt.
     *
     * @return string The salt
     */
    public function getSalt()
    {
        return null;
    }

    /**
     * Returns the username used to authenticate the user.
     *
     * @return string The username
     */
    public function getUsername()
    {
        return $this->username;
    }

    /**
     * Removes sensitive data from the user.
     *
     * This is important if, at any given point, sensitive information like
     * the plain-text password is stored on this object.
     *
     * @return void
     */
    public function eraseCredentials()
    {
        //return void;
    }


     public function getRoles()
     {
         return $this->roles;
     }

    public function setRoles(array $roles)
    {
        $this->roles = $roles;
    }
}

In this example you have three attribut for the User, the email, the fullName and the displayName

then in app/config/paraeters.yml you need to add the following setting, this way the field in Ldap cn will become the FullName of you user, mail will become email and so on:

ldap.settings.user:
      FullName: cn
      Email: mail
      DisplayName: displayname

You will then need to record this user as a service with the following id:

services:
    security_ldap_user:
        class: Acme\DemoBundle\Security\User\CustomLdapUser

And do not forget ta add the encoders

encoders:
    Acme\DemoBundle\Security\User\CustomLdapUser : plaintext #Active directory does not support encrypted password yet

Clone this wiki locally