cakephp - Cakephp 4 - 实现每个路由的授权
问题描述
我有管理任务的特定路线(/admin/...)。用户在组中(管理员,用户)
使用授权插件,只允许管理员用户访问 /admin 路由的简单方法是什么?
有政策可以吗?
谢谢
解决方案
这是我的解决方案,如果我能改进它,请告诉我:
我创建了一个策略:
class UserPolicy
{
/**
* Check if $user have admin access
*
* @param Authorization\IdentityInterface $user The user.
* @param App\Model\Entity\Users $u
* @return bool
*/
public function canAdminAccess(IdentityInterface $user, $u)
{
return (bool)($user->group_id === GROUP_ADMIN);
return false;
}
}
在每个管理控制器中,在 beforeFilter 方法中,我调用授权:
public function beforeFilter(EventInterface $event)
{
parent::beforeFilter($event);
$this->Authorization->authorize($this->user,'adminAccess');
}
推荐阅读
- c# - C#比较2天,分钟没有正确比较
- angular - ngrok 不适用于浏览器中的路径(Angular Webserver)
- azure - Azure ML Studio ML Pipeline - 异常:未找到临时文件
- c# - 将自定义字体加载到控制台 C# .Net Framework
- reactjs - 状态改变时,下一个 JS 不会重新渲染元素
- microsoft-teams - microsoft-teams:我们如何更新所有用户的应用程序?缺乏功能加上问题
- openssl - 包含 openssl 时,cgo CFLAGS 不起作用
- utf-8 - Linux - GREP 打印带有特殊字符的行
- javascript - 关闭选项卡时不会调用 onbeforeunload
- java - 需要帮助来理解计数排序排序算法的实现