K
K
Koal Koalich2018-03-07 07:36:39
Yii
Koal Koalich, 2018-03-07 07:36:39

How to connect Adldap2 in Yii2?

Please tell me from which side to approach https://github.com/Adldap2/Adldap2/ ? I read from https://github.com/Adldap2/Adldap2/blob/master/doc... and further and don't understand how and where to connect it? The package itself is installed. Can someone show with an example?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2018-03-07
@slo_nik

Good morning.
If you installed the package via composer, then your composer.json file should mention this package of yours.
So in the right file, at the very beginning, you must connect the package class through "use" and then use it, as written in the instructions.

use \Adldap\Adldap;

$ad = new Adldap();

D
DieZz, 2018-05-18
@DieZz

If the question is still relevant, I can share my implementation
. First of all, I created a component that is a wrapper for Adldap

spoiler
<?php

namespace app\components;

use Adldap\Adldap;
use Adldap\Auth\BindException;
use Adldap\Auth\PasswordRequiredException;
use Adldap\Auth\UsernameRequiredException;
use Adldap\Connections\ProviderInterface;
use Adldap\Models\Group;
use Adldap\Models\Model;
use Adldap\Models\User;
use app\components\interfaces\ExternalAuthProviderInterface;
use Illuminate\Support\Collection;
use yii\base\Component;

/**
 * Class LdapService
 * Wrapper to work with LDAP package.
 *
 * @package app\components
 */
class LdapService extends Component implements ExternalAuthProviderInterface
{
    /**
     * Account suffix.
     *
     * @var string
     */
    public $accountSuffix = '';
    /**
     * List of domain controllers.
     *
     * @var array
     */
    public $domainControllers = [];
    /**
     * Base DN.
     *
     * @var string
     */
    public $baseDn;
    /**
     * Domain admin user name.
     *
     * @var string
     */
    public $adminUserName;
    /**
     * Domain admin password.
     *
     * @var string
     */
    public $adminPassword;
    /**
     * Connection provider to the AD.
     *
     * @var ProviderInterface
     */
    private $provider;

    /**
     * Attempt to login in domain.
     *
     * @param string $login    login.
     * @param string $password password.
     *
     * @throws BindException if there are connections errors.
     * @throws PasswordRequiredException
     * @throws UsernameRequiredException
     *
     * @return bool
     */
    public function attempt(string $login, string $password): bool
    {
        return $this->getProvider()
            ->auth()
            ->attempt($login, $password);
    }

    /**
     * Find user in the AD by login(account name).
     *
     * @param string $login login/account name.
     *
     * @throws \Adldap\Models\ModelNotFoundException
     * @throws BindException
     * @throws \InvalidArgumentException
     * @return User|Model
     */
    public function findUserByLogin(string $login): User
    {
        return $this->getProvider()
            ->search()
            ->users()
            ->where('samaccountname', '=', $login)
            ->firstOrFail();
    }

    /**
     * Find user in the AD by full name.
     *
     * @param string $username users full name.
     *
     * @throws \Adldap\Models\ModelNotFoundException
     * @throws BindException
     * @throws \InvalidArgumentException
     * @return User|Model
     */
    public function findUserByFullName(string $username): User
    {
        return $this->getProvider()
            ->search()
            ->users()
            ->where('cn', '=', $username)
            ->firstOrFail();
    }

    /**
     * Returns list of all group in the AD.
     *
     * @throws BindException
     * @return Collection|Group[]
     */
    public function getAllGroups(): Collection
    {
        return $this->getProvider()
            ->search()
            ->groups()
            ->get();
    }

    /**
     * Returns configured AD provider.
     *
     * @throws BindException
     * @return ProviderInterface
     */
    private function getProvider(): ProviderInterface
    {
        if (null === $this->provider) {
            $ad = new Adldap($this->getConfig());
            $this->provider = $ad->connect('default');
        }

        return $this->provider;
    }

    /**
     * Returns config to connect to the ldap domain.
     *
     * @return array
     */
    private function getConfig(): array
    {
        return [
            'default' => [
                'domain_controllers' => $this->domainControllers,
                'base_dn'            => $this->baseDn,
                'admin_username'     => $this->adminUserName,
                'admin_password'     => $this->adminPassword,
                'account_suffix'     => $this->accountSuffix,
            ],
        ];
    }
}

Now this component must be connected through the application config
spoiler
'components' => [
        ...
        'ldap' => [
            'class'             => \app\components\LdapService::class,
            'accountSuffix'     => '@test',
            'domainControllers' => ['192.168.1.1'],
            'baseDn'            => 'DC=admin',
            'adminUserName'     => 'admin',
            'adminPassword'     => 'password',
        ],
    ],

And then we can already use it in our code
$user = \Yii::$app->get('ldap')->findUserByLogin('someLogin');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question