10
10
use Adldap \Laravel \Events \DiscoveredWithCredentials ;
11
11
use Adldap \Laravel \Events \Imported ;
12
12
use Adldap \Laravel \Facades \Resolver ;
13
- use Adldap \Laravel \Traits \ValidatesUsers ;
14
13
use Adldap \Models \User ;
15
14
use Illuminate \Auth \EloquentUserProvider ;
16
15
use Illuminate \Contracts \Auth \Authenticatable ;
16
+ use Illuminate \Contracts \Hashing \Hasher as HasherContract ;
17
17
use Illuminate \Support \Facades \Bus ;
18
18
use Illuminate \Support \Facades \Config ;
19
19
use Illuminate \Support \Facades \Event ;
20
+ use Illuminate \Support \Traits \ForwardsCalls ;
20
21
21
- class DatabaseUserProvider extends EloquentUserProvider
22
+ /** @mixin EloquentUserProvider */
23
+ class DatabaseUserProvider extends UserProvider
22
24
{
23
- use ValidatesUsers ;
25
+ use ForwardsCalls ;
24
26
25
27
/**
26
28
* The currently authenticated LDAP user.
@@ -29,86 +31,149 @@ class DatabaseUserProvider extends EloquentUserProvider
29
31
*/
30
32
protected $ user ;
31
33
34
+ /**
35
+ * The fallback eloquent user provider.
36
+ *
37
+ * @var EloquentUserProvider
38
+ */
39
+ protected $ eloquent ;
40
+
41
+ /**
42
+ * Constructor.
43
+ *
44
+ * @param HasherContract $hasher
45
+ * @param string $model
46
+ */
47
+ public function __construct (HasherContract $ hasher , $ model )
48
+ {
49
+ $ this ->eloquent = new EloquentUserProvider ($ hasher , $ model );
50
+ }
51
+
52
+ /**
53
+ * Forward missing method calls to the underlying Eloquent provider.
54
+ *
55
+ * @param string $method
56
+ * @param mixed $parameters
57
+ *
58
+ * @return mixed
59
+ */
60
+ public function __call ($ method , $ parameters )
61
+ {
62
+ return $ this ->forwardCallTo ($ this ->eloquent , $ method , $ parameters );
63
+ }
64
+
65
+ /**
66
+ * @inheritDoc
67
+ */
68
+ public function retrieveById ($ identifier )
69
+ {
70
+ return $ this ->eloquent ->retrieveById ($ identifier );
71
+ }
72
+
73
+ /**
74
+ * @inheritDoc
75
+ */
76
+ public function retrieveByToken ($ identifier , $ token )
77
+ {
78
+ return $ this ->eloquent ->retrieveByToken ($ identifier , $ token );
79
+ }
80
+
81
+ /**
82
+ * @inheritDoc
83
+ */
84
+ public function updateRememberToken (Authenticatable $ user , $ token )
85
+ {
86
+ $ this ->eloquent ->updateRememberToken ($ user , $ token );
87
+ }
88
+
32
89
/**
33
90
* {@inheritdoc}
34
91
*/
35
92
public function retrieveByCredentials (array $ credentials )
36
93
{
37
- // Retrieve the LDAP user who is authenticating.
38
94
$ user = Resolver::byCredentials ($ credentials );
39
95
40
96
if ($ user instanceof User) {
41
- // Set the currently authenticating LDAP user.
42
- $ this ->user = $ user ;
43
-
44
- Event::dispatch (new DiscoveredWithCredentials ($ user ));
45
-
46
- // Import / locate the local user account.
47
- return Bus::dispatch (
48
- new Import ($ user , $ this ->createModel ())
49
- );
97
+ return $ this ->setAndImportAuthenticatingUser ($ user );
50
98
}
51
99
52
100
if ($ this ->isFallingBack ()) {
53
- return parent :: retrieveByCredentials ($ credentials );
101
+ return $ this -> eloquent -> retrieveByCredentials ($ credentials );
54
102
}
55
103
}
56
104
105
+ /**
106
+ * Set and import the authenticating LDAP user.
107
+ *
108
+ * @param User $user
109
+ *
110
+ * @return \Illuminate\Database\Eloquent\Model
111
+ */
112
+ protected function setAndImportAuthenticatingUser (User $ user )
113
+ {
114
+ // Set the currently authenticating LDAP user.
115
+ $ this ->user = $ user ;
116
+
117
+ Event::dispatch (new DiscoveredWithCredentials ($ user ));
118
+
119
+ // Import / locate the local user account.
120
+ return Bus::dispatch (
121
+ new Import ($ user , $ this ->eloquent ->createModel ())
122
+ );
123
+ }
124
+
57
125
/**
58
126
* {@inheritdoc}
59
127
*/
60
128
public function validateCredentials (Authenticatable $ model , array $ credentials )
61
129
{
62
- if ($ this ->user instanceof User) {
63
- // If an LDAP user was discovered, we can go
64
- // ahead and try to authenticate them.
65
- if (Resolver::authenticate ($ this ->user , $ credentials )) {
66
- Event::dispatch (new AuthenticatedWithCredentials ($ this ->user , $ model ));
67
-
68
- // Here we will perform authorization on the LDAP user. If all
69
- // validation rules pass, we will allow the authentication
70
- // attempt. Otherwise, it is automatically rejected.
71
- if ($ this ->passesValidation ($ this ->user , $ model )) {
72
- // Here we can now synchronize / set the users password since
73
- // they have successfully passed authentication
74
- // and our validation rules.
75
- Bus::dispatch (new SyncPassword ($ model , $ credentials ));
76
-
77
- $ model ->save ();
78
-
79
- if ($ model ->wasRecentlyCreated ) {
80
- // If the model was recently created, they
81
- // have been imported successfully.
82
- Event::dispatch (new Imported ($ this ->user , $ model ));
83
- }
84
-
85
- Event::dispatch (new AuthenticationSuccessful ($ this ->user , $ model ));
86
-
87
- return true ;
88
- }
89
-
90
- Event::dispatch (new AuthenticationRejected ($ this ->user , $ model ));
91
- }
92
-
93
- // LDAP Authentication failed.
130
+ // If the user exists in the local database, fallback is enabled,
131
+ // and no LDAP user is was located for authentication, we will
132
+ // perform standard eloquent authentication to "fallback" to.
133
+ if (
134
+ $ model ->exists
135
+ && $ this ->isFallingBack ()
136
+ && !$ this ->user instanceof User
137
+ ) {
138
+ return $ this ->eloquent ->validateCredentials ($ model , $ credentials );
139
+ }
140
+
141
+ if (!Resolver::authenticate ($ this ->user , $ credentials )) {
94
142
return false ;
95
143
}
96
144
97
- if ($ this ->isFallingBack () && $ model ->exists ) {
98
- // If the user exists in our local database already and fallback is
99
- // enabled, we'll perform standard eloquent authentication.
100
- return parent ::validateCredentials ($ model , $ credentials );
145
+ Event::dispatch (new AuthenticatedWithCredentials ($ this ->user , $ model ));
146
+
147
+ // Here we will perform authorization on the LDAP user. If all
148
+ // validation rules pass, we will allow the authentication
149
+ // attempt. Otherwise, it is automatically rejected.
150
+ if (!$ this ->passesValidation ($ this ->user , $ model )) {
151
+ Event::dispatch (new AuthenticationRejected ($ this ->user , $ model ));
152
+
153
+ return false ;
154
+ }
155
+
156
+ Bus::dispatch (new SyncPassword ($ model , $ credentials ));
157
+
158
+ $ model ->save ();
159
+
160
+ if ($ model ->wasRecentlyCreated ) {
161
+ // If the model was recently created, they
162
+ // have been imported successfully.
163
+ Event::dispatch (new Imported ($ this ->user , $ model ));
101
164
}
102
165
103
- return false ;
166
+ Event::dispatch (new AuthenticationSuccessful ($ this ->user , $ model ));
167
+
168
+ return true ;
104
169
}
105
170
106
171
/**
107
172
* Determines if login fallback is enabled.
108
173
*
109
174
* @return bool
110
175
*/
111
- protected function isFallingBack (): bool
176
+ protected function isFallingBack ()
112
177
{
113
178
return Config::get ('ldap_auth.login_fallback ' , false );
114
179
}
0 commit comments