我有一个用户登录系统,它由两个步骤组成:
- 用户登录
- 尝试将用户绑定到AD(这是LDAP认证模块)
- 如果用户可以通过AD登录,检查Users表中是否存在该用户
- 如果Users表中不存在user,只添加"username"answers"nice name"。
- 如果用户被添加,他们将不属于任何团队,并将被通知联系其经理继续设置帐户。
- 将用户列加载到Laravel Session
credentials
-所以现在我们有AD登录名,"nice"名称(第一个最后一个),选定的团队ID等。没有密码,因为用户已通过身份验证。
- 团队选择-表
userTeams
透视表 - 将用户路由到团队仪表板
现在,我有这个路由:
// This group forces user to be logged in; auth() will check if user has selected a team and ensures the team exists.
Route::group(array('before' => 'auth'), function() {
// Handle team - URL will be /dashboard -- Team controller based on view_name from `teamUsers` table
$teamUser = php_sapi_name() == "cli" ? null : @TeamUser::find(Session::get('credentials.team'));
if(!empty($teamUser)) {
$team = ucfirst(strtolower($teamUser->teams()->first()->view_name));
Route::controller('dashboard', 'Team'.$team.'Controller');
}
// Handle index
Route::controller('/', 'IndexController');
});
然而,我不觉得这是一个非常优雅的解决方案——主要是因为我不能用这种方法实现反向路由。
有什么建议吗?
我个人会选择在它自己的过滤器中做这个逻辑,而不是在Route Group闭包中,因为你所做的只是根据变量将用户"过滤"到不同的路由(与Auth过滤器所做的一样)。
Route::filter('whatever', function()
{
$teamUser = php_sapi_name() == "cli" ? null : @TeamUser::find(Session::get('credentials.team'));
if ( empty($teamUser) )
Redirect::route('IndexController');
});
Route::group(array('before' => 'auth|whatever', function()
{
Route::controller('someController');
Route::controller('/', 'IndexController');
}));