我只是想在一个名为“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'))
);
IE
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'));
}
案例二:
如果我允许所有人的操作名称。管理员拥有所有权限,尽管我还需要为管理员编写 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 就不起作用。即所有组都获得相同的权限。
请帮我。我很长一段时间都在这方面。