Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.

Commit 80b6f9c

Browse files
committed
Remove overridden trait methods, fix tests.
1 parent 17963bf commit 80b6f9c

File tree

2 files changed

+6
-194
lines changed

2 files changed

+6
-194
lines changed

src/AdldapAuthUserProvider.php

Lines changed: 0 additions & 194 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Adldap\Models\User;
77
use Illuminate\Auth\EloquentUserProvider;
88
use Illuminate\Contracts\Auth\Authenticatable;
9-
use Illuminate\Database\Eloquent\Model;
109
use Illuminate\Support\Arr;
1110
use Illuminate\Support\Facades\Config;
1211

@@ -87,111 +86,6 @@ public function retrieveByCredentials(array $credentials)
8786
}
8887
}
8988

90-
/**
91-
* Creates a local User from Active Directory.
92-
*
93-
* @param User $user
94-
* @param string $password
95-
*
96-
* @return \Illuminate\Database\Eloquent\Model
97-
*/
98-
protected function getModelFromAdldap(User $user, $password)
99-
{
100-
// Get the username attributes.
101-
$attributes = $this->getUsernameAttribute();
102-
103-
// Get the model key.
104-
$key = key($attributes);
105-
106-
// Get the username from the AD model.
107-
$username = $user->{$attributes[$key]};
108-
109-
// Make sure we retrieve the first username
110-
// result if it's an array.
111-
if (is_array($username)) {
112-
$username = Arr::get($username, 0);
113-
}
114-
115-
// Try to retrieve the model from the model key and AD username.
116-
$model = $this->createModel()->newQuery()->where([$key => $username])->first();
117-
118-
// Create the model instance of it isn't found.
119-
if (!$model instanceof Model) {
120-
$model = $this->createModel();
121-
}
122-
123-
// Set the username and password in case
124-
// of changes in active directory.
125-
$model->{$key} = $username;
126-
127-
// Sync the users password.
128-
$model = $this->syncModelPassword($model, $password);
129-
130-
// Synchronize other active directory
131-
// attributes on the model.
132-
$model = $this->syncModelFromAdldap($user, $model);
133-
134-
if ($this->getBindUserToModel()) {
135-
$model = $this->bindAdldapToModel($user, $model);
136-
}
137-
138-
return $model;
139-
}
140-
141-
/**
142-
* Fills a models attributes by the specified Users attributes.
143-
*
144-
* @param User $user
145-
* @param Authenticatable $model
146-
*
147-
* @return Authenticatable
148-
*/
149-
protected function syncModelFromAdldap(User $user, Authenticatable $model)
150-
{
151-
$attributes = $this->getSyncAttributes();
152-
153-
foreach ($attributes as $modelField => $adField) {
154-
if ($this->isAttributeCallback($adField)) {
155-
$value = $this->handleAttributeCallback($user, $adField);
156-
} else {
157-
$value = $this->handleAttributeRetrieval($user, $adField);
158-
}
159-
160-
$model->{$modelField} = $value;
161-
}
162-
163-
if ($model instanceof Model) {
164-
$model->save();
165-
}
166-
167-
return $model;
168-
}
169-
170-
/**
171-
* Syncs the models password with the specified password.
172-
*
173-
* @param Authenticatable $model
174-
* @param string $password
175-
*
176-
* @return Authenticatable
177-
*/
178-
protected function syncModelPassword(Authenticatable $model, $password)
179-
{
180-
if ($model instanceof Model && $model->hasSetMutator('password')) {
181-
// If the model has a set mutator for the password then
182-
// we'll assume that the dev is using their
183-
// own encryption method for passwords.
184-
$model->password = $password;
185-
186-
return $model;
187-
}
188-
189-
// Always encrypt the model password by default.
190-
$model->password = bcrypt($password);
191-
192-
return $model;
193-
}
194-
19589
/**
19690
* Retrieves the Adldap User model from the
19791
* specified Laravel model.
@@ -221,32 +115,6 @@ protected function discoverAdldapFromModel($model)
221115
return $model;
222116
}
223117

224-
/**
225-
* Binds the Adldap User instance to the Eloquent model instance
226-
* by setting its `adldapUser` public property.
227-
*
228-
* @param User $user
229-
* @param Authenticatable $model
230-
*
231-
* @return Authenticatable
232-
*/
233-
protected function bindAdldapToModel(User $user, Authenticatable $model)
234-
{
235-
$model->adldapUser = $user;
236-
237-
return $model;
238-
}
239-
240-
/**
241-
* Returns a new Adldap user query.
242-
*
243-
* @return \Adldap\Query\Builder
244-
*/
245-
protected function newAdldapUserQuery()
246-
{
247-
return $this->getAdldap()->search()->select($this->getSelectAttributes());
248-
}
249-
250118
/**
251119
* Authenticates a user against Active Directory.
252120
*
@@ -260,68 +128,6 @@ protected function authenticate($username, $password)
260128
return $this->getAdldap()->auth()->attempt($username, $password);
261129
}
262130

263-
/**
264-
* Returns true / false if the specified string
265-
* is a callback for an attribute handler.
266-
*
267-
* @param string $string
268-
*
269-
* @return bool
270-
*/
271-
protected function isAttributeCallback($string)
272-
{
273-
$matches = preg_grep("/(\w)@(\w)/", explode("\n", $string));
274-
275-
return count($matches) > 0;
276-
}
277-
278-
/**
279-
* Handles retrieving the value from an attribute callback.
280-
*
281-
* @param User $user
282-
* @param string $callback
283-
*
284-
* @return mixed
285-
*/
286-
protected function handleAttributeCallback(User $user, $callback)
287-
{
288-
// Explode the callback into its class and method.
289-
list($class, $method) = explode('@', $callback);
290-
291-
// Create the handler.
292-
$handler = app($class);
293-
294-
// Call the attribute handler method and return the result.
295-
return call_user_func_array([$handler, $method], [$user]);
296-
}
297-
298-
/**
299-
* Handles retrieving the specified field from the User model.
300-
*
301-
* @param User $user
302-
* @param string $field
303-
*
304-
* @return string|null
305-
*/
306-
protected function handleAttributeRetrieval(User $user, $field)
307-
{
308-
if ($field === $this->getSchema()->thumbnail()) {
309-
// If the field we're retrieving is the users thumbnail photo, we need
310-
// to retrieve it encoded so we're able to save it to the database.
311-
$value = $user->getThumbnailEncoded();
312-
} else {
313-
$value = $user->{$field};
314-
315-
if (is_array($value)) {
316-
// If the AD Value is an array, we'll
317-
// retrieve the first value.
318-
$value = Arr::get($value, 0);
319-
}
320-
}
321-
322-
return $value;
323-
}
324-
325131
/**
326132
* Returns the password key to retrieve the
327133
* password from the user input array.

tests/AdldapTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public function test_auth_passes()
8383
$mockedProvider->shouldReceive('getSchema')->andReturn(Schema::get());
8484
$mockedProvider->shouldReceive('auth')->once()->andReturn($mockedAuth);
8585

86+
$mockedSearch->shouldReceive('users')->once()->andReturn($mockedSearch);
8687
$mockedSearch->shouldReceive('select')->once()->andReturn($mockedBuilder);
8788
$mockedBuilder->shouldReceive('whereEquals')->once()->andReturn($mockedBuilder);
8889
$mockedBuilder->shouldReceive('first')->once()->andReturn($adUser);
@@ -143,6 +144,7 @@ public function test_auth_fails()
143144
$mockedProvider->shouldReceive('getSchema')->andReturn(Schema::get());
144145
$mockedProvider->shouldReceive('auth')->once()->andReturn($mockedAuth);
145146

147+
$mockedSearch->shouldReceive('users')->once()->andReturn($mockedSearch);
146148
$mockedSearch->shouldReceive('select')->once()->andReturn($mockedBuilder);
147149
$mockedBuilder->shouldReceive('whereEquals')->once()->andReturn($mockedBuilder);
148150
$mockedBuilder->shouldReceive('first')->once()->andReturn($adUser);
@@ -169,6 +171,7 @@ public function test_auth_fails_when_user_not_found()
169171
$mockedProvider->shouldReceive('search')->once()->andReturn($mockedSearch);
170172
$mockedProvider->shouldReceive('getSchema')->andReturn(Schema::get());
171173

174+
$mockedSearch->shouldReceive('users')->once()->andReturn($mockedSearch);
172175
$mockedSearch->shouldReceive('select')->once()->andReturn($mockedBuilder);
173176
$mockedBuilder->shouldReceive('getConnection')->once()->andReturn($mockedConnection);
174177
$mockedBuilder->shouldReceive('whereEquals')->once()->andReturn($mockedBuilder);
@@ -197,6 +200,7 @@ public function test_credentials_key_does_not_exist()
197200
Adldap::shouldReceive('getManager')->andReturn($manager);
198201

199202
$mockedProvider->shouldReceive('search')->once()->andReturn($mockedSearch);
203+
$mockedSearch->shouldReceive('users')->once()->andReturn($mockedSearch);
200204
$mockedProvider->shouldReceive('getSchema')->andReturn(Schema::get());
201205

202206
$nonExistantInputKey = 'non-existent-key';
@@ -229,6 +233,7 @@ public function test_config_login_fallback()
229233

230234
$mockedConnection->shouldReceive('isBound')->twice()->andReturn(true);
231235

236+
$mockedSearch->shouldReceive('users')->twice()->andReturn($mockedSearch);
232237
$mockedSearch->shouldReceive('select')->twice()->andReturn($mockedSearch);
233238
$mockedSearch->shouldReceive('getConnection')->twice()->andReturn($mockedConnection);
234239
$mockedSearch->shouldReceive('whereEquals')->twice()->andReturn($mockedSearch);
@@ -279,6 +284,7 @@ public function test_config_login_fallback_no_connection()
279284
$mockedConnection->shouldReceive('isBound')->once()->andReturn(false);
280285

281286
$mockedSearch->shouldReceive('select')->once()->andReturn($mockedSearch);
287+
$mockedSearch->shouldReceive('users')->once()->andReturn($mockedSearch);
282288
$mockedSearch->shouldReceive('getConnection')->once()->andReturn($mockedConnection);
283289

284290
$manager = new Manager();

0 commit comments

Comments
 (0)