LoginSignup
6
8

More than 5 years have passed since last update.

php7でopenLDAPに繋ぎユーザ管理を行う(前編)

Last updated at Posted at 2017-06-11

openLDAPの構築は済んでいるものとします。
http://qiita.com/minechan1234/items/4d1cc407f348fa125819

済んでない方もphp-ldapを入れてもらえれば動くはずです。
# yum install --enablerepo=remi,remi-php70 php-ldap

データ構造を考える

単純にou配下にユーザが居る構成にします。
ouは複数存在可能です。
ou配下には0以上のユーザが居るものとします。
1.png

ouというのは部署や会社、テナントだと思うとイメージしやすいと思います。
今回は「Sales」と「Development」と「Planning」の3つの部署を作成します。

ldifで初期データ作成

まずは初期データを作ります。
vi /home/.ldap_work/data.ldif

dn: dc=example,dc=com
dc: example
description: The Example Corporation
o: Example Corporation
objectclass: dcObject
objectclass: organization

dn: ou=Development,dc=example,dc=com
objectclass: organizationalUnit
objectclass: top
ou: Development

dn: cn=0001,ou=Development,dc=example,dc=com
cn: 0001
objectclass: inetOrgPerson
objectclass: organizationalPerson
objectclass: person
objectclass: top
sn: HOGE
uid: hoge
userpassword: hogehoge

dn: cn=0002,ou=Development,dc=example,dc=com
cn: 0002
objectclass: inetOrgPerson
objectclass: organizationalPerson
objectclass: person
objectclass: top
sn: FUGA
uid: fuga
userpassword: fugafuga

dn: ou=Planning,dc=example,dc=com
objectclass: organizationalUnit
objectclass: top
ou: Planning

dn: cn=0003,ou=Planning,dc=example,dc=com
cn: 0003
objectclass: inetOrgPerson
objectclass: organizationalPerson
objectclass: person
objectclass: top
sn: NUGA
uid: nuga
userpassword: nuga

dn: ou=Sales,dc=example,dc=com
objectclass: organizationalUnit
objectclass: top
ou: Sales

エントリを追加します。
ldapadd -f /home/.ldap_work/data.ldif -x -D "cn=Manager,dc=example,dc=com" -w Hoge1234

phpでldapsearch

phpからldapに接続してデータを検索してみます。
まずはou一覧を取得してみます。

<?php

class MyLdap
{

    //LDAP接続情報
    const LDAP_HOST = "localhost";
    const LDAP_PORT = 389;
    const LDAP_DC = "dc=example,dc=com";
    const LDAP_DN = "cn=Manager,dc=example,dc=com";
    const LDAP_PASS = "Hoge1234";

    //接続状態を保持
    protected $ldap_conn;
    protected $ldap_bind;

    /**
     * LDAPに接続するための初期設定
     */
    public function __construct()
    {
        $this->ldap_conn = ldap_connect(self::LDAP_HOST, self::LDAP_PORT);
        if (!$this->ldap_conn) {
            exit('ldap_conn');
        }
        $this->ldap_bind = ldap_bind($this->ldap_conn, self::LDAP_DN, self::LDAP_PASS);
        if (!$this->ldap_bind) {
            exit('ldap_bind');
        }
    }

    /**
     * コネクションを切るメソッド
     */
    public function close()
    {
        ldap_close($this->ldap_conn);
    }

    /**
     * ouの一覧を返すメソッド
     * @return array
     */
    public function getAllOu()
    {
        $ldap_search = ldap_search($this->ldap_conn, self::LDAP_DC, "ou=*");
        $get_entries = ldap_get_entries($this->ldap_conn, $ldap_search);
        return $get_entries;
    }

}

//以下ou一覧を表示する
$ldap = new MyLdap();
$ous = $ldap->getAllOu();
print_r($ous);
$ldap->close();

Development配下のユーザも取得してみます。

<?php

class MyLdap
{

    //LDAP接続情報
    const LDAP_HOST = "localhost";
    const LDAP_PORT = 389;
    const LDAP_DC = "dc=example,dc=com";
    const LDAP_DN = "cn=Manager,dc=example,dc=com";
    const LDAP_PASS = "Hoge1234";

    //接続状態を保持
    protected $ldap_conn;
    protected $ldap_bind;

    /**
     * LDAPに接続するための初期設定
     */
    public function __construct()
    {
        $this->ldap_conn = ldap_connect(self::LDAP_HOST, self::LDAP_PORT);
        if (!$this->ldap_conn) {
            exit('ldap_conn');
        }
        $this->ldap_bind = ldap_bind($this->ldap_conn, self::LDAP_DN, self::LDAP_PASS);
        if (!$this->ldap_bind) {
            exit('ldap_bind');
        }
    }

    /**
     * コネクションを切るメソッド
     */
    public function close()
    {
        ldap_close($this->ldap_conn);
    }

    /**
     * ouの一覧を返すメソッド
     * @return array
     */
    public function getAllOu()
    {
        $ldap_search = ldap_search($this->ldap_conn, self::LDAP_DC, "ou=*");
        $get_entries = ldap_get_entries($this->ldap_conn, $ldap_search);
        return $get_entries;
    }

    /**
     * 指定ouのcn一覧を返すメソッド
     * @param string $ou
     * @return array
     */
    public function getUsers($ou)
    {
        $ldap_search = ldap_search($this->ldap_conn, "ou=$ou," . self::LDAP_DC, "cn=*");
        $get_entries = ldap_get_entries($this->ldap_conn, $ldap_search);
        return $get_entries;
    }

}

//以下cn一覧を表示する
$ldap = new MyLdap();
$ous = $ldap->getUsers('Development');
print_r($ous);
$ldap->close();

こんな感じでユーザ情報を取得出来ます。
パスワードも取得できるので、ログイン認証なんかも自前で出来ます。
(本来はパスワードでフィルターかけるべきですが)

長くなってしまったので、続きは次回の記事にします。

6
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
8