forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiColumnAuthenticate.php
More file actions
84 lines (80 loc) · 2.61 KB
/
Copy pathMultiColumnAuthenticate.php
File metadata and controls
84 lines (80 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
App::uses('FormAuthenticate', 'Controller/Component/Auth');
/**
* An authentication adapter for AuthComponent. Provides the ability to authenticate using POST
* data. The username form input can be checked against multiple table columns, for instance username and email
*
* {{{
* $this->Auth->authenticate = array(
* 'Authenticate.MultiColumn' => array(
* 'fields' => array(
* 'username' => 'username',
* 'password' => 'password'
* ),
* 'columns' => array('username', 'email'),
* 'userModel' => 'User',
* 'scope' => array('User.active' => 1)
* )
* )
* }}}
*
* @author Ceeram
* @copyright Ceeram
* @license MIT
* @link https://github.com/ceeram/Authenticate
*/
class MultiColumnAuthenticate extends FormAuthenticate {
/**
* Settings for this object.
*
* - `fields` The fields to use to identify a user by.
* - 'columns' array of columns to check username form input against
* - `userModel` The model name of the User, defaults to User.
* - `scope` Additional conditions to use when looking up and authenticating users,
* i.e. `array('User.is_active' => 1).`
*
* @var array
*/
public $settings = array(
'fields' => array(
'username' => 'username',
'password' => 'password'
),
'columns' => array(),
'userModel' => 'User',
'scope' => array()
);
/**
* Find a user record using the standard options.
*
* @param string $username The username/identifier.
* @param string $password The unhashed password.
* @return Mixed Either false on failure, or an array of user data.
*/
protected function _findUser($username, $password = null) {
$userModel = $this->settings['userModel'];
list($plugin, $model) = pluginSplit($userModel);
$fields = $this->settings['fields'];
$conditions = array($model . '.' . $fields['username'] => $username);
if ($this->settings['columns'] && is_array($this->settings['columns'])) {
$columns = array();
foreach ($this->settings['columns'] as $column) {
$columns[] = array($model . '.' . $column => $username);
}
$conditions = array('OR' => $columns);
}
$conditions = array_merge($conditions, array($model . '.' . $fields['password'] => $this->_password($password)));
if (!empty($this->settings['scope'])) {
$conditions = array_merge($conditions, $this->settings['scope']);
}
$result = ClassRegistry::init($userModel)->find('first', array(
'conditions' => $conditions,
'recursive' => 0
));
if (empty($result) || empty($result[$model])) {
return false;
}
unset($result[$model][$fields['password']]);
return $result[$model];
}
}