[英]Get current logged in user's user role from a laravel controller
在我的 laravel 應用程序中,我有兩個用戶類型,管理員和普通用戶。
我已經實現了 function 供用戶下載他們的證書。
為此,我在 controller 中關注了 function
public function index(string $locale, CertificateUser $certificateUser)
{
$this->authorize('downloadCertificate', [Institute::class, $certificateUser, $institute]);
try {
return Storage::download($certificateUser->certificate_url);
} catch (FileNotFoundException $exception) {
return redirect()->back()->withErrors(__('Certificate could not be found.'));
}
}
現在我想執行這個
$this->authorize('downloadCertificate', [Institute::class, $certificateUser, $institute]);
僅當登錄用戶的用戶角色是管理員時...
如何從這里獲取當前登錄用戶的用戶角色?
> $user_role = Auth::user()->role;
> if($user_role == 'admin'){
> // You code Here
> $this->authorize('downloadCertificate', [Institute::class, $certificateUser, $institute]); } What's the issue ?
.也在這里分享你的用戶 Model
其他方法....
if($user->hasRole->name('Admin'))
$user_roles = Auth::user()->roles()->get();
現在你可以遍歷 $user_roles..
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.