認証用に users テーブルの代わりに「accounts」という名前のテーブルを使用しようとしています。使用してみまし$this->Auth->userModel = 'Account';
たが、ログインしようとすると authError が表示されますが、他のものにはアクセスできません。次に、ここで提案されていることを試しました:cakephp-auth-component-using-different-table
ですが、それを行うと、エラー「致命的なエラー:未定義のクラス定数 'ALL' in ../app_controller.php on line 21 'わからない他に試してみてください。
アプリコントローラー:
<?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.'
)
)
);
?>