2015-10-22 8 views
8

私のファインダーAuthには、$this->requestにアクセスする必要のある条件がありますが、UsersTableにアクセスできません。ファインダー認証に余分なデータを渡す

のAppController ::私はUsersTable$this->request->dataへのアクセスもを持っていけないので、finderauthに余分なデータFOM AppControllerを渡す必要

public function findAuth(Query $query, array $options) 
{ 
    $query 
     ->select([ 
      'Users.id', 
      'Users.name', 
      'Users.username', 
      'Users.password', 
     ]) 
     ->where(['Users.is_active' => true]); // If i had access to extra data passed I would use here. 

    return $query; 
} 

$this->loadComponent('Auth', [ 
     'authenticate' => [ 
      'Form' => [ 
       'finder' => 'auth', 
      ] 
     ] 
    ]); 

がUsersTable初期化します。

更新

人々は、私はまさに私が必要なものを説明しますので、悪いデザインであることをコメントに言っています。

私はテーブルusersを持っていますが、各ユーザはgymに属しています。 username(email)は、特定のgymにしかユニークではないので、gym_id 1から[email protected]を、gym_id 2から別の[email protected]を得ることができます。 私はログインページにgym_slugauth finderと書いています。gym私が提供したユーザusernameが所属しています。

+0

これはおそらく私には悪いデザインのように聞こえる、あなたが渡したいものを示すことができますか?しかし何らかの形で動作する必要があります:-) – Spriz

+0

私はURLにジムスラッグを渡していますので、私はジムのIDを取得する必要があります。 'users'テーブルです。私は 'this-> request-> params ['gym_slug]'でスラッグにアクセスできますが、これは 'UsersTable'にアクセスしていません –

+0

ここに行くにはもう少し情報が必要です。質問とコメントの両方から、ユーザーを認証してからジムのページにリダイレクトしようとしていると思われますか?あなたの質問を編集し、あなたがやっていることとその理由を正確に伝えてください。あなたは上記のコメントまでジムについて言及していませんし、gym_idが同じテーブルに格納されているので、リダイレクトするリクエストデータが必要なのはなぜか分かります。 –

答えて

2

私の知る限り、これを3.1の設定に渡すことでこれを行う方法はありません。これは、機能要求としてcakephp git hubに送信することをお勧めします。

基本認証を拡張し、_findUserと_queryをオーバーライドする新しい認証オブジェクトを作成することで、それを行う方法があります。このような何か:

class GymFormAuthenticate extends BaseAuthenticate 
{ 

/** 
    * Checks the fields to ensure they are supplied. 
    * 
    * @param \Cake\Network\Request $request The request that contains login information. 
    * @param array $fields The fields to be checked. 
    * @return bool False if the fields have not been supplied. True if they exist. 
    */ 
protected function _checkFields(Request $request, array $fields) 
{ 
    foreach ([$fields['username'], $fields['password'], $fields['gym']] as $field) { 
     $value = $request->data($field); 
     if (empty($value) || !is_string($value)) { 
      return false; 
     } 
    } 
    return true; 
} 

/** 
    * Authenticates the identity contained in a request. Will use the `config.userModel`, and `config.fields` 
    * to find POST data that is used to find a matching record in the `config.userModel`. Will return false if 
    * there is no post data, either username or password is missing, or if the scope conditions have not been met. 
    * 
    * @param \Cake\Network\Request $request The request that contains login information. 
    * @param \Cake\Network\Response $response Unused response object. 
    * @return mixed False on login failure. An array of User data on success. 
    */ 
public function authenticate(Request $request, Response $response) 
{ 
    $fields = $this->_config['fields']; 
    if (!$this->_checkFields($request, $fields)) { 
     return false; 
    } 
    return $this->_findUser(
     $request->data[$fields['username']], 
     $request->data[$fields['password']], 
     $request->data[$fields['gym']], 
    ); 
} 

/** 
    * Find a user record using the username,password,gym provided. 
    * 
    * Input passwords will be hashed even when a user doesn't exist. This 
    * helps mitigate timing attacks that are attempting to find valid usernames. 
    * 
    * @param string $username The username/identifier. 
    * @param string|null $password The password, if not provided password checking is skipped 
    * and result of find is returned. 
    * @return bool|array Either false on failure, or an array of user data. 
    */ 
protected function _findUser($username, $password = null, $gym = null) 
{ 
    $result = $this->_query($username, $gym)->first(); 

    if (empty($result)) { 
     return false; 
    } 

    if ($password !== null) { 
     $hasher = $this->passwordHasher(); 
     $hashedPassword = $result->get($this->_config['fields']['password']); 
     if (!$hasher->check($password, $hashedPassword)) { 
      return false; 
     } 

     $this->_needsPasswordRehash = $hasher->needsRehash($hashedPassword); 
     $result->unsetProperty($this->_config['fields']['password']); 
    } 

    return $result->toArray(); 
} 

/** 
    * Get query object for fetching user from database. 
    * 
    * @param string $username The username/identifier. 
    * @return \Cake\ORM\Query 
    */ 
protected function _query($username, $gym) 
{ 
    $config = $this->_config; 
    $table = TableRegistryget($config['userModel']); 

    $options = [ 
     'conditions' => [$table->aliasField($config['fields']['username']) => $username, 'gym' => $gym] 
    ]; 

    if (!empty($config['scope'])) { 
     $options['conditions'] = array_merge($options['conditions'], $config['scope']); 
    } 
    if (!empty($config['contain'])) { 
     $options['contain'] = $config['contain']; 
    } 

    $query = $table->find($config['finder'], $options); 

    return $query; 
} 
} 

詳細については、この参照してください。私は、これは古い質問です知っているが、私は、私は、CakePHP 3の上に構築された当社のSaaSアプリケーションの一つに、私が使用していますファインダーを投稿するだろうと思ったCreating Custom Authentication Objects

+1

私に働いて、それは残念ですが、それが期限切れになる前に恩恵を与えることはできませんでした –

+0

それはかなり大丈夫です!仲間の開発者を助ける何か! – chrisShick

1

をそれはおそらくDRYなどに続くでしょうか。すべてのことをXまたはYの方法で行うことができます.....あなたは常にルールを曲げる必要があります。この場合、URLに応じて、(xdomain.comまたはydomain.com)我々のアプリの数字アウト顧客であり、などのレイアウトを変更します。また基づいて、ユーザーが&をメールで送信するために結ばれる人とにかくずっとあなた

public function findAuth(\Cake\ORM\Query $query, array $options) { 
    $query 
      ->select([ 
       'Users.id', 
       'Users.email', 
       'Users.password', 
       'Users.site_id', 
       'Users.firstname', 
       'Users.lastname']) 
      ->where([ 
       'Users.active' => 1, 
       'Users.site_id'=> \Cake\Core\Configure::read('site_id') 
      ]); 

    return $query; 
} 

ようSITE_IDそれが誰かを助けることを願って

関連する問題