Skip to content

Commit

Permalink
Merge pull request #12 from edvler/v3
Browse files Browse the repository at this point in the history
V3
  • Loading branch information
EDVler authored Mar 8, 2017
2 parents 4da1193 + 68e9ed7 commit 92af9d2
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 36 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
],
"require": {
"adldap2/adldap2": "^6.0.0",
"adldap2/adldap2": "^7.0.0",
"yiisoft/yii2": "^2.0.0"
},
"autoload": {
Expand Down
60 changes: 48 additions & 12 deletions docs/USAGE_WITHOUT_USER_MODEL.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
# Usage method 1: Simple usage without a user model

## This is only a Quick-Start-Guide!
yii2-adldap-module is only a wrapper class. The examples below are all taken from the official documentation of the Adldap2 repository.

You can find the documentation here: https://github.com/Adldap2/Adldap2/tree/master/docs

## Syntax basics

### Different ways to call Adldap2 functions
```php
//...
$un = 'testuser';

/*
There are three ways available to call Adldap2 function.
If you use more providers (multiple Active Directory connections)
you make one as default and you can call this one with Method1 or Method2
and the second one will be called with Method3.
*/

// Method 1: uses the default provider given in the configuration above (array key defaultProvider)
$user = \Yii::$app->ad->search()->findBy('sAMAccountname', $un);
// Method 2: uses the default provider given in the configuration above (array key defaultProvider)
$user = \Yii::$app->ad->getDefaultProvider()->search()->findBy('sAMAccountname', $un);
// Method 3: get the provider by name (here name default is used).
$user = \Yii::$app->ad->getProvider('default')->search()->findBy('sAMAccountname', $un);
print_r($user); //print all informations retrieved from Active Directory
//...
```

### Multiline example
For almost all operations you need a provider. You can access the provider in the following ways.
```php
Expand All @@ -24,16 +51,25 @@ echo print_r($result,true);
echo '</pre>';
```

### Oneline example without getDefaultProvider()
```php
$result = \Yii::$app->ad->search()->select(['cn', 'samaccountname', 'telephone', 'mail'])->where('samaccountname', '=', 'matthias')->get();

echo '<pre>';
echo print_r($result,true);
echo '</pre>';
```

---

## Examples

### Authenticate user
https://github.com/Adldap2/Adldap2/blob/v6.1/docs/authenticating.md
https://github.com/Adldap2/Adldap2/blob/master/docs/authenticating.md
```php
$un = 'testuser';
$pw = 'VeryStrongPw';
if(\Yii::$app->ad->getDefaultProvider()->auth()->attempt($un,$pw)) {
if(\Yii::$app->ad->auth()->attempt($un,$pw)) {
echo 'User successfully authenticated';
} else {
echo 'User or Password wrong';
Expand All @@ -43,10 +79,10 @@ if(\Yii::$app->ad->getDefaultProvider()->auth()->attempt($un,$pw)) {
### Find records
#### With findBy() function
Finding a specific record by a specific attribute. We're looking for a record with the 'samaccountname' of 'testuser'. This euqals to the username in Active Directory.
https://github.com/Adldap2/Adldap2/blob/v6.1/docs/query-builder.md
https://github.com/Adldap2/Adldap2/blob/master/docs/query-builder.md
```php
$un = 'testuser';
$user = \Yii::$app->ad->getDefaultProvider()->search()->findBy('sAMAccountname', $un);
$user = \Yii::$app->ad->search()->findBy('sAMAccountname', $un);

//print all informations of the user object
echo '<pre>';
Expand All @@ -57,7 +93,7 @@ echo '</pre>';
#### With get() function
```php
$un = 'testuser';
$user = \Yii::$app->ad->getDefaultProvider()->search()->where('sAMAccountName', '=', $un)->get();
$user = \Yii::$app->ad->search()->where('sAMAccountName', '=', $un)->get();

//print all informations of the user object
echo '<pre>';
Expand All @@ -66,16 +102,16 @@ echo '</pre>';
```

### Group Membership
See sourcecode function getMememberOf() or inGroup().
https://github.com/Adldap2/Adldap2/blob/v6.1/src/Models/Traits/HasMemberOfTrait.php
See sourcecode function getGroups() or inGroup().
https://github.com/Adldap2/Adldap2/blob/master/src/Models/Traits/HasMemberOf.php

#### Check if user is in group with getMemberOf() function.
#### Check if user is in group with getGroups() function.
```php
$un = 'testuser';
$user = \Yii::$app->ad->getDefaultProvider()->search()->findBy('sAMAccountname', $un);
$user = \Yii::$app->ad->search()->findBy('sAMAccountname', $un);

$gn = 'the-group-name';
foreach($user->getMemberOf() as $group)
foreach($user->getGroups() as $group)
{
if($gn == $group->getName()) {
echo 'TRUE';
Expand All @@ -85,7 +121,7 @@ foreach($user->getMemberOf() as $group)
#### Check if user is in group with inGroup() function.
```php
$un = 'testuser';
$user = \Yii::$app->ad->getDefaultProvider()->search()->findBy('sAMAccountname', $un);
$user = \Yii::$app->ad->search()->findBy('sAMAccountname', $un);

$gn = 'the-group-name';
if ($user->inGroup($gn)) {
Expand All @@ -98,4 +134,4 @@ if ($user->inGroup($gn)) {
## More Examples
yii2-adldap-module is only a wrapper class. The examples above are all taken from the official documentation of the Adldap2 repository.

You can find the documentation here: https://github.com/Adldap2/Adldap2/tree/v6.1/docs
You can find the documentation here: https://github.com/Adldap2/Adldap2/tree/master/docs
6 changes: 3 additions & 3 deletions docs/USAGE_WITH_USER_MODEL.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ If you leave the default configuration, the following is happening on login (and
For a working group to role assignment you have to create the roles in yii2! The roles would NOT be automatically created.

### Example for group configuration
In Step 7 of Task 1 you are have already done a successfull login hopefully. But the problem is that every user in Active Directory with a valid password and active account now can login in yii2. Thats not a good solution!
In Step 10 of Task 1 you are have already done a successfull login hopefully. But the problem is that every user in Active Directory with a valid password and active account now can login in yii2. Thats not a good solution!

Before you continue read the the commets in source code starting at line 125. (see file @vendor/edvlerblog/yii2-adldap-module/src/model/UesrDbLdap.php).
Before you continue read the the commets in source code starting at line 148 ([/src/model/UserDbLdap.php#L148](/src/model/UserDbLdap.php#L148)).

#### Login only possible when a role is assigned to the user
Now add the following to your config/params.php
Expand Down Expand Up @@ -201,7 +201,7 @@ The answer is simple for two reasons:
- yii2 has no corresponding role

#### Create example role
Look into the source code of the function actionCreateExampleRole (see file @vendor/edvlerblog/yii2-adldap-module/src/commands/LdapController.php).
Look into the source code of the function actionCreateExampleRole ([/src/commands/LdapController.php](/src/commands/LdapController.php)).

As you can see two permissions are created **(permissionDisplayDetailedAbout, permissionToUseContanctPage)** and assigend to the role
**yii2_example_group**.
Expand Down
46 changes: 36 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
## Version

Current Version:
yii2-adldap-module Releases beginning with tag v2.*.* are reserved for Adldap2 v6.*
The corresponding Adldap2 repository is https://github.com/Adldap2/Adldap2/tree/v6.1
yii2-adldap-module Releases beginning with tag v3.*.* are reserved for Adldap2 v7.*
The corresponding Adldap2 repository is https://github.com/Adldap2/Adldap2/tree/master

**Keep this in mind if you are browsing the GitHub Repository of Adldap2**

Expand All @@ -21,11 +21,11 @@ The preferred way to install this extension is through [Composer](http://getcomp

Either run
```
php composer.phar require edvlerblog/yii2-adldap-module "^2.0.0"
php composer.phar require edvlerblog/yii2-adldap-module "^3.0.0"
```
or add
```
"edvlerblog/yii2-adldap-module": "^2.0.0"
"edvlerblog/yii2-adldap-module": "^3.0.0"
```
to the require section of your composer.json

Expand All @@ -42,7 +42,20 @@ Add this code in your components section of the application configuration (eg. c
'class' => 'Edvlerblog\Adldap2\Adldap2Wrapper',

/*
* ADLap2 v6.X.X can handle multiple providers to different Active Directory sources.
* Set the default provider to one of the providers defined in the
* providers array.
*
* If this is commented out, the entry 'default' in the providers array is
* used.
*
* See https://github.com/Adldap2/Adldap2/blob/master/docs/connecting.md
* Setting a default connection
*
*/
//'defaultProvider' => 'another_provider',

/*
* Adlapd2 v7.X.X can handle multiple providers to different Active Directory sources.
* Each provider has it's own config.
*
* In the providers section it's possible to define multiple providers as listed as example below.
Expand All @@ -56,12 +69,12 @@ Add this code in your components section of the application configuration (eg. c
* $provider = \Yii::$app->ad->getDefaultProvider();
* or with $provider = \Yii::$app->ad->getProvider('default');
*/
'default' => [
'default' => [ //Providername default
// Connect this provider on initialisation of the LdapWrapper Class automatically
'autoconnect' => true,

// The config has to be defined as described in the Adldap2 documentation.
// e.g. https://github.com/Adldap2/Adldap2/blob/v6.1/docs/configuration.md
// https://github.com/Adldap2/Adldap2/blob/master/docs/configuration.md
'config' => [
// Your account suffix, for example: [email protected]
'account_suffix' => '@example.lan',
Expand All @@ -86,12 +99,12 @@ Add this code in your components section of the application configuration (eg. c
* You can get the provider with:
* or with $provider = \Yii::$app->ad->getProvider('another_provider');
*/
'another_provider' => [
'another_provider' => [ //Providername another_provider
// Connect this provider on initialisation of the LdapWrapper Class automatically
'autoconnect' => false,

// The config has to be defined as described in the Adldap2 documentation.
// e.g. https://github.com/Adldap2/Adldap2/blob/v6.1/docs/configuration.md
// https://github.com/Adldap2/Adldap2/blob/master/docs/configuration.md
'config' => [
// Your account suffix, for example: [email protected]
'account_suffix' => 'test.lan',
Expand All @@ -113,7 +126,7 @@ Add this code in your components section of the application configuration (eg. c
```

See official documentation for all config options.
https://github.com/Adldap2/Adldap2/blob/v6.1/docs/configuration.md
https://github.com/Adldap2/Adldap2/blob/master/docs/configuration.md

## Usage - Method 1 and/or Method 2

Expand All @@ -125,7 +138,20 @@ You only call the the component as usual.
```php
//...
$un = 'testuser';

/*
There are three ways available to call Adldap2 function.
If you use more providers (multiple Active Directory connections)
you make one as default and you can call this one with Method1 or Method2
and the second one will be called with Method3.
*/

// Method 1: uses the default provider given in the configuration above (array key defaultProvider)
$user = \Yii::$app->ad->search()->findBy('sAMAccountname', $un);
// Method 2: uses the default provider given in the configuration above (array key defaultProvider)
$user = \Yii::$app->ad->getDefaultProvider()->search()->findBy('sAMAccountname', $un);
// Method 3: get the provider by name (here name default is used).
$user = \Yii::$app->ad->getProvider('default')->search()->findBy('sAMAccountname', $un);
print_r($user); //print all informations retrieved from Active Directory
//...
```
Expand Down
23 changes: 15 additions & 8 deletions src/Adldap2Wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ class Adldap2Wrapper extends Component
*/
public $providers;

/*
* The name of the default provider
*/
public $defaultProvider = "default";

/**
* init() called by yii.
Expand All @@ -69,18 +73,25 @@ public function init()

foreach($this->providers as $providerName=>$prodivderSettings) {
$config = new \Adldap\Connections\Provider($prodivderSettings['config']);
$this->adLdapInstance->addProvider($providerName, $config);
$this->adLdapInstance->addProvider($config, $providerName);

if($prodivderSettings['autoconnect'] == true) {
$this->adLdapInstance->connect($providerName);
}
}


$providers = $this->adLdapInstance->getProviders();

if (array_key_exists($this->defaultProvider, $providers)) {
$this->adLdapInstance->setDefaultProvider($this->defaultProvider);
} else {
throw new \yii\base\Exception("The given defaultprovder with the name " . $this->defaultProvider . " could not be found. See https://github.com/edvler/yii2-adldap-module/blob/master/readme.md");
}
}


/**
* Use magic PHP function __call to route function calls to the Adldap class.
* Use magic PHP function __call to route ALL function calls to the Adldap class.
* Look into the Adldap class for possible functions.
*
* @param string $methodName Method name from Adldap class
Expand All @@ -89,10 +100,6 @@ public function init()
*/
public function __call($methodName, $methodParams)
{
if (method_exists($this->adLdapInstance, $methodName)) {
return call_user_func_array(array($this->adLdapInstance, $methodName), $methodParams);
} else {
return parent::__call($methodName, $methodParams);
}
return call_user_func_array([$this->adLdapInstance, $methodName], $methodParams);
}
}
20 changes: 18 additions & 2 deletions src/model/UserDbLdap.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ public function validateAuthKey($authKey)
public function validatePassword($password)
{
\Yii::beginProfile('LDAP validatePassword function');
$passwordValid = \Yii::$app->ad->getDefaultProvider()->auth()->attempt($this->username,$password);
$passwordValid = $this->getAdldap2Provider()->auth()->attempt($this->username,$password);
\Yii::endProfile('LDAP validatePassword function');
return $passwordValid;
}
Expand Down Expand Up @@ -763,7 +763,7 @@ public function queryLdapUserObject() {
throw new \yii\base\Exception("Please set username attribute before calling queryLdapUserObject() function.");
}

$userObjectsFound = \Yii::$app->ad->getDefaultProvider()->search()->findBy('sAMAccountname', $this->username);
$userObjectsFound = $this->getAdldap2Provider()->search()->findBy('sAMAccountname', $this->username);

if(count($userObjectsFound) != 1) {
$this->ldapUserObject = null;
Expand All @@ -776,4 +776,20 @@ public function queryLdapUserObject() {

return $this->ldapUserObject;
}

/**
* Get the Adldap2 provider name
*
*
*/

private function getAdldap2Provider() {
if(isset(\Yii::$app->params["yii2-adldap-providername"])) {
$provider =\Yii::$app->ad->getProvider(\Yii::$app->params["yii2-adldap-providername"]);
} else {
$provider =\Yii::$app->ad->getDefaultProvider();
}

return $provider;
}
}

0 comments on commit 92af9d2

Please sign in to comment.