@@ -38,36 +38,15 @@ public function retrieveByToken($identifier, $token)
3838 */
3939 public function retrieveByCredentials (array $ credentials )
4040 {
41- // Get the search query for users only.
42- $ query = $ this ->newAdldapUserQuery ();
41+ $ user = $ this ->authenticateWithCredentials ($ credentials );
4342
44- // Make sure the connection is bound
45- // before we try to utilize it.
46- if ($ query ->getConnection ()->isBound ()) {
47- // Get the username input attributes.
48- $ attributes = $ this ->getUsernameAttribute ();
49-
50- // Get the input key.
51- $ key = key ($ attributes );
52-
53- // Filter the query by the username attribute and retrieve the first user result.
54- $ user = $ query ->where ([$ attributes [$ key ] => $ credentials [$ key ]])->first ();
55-
56- // If the user is an Adldap User model instance.
57- if ($ user instanceof User) {
58- // Retrieve the users login attribute.
59- $ username = $ this ->getUsernameFromUser ($ user );
60-
61- // Retrieve the password from the submitted credentials.
62- $ password = $ this ->getPasswordFromCredentials ($ credentials );
43+ // If the user is an Adldap User model instance.
44+ if ($ user instanceof User) {
45+ // Retrieve the password from the submitted credentials.
46+ $ password = $ this ->getPasswordFromCredentials ($ credentials );
6347
64- // Try to log the user in.
65- if (!is_null ($ password ) && $ this ->authenticate ($ username , $ password )) {
66- // Login was successful, we'll create a new
67- // Laravel model with the Adldap user.
68- return $ this ->getModelFromAdldap ($ user , $ password );
69- }
70- }
48+ // Construct / retrieve the eloquent model from our Adldap user.
49+ return $ this ->getModelFromAdldap ($ user , $ password );
7150 }
7251
7352 if ($ this ->getLoginFallback ()) {
@@ -82,17 +61,21 @@ public function retrieveByCredentials(array $credentials)
8261 */
8362 public function validateCredentials (Authenticatable $ user , array $ credentials )
8463 {
85- if ($ this ->getPasswordSync ()) {
86- // If password syncing is enabled. We can hit our
87- // local database to check the hashed password.
64+ if ($ this ->authenticateWithCredentials ($ credentials )) {
65+ // We've authenticated successfully, we'll finally
66+ // save the user to our local database.
67+ $ this ->saveModel ($ user );
68+
69+ return true ;
70+ }
71+
72+ if ($ this ->getLoginFallback () && $ user ->exists ) {
73+ // If the user exists in our local database already and fallback is
74+ // enabled, we'll perform standard eloquent authentication.
8875 return parent ::validateCredentials ($ user , $ credentials );
8976 }
9077
91- // We've already performed LDAP authentication on the user
92- // and password synchronization is disabled, therefore
93- // we can't validate the submitted password in our
94- // local database. We'll return true here.
95- return true ;
78+ return false ;
9679 }
9780
9881 /**
@@ -122,7 +105,17 @@ protected function discoverAdldapFromModel($model)
122105 }
123106
124107 /**
125- * Authenticates a user against Active Directory.
108+ * Checks if we're currently connected to our configured LDAP server.
109+ *
110+ * @return bool
111+ */
112+ protected function isConnected ()
113+ {
114+ return $ this ->getAdldap ()->getConnection ()->isBound ();
115+ }
116+
117+ /**
118+ * Authenticates a user against our LDAP connection.
126119 *
127120 * @param string $username
128121 * @param string $password
@@ -135,23 +128,49 @@ protected function authenticate($username, $password)
135128 }
136129
137130 /**
138- * Returns the configured username from the specified AD user .
131+ * Authenticates against Active Directory using the specified credentials .
139132 *
140- * @param User $user
133+ * @param array $credentials
141134 *
142- * @return string
135+ * @return User|false
143136 */
144- protected function getUsernameFromUser ( User $ user )
137+ protected function authenticateWithCredentials ( array $ credentials = [] )
145138 {
146- $ username = $ user ->{$ this ->getLoginAttribute ()};
139+ // Make sure we're connected to our LDAP server before we run any operations.
140+ if ($ this ->isConnected ()) {
141+ // Retrieve the Adldap user.
142+ $ user = $ this ->newAdldapUserQuery ()->where ([
143+ $ this ->getUsernameValue () => $ this ->getUsernameFromCredentials ($ credentials )
144+ ])->first ();
145+
146+ if ($ user instanceof User) {
147+ // Retrieve the authentication username for the AD user.
148+ $ username = $ this ->getUsernameFromAdUser ($ user );
147149
148- if (is_array ($ username )) {
149- // We'll make sure we retrieve the users first username
150- // attribute if it's contained in an array.
151- $ username = Arr::get ($ username , 0 );
150+ // Retrieve the users password.
151+ $ password = $ this ->getPasswordFromCredentials ($ credentials );
152+
153+ // Perform LDAP authentication.
154+ if ($ this ->authenticate ($ username , $ password )) {
155+ // Passed, return the user instance.
156+ return $ user ;
157+ }
158+ }
152159 }
153160
154- return $ username ;
161+ return false ;
162+ }
163+
164+ /**
165+ * Returns the username from the specified credentials.
166+ *
167+ * @param array $credentials
168+ *
169+ * @return string
170+ */
171+ protected function getUsernameFromCredentials (array $ credentials = [])
172+ {
173+ return Arr::get ($ credentials , $ this ->getUsernameKey ());
155174 }
156175
157176 /**
0 commit comments