私は現時点で、管理者、通常のユーザーの2つの役割を持つログインモジュールをサイトに設定しています。 UsersControllerは既にバックエンドログイン用に作成されており、動作しています。 別のコントローラClientControllerを作成し、データベースに新しいテーブルクライアントを作成しました。Cakephp認証ログインの問題。ログインにClientControllerを使用できますか?
ログインが機能していないため、エラーのユーザー名とパスワードが間違っています。 ログインにUsersControllerを使用する必要があります。 ここに私が使用しているコードがあります。
ご協力いただきありがとうございます。
AppController.php:
public $components = array(
'Acl',
'Auth' => array(
'authorize' => array(
'Actions' => array(
'actionPath' => 'controllers',
'userModel' => 'Clients '
)
),
'authError' => 'Sorry, you are not authorised to do that.',
),
'Session'
);
ClientController.php:
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login() {
$this->Session->setFlash('Your login was successful.');
return $this->redirect(array('action' => 'dashboard'));
}
$this->Session->setFlash('Your username or password was incorrect. Please, try again.');
return $this->redirect('/login');
}
}
Client.php モデル
class Client extends AppModel {
public $useTable = 'clients';
public $validate = array(
'username' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A username is required'
)
),
'password' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A password is required'
)
),
'email' => array(
'email' => array(
'rule' => array('email', true),
'message' => 'Please supply a valid email address.'
),
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A email is required'
)
)
);
public $belongsTo = array(
'Group' => array(
'className' => 'Group',
'foreignKey' => 'group_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public function beforeSave($options = array()) {
if (empty($this->data[$this->alias]['password'])) {
unset($this->data[$this->alias]['password']);
} else {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
}
return true;
}
}