PHP 中的 beforefilter() 取消请求
我使用 CakePHP 、 CAS 进行身份验证,使用 ACL 进行授权。 如果用户无权查看该页面,我需要显示一条消息,说明不允许或重定向到另一个页面。
例如:如果用户正在查看 /users/view/1 。现在用户请求 /users/delete/1 。用户没有删除权限。所以我想在他请求的页面(/users/view/1)上显示一条闪存消息。
在我的 app_controller 中,我有以下功能:
function beforeFilter() {
$this->__initPhpCas();
if (isset($_SESSION['loggedIn'])){
if(!$this->Acl->check(.....){
//User do not have permission to view the page.
// Need to cancel this request and flash a message
}
}
任何建议都表示赞赏
最终答案是
function beforeFilter() {
$this->__initPhpCas();
if (isset($_SESSION['loggedIn'])){
if(!$this->Acl->check(.....){
//User do not have permission to view the page.
// Need to cancel this request and flash a message
$this->Session->setFlash(__('You are not authorized to view this page.', true));
$this->redirect($_SERVER['HTTP_REFERER']);
}
}
I'm using CakePHP , CAS for Authentication and ACL for Authorization.
If the user donot have permission to view the page, i need to flash a message stating Not permitted OR redirect to another page.
Ex: If the user is viewing /users/view/1 .Now the user requests /users/delete/1. The user donot have permission to delete. So I want to display a flash message on the page he requested from (/users/view/1).
In my app_controller, i have the following function:
function beforeFilter() {
$this->__initPhpCas();
if (isset($_SESSION['loggedIn'])){
if(!$this->Acl->check(.....){
//User do not have permission to view the page.
// Need to cancel this request and flash a message
}
}
Any suggestions are appreciated
Final answer is
function beforeFilter() {
$this->__initPhpCas();
if (isset($_SESSION['loggedIn'])){
if(!$this->Acl->check(.....){
//User do not have permission to view the page.
// Need to cancel this request and flash a message
$this->Session->setFlash(__('You are not authorized to view this page.', true));
$this->redirect($_SERVER['HTTP_REFERER']);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
到 重定向 使用
$this->redirect();
并使用$this->Session->setFlash 添加消息 ();
。我已经添加了链接来向您展示。编辑:
我建议设置闪存消息,然后进行重定向。然后在重定向的页面上,使用
$session->flash();
显示 Flash 消息。EDIT2:
由于您不想进行重定向,因此您需要执行类似的操作。
编辑3:
试试这个。查看链接中的最后一篇文章。
编辑4:尝试使用 deny()
编辑5:
如果我理解正确,你想要使用 beforeFilter 检查他们是否具有访问权限,如果没有,则不要继续运行操作。 CakePHP 并不真正允许这样做,但有一个解决方法是。
to redirect use
$this->redirect();
and add a message by using$this->Session->setFlash();
. I have included links to show you.EDIT:
I would recommend setting the flash message then doing the redirect. Then on the redirected page, display the flash message with
$session->flash();
.EDIT2:
Since you are not wanting to do a redirect you will need to do something like this.
EDIT 3:
Try this. Take a look at the last post in the link.
Edit 4: Try using deny()
Edit 5:
If I understand you correctly you want to use beforeFilter to check if they have access and if not then don't continue running the actions. CakePHP doesn't really allow this but a work around is.