【问题标题】:how to stop execution of ctp file in cakephp 2.x after validating the url验证 url 后如何在 cakephp 2.x 中停止执行 ctp 文件
【发布时间】:2017-10-27 19:29:34
【问题描述】:

在我的 CakePHP 应用程序中,我应用了 Url 验证,以便管理员只能访问那些为管理员定义且与用户相同的操作。 在我的应用程序中,“调查列表”是管理员的操作,当任何用户直接访问该操作(调查列表)时,URL 验证工作(显示未经授权的访问消息)。 但是在surveylist的该消息ctp文件下方强制执行并显示错误,因为我已经通过try-catch块验证了URL并且它无法获取设置的操作变量。 如果出现未授权错误,我希望不执行 ctp 文件。

我的调查表代码是:-

public function surveylist($pg=null){
  try{
      if($this->checkPageAccess($this->params['controller'] . '/' . $this->params['action'])){
          $this->Paginator->settings = array(
                                          'Survey' => array(
                                                        'limit' => 5,
                                                        'order' => 'created desc',
                                                        'conditions'=>array('is_deleted'=> 0),
                                                    'page' => $pg
                                                       )
                                       );
          $numbers = $this->Paginator->paginate('Survey');
          $this->set(compact('numbers'));
      }else{
        $this->Flash->set(__('Unauthorised access'));

      }
  }catch(Exception $e){
  $this->Flash->set(__($e->getMessage()));
}

}

如果控制权来了,我不希望执行surveylist 的ctp 文件。 LZ,帮帮我...... 提前谢谢...

【问题讨论】:

    标签: validation cakephp


    【解决方案1】:

    我想你正在使用前缀来分隔管理员和用户,如果没有请这样做,这是处理和限制方法的好方法。

    这样做之后,您必须创建条件以检查哪个前缀(管理员,用户)当前处于活动状态,并根据该加载 Auth 组件并允许在 Auth 的 allow() 方法中执行操作。

    例子:

    $this->loadComponent('Auth',[
        /*'authorize' => [
            'Acl.Actions' => ['actionPath' => 'controllers/']
        ],*/
        'loginRedirect' => [
            'controller' => 'Users',
            'action'     => 'index'
        ],
        'authenticate' => [
            'Form' => [
                'fields' => [
                    'username' => 'email',
                    'password' => 'password'
                ]
            ]
        ],
        'loginAction' => [
            'controller' => 'Users',
            'action' => 'login'
        ],
        'unauthorizedRedirect' => [
            'controller' => 'Users',
            'action' => 'login',
            'prefix' => false
        ],
        'authError' => 'You are not authorized to access that location.',
    ]);
    
    if ($this->request->params['prefix']=='admin') {
        // Put actions you want to access to admin in allow method's array
        $this->Auth->allow(array('add', 'edit', etc...));
    } else if ($this->request->params['prefix']=='user') {
        // Put actions you want to access to user in allow method's array
        $this->Auth->allow(array('login', 'view', etc...));
    }
    

    这样您可以限制特定角色的操作。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多