2014-12-01 2 views
0

저는 cakePHP 3을 배우면서 인턴쉽을 신청하고 있습니다. 현재는 Official cookbook from cakePHP.org의 자습서를 따르고 있지만이 책은 싫습니다. 매우 혼란 스럽습니다.CakePHP 3 : 사용자는 로그 아웃 할 수 없습니까?

어쨌든 Bookmarker 예제의 단계를 수행했는데 다소 효과가 있습니다.이 책이 로그인 섹션 & 로그 아웃 섹션까지 나에게 말했듯이 모든 작업을 수행했지만 시스템에서 로그 아웃하려고하면 나에게 알려줍니다 "해당 위치에 액세스 할 수있는 권한이 없습니다."

내 프로젝트의 코드가 더 필요하면 알려주십시오.

<?= $this->Html->link(__('Log out'), ['controller' => 'Users', 'action' => 'logout']) ?> 

/rootOfProject/src/Controller/AppController.php :

namespace App\Controller; 
use Cake\Controller\Controller; 

class AppController extends Controller { 
    public function initialize() { 
     $this->loadComponent('Flash'); 
     $this->loadComponent('Auth', [ 
      'authenticate' => [ 
       'Form' => [ 
        'fields' => [ 
         'username' => 'email', 
         'password' => 'password' 
        ] 
       ] 
      ], 
      'unauthorizedRedirect' => [ 
       'controller' => 'Users', 
       'action' => 'login' 
      ], 
      'authorize' => 'Controller' 
     ]); 
     $this->Auth->allow(['display']); 
    } 
    public function isAuthorized($user) { 
     return false; 
    } 
} 

, 내가 server/users/logout에 대한 하이퍼 링크를 생성하는 다음 코드로 사용자를 유도하고있어 로그 아웃

/rootOfProject/src/Controller/UsersController.php :

namespace App\Controller; 
use App\Controller\AppController; 
class UsersController extends AppController { 
    public function index() { 
     $this->set('users', $this->paginate($this->Users)); 
    } 
    public function view($id = null) { 
     $user = $this->Users->get($id, [ 
      'contain' => ['Bookmarks'] 
     ]); 
     $this->set('user', $user); 
    } 
    public function add() { 
     $user = $this->Users->newEntity($this->request->data); 
     if ($this->request->is('post')) { 
      if ($this->Users->save($user)) { 
       $this->Flash->success('The user has been saved.'); 
       return $this->redirect(['action' => 'index']); 
      } else { 
       $this->Flash->error('The user could not be saved. Please, try again.'); 
      } 
     } 
     $this->set(compact('user')); 
    } 
    public function edit($id = null) { 
     $user = $this->Users->get($id, [ 
      'contain' => [] 
     ]); 
     if ($this->request->is(['patch', 'post', 'put'])) { 
      $user = $this->Users->patchEntity($user, $this->request->data); 
      if ($this->Users->save($user)) { 
       $this->Flash->success('The user has been saved.'); 
       return $this->redirect(['action' => 'index']); 
      } else { 
       $this->Flash->error('The user could not be saved. Please, try again.'); 
      } 
     } 
     $this->set(compact('user')); 
    } 
    public function delete($id = null) { 
     $user = $this->Users->get($id); 
     $this->request->allowMethod(['post', 'delete']); 
     if ($this->Users->delete($user)) { 
      $this->Flash->success('The user has been deleted.'); 
     } else { 
      $this->Flash->error('The user could not be deleted. Please, try again.'); 
     } 
     return $this->redirect(['action' => 'index']); 
    } 
    public function login() { 
     if ($this->request->is('post')) { 
      $user = $this->Auth->identify(); 
      if ($user) { 
       $this->Auth->setUser($user); 
       return $this->redirect($this->Auth->redirectUrl()); 
      } 
      $this->Flash->error('Your username or password is incorrect.'); 
     } 
    } 
    public function logout() { 
     $this->Flash->success('You are now logged out.'); 
     return $this->redirect($this->Auth->logout()); 
    } 
    public function beforeFilter(\Cake\Event\Event $event) { 
     $this->Auth->allow(['add']); 
    } 
} 
+1

을, 나는의 코드를 변경 한'$ this-> Auth-> $ this-'에'([ '추가']) 허용 > Auth-> allow ([ 'add', 'logout'])''beforeFilter' 메서드를 호출하면 성공했습니다. @ndm –

답변

2

당신은 액세스를 거부하고 있습니다 isAuthorized() 콜백을 사용하는 모든 사용자가 false를 반환합니다. 따라서 명시 적으로 허용 된 작업 ($this->Auth->allow())과 암시 적 허용 로그인 작업 만 액세스 할 수 있습니다.

인증 (인증! = 인증) 검사를 구현하지 않으려면 인증 구성 요소 구성에서 authorize 옵션뿐 아니라 컨트롤러에서 콜백을 제거하십시오.

인증에 대한 자세한 내용은 http://book.cakephp.org/3.0/en/controllers/components/authentication.html#authorization을 참조하십시오. 당신의 AppController에서

+0

에게 감사드립니다. 이 책의 저자는 학생이 모든 액세스를 거부하고 자신이 원했던 것을 명시 적으로 허용해야하지만 액세스를 허용하는 방법을 말하지 않았 음을 지적했습니다. 이 책은 나에게 코드를 던지고, 복사하고 붙여 넣으라고 알려주며, 자연스럽게 물건을 이해할 것을 기대합니다. 고맙습니다. –

0

다음과 같은 추가 : 관심있는 사람들을위한

<?php 
    public function isAuthorized($user) 
    { 
     $action = $this->request->params['action']; 

     // The add and index actions are always allowed. 
     if (in_array($action, ['logout'])) { 
      return true; 
     }else{ 
      return false; 
     } 
} 
?> 
관련 문제