Laravel 7.x 只有个人资料的所有者和某些角色可以进入或执行操作



我是Laravel的新手,我不太确定如何获得用户的会话,这样他就可以看到自己的个人资料,但除了管理员角色之外,其他用户看不到。

这就是我认为的

profile/ProfileControllers.php

public function show(User $user)
{
if(Gate::allows('manageUsers')){
return view('Profile.Users.profile')->with([
'user'  =>$user,
]);
}
if ($user->id === $user->id) {
return view('Profile.Users.profile')->with([
'user'  =>$user,
]);
}
return redirect(route('home'));
}

我认为使用$user->id == $user->id意味着session user id = this id http://127.0.0.1:8000/profile/users/{id},但事实并非如此,我可以使用单个用户查看其他用户的配置文件。

manageUsers大门内是用户管理员,如(出纳、销售等(

我认为其他一些方法正在web路由中使用中间件。但我不明白在我的中间件上要放什么

路由/web.php

Route::namespace('Profile')->prefix('profile')->name('profile.')->middleware('?')->group(function(){
Route::resource('/users', 'ProfilesController', ['except' =>['store', 'create']]);
});

我想做的是,只有配置文件的所有者和某些角色才能查看配置文件。客人将被重定向到家中。

也许你想要这样的东西?

public function show(User $user)
{
if(Gate::allows('manageUsers')){
return view('Profile.Users.profile')->with([
'user'  =>$user,
]);
}
if (auth()->user()->id === $user->id) {
return view('Profile.Users.profile')->with([
'user'  =>$user,
]);
}
return redirect(route('home'));
}

尝试$user->id == auth()->id()

您可以创建自己的中间件并将登录名放在那里。

php artisan make:middleware IsAdmin

然后将其注册到Kernel.php文件

内部中间件

If(auth()->user()->type == 'Admin'){
//Ruj your script inside if block , and instead of attribute TYPE 
//you must put what you have used in databse to check if user 
//is admin or not.
}

相关内容

最新更新