2014-09-27 2 views
0

CakePHP 2.3.6을 사용하고 있습니다. 한 프로젝트에서 여러 종류의 사용자 (Users, Admins, etc.)에 대해 User Management System을 구현해야합니다. 현재로서는 Admin Panel(only 1 Admin for now)User Panel에 대해서만 우려하고 있습니다. 평소와 같이 특정 지역 (페이지)에 액세스하려면 AdminUser Panel의 모든 페이지를 포함한 모든 영역에 액세스 할 수 있으며 Userslogin이어야합니다. CakePHP 2.x의 사용자 관리 시스템이 이상하게 동작합니다.

는 I 대신에 다른 다양한 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을 사용하려고 생각했지만 그게 나를 미치게 만들었다.

아무도 도와 줄 수 있습니까?

답변

2

중요한 것부터 먼저하지 그것은 훨씬 쉽게 코드를 살펴 가지고 그것을 이해하는 것 등, HTML과 인라인 CSS를 혼합처럼 coding standards 및 모범 사례를 따라하려고

감사합니다.

문제가 생겼습니다. AuthenticationComponent은 먼저 에 전달되지 않으면 모든 것이 거부되었다고 가정합니다. 따라서 Auth::deny()은 코드에서 이미 거부 된 메소드를 거부하고 나머지는 허용하지 않습니다. 먼저 Auth::allow('*')이라고 말하면서 무언가를 거절 할 수는 있겠지만 명시 적으로 거부하는 것보다 명시 적으로 행동을 허용하는 것이 더 좋습니다 (그리고 나서 새로운 것을 거부하고 그것을 세계에 노출시키는 것을 잊지 마십시오).

$this->redirect의 경우 return 문 앞에 붙습니다. 이렇게하면 코드 실행을 그 시점에서 중단 할 수 있습니다. 그렇지 않으면 예상치 못한 경로가 계속 될 수 있습니다. 문제가 잘못된 리디렉션 인 것 같습니다. $this->Auth->redirect()으로 리디렉션하지 않고 $this->redirect(array('controller => 'admin', 'action' => 'index'));으로 시도하십시오.

마지막으로 AppController::isAuthorized()의 목적은 무엇입니까? 어떤 곳에서 전화가 왔습니까?

+0

감사합니다. 문제가 해결 될 것 같습니다. 실제로, 나는 allow()도 사용했으나 같은 결과를 보였다. 그리고 isAuthorized()는 어디에서도 사용되지 않습니다. CakePHP 문서의 예제에 따라 여기에 보관했습니다. 다시 제거하겠습니다[email protected] user221931 –

+0

그리고 이것을 사용해야합니까? $ this-> Auth-> authorize ('Controller')? –

+0

더 잘 작동하지만 여전히 이상하게 작동합니다. 나는 'Admin'패널과 'User'패널에서 동일한 브라우저, 동일한 탭, 하나씩 로그인하는 것을 원합니다. 그게 문제 야? '쿠키'문제가 있습니까? @ user221931 –

관련 문제