我正在尝试使用名为“accounts”的表代替 users 表进行身份验证。我试过使用$this->Auth->userModel = 'Account';
,但是当我尝试登录时,我会显示我的 authError,但它确实让我无法访问其他任何东西。然后我尝试了这里的建议:cakephp-auth-component-using-different-table
但这样做我在第 21 行的 ../app_controller.php 中收到错误“致命错误:未定义的类常量 'ALL”'不确定是什么其他尝试。
应用程序控制器:
<?php
class AppController extends Controller {
var $components = array(
'Auth'=> array(
'loginRedirect' => array('controller' => 'drugs', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'accounts','action' => 'login')
),
'Session','Security');
var $helpers = array('Form', 'Html', 'Session', 'Javascript');
function beforeFilter() {
$this->Auth->userModel = 'Account';
// $this->Auth->allow('index','view');
$this->Auth->authError = 'Please login to view that page.';
$this->Auth->loginError = 'Incorrect Username/Password combination.';
//$this->Auth->loginRedirect = array('controller'=>'drugs', 'action'=>'index');
//$this->Auth->logoutRedirect = array('controller'=>'users', 'action'=>'login');
$this->Auth->authenticate = array(
//AuthComponent::ALL => array('userModel' => 'Account', 'scope' => array("Account.status" => 1), "fields" => array("username" => "email", "password" => "your_password_field"), 'Form', 'Basic'
//);
AuthComponent::ALL => array('userModel' => 'Account', 'scope' => array("Account.status" => 1), 'Form', 'Basic'
));
$this->set('admin', $this->_isAdmin());
$this->set('logged_in', $this->_loggedIn());
$this->set('users_username', $this->_usersUsername());
$this->set('user', $this->Auth->user('id'));
$this->set('language', $this->Auth->user('language'));
}
账户控制人:
<?php
class AccountsController extends AppController {
var $name = 'Accounts';
function beforeFilter() {
parent::beforeFilter();
//$this->Auth->allow('add');
if ($this->action == 'add' || $this->action == 'edit') {
$this->Auth->authenticate = $this->Account;
}
}
function login() {
}
function logout() {
$this->redirect($this->Auth->logout());
}
function index() {
$this->Account->recursive = 0;
$this->set('users', $this->paginate());
}
function add() {
if (!empty($this->data)) {
$this->Account->create();
if ($this->Account->save($this->data)) {
$this->Session->setFlash(__('The user has been saved', true));
$this->redirect(array('action' => 'index'));
}
else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
}
}
}
function edit($id = null) {
$this->Account->id = $id;
$this->set('title_for_layout', 'Current Users');
if (!is_numeric($id) && empty($this->data)) {
$this->Session->setFlash('Invalid User');
$this->redirect(array('action'=> 'index'));
}
if (!empty($this->data)) {
if ($this->Account->save($this->data)) {
$this->Session->setFlash('The user has been updated.');
$this->redirect(array('action'=>'index'));
}
else {
$this->Session->setFlash('The user could not be saved. Please, try again.');
}
}
if (empty($this->data)) {
$this->data = $this->Account->read(null, $id);
}
}
function delete($id = null) {
if (!is_numeric($id)) {
$this->Session->setFlash(__('Invalid id for user.'));
}
if ($this->Account->delete($id)) {
$this->Session->setFlash(__('User deleted.', true));
$this->redirect(array('action'=>'index'));
}
$this->Session->setFlash(__('User was not deleted.',true));
$this->redirect(array('action'=>'index'));
}
}
?>
账户模式:
<?php
class Account extends AppModel {
var $name = 'Account';
var $displayField = 'name';
var $order = "Account.name ASC";
var $hasMany = array(
'FrenchTranslation'=> array(
'className'=>'FrenchTranslation',
'foreignKey' => 'user_id',
'dependent' => false,
),
);
var $validate = array(
'name'=>array(
'Please enter the user\'s name.'=>array(
'rule'=>'notEmpty',
'message'=>'Please enter the user\'s name.'
)
),
'username'=>array(
'Please enter the user\'s username.'=>array(
'rule'=>'notEmpty',
'message'=>'Please enter the username.'
),
'The username must be between 3 and 15 characters.'=>array(
'rule'=>array('between', 3, 15),
'message'=>'The username must be between 3 and 15 characters.'
),
'That username has already been taken.'=>array(
'rule'=>'isUnique',
'message'=>'That username has already been taken.'
)
),
'password'=>array(
'The password must be between 5 and 15 characters.' =>array(
'rule'=>array('between', 5, 15),
'message'=>'The password must be between 5 and 15 characters.'
),
'The passwords do not match'=>array(
'rule'=>'matchPasswords',
'message'=>'The passwords do not match.'
)
),
'password_confirmation'=>array(
'You must confirm the password'=>array(
'rule'=>'notEmpty',
'message'=>'You must confirm the password.'
)
)
);
?>