1010use Adldap \Laravel \Events \DiscoveredWithCredentials ;
1111use Adldap \Laravel \Events \Imported ;
1212use Adldap \Laravel \Facades \Resolver ;
13- use Adldap \Laravel \Traits \ValidatesUsers ;
1413use Adldap \Models \User ;
1514use Illuminate \Auth \EloquentUserProvider ;
1615use Illuminate \Contracts \Auth \Authenticatable ;
16+ use Illuminate \Contracts \Hashing \Hasher as HasherContract ;
1717use Illuminate \Support \Facades \Bus ;
1818use Illuminate \Support \Facades \Config ;
1919use Illuminate \Support \Facades \Event ;
20+ use Illuminate \Support \Traits \ForwardsCalls ;
2021
21- class DatabaseUserProvider extends EloquentUserProvider
22+ /** @mixin EloquentUserProvider */
23+ class DatabaseUserProvider extends UserProvider
2224{
23- use ValidatesUsers ;
25+ use ForwardsCalls ;
2426
2527 /**
2628 * The currently authenticated LDAP user.
@@ -29,86 +31,149 @@ class DatabaseUserProvider extends EloquentUserProvider
2931 */
3032 protected $ user ;
3133
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+
3289 /**
3390 * {@inheritdoc}
3491 */
3592 public function retrieveByCredentials (array $ credentials )
3693 {
37- // Retrieve the LDAP user who is authenticating.
3894 $ user = Resolver::byCredentials ($ credentials );
3995
4096 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 );
5098 }
5199
52100 if ($ this ->isFallingBack ()) {
53- return parent :: retrieveByCredentials ($ credentials );
101+ return $ this -> eloquent -> retrieveByCredentials ($ credentials );
54102 }
55103 }
56104
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+
57125 /**
58126 * {@inheritdoc}
59127 */
60128 public function validateCredentials (Authenticatable $ model , array $ credentials )
61129 {
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 )) {
94142 return false ;
95143 }
96144
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 ));
101164 }
102165
103- return false ;
166+ Event::dispatch (new AuthenticationSuccessful ($ this ->user , $ model ));
167+
168+ return true ;
104169 }
105170
106171 /**
107172 * Determines if login fallback is enabled.
108173 *
109174 * @return bool
110175 */
111- protected function isFallingBack (): bool
176+ protected function isFallingBack ()
112177 {
113178 return Config::get ('ldap_auth.login_fallback ' , false );
114179 }
0 commit comments