Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
263 changes: 139 additions & 124 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,72 +9,77 @@
## Installation

Insert Adldap2-Laravel into your `composer.json` file:

"adldap2/adldap2-laravel": "1.2.*",
```json
"adldap2/adldap2-laravel": "1.2.*",
```

Then run `composer update`.

Once finished, insert the service provider in your `config/app.php` file:
```php
Adldap\Laravel\AdldapServiceProvider::class,
```

Adldap\Laravel\AdldapServiceProvider::class,

Then insert the facade:

'Adldap' => Adldap\Laravel\Facades\Adldap::class
```php
'Adldap' => Adldap\Laravel\Facades\Adldap::class
```

Publish the configuration file by running:

php artisan vendor:publish
```bash
php artisan vendor:publish
```

Now you're all set!

## Usage

You can perform all methods on Adldap through its facade like so:
```php
$user = Adldap::users()->find('john doe');

$user = Adldap::users()->find('john doe');

$search = Adldap::search()->where('cn', '=', 'John Doe')->get();


if(Adldap::authenticate($username, $password))
{
// Passed!
}
$search = Adldap::search()->where('cn', '=', 'John Doe')->get();

Or you can inject the Adldap contract:

use Adldap\Contracts\Adldap;
if (Adldap::authenticate($username, $password)) {
// Passed!
}
```

Or you can inject the Adldap contract:
```php
use Adldap\Contracts\Adldap;

class UserController extends Controller
{
/**
* @var Adldap
*/
protected $adldap;

/**
* Constructor.
*
* @param Adldap $adldap
*/
public function __construct(Adldap $adldap)
{
$this->adldap = $adldap;
}

class UserController extends Controller
/**
* Displays the all LDAP users.
*
* @return \Illuminate\View\View
*/
public function index()
{
/**
* @var Adldap
*/
protected $adldap;
$users = $this->adldap->users()->all();

/**
* Constructor.
*
* @param Adldap $adldap
*/
public function __construct(Adldap $adldap)
{
$this->adldap = $adldap;
}

/**
* Displays the all LDAP users.
*
* @return \Illuminate\View\View
*/
public function index()
{
$users = $this->adldap->users()->all();

return view('users.index', compact('users'));
}
return view('users.index', compact('users'));
}
}
```

To see more usage in detail, please visit the [Adldap2 Repository](http://github.com/Adldap2/Adldap2);

Expand All @@ -87,29 +92,33 @@ to the users as you would a regular laravel application.
### Installation

Insert the `AdldapAuthServiceProvider` into your `config/app.php` file:
```php
Adldap\Laravel\AdldapAuthServiceProvider::class,
```

Adldap\Laravel\AdldapAuthServiceProvider::class,

Publish the auth configuration:

php artisan vendor:publish
Change the auth driver in `config/auth.php` to `adldap`:
```bash
php artisan vendor:publish
```

/*
|--------------------------------------------------------------------------
| Default Authentication Driver
|--------------------------------------------------------------------------
|
| This option controls the authentication driver that will be utilized.
| This driver manages the retrieval and authentication of the users
| attempting to get access to protected areas of your application.
|
| Supported: "database", "eloquent"
|
*/

'driver' => 'adldap',
Change the auth driver in `config/auth.php` to `adldap`:
```php
/*
|--------------------------------------------------------------------------
| Default Authentication Driver
|--------------------------------------------------------------------------
|
| This option controls the authentication driver that will be utilized.
| This driver manages the retrieval and authentication of the users
| attempting to get access to protected areas of your application.
|
| Supported: "database", "eloquent"
|
*/

'driver' => 'adldap',
```

### Usage

Expand All @@ -123,27 +132,31 @@ This option just allows you to set your input name to however you see fit, and a
In your login form, change the username form input name to your configured input name.

By default this is set to `email`:
```html
<input type="text" name="email" />

<input type="text" name="email" />

<input type="password" name="password" />

You'll also need to add the following to your AuthController if you're not overriding the default postLogin method.
<input type="password" name="password" />
```

protected $username = 'email';
You'll also need to add the following to your AuthController if you're not overriding the default postLogin method.
```php
protected $username = 'email';
```

If you'd like to use the users `samaccountname` to login instead, just change your input name and auth configuration:
```html
<input type="text" name="username" />

<input type="text" name="username" />

<input type="password" name="password" />
<input type="password" name="password" />
```

> **Note**: If you're using the `username` input field, make sure you have the `username` field inside your users database
table as well. By default, laravel's migrations use the `email` field.

Inside `config/adldap_auth.php`

'username_attribute' => ['username' => 'samaccountname'],
```php
'username_attribute' => ['username' => 'samaccountname'],
```

> **Note** The actual authentication is done with the `login_attribute` inside your `config/adldap_auth.php` file.

Expand Down Expand Up @@ -171,57 +184,59 @@ attributes here, however be sure that your database table contains the key you'v

Inside your `config/adldap_auth.php` file there is a configuration option named `bind_user_to_model`. Setting this to
true sets the `adldapUser` property on your configured auth User model to the Adldap User model. For example:
```php
if (Auth::attempt($credentials)) {
$user = Auth::user();

if(Auth::attempt($credentials))
{
$user = Auth::user();

var_dump($user); // Returns instance of App\User;

var_dump($user->adldapUser); // Returns instance of Adldap\Models\User;

// Retrieving the authenticated LDAP users groups
$groups = $user->adldapUser->getGroups();
}
var_dump($user); // Returns instance of App\User;

var_dump($user->adldapUser); // Returns instance of Adldap\Models\User;

// Retrieving the authenticated LDAP users groups
$groups = $user->adldapUser->getGroups();
}
```

You **must** insert the trait `Adldap\Laravel\Traits\AdldapUserModelTrait` onto your configured auth User model, **OR**
Add the public property `adldapUser` to your model.

// app/User.php

<?php

namespace App;

use Adldap\Laravel\Traits\AdldapUserModelTrait;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
use Authenticatable, CanResetPassword, AdldapUserModelTrait; // Insert trait here

/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'email', 'password'];

/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
}
```php
// app/User.php

<?php

namespace App;

use Adldap\Laravel\Traits\AdldapUserModelTrait;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
use Authenticatable, CanResetPassword, AdldapUserModelTrait; // Insert trait here

/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'email', 'password'];

/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
}
```