CakePHP 2.3.6을 사용하고 있습니다. 한 프로젝트에서 여러 종류의 사용자 (Users, Admins, etc.
)에 대해 User Management System
을 구현해야합니다. 현재로서는 Admin Panel(only 1 Admin for now)
과 User Panel
에 대해서만 우려하고 있습니다. 평소와 같이 특정 지역 (페이지)에 액세스하려면 Admin
이 User Panel
의 모든 페이지를 포함한 모든 영역에 액세스 할 수 있으며 Users
은 login
이어야합니다. CakePHP 2.x의 사용자 관리 시스템이 이상하게 동작합니다.
user panels
plugins
데에, 그리고
Admin Panel
User Panel
2
controllers
을 만들었다. 나는 두 패널이 다른
login pages
을 가지고 있기 때문에, 여기에
AppController.php :
public $components=array('Session','RequestHandler','Auth');
public function isAuthorized($user){
if(isset($user['role']) && $user['role']==='admin')
return true;
return false;
}
UsersController.php :
public function beforeFilter(){
parent::beforeFilter();
$this->Auth->loginRedirect=array('action'=>'editProfile');
$this->Auth->logoutRedirect=array('action'=>'index');
$this->Auth->authenticate=array('Form'=>array('scope'=>array('User.role'=>"user"),'userModel'=>'User','fields'=>array('username','password')));
$this->Auth->unauthorizedRedirect=array('action'=>'login');
$this->Auth->loginAction=array('action'=>'login');
$this->Auth->deny('editCv','logout');
$this->layout='user_layout';
}
public function login(){
if($this->request->is('post'))
if($this->Auth->login()){
$this->Session->setFlash('Welcome '.$this->User->field('name',array('User.id'=>$this->Auth->user('id'))));
$this->redirect($this->Auth->redirect());
}else{
$this->Session->setFlash('Invalid username or password, please try again');
$this->set('title_for_layout','Error - Login');
}
}
public function logout(){
$this->redirect($this->Auth->logout());
}
AdminsController.php
public function beforeFilter(){
parent::beforeFilter();
$this->Auth->loginRedirect=array('action'=>'myJobs');
$this->Auth->logoutRedirect=array('action'=>'index');
$this->Auth->authenticate=array('Form'=>array('scope'=>array('User.role'=>"admin"),'userModel'=>'User','fields'=>array('username','password')));
$this->Auth->authError='Did you really think you are allowed to see that ?';
$this->Auth->unauthorizedRedirect=array('action'=>'index');
$this->Auth->loginAction=array('action'=>'index');
$this->Auth->deny('myUsers','deleteUser','logout');
$this->layout='admin_layout';
}
public function index(){
if($this->request->is('post'))
if($this->Auth->login()){
$this->Session->setFlash("<p style='margin-left:20px;color:#366;'><strong>Welcome Admin, You have successfully entered to your Admin Panel!</strong></p>");
$this->redirect($this->Auth->redirect());
}else{
$this->Session->setFlash('Invalid username or password, please try again');
$this->set('title_for_layout','Error - Login');
}
else
$this->set('title_for_layout','Admin');
$this->layout=false;
}
public function logout(){
$this->redirect($this->Auth->logout());
}
, I 개별적으로 모두 패널에서 login
을 구현하고 다음은 전체 프로젝트의 코드입니다. 그래서 AuthComponent
을 2 개의 패널에 개별적으로 구성했습니다.
지금은 User panel(UsersController)
에서 로그인하지 않고도 아무 페이지 에나 액세스 할 수 없지만 로그인하지 않고 내 사용자에게 index
페이지가 표시되기를 바랍니다.
그리고, 내가 Admin panel
(AdminsController)에서 로그인 할 때, 그것이 내가 성공적으로 내 관리자 패널에 입력 한 말하면서 User panel(UsersController)
의 login
페이지로 저를 얻을 수 있지만 여전히 캔트 액세스 관리자 패널.
$this->Auth->authorize('Controller')
을 시도했지만 동일한 결과가 나타납니다. 나는 allow()
또는 deny()
기능이 여기에 충분하다고 생각했지만, 무슨 일이 일어나고 있는지, 내 잘못은 무엇인지 모릅니다.
login/logout
시스템을 Session
을 사용하여 수동으로 구현하기 전에 정상적으로 작동했습니다. 그렇다면 나는 AuthComponent
을 사용하려고 생각했지만 그게 나를 미치게 만들었다.
아무도 도와 줄 수 있습니까?
감사합니다. 문제가 해결 될 것 같습니다. 실제로, 나는 allow()도 사용했으나 같은 결과를 보였다. 그리고 isAuthorized()는 어디에서도 사용되지 않습니다. CakePHP 문서의 예제에 따라 여기에 보관했습니다. 다시 제거하겠습니다[email protected] user221931 –
그리고 이것을 사용해야합니까? $ this-> Auth-> authorize ('Controller')? –
더 잘 작동하지만 여전히 이상하게 작동합니다. 나는 'Admin'패널과 'User'패널에서 동일한 브라우저, 동일한 탭, 하나씩 로그인하는 것을 원합니다. 그게 문제 야? '쿠키'문제가 있습니까? @ user221931 –