4
4
5
5
use Adldap \Models \User ;
6
6
use Adldap \AdldapInterface ;
7
+ use Adldap \Laravel \Events \Authenticated ;
8
+ use Adldap \Laravel \Events \Authenticating ;
9
+ use Adldap \Laravel \Events \AuthenticationFailed ;
10
+ use Adldap \Laravel \Auth \DatabaseUserProvider ;
11
+ use Adldap \Laravel \Auth \NoDatabaseUserProvider ;
12
+ use Illuminate \Support \Facades \Event ;
7
13
use Illuminate \Support \Facades \Config ;
8
14
use Illuminate \Contracts \Auth \Authenticatable ;
9
15
10
16
class UserResolver implements ResolverInterface
11
17
{
12
18
/**
13
- * The underlying Adldap instance.
19
+ * The Adldap instance.
14
20
*
15
21
* @var AdldapInterface
16
22
*/
@@ -32,11 +38,7 @@ public function __construct(AdldapInterface $ldap)
32
38
}
33
39
34
40
/**
35
- * Sets the LDAP connection to use.
36
- *
37
- * @param string $connection
38
- *
39
- * @return void
41
+ * {@inheritdoc}
40
42
*/
41
43
public function setConnection ($ connection )
42
44
{
@@ -60,19 +62,32 @@ public function byCredentials(array $credentials = [])
60
62
return ;
61
63
}
62
64
63
- return $ this ->query ()
64
- ->whereEquals ($ this ->getLdapDiscoveryAttribute (), $ credentials [$ this ->getEloquentUsernameAttribute ()])
65
- ->first ();
65
+ $ provider = Config::get ('adldap_auth.provider ' , DatabaseUserProvider::class);
66
+
67
+ // Depending on the configured user provider, the
68
+ // username field will differ for retrieving
69
+ // users by their credentials.
70
+ if ($ provider == NoDatabaseUserProvider::class) {
71
+ $ username = $ credentials [$ this ->getLdapDiscoveryAttribute ()];
72
+ } else {
73
+ $ username = $ credentials [$ this ->getEloquentUsernameAttribute ()];
74
+ }
75
+
76
+ $ field = $ this ->getLdapDiscoveryAttribute ();
77
+
78
+ return $ this ->query ()->whereEquals ($ field , $ username )->first ();
66
79
}
67
80
68
81
/**
69
82
* {@inheritdoc}
70
83
*/
71
84
public function byModel (Authenticatable $ model )
72
85
{
73
- return $ this ->query ()
74
- ->whereEquals ($ this ->getLdapDiscoveryAttribute (), $ model ->{$ this ->getEloquentUsernameAttribute ()})
75
- ->first ();
86
+ $ field = $ this ->getLdapDiscoveryAttribute ();
87
+
88
+ $ username = $ model ->{$ this ->getEloquentUsernameAttribute ()};
89
+
90
+ return $ this ->query ()->whereEquals ($ field , $ username )->first ();
76
91
}
77
92
78
93
/**
@@ -82,7 +97,19 @@ public function authenticate(User $user, array $credentials = [])
82
97
{
83
98
$ username = $ user ->getFirstAttribute ($ this ->getLdapAuthAttribute ());
84
99
85
- return $ this ->getProvider ()->auth ()->attempt ($ username , $ credentials ['password ' ]);
100
+ $ password = $ this ->getPasswordFromCredentials ($ credentials );
101
+
102
+ Event::fire (new Authenticating ($ user , $ username ));
103
+
104
+ if ($ this ->getProvider ()->auth ()->attempt ($ username , $ password )) {
105
+ Event::fire (new Authenticated ($ user ));
106
+
107
+ return true ;
108
+ }
109
+
110
+ Event::fire (new AuthenticationFailed ($ user ));
111
+
112
+ return false ;
86
113
}
87
114
88
115
/**
@@ -92,27 +119,21 @@ public function query()
92
119
{
93
120
$ query = $ this ->getProvider ()->search ()->users ();
94
121
95
- foreach ($ this ->getScopes () as $ scope ) {
96
- // Create the scope.
122
+ $ scopes = Config::get ('adldap_auth.scopes ' , []);
123
+
124
+ foreach ($ scopes as $ scope ) {
125
+ // Here we will use Laravel's IoC container to construct our scope.
126
+ // This allows us to utilize any Laravel dependencies in
127
+ // the scopes constructor that may be needed.
97
128
$ scope = app ($ scope );
98
129
99
- // Apply it to our query.
130
+ // With the scope constructed, we can apply it to our query.
100
131
$ scope ->apply ($ query );
101
132
}
102
133
103
134
return $ query ;
104
135
}
105
136
106
- /**
107
- * Returns the configured connection provider.
108
- *
109
- * @return \Adldap\Connections\ProviderInterface
110
- */
111
- protected function getProvider ()
112
- {
113
- return $ this ->ldap ->getProvider ($ this ->connection );
114
- }
115
-
116
137
/**
117
138
* {@inheritdoc}
118
139
*/
@@ -138,12 +159,26 @@ public function getEloquentUsernameAttribute()
138
159
}
139
160
140
161
/**
141
- * Returns the configured query scopes.
162
+ * Returns the password field to retrieve from the credentials.
163
+ *
164
+ * @param array $credentials
165
+ *
166
+ * @return string|null
167
+ */
168
+ protected function getPasswordFromCredentials ($ credentials )
169
+ {
170
+ return array_get ($ credentials , 'password ' );
171
+ }
172
+
173
+ /**
174
+ * Retrieves the provider for the current connection.
142
175
*
143
- * @return array
176
+ * @throws \Adldap\AdldapException
177
+ *
178
+ * @return \Adldap\Connections\ProviderInterface
144
179
*/
145
- protected function getScopes ()
180
+ protected function getProvider ()
146
181
{
147
- return Config:: get ( ' adldap_auth.scopes ' , [] );
182
+ return $ this -> ldap -> getProvider ( $ this -> connection );
148
183
}
149
184
}
0 commit comments