2015-10-22 13 views
8

पर अतिरिक्त डेटा पास करें Auth से मेरे खोजक में ऐसी स्थितियां हैं जिन्हें मुझे $this->request तक पहुंचने की आवश्यकता है लेकिन मेरे पास UsersTable पर इसका उपयोग नहीं है।खोजकर्ता auth

AppController :: प्रारंभ

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

UsersTable

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; 
} 

मैं के बाद से मैं न UsersTable पर $this->request->data के लिए acces है finderauth के लिए एक अतिरिक्त डेटा FOM AppController पारित की जरूरत है।

अद्यतन

लोग टिप्पणियों पर कह रहे हैं एक बुरा डिजाइन तो मैं वास्तव में मैं क्या जरूरत है समझा जाएगा है।

मैं एक मेज users है, लेकिन प्रत्येक उपयोगकर्ता एक gym के अंतर्गत आता है। username(email) केवल एक विशेष gym के लिए अद्वितीय है, इसलिए gym_id 1 से [email protected] और gym_id 2 से [email protected] पर [email protected] हो सकता है। प्रवेश पृष्ठ पर मैं auth finder जो gym उपयोगकर्ता username कि मैं provied अंतर्गत आता है बताने के लिए gym_slug है।

+0

यह मेरे लिए संभवतः खराब डिजाइन की तरह लगता है, तो आप दिखा सकते हैं कि आप किस पर पारित करने के लिए चाहते थे? यद्यपि यह किसी तरीके से व्यवहार के रूप में संभव होना चाहिए :-) – Spriz

+0

मैं यूआरएल पर एक जिम स्लग पास कर रहा हूं इसलिए मुझे उस स्लग के आधार पर जिम की आईडी प्राप्त करने की आवश्यकता है जो उपयोगकर्ता को ढूंढने के लिए फ़िल्टर करता है क्योंकि मेरे पास 'जिम_आईडी' है 'उपयोगकर्ता' टेबल। मैं 'this-> request-> पैराम्स [' gym_slug] 'द्वारा स्लग तक पहुंच सकता हूं, लेकिन मुझे' UserTable' –

+0

पर इस पर accesa नहीं है हमें यहां जाने के लिए थोड़ी अधिक जानकारी चाहिए। यह दोनों सवाल और टिप्पणी है कि हो सकता है आप एक उपयोगकर्ता के सत्यापन कर रहे हैं तो उन्हें अपने जिम के पृष्ठ पर रीडायरेक्ट करने की कोशिश कर से लगता है? अपना प्रश्न संपादित करें और वास्तव में प्रदान करें कि आप क्या कर रहे हैं और क्यों।आप ऊपर टिप्पणी तक जिम का जिक्र नहीं करते हैं और मुझे उत्सुकता है कि आपको अनुरोध डेटा को रीडायरेक्ट करने की आवश्यकता क्यों होगी (यदि आप यह भी कर रहे हैं) तो आपके पास उसी तालिका में जिम_आईडी संग्रहीत है। –

उत्तर

2

मेरी जानकारी के लिए, 3.1 में विन्यास में यह पारित करके यह करने के लिए कोई रास्ता नहीं है। एक सुविधा अनुरोध के रूप में केकफ़्प गिट हब पर यह एक अच्छा विचार हो सकता है।

एक नया प्रमाणीकरण उद्देश्य यह है कि आधार प्रमाणित फैली बनाने के द्वारा यह कर और फिर _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; 
} 
} 

अधिक जानकारी के लिए इस देखें: Creating Custom Authentication Objects

+1

मेरे लिए काम किया, यह एक शर्म की बात है कि मैं आपको –

+0

की अवधि समाप्त होने से पहले बक्षीस नहीं दे सका यह काफी ठीक है! एक साथी डेवलपर की मदद करने के लिए कुछ भी! – chrisShick

1

मैं जानता हूँ कि यह एक पुराने सवाल है, लेकिन मैं मैं खोजक मैं CakePHP 3 पर बनाया गया हमारे सास क्षुधा में से एक में उपयोग कर रहा हूँ पोस्ट होगा सोचा क्या यह शायद DRY आदि का पालन नहीं करता है। कहने के लिए सब कुछ एक्स या वाई रास्ता किया जा सकता है ..... आपको हमेशा नियमों को झुकाव करना पड़ता है। इस मामले यूआरएल के आधार पर (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 वैसे भी आशा है कि यह मदद करता है किसी को

संबंधित मुद्दे