2 回答
TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊
在您的 User 模型中,如果您還沒(méi)有將角色設(shè)置為關(guān)系,您可以執(zhí)行以下操作:
public function roles()
{
return $this->belongsToMany('App\Role');
}
public function hasRole($roleName) : bool
{
// you could always change this function so it could take
// an array of role IDs instead of the name
$roles = $this->roles()->where('name', roleName)->firstOrFail();
if ($roles) {
return true;
}
return false;
}
在您的呼叫中心模型中,您可以設(shè)置如下功能:
public function canBeViewedBy(User $user) : bool
{
// customise role name to whichever roles you want
if ($user->hasRole('canViewAllCentres') {
return true;
}
// you can add additional custom logic here
return false;
}
然后在您的 Dashboard 控制器中的函數(shù)中:
if (!$call_center->canBeViewedBy(Auth::user()) {
return abort(403, 'Forbidden');
}
TA貢獻(xiàn)1776條經(jīng)驗(yàn) 獲得超12個(gè)贊
首先,停止在 URL 中傳遞數(shù)據(jù)。如果可能,請(qǐng)?jiān)谟脩舻卿洉r(shí)使用會(huì)話來(lái)處理此問(wèn)題。
如果您不喜歡上述方法,那么您可以為此使用加密/解密。永遠(yuǎn)不要在 URL 中傳遞像 22 或 33 這樣的原始數(shù)字。不要讓最終用戶感到好奇。
use Illuminate\Support\Facades\Crypt;
$encrypted = Crypt::encryptString('123');
$decrypted = Crypt::decryptString($encrypted);
此外,您可以閱讀 Laravel 授權(quán)文檔,它對(duì)您面臨的確切問(wèn)題很有用。
- 2 回答
- 0 關(guān)注
- 133 瀏覽
添加回答
舉報(bào)
