我只是想在名为“Cauth”的插件中实现 Cakephp ACL 用户身份验证。我之前实现的同样的东西工作得很好。这次不同的是,它在插件下。在这里,在 Controller 中, $this->用户->组->find('list');不管用。我收到以下 fatal error :
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' => ''
)
);
用户 Controller 添加操作,组选择框不起作用。
用户 Controller 添加操作:
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:
如果我从 Controller 绑定(bind)模型,它就可以正常工作。
$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 将不起作用。即所有组都获得相同的权限。
请帮助我。我对此已经有很长一段时间了。
最佳答案
经过长时间的努力,我终于从以下链接找到了解决方案 -
当我使用 Auth 组件时,用户 Controller 没有使用 Cauth.user 模型。它是使用 AppModel 加载的。但没有身份验证组件,它可以正常工作。这样使用 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');
}
}
现在一切正常。感谢大家。
关于php - 插件模型关联在 cakephp 2.3 中无法使用 cake ACL 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15562664/