cakephp - CakePHP 4 身份验证插件,允许访问除 admin 目录之外的所有目录
问题描述
我正在为网站构建一个非常简单的 CMS 后端,我选择使用 CakePHP 4。我想知道是否有办法允许访问网站的所有部分,除非它们位于 Controller/Admin/ 目录中(所有行政控制都将发生)。
在文档中,它显示了这个例子:
public function beforeFilter(\Cake\Event\EventInterface $event)
{
parent::beforeFilter($event);
// for all controllers in our application, make index and view
// actions public, skipping the authentication check
$this->Authentication->addUnauthenticatedActions(['view', 'index']);
}
所以我可以添加无需身份验证就允许的控制器操作,但是由于我已将登录名与 /Admin/ 前缀分开,有没有办法只允许 /Admin/ 前缀中的所有内容?
谢谢
解决方案
尝试实现请求授权中间件
https://book.cakephp.org/authorization/2/en/request-authorization-middleware.html
喜欢:
public function canAccess($identity, ServerRequest $request)
{
if ($request->getParam('prefix') !== 'Admin') {
return true;
}
if (
$identity &&
$request->getParam('prefix') === 'Admin' &&
in_array($request->getParam('action'), ['add', 'edit'])
) {
return true;
}
return false;
}
推荐阅读
- angular - 尝试使用 Angular TestBed 时出现奇怪的 RxJS 错误
- laravel - Intervention\Image\Exception\NotWritableException: Can't write image data to path Image\Image.php on line 143
- visual-studio - 即使已登录,Visual Studio 也会不断要求选择 Azure 帐户
- elixir - 在并行长生不老药中运行多个进程
- css - 第一行具有右对齐静态项目的 Flex 项目
- python - 如何过滤和替换 Pandas df 中给定列中的数据?
- json - 使用 T-SQL 解析 JSON
- python - 保存类型的对象
到磁盘 - ionic-framework - 使用 Ionic 在后台执行任务(关闭应用程序时显示呼叫屏幕)
- linux - 如何在其子文件的第 2 列中 grep 包含值范围(-6 到 -7)的文件名?