5月に戻ると、私はthis questionを投稿しました。私は別のアプリケーションで同じことをやり直そうとしていますが、この問題の解決策は見つかりませんでした。私はより多くの情報とより良いコードを持っているので、皆さんがこれを整理する手助けをしてくれることを願っています。CakePHP 2.x 2つの別個のログインによる認証
使用例: 医師のオフィスには、管理者のユーザーがいるウェブサイトがあります。ユーザはUser
モデルとUsersController
でCakePHPのAuthで正常にログインします。
医師は、医師に全く異なるプロファイルと行動を指しています。医師は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());
}
ここは私のです作業ではありませんコード:
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に切り替えるにはしたくない - 私はそれがちょうど2つのログインに必要だか分かりません。ヘルプは非常に高く評価されるだろう!
編集:以下のジョシュアの答えは素晴らしいと非常に役立ちます。私はそれを実装しましたが、phys/physican/login(接頭辞/コントローラ/アクション)経由で医師としてログインしようとすると、私はまだ不正なエラーを受けています。管理者の設定は素晴らしいです。
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()
}
感謝を使用する必要があります - あなたは岩。私は今日実装し、adminプレフィックスとUsersコントローラに問題はない。しかし、医師のコントローラは、私には同じ "あなたが許可されていません"というエラーを表示しています。だから事実上何も変わっていない。上記のデバッグコードを投稿すると役立ちます。 – deewilcox
あなたは「無効なユーザー名またはパスワードを取得しました。もう一度やり直してください」、まったくそのようになっておらず、「あなたはその場所にアクセスする権限がありません。つまり、あなたの "if($ this-> Auth-> login()){"合格すれば、ログイン後のリダイレクトに問題が発生しますか?あるいは、その行は失敗するのだろうか? –
「あなたは承認されていません」というエラーが表示されます。 '($ this-> request-> data)'を渡すと動作します。 '$ this-> Auth-> login($ this-> request-> data);で無効なユーザ名とパスワードを使用してテストしましたが、ユーザ名またはパスワードが間違っていればログインは失敗します。 – deewilcox