我想知道在laravel 4
中如何实现以下功能。我有一个过滤器来检查用户是否在所有具有user/*的路由上进行了身份验证。我的过滤器按预期工作,但假设一个用户登录了他们的url
,它看起来像这个user/id
。如何防止经过身份验证的用户查看其他用户?
另一种方法是更改url。。为什么有类似url的用户/{id}?只需将其更改为例如
user/profile
并且在控制器内部执行类似的操作:
$user = Auth::user();
这样用户就不能伪造id了。。我只在需要编辑一些用户的管理区域中使用带有id的url:
/admin/{id}/edit
在Auth
过滤器中,您可以访问路由参数('user/{id}'
),并可以使用类似的url
中传递的id
检查登录用户的id
Route::filter('auth', function($route)
{
// get the id from rouqe
$id = $route->getParameter('id');
if( Auth::check() && Auth::user()->id != $id) {
// not authenticated user, so access is denied
return Redirect::to('/');
}
});
如果用户只能查看自己的配置文件是一项总括策略,那么您可以检查来自用户/{id}路由的id是否与登录会话的当前用户id匹配吗?例如,在配置文件控制器中,类似于:
public function getProfile($profile_id) {
if (Auth::user()->id != $profile_id) {
//this is not your profile - don't be nosey!
} else {
//have you got nothing better to do than look at yourself all day!
}
}
Glen