php - CakePHP - 处理未授权但已登录的用户
问题描述
我在一个 CakePHP 网站上工作,但是我在它的授权部分遇到了一些问题。如果我有一个登录用户尝试访问不允许他们访问的资源,则会引发禁止的异常。我需要这个重定向到一个友好的页面,说明它们是不允许的以及为什么。我可以在不设置全局错误 403 页面的情况下执行此操作吗?
解决方案
检查授权说:
当权限被拒绝时,authorize() 方法将引发 Authorization\Exception\ForbiddenException。 如果要检查授权并获得布尔结果,可以使用 can() 方法:
if ($this->Authorization->can($article, 'update')) {
// Do something to the article.
} else {
$this->Flash->error('Your error message here');
// or set view vars:
// $not_authorized = [
// 'title' => '...',
// 'reson' => ...,
// 'solution' => ....
// ];
// $this->set('not_authorized', $not_authorized);
$this->render('not_authorized');
}
推荐阅读
- c++ - c++中字符串向量的索引
- android - DeepLinking React Native App 与 firebase deeplink
- cluster-computing - 有什么方法可以知道 qsub 中接下来会开始哪个工作
- python-3.x - 对 DataFrameGroupBy 中的字符串使用 Pandas Rank
- sockets - 为什么我在 WebRTC 会议中收到 Websocket 错误?
- docusignapi - 有没有办法通过使用 DocuSign API 检查另一个单独的复选框来自动检查 A 组中的所有复选框
- office365 - MailKit 与 Microsoft 365 服务器的 SMTP OAuth 失败
- pandas - 在 pandas 数据框中以单行表示有效地聚合多行数据
- vb.net - 如何从文本框更新我的 Access 数据库
- php - 如何使用 PHP 将表格垂直翻转