phpcodemaker / ci4-authldap
使用LDAP协议进行CodeIgniter4用户认证
v1.0
2020-05-18 04:24 UTC
Requires
- php: >=7.2
This package is auto-updated.
Last update: 2024-09-18 19:28:49 UTC
README
[CodeIgniter4.0.3 LDAP认证]
composer require phpcodemaker/ci4-authldap:1.0
感谢Forumsys网站提供的LDAP连接测试
https://www.forumsys.com/tutorials/integration-how-to/ldap/online-ldap-test-server
为了在本地检查您的LDAP连接,请在终端中执行以下命令
/var/www/html$ ldapsearch -W -h ldap.forumsys.com -D "uid=riemann,dc=example,dc=com" -b "dc=example,dc=com"
#公共方法通过用户名和密码验证用户 AuthLdap::authenticate($userName, $password);
从LDAP目录检索所有用户
AuthLdap::getAllUsers();
从LDAP目录检索所有组
AuthLdap::getAllGroups();
创建自己的用户控制器以实现登录和注销,如下所示
<?php
namespace App\Controllers;
use AuthLdap\Libraries\AuthLdap;
use CodeIgniter\View\View;
/**
* Class User
* @package App\Controllers
* @author Karthikeyan C <karthikn.mca@gmail.com>
*/
class User extends BaseController
{
/**
* @var AuthLdap $authLdap
*/
private $authLdap;
/**
* If Already declared Session in BaseController,
* then comment the below declaration
* @var \CodeIgniter\Session\Session
*/
private $session;
/**
* User constructor.
*/
public function __construct()
{
/**
* If Already declared Session in BaseController,
* then comment the below declaration
*/
$this->session = \Config\Services::session();
}
/**
* @return \CodeIgniter\HTTP\RedirectResponse|View (postlogin redirect | pre-login template)
* @author Karthikeyan C <karthikn.mca@gmail.com>
*/
public function login()
{
if (null !== $this->request->getPost('username')
&& null !== $this->request->getPost('password'))
{
$this->authLdap = new AuthLdap();
if (is_object($this->authLdap)
&& method_exists($this->authLdap, 'authenticate'))
{
$authenticatedUserData = $this->authLdap->authenticate(
trim($this->request->getPost('username')),
trim($this->request->getPost('password'))
);
if (!empty($authenticatedUserData))
{
$this->session->set($authenticatedUserData);
return redirect()->to('/user/dashboard');
}
else {
// report login failure
}
}
else {
//report about ldap error
}
}
return view('user/login');
}
/**
* @return string
* @author Karthikeyan C <karthikn.mca@gmail.com>
*/
public function logout()
{
$this->session->destroy();
return view('user/logout');
}
/**
* @author Karthikeyan C <karthikn.mca@gmail.com>
*/
public function dashboard()
{
// do your own stuff here
}
}
[登录]
登录 : https:///user/login
注销 : https:///user/logout
[测试凭证]
用户名 : riemann
密码 : password