phantomnet / laravel-ldap-provider

该软件包的官方仓库似乎已不存在,因此软件包已被冻结。

dev-master 2015-03-27 21:04 UTC

This package is not auto-updated.

Last update: 2024-01-20 12:47:55 UTC


README

本项目的目标是连接现有的认证系统,并通过LDAP提供连接和访问,而不实现自己的会话控制系统。本软件包内建了对cartalyst的Sentry软件包的支持,以提供认证服务。

如果您想连接您的认证系统,只需发送一个拉取请求,您的代码将得到审查并加入!:-)

支持的认证系统

支持的认证系统包括

配置

打开您的composer.json文件,并在require数组中添加以下内容

"phantomnet/laravel-ldap-provider": "dev-master"

修改composer.json文件后,请确保对其进行验证

composer validate

使用以下命令安装依赖和软件包

composer install

或者

composer update

Laravel 4

以下配置选项通过app/config/app.php文件进行

providers数组中添加以下内容

/* LDAP Provider */
'Phantomnet\Ldap\LdapServiceProvider',

在aliases数组中添加以下内容

/* LDAP Auth Provider */
'LDAP'   => 'Phantomnet\Ldap\Facades\Laravel\Ldap',

迁移

php artisan migrate --package=phantomnet/ldap

配置

php artisan config:publish phantomnet/ldap

这将发布app/config/packages/phantomnet/ldap/config.php,在那里可以对其进行修改。

使用

目标是尽可能重用现有代码,而不必创建如会话管理等独立的认证控制,因此通过桥接器提供与现有认证解决方案的集成。

以下示例是使用LDAP进行认证的逻辑,应将其放置在处理登录的控制器中。请注意,这不是一个完整的示例,仅应作为模型使用!

使用Laravel 4语法。

$credentials = array(
    /* this is the login attribute. See the 
     * configuration file for properly changing it */
	'email'    => Input::get('username'), 
	'password' => Input::get('password')
);

$remember = (bool)Input::has('remember-me');

try
{
	/* Attempt to authenticate the user with the given credentials */
	LDAP::getAuthProvider()->authenticate($credentials, $remember);

	/* Assuming login worked, we can route to the admin dashboard */
	return Redirect::intended('admin');
}
catch (Phantomnet\Ldap\users\WrongPasswordException $e)
{
	// If we get to this point, just return an error
    return Redirect::route('login') // named route "login"
        ->withInput()
        ->with('error', 'Incorrect username or password.');
}
catch (Phantomnet\Ldap\Users\UserNotFoundException $e)
{
	/* This will cover a wrong password as well */
	try
	{
		/* Assuming we have any targets with on-the-fly creation 
		 * allowed we can try to login a new user here and then 
		 * redirect them */
		LDAP::onTheFlyCreation($credentials, $remember);

		/* If we made it here .. we found a valid user and 
		 * attempted to authenticate them */
		return Redirect::intended('admin');
	}
	catch (Phantomnet\Ldap\Users\UserNotFoundException $e) 
	{
		// We do nothing in here
	}

	// If we get to this point, just return an error
    return Redirect::route('login') // named route "login"
        ->withInput()
        ->with('error', 'Incorrect username or password.');
}