我正在嘗試使用名爲'accounts'的表代替用戶表進行身份驗證。我嘗試過使用$this->Auth->userModel = 'Account';
,但是當我嘗試登錄時,顯示authError,但它確實阻止我訪問其他任何內容。然後我嘗試了這裏建議的內容:cakephp-auth-component-using-different-table 但是這樣做我得到了錯誤'致命錯誤:未定義的類常量'所有'在../app_controller.php第21行'不知道還有什麼要嘗試。在cakephp 1.3中使用除用戶以外的表進行身份驗證
App_controller:
<?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.'
)
)
);
?>
'AuthComponent :: ALL'蛋糕2.0加入,所以這不會對你有用,'userModel'應該可以工作:http://book.cakephp.org/1.3/en/view/1265/AuthComponent-Variables#userModel-1266 –
所以一旦你刪除了:: ALL並致命的是避免發生什麼事 帳戶表被查詢登錄嘗試? – Leo
@Leo我厭倦了,並決定只是指database.php配置中的其他數據庫,並告訴使用var $ useDbConfig ='databasename'的模型;我想把它全部放在同一個數據庫中,但是這樣工作得很好。謝謝你的幫助! – Jonathan