dev-master 2014-07-22 09:47 UTC

This package is not auto-updated.

Last update: 2024-09-24 07:13:34 UTC


README

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

如果您想连接您的认证系统,只需发送一个pull request,您的代码将被审查以供录入!:-)

支持的认证系统

支持的认证系统包括

配置

打开您的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.');
}