回到五月,我發佈this question。我試圖在另一個應用上再次做同樣的事情,但我還沒有找到解決這個問題的方法。我確實有更多的信息和更好的代碼,所以我希望你們能幫我解決這個問題。CakePHP 2.x認證與兩個單獨的登錄
使用案例: 醫生的辦公室有一個管理員用戶的網站。用戶通過User
模型和UsersController
以CakePHP的身份驗證成功登錄。
醫生已經向醫生提出了完全不同的概況和行爲。醫生需要通過example.com/physicians/login
登錄。然而,這種登錄與此
authError => 'You are not authorized to access that location.'
這裏失敗是我的代碼在AppController
:
class AppController extends Controller {
public $helpers = array('Form', 'Html', 'Time', 'Session', 'Js' => array('Jquery'));
public $components = array(
'Session',
'Auth' => array(
'autoRedirect' => false,
'authorize' => 'Controller'
)
);
public function beforeFilter() {
$this->Auth->allow('index', 'view', 'edit', 'display', 'featured', 'events', 'contact', 'signup', 'search', 'view_category', 'view_archive', 'addComment', 'schedule', 'login');
}
}
這裏是我的UsersController
正在工作:
class UsersController extends AppController {
public $components = array(
'Auth' => array(
'authenticate' => array(
'Form' => array(
'userModel' => 'User',
'fields' => array(
'username' => 'username',
'password' => 'password'
)
)
),
'loginRedirect' => array('controller' => 'users', 'action' => 'admin'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'index'),
'loginAction' => array('controller' => 'users', 'action' => 'login'),
'sessionKey' => 'Admin'
)
);
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('add', 'login', 'logout');
}
function isAuthorized() {
return true;
}
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
}
public function logout() {
$this->Session->destroy();
$this->redirect($this->Auth->logout());
}
這是我的PhysiciansController
代碼不工作:
class PhysiciansController extends AppController {
public $components = array(
'Auth' => array(
'authenticate' => array(
'Form' => array(
'userModel' => 'Physician',
'fields' => array(
'username' => 'username',
'password' => 'password'
)
)
),
'loginRedirect' => array('controller' => 'physicians', 'action' => 'dashboard'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'index'),
'loginAction' => array('controller' => 'physicians', 'action' => 'login'),
'sessionKey' => 'Physician'
)
);
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->authorize = array(
'Actions' => array(
'userModel' => 'Physician',
'actionPath' => 'physicians'
)
);
$this->Auth->allow('login', 'logout');
// $this->Session->write('Auth.redirect','/physicians/index');
}
function isAuthorized() {
return true;
}
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect(array('controller' => 'physicians', 'action' => 'dashboard'));
} else {
$this->Session->read();
debug($this->Auth);
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
}
public function logout() {
$this->Session->destroy();
$this->redirect($this->Auth->logout());
}
我真的不希望重新開始,並切換到ACL - 我不知道,對於只有兩個登錄是必要的。非常感謝幫助!
編輯:喬舒亞的回答下面是真棒和超級有用。我實現了它,但是當我嘗試通過/ phys/physican/login(前綴/控制器/操作)登錄爲Physician時,仍然收到未經授權的錯誤。管理員設置很好。這裏的調試代碼,當我嘗試登錄:
object(AuthComponent) {
components => array(
(int) 0 => 'Session',
(int) 1 => 'RequestHandler'
)
authenticate => array(
'Form' => array(
'userModel' => 'Physician'
)
)
authorize => false
ajaxLogin => null
flash => array(
'element' => 'default',
'key' => 'auth',
'params' => array()
)
loginAction => array(
'controller' => 'physicians',
'action' => 'phys_login'
)
loginRedirect => null
logoutRedirect => '/'
authError => 'You are not authorized to access that location.'
allowedActions => array()
request => object(CakeRequest) {
params => array(
'prefix' => '*****',
'plugin' => null,
'controller' => 'physicians',
'action' => 'phys_login',
'named' => array(),
'pass' => array(),
'phys' => true,
'_Token' => array(
'key' => 'ad1ea69c3b2c7b9e833bbda03ef18b04079b23c3',
'unlockedFields' => array()
),
'isAjax' => false
)
data => array(
'Physician' => array(
'password' => '*****',
'username' => 'deewilcox'
)
)
query => array()
url => 'phys/physicians/login'
base => ''
webroot => '/'
here => '/phys/physicians/login'
}
response => object(CakeResponse) {
}
settings => array()
}
非常感謝這一點 - 你搖滾。我今天實施了,並且對管理員前綴和用戶控制器沒有任何問題。但是,醫師控制器仍然給我提供相同的「你沒有被授權」的錯誤。所以幾乎沒有改變。如果有幫助,我會發布上面的調試代碼。 – deewilcox
那麼你會得到'無效的用戶名或密碼,再試一次',或者你沒有得到,只有'你無權訪問該位置'。也就是說,你的「if($ this-> Auth-> login()){」是否通過,然後你在登錄後重定向時只會遇到問題?或者那條線路失敗? –
我收到「您沒有授權」錯誤。如果我通過'($ this-> request-> data)',它就可以工作。我使用'$ this-> Auth-> login($ this-> request-> data)'使用無效的用戶名和密碼進行測試;',如果用戶名或密碼錯誤,則登錄失敗(因爲它應該)。 – deewilcox