【问题标题】:Plugin model assiciation is not working in cakephp 2.3 with cake ACL插件模型关联在带有 cake ACL 的 cakephp 2.3 中不起作用
【发布时间】:2023-04-10 07:48:01
【问题描述】:

我只是想在一个名为“Cauth”的插件中实现 Cakephp ACL 用户身份验证。我之前实施的同样的事情工作正常。这次不同的是,它在插件下。在这里,在控制器中, $this->User->Group->find('list');不管用。我收到以下致命错误:

Fatal Error

Error: Call to a member function find() on a non-object
File: my_dir_path\app\Plugin\Cauth\Controller\UsersController.php
Line: 60

Notice: If you want to customize this error message, create app\View\Errors\fatal_error.ctp

我的代码如下:

组模型:

var $useTable = 'groups';
public $hasMany = array (
    'User' => array (
        'className'    => 'Cauth.User',
        'foreignKey'   => 'group_id',
        'dependent'    => false,
        'conditions'   => '',
        'fields'       => '',
        'order'        => '',
        'limit'        => '',
        'offset'       => '',
        'exclusive'    => '',
        'finderQuery'  => '',
        'counterQuery' => ''
    )
);

用户模型:

var $useTable = 'users';
public $belongsTo = array (
    'Group' => array (
        'className'  => 'Cauth.Group',
        'foreignKey' => 'group_id',
        'conditions' => '',
        'fields'     => '',
        'order'      => ''
    )
);

用户控制器添加操作,组选择框不起作用。

用户控制器添加操作:

App::uses('CauthAppController', 'Cauth.Controller');
class UsersController extends CauthAppController {
public function add() {
    if ($this->request->is('post')) {
        $this->User->create();
        if ($this->User->save($this->request->data)) {
            $this->Session->setFlash(__('The user has been saved'));
            $this->redirect(array ('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
        }
    }

    $groups = $this->User->Group->find('list');
    $this->set(compact('groups'));

}

谁能帮我解决这个问题。

特别说明: 它正在处理以下案例。

案例 1:

如果我从控制器绑定模型,它就可以正常工作。

$this->User->bindModel(
    array ('belongsTo' => array ('Group'))
);

public function add() {
    $this->User->bindModel(
        array ('belongsTo' => array ('Group'))
    );
    if ($this->request->is('post')) {
        $this->User->create();
        if ($this->User->save($this->request->data)) {
            $this->Session->setFlash(__('The user has been saved'));
            $this->redirect(array ('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
        }
    }

    $groups = $this->User->Group->find('list');
    $this->set(compact('groups'));

}

案例 2:

如果我允许所有人的操作名称。管理员拥有所有权限,尽管我还需要为管理员编写 beforeFilter。

public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('index', 'add');
}

我又找到了一个案例来使它可行。

案例 3:

我的 AppController 的代码是 -

class AppController extends Controller {


    public $helpers    = array ('Form', 'Time', 'Html', 'Session', 'Js', 'DebugKit.Toolbar');
    public $counter    = 0;
    public $components = array (
        'DebugKit.Toolbar',
        'RequestHandler',
        'Acl',
        'Auth' => array (
            'authorize' => array (
                'Actions' => array ('actionPath' => 'controllers')
            )
        ),
        'Session'
    );

    public function beforeFilter() {
        //Configure AuthComponent
        $this->Auth->loginAction    = array ('plugin'     => 'cauth', 'controller' => 'users', 'action'     => 'login');
        $this->Auth->logoutRedirect = array ('plugin'     => 'cauth', 'controller' => 'users', 'action'     => 'login');
        $this->Auth->loginRedirect  = array ('plugin'     => '', 'controller' => 'pages', 'action'     => 'display');

    }

}

在这种情况下,它不起作用。但是当我成功时 -

class AppController extends Controller {

    public $helpers    = array ('Form', 'Time', 'Html', 'Session', 'Js', 'DebugKit.Toolbar');
    public $counter    = 0;
    public $components = array (
        'DebugKit.Toolbar',
        'RequestHandler',
        'Acl',
        'Auth' => array (
            'authenticate' => array('Form')

        ),
        'Session'
    );

    public function beforeFilter() {
        //Configure AuthComponent
        $this->Auth->loginAction    = array ('plugin'     => 'cauth', 'controller' => 'users', 'action'     => 'login');
        $this->Auth->logoutRedirect = array ('plugin'     => 'cauth', 'controller' => 'users', 'action'     => 'login');
        $this->Auth->loginRedirect  = array ('plugin'     => '', 'controller' => 'pages', 'action'     => 'display');

    }

}

然后关联再次开始工作。还是不明白这个组件声明有什么问题。

public $components = array (
    'DebugKit.Toolbar',
    'RequestHandler',
    'Acl',
    'Auth' => array (
        'authorize'    => array (
            'Actions' => array ('actionPath' => 'controllers')
        )
    ),
    'Session'
);

如果我不这样声明,我的 ACL 就不起作用。即所有组都获得相同的权限。

请帮助我。我在这方面坚持了很长时间。

【问题讨论】:

    标签: php cakephp acl cakephp-2.3


    【解决方案1】:

    经过长时间的努力,我终于从以下链接找到了解决方案-

    https://cakephp.lighthouseapp.com/projects/42648/tickets/2464-plugin-using-acl-not-loading-proper-model

    当我使用 Auth 组件时,用户控制器没有使用 Cauth.user 模型。它是使用 AppModel 加载的。但是没有 auth 组件,它可以正常工作。这样使用 auth 组件的正确方法将是

        'Auth' => array (
            'authorize' => array (
                'Actions' => array (
                    'actionPath' => 'controllers',
                    'userModel'  => 'Cauth.User',
                ),
            )
        ),
    

    这就是 AppController 的总和将是 -

    class AppController extends Controller {
    
        public $helpers    = array ('Form', 'Time', 'Html', 'Session', 'Js', 'DebugKit.Toolbar');
        public $counter    = 0;
        public $components = array (
            'DebugKit.Toolbar',
            'RequestHandler',
            'Acl',
            'Auth' => array (
                'authorize' => array (
                    'Actions' => array (
                        'actionPath' => 'controllers',
                        'userModel'  => 'Cauth.User',
                    ),
                )
            ),
            'Session'
        );
    
        public function beforeFilter() {
            //Configure AuthComponent
            $this->Auth->loginAction    = array ('plugin'     => 'cauth', 'controller' => 'users', 'action'     => 'login');
            $this->Auth->logoutRedirect = array ('plugin'     => 'cauth', 'controller' => 'users', 'action'     => 'login');
            $this->Auth->loginRedirect  = array ('plugin'     => '', 'controller' => 'pages', 'action'     => 'display');
    
        }
    
    }
    

    现在一切正常。谢谢大家。

    【讨论】:

      【解决方案2】:

      假设组是模型(表)的名称,将这一行更改为:

        $groups = $this->User->Group->find('list');
      

        $groups = $this->Group->find('list');
      

      希望它会有所帮助!

      【讨论】:

      • 感谢您的回答,但这不是解决方案。要这样使用,我必须加载组模型。此外,它不是单一的情况。并且关联与您的解决方案无关。所以在递归调用的时候,会获取不到结果。
      【解决方案3】:

      尝试两件事来解决这个问题。首先将以下内容添加到您的用户和组插件控制器中。

      public $uses = array('Users.User', 'Users.Group');
      

      其次,在插件控制器中定义组件。在蛋糕 ACL Auth 教程中,他们将其放在主 AppController 中,您需要将其移动到插件中。

      您可以在 UsersAppController 或 UsersController 中执行以下操作

      public $components = array(
              'Acl',
              'Auth',
              'Session'
          );
      

      希望它有效,它对我有用。

      【讨论】:

      • 但是ACL在这种情况下不起作用。所有组都获得相同的权限。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多