2016-04-15 1 views
0

この質問はcakePHPブックマークチュートリアルを完了した人を対象としています。CakePHPのブックマークチュートリアル、ログインは目的のページの代わりにhome.ctpにリダイレクトされます

私は本のcakePHPブックマークチュートリアルに取り組んでいます。私はいくつか問題があります。私は、あなたがブックマークを作成したユーザーにブックマークのアクセスを制限する必要がある部分にあります。ブックで指示されたコードをAppController.phpとBookmarksController.phpに追加しました。

これを実行するとログインできなくなり、ブックマークページにリダイレクトされるはずですが、私はhome.ctpにリダイレクトされます。私は何が問題であるか把握できません。私はここで答えを見つけることができれば幸いです。

ここに私のAppController.php

<?php 
/** 
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) 
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) 
* 
* Licensed under The MIT License 
* For full copyright and license information, please see the LICENSE.txt 
* Redistributions of files must retain the above copyright notice. 
* 
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) 
* @link  http://cakephp.org CakePHP(tm) Project 
* @since  0.2.9 
* @license http://www.opensource.org/licenses/mit-license.php MIT License 
*/ 
namespace App\Controller; 

use Cake\Controller\Controller; 
use Cake\Event\Event; 

/** 
* Application Controller 
* 
* Add your application-wide methods in the class below, your controllers 
* will inherit them. 
* 
* @link http://book.cakephp.org/3.0/en/controllers.html#the-app-controller 
*/ 
class AppController extends Controller 
{ 

    /** 
    * Initialization hook method. 
    * 
    * Use this method to add common initialization code like loading components. 
    * 
    * e.g. `$this->loadComponent('Security');` 
    * 
    * @return void 
    */ 
    public function initialize() 
    { 
     parent::initialize(); 

     $this->loadComponent('RequestHandler'); 
     $this->loadComponent('Flash'); 
     $this->loadComponent('Auth', [ 
      'authorize' => 'Controller', 

      'authenticate' => [ 
       'Form' => [ 
        'fields' => [ 
         'username' => 'email', 
         'password' => 'password' 
        ] 
       ] 
      ], 

      'loginAction' => [ 
       'controller' => 'Users', 
       'action' => 'login' 
      ], 


      'unauthorizedRedirect' => $this->referer() 
     ]); 

     $this->Auth->allow(['display']); 
    } 

    public function isAuthorized($user) 
    { 
     return false; 
    } 

    /** 
    * Before render callback. 
    * 
    * @param \Cake\Event\Event $event The beforeRender event. 
    * @return void 
    */ 
    public function beforeRender(Event $event) 
    { 
     if (!array_key_exists('_serialize', $this->viewVars) && 
      in_array($this->response->type(), ['application/json', 'application/xml']) 
     ) { 
      $this->set('_serialize', true); 
     } 
    } 
} 

相続人は私のBookmarkersController.php

<?php 
namespace App\Controller; 

use App\Controller\AppController; 

/** 
* Bookmarks Controller 
* 
* @property \App\Model\Table\BookmarksTable $Bookmarks 
*/ 
class BookmarksController extends AppController 
{ 

    /** 
    * Index method 
    * 
    * @return \Cake\Network\Response|null 
    */ 
    public function index() 
    { 
     $this->paginate = [ 
      'contain' => ['Users'] 
     ]; 
     $bookmarks = $this->paginate($this->Bookmarks); 

     $this->set(compact('bookmarks')); 
     $this->set('_serialize', ['bookmarks']); 
    } 

    /** 
    * View method 
    * 
    * @param string|null $id Bookmark id. 
    * @return \Cake\Network\Response|null 
    * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. 
    */ 
    public function view($id = null) 
    { 
     $bookmark = $this->Bookmarks->get($id, [ 
      'contain' => ['Users', 'Tags'] 
     ]); 

     $this->set('bookmark', $bookmark); 
     $this->set('_serialize', ['bookmark']); 
    } 

    /** 
    * Add method 
    * 
    * @return \Cake\Network\Response|void Redirects on successful add, renders view otherwise. 
    */ 
    public function add() 
    { 
     $bookmark = $this->Bookmarks->newEntity(); 
     if ($this->request->is('post')) { 
      $bookmark = $this->Bookmarks->patchEntity($bookmark, $this->request->data); 
      if ($this->Bookmarks->save($bookmark)) { 
       $this->Flash->success(__('The bookmark has been saved.')); 
       return $this->redirect(['action' => 'index']); 
      } else { 
       $this->Flash->error(__('The bookmark could not be saved. Please, try again.')); 
      } 
     } 
     $users = $this->Bookmarks->Users->find('list', ['limit' => 200]); 
     $tags = $this->Bookmarks->Tags->find('list', ['limit' => 200]); 
     $this->set(compact('bookmark', 'users', 'tags')); 
     $this->set('_serialize', ['bookmark']); 
    } 

    /** 
    * Edit method 
    * 
    * @param string|null $id Bookmark id. 
    * @return \Cake\Network\Response|void Redirects on successful edit, renders view otherwise. 
    * @throws \Cake\Network\Exception\NotFoundException When record not found. 
    */ 
    public function edit($id = null) 
    { 
     $bookmark = $this->Bookmarks->get($id, [ 
      'contain' => ['Tags'] 
     ]); 
     if ($this->request->is(['patch', 'post', 'put'])) { 
      $bookmark = $this->Bookmarks->patchEntity($bookmark, $this->request->data); 
      if ($this->Bookmarks->save($bookmark)) { 
       $this->Flash->success(__('The bookmark has been saved.')); 
       return $this->redirect(['action' => 'index']); 
      } else { 
       $this->Flash->error(__('The bookmark could not be saved. Please, try again.')); 
      } 
     } 
     $users = $this->Bookmarks->Users->find('list', ['limit' => 200]); 
     $tags = $this->Bookmarks->Tags->find('list', ['limit' => 200]); 
     $this->set(compact('bookmark', 'users', 'tags')); 
     $this->set('_serialize', ['bookmark']); 
    } 

    /** 
    * Delete method 
    * 
    * @param string|null $id Bookmark id. 
    * @return \Cake\Network\Response|null Redirects to index. 
    * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. 
    */ 
    public function delete($id = null) 
    { 
     $this->request->allowMethod(['post', 'delete']); 
     $bookmark = $this->Bookmarks->get($id); 
     if ($this->Bookmarks->delete($bookmark)) { 
      $this->Flash->success(__('The bookmark has been deleted.')); 
     } else { 
      $this->Flash->error(__('The bookmark could not be deleted. Please, try again.')); 
     } 
     return $this->redirect(['action' => 'index']); 
    } 

    public function tags() 
    { 
     $tags = $this->request->params['pass']; 
     $bookmarks = $this->Bookmarks->find('tagged', [ 
      'tags' => $tags 
     ]); 

     $this->set([ 
      'bookmarks' => $bookmarks, 
      'tags' => $tags 
     ]); 
    } 

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

     if (in_array($action, ['index', 'add', 'tags'])) { 
      return true; 
     } 

     if (empty($this->request->params['pass'][0])) { 
      return false; 
     } 

     $id = $this->request->params['pass'][0]; 
     $bookmark = $this->Bookmarks->get($id); 
     if ($bookmark->user_id == $user['id']) { 
      return true; 
     } 

     return parent::isAuthorized($user); 
    } 
} 

そして最後に、ここに私のUsersController.phpが

<?php 
namespace App\Controller; 

use App\Controller\AppController; 

/** 
* Users Controller 
* 
* @property \App\Model\Table\UsersTable $Users 
*/ 
class UsersController extends AppController 
{ 

    /** 
    * Index method 
    * 
    * @return \Cake\Network\Response|null 
    */ 
    public function index() 
    { 
     $users = $this->paginate($this->Users); 

     $this->set(compact('users')); 
     $this->set('_serialize', ['users']); 
    } 

    /** 
    * View method 
    * 
    * @param string|null $id User id. 
    * @return \Cake\Network\Response|null 
    * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. 
    */ 
    public function view($id = null) 
    { 
     $user = $this->Users->get($id, [ 
      'contain' => ['Bookmarks'] 
     ]); 

     $this->set('user', $user); 
     $this->set('_serialize', ['user']); 
    } 

    /** 
    * Add method 
    * 
    * @return \Cake\Network\Response|void Redirects on successful add, renders view otherwise. 
    */ 
    public function add() 
    { 
     $user = $this->Users->newEntity(); 
     if ($this->request->is('post')) { 
      $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')); 
     $this->set('_serialize', ['user']); 
    } 

    /** 
    * Edit method 
    * 
    * @param string|null $id User id. 
    * @return \Cake\Network\Response|void Redirects on successful edit, renders view otherwise. 
    * @throws \Cake\Network\Exception\NotFoundException When record not found. 
    */ 
    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')); 
     $this->set('_serialize', ['user']); 
    } 

    /** 
    * Delete method 
    * 
    * @param string|null $id User id. 
    * @return \Cake\Network\Response|null Redirects to index. 
    * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. 
    */ 
    public function delete($id = null) 
    { 
     $this->request->allowMethod(['post', 'delete']); 
     $user = $this->Users->get($id); 
     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 initialize() 
    { 
     parent::initialize(); 
     $this->Auth->allow(['logout', 'add']); 
    } 

    public function logout() 
    { 
     $this->Flash->success('You are now logged out'); 
     return $this->redirect($this->Auth->logout()); 
    } 
} 
です

EDIT:

はチュートリアルによると、かつて私は、ブックマークのページにリダイレクトする必要がある、ログイン。

Here'reチュートリアルへのリンク:

http://book.cakephp.org/3.0/en/tutorials-and-examples/bookmarks/intro.html http://book.cakephp.org/3.0/en/tutorials-and-examples/bookmarks/part-two.html

また、私はまだ自分でこれを理解しようとしています。私が解決できれば、私は解決策を提供します。

+0

** loginRedirect **パラメータをAppController.phpのauthオプションに追加しようとしましたか? 'loginRedirect' => [ \t \t 'コントローラ' => 'DefaultUserLoggedController' \t \t '作用' => 'welcomeAction' \t \t]、 –

+1

@Brunoランプ、無Iの避難所」:このよう t。しかし、私はチュートリアルで指示するように指示されていませんでした。私はその手順にちょうど従っていた。それは、私が適切にログインすることができなければならないと言います。それは、ユーザーがログインすると適切なページにリダイレクトされ、私の状況ではそうではありません。 – skipper

答えて

1

これは、ユーザーがログインするとブックマークページではなくユーザーページに移動するという理由があるようです。制限が設定されているため、ユーザーはハッシュされたパスワードなどを含む他のユーザーに関する情報にユーザーがアクセスできないようにする必要があるため、ユーザーページにアクセスすることはできません。

この制限は、ユーザーが拒否された要求を行うと、ユーザーを前のページに戻すように設計されています。しかし、ユーザーはすでにログインしており、まだログアウトしていないので、ログインページに戻すことは意味がありません。したがって、プログラムはログアウトせずにhome.ctp、別名ホームページにユーザーを戻します。

ユーザーは彼に限定されないページにはまだアクセスできますが、ユーザーは彼に属していないものには変更を加えることができません。

関連する問題