PHP code example of tyler36 / ldap

1. Go to this page and download the library: Download tyler36/ldap library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tyler36 / ldap example snippets


$ php artisan vendor:publish --provider="Tyler36\Ldap\LdapServiceProvider"

/**
  * Get the login username to be used by the controller. ['email']
  *
  * @return string
  */
public function username()
{
    return config('ldap.username');
}

/**
 * Attempt to log the user into the application.
 *
 * @param \Illuminate\Http\Request $request
 *
 * @return mixed
 */
protected function attemptLogin(Request $request)
{
    // This is where authentication happens. It SHOULD return an array containing the user
    $ldap     = new LdapAuthenticator($request);
    $ldapUser = $ldap->authenticate();
    if (!$ldapUser || 'array' !== gettype($ldapUser)) {
        return $ldapUser;
    }

    // Un-comment the following to see details of the user array
    // dd($ldapUser)

    // Update or create a new user based on the username. The second array determines how to populate new users.
    $user   = User::updateOrCreate(
        [$this->username() => $request->get('username')],
        [
            $this->username() => $request->get('username'),
            'name'            => optional($ldapUser)['displayname'][0],
            'email'           => optional($ldapUser)['mail'][0],
        ]
    );

    auth()->login($user);
}

    public function rules()
    {
        return config('ldap.rules');
    }