Quay lại tháng 5, tôi đã đăng this question. Tôi đang cố gắng làm điều tương tự một lần nữa trên một ứng dụng khác, nhưng tôi đã không tìm thấy một giải pháp cho vấn đề này. Tôi có nhiều thông tin hơn và mã tốt hơn, vì vậy tôi hy vọng các bạn có thể giúp tôi sắp xếp điều này.CakePHP 2.x Auth với hai thông tin đăng nhập riêng biệt
Trường hợp sử dụng: Văn phòng của bác sĩ có trang web với người dùng quản trị. Người dùng đăng nhập thành công với Auth CakePHP qua mô hình User
và UsersController
.
Các bác sĩ đã giới thiệu các bác sĩ có hồ sơ và hành động hoàn toàn khác nhau. Các bác sĩ cần đăng nhập qua số example.com/physicians/login
. Tuy nhiên, đăng nhập này là không với điều này
authError => 'You are not authorized to access that location.'
Đây là mã của tôi trong 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');
}
}
Và đây là của tôi UsersController
rằng đang làm việc:
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());
}
Đây là sốcủa tôimã mà KHÔNG làm việc:
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());
}
tôi thực sự không muốn bắt đầu lại và chuyển sang ACL - Tôi không chắc đó là cần thiết để chỉ hai thông tin đăng nhập. Sự giúp đỡ được đánh giá cao!
EDIT: Câu trả lời của Joshua bên dưới là tuyệt vời và cực kỳ hữu ích. Tôi thực hiện nó, nhưng tôi vẫn nhận được một lỗi trái phép khi tôi cố gắng đăng nhập như một bác sĩ thông qua/phys/physican/login (tiền tố/điều khiển/hành động). Thiết lập Quản trị hoạt động tốt. Dưới đây là đoạn code debug khi tôi cố gắng đăng nhập:
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()
}
Cảm ơn rất nhiều cho điều này - bạn đá. Tôi thực hiện ngày hôm nay và không gặp rắc rối với tiền tố quản trị và điều khiển người dùng. Tuy nhiên, các bác sĩ điều khiển vẫn còn cho tôi rằng cùng một "Bạn không được ủy quyền" lỗi. Vì vậy, hầu như không có gì thay đổi. Tôi sẽ đăng các mã gỡ lỗi ở trên nếu điều đó giúp. – deewilcox
Vì vậy, bạn nhận được 'Tên người dùng hoặc mật khẩu không hợp lệ, hãy thử lại' hoặc bạn không nhận được điều đó chút nào và chỉ nhận được 'Bạn không được phép truy cập vào vị trí đó.'? Đó là để nói, có "if ($ this-> Auth-> login()) {" của bạn vượt qua, và sau đó bạn chỉ nhận được một vấn đề về chuyển hướng sau đăng nhập? Hay dòng đó thất bại? –
Tôi nhận được lỗi "Bạn không được ủy quyền". Nếu tôi chuyển '($ this-> request-> data)' vào, nó hoạt động. Tôi đã thử nghiệm bằng cách sử dụng tên người dùng và mật khẩu không hợp lệ với '$ this-> Auth-> login ($ this-> request-> data);' và thông tin đăng nhập không thành công (nếu cần) nếu tên người dùng hoặc mật khẩu sai. – deewilcox