Browse Source

Merge pull request #8299 from cakephp/auth-finder-options

Allow passing custom finder options to AuthComponent config.
ADmad 10 years ago
parent
commit
e7db494ec4

+ 10 - 1
src/Auth/BaseAuthenticate.php

@@ -35,6 +35,9 @@ abstract class BaseAuthenticate implements EventListenerInterface
      * - `fields` The fields to use to identify a user by.
      * - `userModel` The alias for users table, defaults to Users.
      * - `finder` The finder method to use to fetch user record. Defaults to 'all'.
+     *   You can set finder name as string or an array where key is finder name and value
+     *   is an array passed to `Table::find()` options.
+     *   E.g. ['finderName' => ['some_finder_option' => 'some_value']]
      * - `passwordHasher` Password hasher class. Can be a string specifying class name
      *    or an array containing `className` key, any other keys will be passed as
      *    config to the class. Defaults to 'Default'.
@@ -144,7 +147,13 @@ abstract class BaseAuthenticate implements EventListenerInterface
             $options['contain'] = $config['contain'];
         }
 
-        $query = $table->find($config['finder'], $options);
+        $finder = $config['finder'];
+        if (is_array($finder)) {
+            $options += current($finder);
+            $finder = key($finder);
+        }
+
+        $query = $table->find($finder, $options);
 
         return $query;
     }

+ 1 - 1
src/Auth/FormAuthenticate.php

@@ -25,7 +25,7 @@ use Cake\Network\Response;
  * ```
  *  $this->Auth->authenticate = [
  *      'Form' => [
- *          'scope' => ['Users.active' => 1]
+ *          'finder' => ['auth' => ['some_finder_option' => 'some_value']]
  *      ]
  *  ]
  * ```

+ 12 - 0
tests/TestCase/Auth/FormAuthenticateTest.php

@@ -307,6 +307,18 @@ class FormAuthenticateTest extends TestCase
             'username' => 'mariano',
         ];
         $this->assertEquals($expected, $result, 'Result should not contain "created" and "modified" fields');
+
+        $this->auth->config([
+            'finder' => ['auth' => ['return_created' => true]]
+        ]);
+
+        $result = $this->auth->authenticate($request, $this->response);
+        $expected = [
+            'id' => 1,
+            'username' => 'mariano',
+            'created' => new Time('2007-03-17 01:16:23'),
+        ];
+        $this->assertEquals($expected, $result);
     }
 
     /**

+ 3 - 0
tests/test_app/TestApp/Model/Table/AuthUsersTable.php

@@ -32,6 +32,9 @@ class AuthUsersTable extends Table
     public function findAuth(Query $query, array $options)
     {
         $query->select(['id', 'username', 'password']);
+        if (!empty($options['return_created'])) {
+            $query->select(['created']);
+        }
 
         return $query;
     }