简体   繁体   English

按角色cakephp管理路线

[英]Manage routes by roles cakephp

I'm working on cakephp project. 我正在开发cakephp项目。 I have problem but don't know how to resolve it. 我有问题,但不知道如何解决它。 I have variable store in Session call is 'roles'. 我在Session调用中有变量存储是'角色'。 And I have some routes are manage by this roles but denied with other roles. 我有一些路由由这个角色管理,但与其他角色拒绝。 So how can I config routes by roles like this. 那么如何按照这样的角色配置路由呢。 Give me a tips. 给我一个提示。 Thank you so much 非常感谢

Sample maybe I want like this 样品也许我想这样

if($this->Session->read("role")=="admin"){
   allow("/admin/dashboard");
}else{
   denied("/admin/dashboard");
}

if($this->Session->read("role")=="staff"){
  allow("/staff/dashboard");
}

Ello, mate. 埃洛,伙计。

Are you using the Auth component to authenticate logins? 您是否使用Auth组件验证登录? if you're using it, you can allow and deny controller actions with: 如果您正在使用它,您可以允许和拒绝控制器操作:

//AdminController
$this->Auth->allow('dashboard'); //Allow the dashboard method on admin controller
$this->Auth->deny('dashboard'); //Deny the dashboard method on admin controller

You can also make conditions on beforeFilter() to redirect it based on the role: 您还可以在beforeFilter()上创建条件,以根据角色重定向它:

//ExampleController
public function beforeFilter()
{
     if($this->Session->read("role")=="admin")
     {
        return $this->redirect(array('controller' => 'yo_controller', 'action' => 'yo_action'));
     }
     //.......
}

If you want, have a look on the cookbook about authorization, it may help you. 如果您愿意,可以查看有关授权的食谱 ,它可能会对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM