xavrsl/ldap 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

xavrsl/ldap

Composer 安装命令:

composer require xavrsl/ldap

包简介

Add Ldap Search possibilities to Laravel

README 文档

README

This package is an attempt to provide a way to search through an Ldap Directory like you would query a database with Eloquent.

It now also comes with a AuthUserProvider so that you can hook it in as a AuthProvider for your Laravel app. In order to accomplish that, I have stolen very large pieces of this great package : Adldap2/Adldap2-Laravel

The only reason why I'm not using Adldap2-Laravel for my projects is because it doesn't support OpenLdap yet, so if you use ActiveDirectory, go check it out !

Installing

Require it from your command line :

composer require xavrsl/ldap

Add the service provider to config/app :

Xavrsl\Ldap\LdapServiceProvider::class,

Add the facade to the alias array (also in config/app):

'Ldap' => Xavrsl\Ldap\Facades\Ldap::class,

You then need to publish and customize the config file to indicate the location of your ldap server and also set your dn, attributes etc. :

php artisan vendor:publish

You're now ready to use this package !

Usage

First remember to set ALL your config parameters. All sections have been well documented in the comments. Any attribute that you want to retrieve MUST be specified in the 'attributes' array. This may seem strange but the reason I built this package was because I needed a way to query an LDAP Directory every two seconds on a big list of users. So I needed Caching. The only way I was able to cache all the fields was by providing a retrieved attributes array.

  • Return an attribute from one member of your organisation :
// First possibility, with find/where methods and get
Ldap::find('people')->where('uid', 8162)->get('displayname');

// Second possibility, using an alias for the get method
Ldap::find('people')->where('uid', 8162)->displayname;

// Third possibility, attribute in camelCase format
Ldap::find('people')->where('uid', 8162)->displayName;

// If default attribute is set to 'uid' in conf, you can use the short method
Ldap::people(8162)->displayname;

All those possibilities should return the same string (our user's displayname) :

Bobby Blake
  • Return multiple attributes for a single member of organisation :
// Let's directly use the short method
Ldap::people(8162)->get('displayname, mail');

// May as well use an array instead of a string
Ldap::people(8162)->get(['displayName', 'mail']);

This should return :

array(1) [
    '8162' => array(2) [
        'displayname' => string (11) "Bobby Blake"
        'mail' => string (22) "bobby.blake@domain.org"
    ]
]

If you change the key in your config to some attribute like 'login' for exemple, you get :

array(1) [
    'bobblake' => array(2) [
        'displayname' => string (11) "Bobby Blake"
        'mail' => string (22) "bobby.blake@domain.org"
    ]
]

NOTE : You don't need to add the 'key' attribute's value in the 'attributes' array in the config. The package does that for you.

  • Return multiple attributes from multiple members of the organisation :
// Let's use the short method again
Ldap::people('8162, 128')->get('displayname, mail');

// Same thing using arrays
Ldap::people(['8162', '128'])->get(['displayName', 'mail']);

// Longer syntax
Ldap::find('people')->where('uid', ['8162', '128'])->get(['displayName', 'mail']);

// Base your search on another attribute
Ldap::find('people')->where('login', ['bobblake', 'johndoe'])->get(['displayName', 'mail']);

This should return :

array(2) [
    '108' => array(2) [
        'displayname' => string (8) "John Doe"
        'mail' => string (20) "john.doe@domain.org"
    ]
    '8162' => array(2) [
        'displayname' => string (11) "Bobby Blake"
        'mail' => string (22) "bobby.blake@domain.org"
    ]
]

You can also return all the attributes you've set in the 'attributes' config property :

// The long way
Ldap::find('people')->where('login', ['bobblake', 'johndoe'])->get();

// The short way
Ldap::people('108, 8162')->get();
  • Query the Ldap Directory based on a wildcard :
// The long way
Ldap::find('people')->where('login', 'bob*')->get(['displayName', 'mail']);

// The short way (assuming you have set the 'filter' attribute to 'login' in config)
Ldap::people('bob*')->get(['displayName', 'mail']);

// Also works with multiple wildcards
Ldap::people('bob*, john*')->get(['displayName', 'mail']);

You get a result looking something like this :

array(2) [
    '108' => array(2) [
        'displayname' => string (8) "John Doe"
        'mail' => string (20) "john.doe@domain.org"
    ]
    '4021' => array(2) [
        'displayname' => string (10) "John Smith"
        'mail' => string (22) "john.smith@domain.org"
    ]
    '8162' => array(2) [
        'displayname' => string (11) "Bobby Blake"
        'mail' => string (22) "bobby.blake@domain.org"
    ]
    '9520' => array(2) [
        'displayname' => string (12) "Bob McCormac"
        'mail' => string (24) "bobby.mccormac@domain.org"
    ]
]

You get the idea !!

  • Authenticate against the Ldap Directory :
// Depending on the filter attribute you've set in the config
Ldap::auth('bobblake', 'm7V3ryStr0ngP@ssw0rd!')

Will simply return TRUE or FALSE.

NOTE : Don't forget to set the dn attribute in config for user authentication.

TODOs :

There is still a lot of work ahead to make this package complete. Here's a list of what you could expect in the future :

  • Create / update attributes from the Ldap. For now, the package can only read the Ldap.
  • Query multiple Organisation Units (Ldap branches, or OU. ex. : People, Groups, Mail, ...). This should work pretty soon...
  • Use Active Directory and Open Ldap. For now, only Open Ldap directories work.

xavrsl/ldap 适用场景与选型建议

xavrsl/ldap 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.92k 次下载、GitHub Stars 达 22, 最近一次更新时间为 2013 年 11 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「search」 「ldap」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 xavrsl/ldap 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 xavrsl/ldap 我们能提供哪些服务?
定制开发 / 二次开发

基于 xavrsl/ldap 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.92k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 23
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 22
  • Watchers: 5
  • Forks: 17
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-11-14