我的数据库中有两个表,一个用于管理员(命名为Admins),另一个用于普通用户,名为:utilisateur(法语)。我知道我必须使用cakePHP的约定,它要求我必须创建一个名为users的表,其中包含用户名和密码。但问题是我有两张桌子,而不仅仅是一张。每个领域都有自己的特点,所以我真的需要它们分开。而且,就连登录所需的信息也因用户而异:
我想要做的是创建两个登录页面,一个用于管理员,另一个用于普通用户。登录后,用户将被重定向到他应该看到的页面。但是,如果用户尝试尝试一个禁止的位置。我想阻止他(beforeFilter + isAuthorized )
我怎么才能把这一切做好呢?
我不是cakephp的初学者,我已经使用Auth组件在anotehr应用程序中创建了一个身份验证系统,但是比较简单,因为用户只需要一个表。
你的帮助将不胜感激。
发布于 2014-07-08 18:12:07
假设如下:
User和Admin相关联的表,其中:User有idcard和password字段。Admin有login和passowrd字段。
User::hasPassword和Admin::hasPassword函数进行散列。您可以在Authenticate下创建一个新的App/Controller/Component/Auth/MyAuthenticate.php组件
<?php
App::uses('FormAuthenticate', 'Controller/Component/Auth');
class MyAuthenticate extends FormAuthenticate {
public function authenticate(CakeRequest $request, CakeResponse $response) {
$username = $request->data['login'] ;
$password = $request->data['password'] ;
App::import('Model', 'User') ;
$userModel = new User () ;
/* Try to authenticate as a user... */
$user = $userModel->find('first', array(
'conditions' => array(
'idcard' => $username,
'password' => User::hashPassword($password) ;
)
)) ;
if ($user) {
$user = $user['User'] ; // Get only useful info
$user['type'] = 'user'; // Save user type
return $user ;
}
/* Same thing for admin. */
App::import('Model', 'Admin') ;
$adminModel = new Admin () ;
$user = $adminModel->find('first', array(
'conditions' => array(
'login' => $username,
'password' => Admin::hashPassword($password) ;
)
)) ;
if ($user) {
$user = $user['Admin'] ; // Get only useful info
$user['type'] = 'admin'; // Save user type
return $user ;
}
return null ;
}
};您只需要确保管理员不能作为用户进行身份验证,并进行反向验证。
在你的AppController里
public $components = array(
'Auth' => array(
'authenticate' => array('My'), // The prefix in front of your component
'loginAction' => array(/* ... */),
'loginRedirect' => array(/* ... */),
'logoutRedirect' => array(/* ... */),
'authError' => "...",
'authorize' => 'Controller'
)
) ;登录操作与普通表相同:
public function login () {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
}
else {
$this->request->data['password'] = "" ;
$this->Session->setFlash('Invalid login/id or password.');
}
}
}然后,在beforeFilter或isAuthorized中,您可以检查$this->Auth->user('type');。例如,在AppController中:
public function isAuthorized () {
/* Assuming you keep CakePHP convention where action starting with admin_ set admin params. */
if(isset($this->params['admin'])) {
return $this->Auth->user('type') == 'admin' ;
}
return true ;
}或者,如果要禁用非管理用户对AdminController中所有操作的访问,请使用beforeFilter
class AdminController extends AppController {
public function beforeFilter () {
if (!$this->Auth->loggedIn()) {
$this->Session->setFlash('You need to be logged to access this page.');
$this->redirect(/* Login url. */) ;
return ;
}
if ($this->Auth->user('type') != 'admin') {
$this->Session->setFlash('You need to be admin to access this page.');
$this->redirect(/* Somewhere... */) ;
return ;
}
return parent::beforeFilter () ;
}
}https://stackoverflow.com/questions/24636263
复制相似问题