|
7 | 7 | use Adldap\Models\User; |
8 | 8 | use Illuminate\Support\Arr; |
9 | 9 | use Illuminate\Support\Facades\Config; |
| 10 | +use Illuminate\Database\Eloquent\Model; |
10 | 11 | use Illuminate\Contracts\Auth\Authenticatable; |
11 | 12 | use Illuminate\Auth\EloquentUserProvider; |
12 | 13 |
|
@@ -85,36 +86,36 @@ public function retrieveByCredentials(array $credentials) |
85 | 86 | */ |
86 | 87 | protected function getModelFromAdldap(User $user, $password) |
87 | 88 | { |
88 | | - // Get the username attributes |
| 89 | + // Get the username attributes. |
89 | 90 | $attributes = $this->getUsernameAttribute(); |
90 | 91 |
|
91 | | - // Get the model key |
| 92 | + // Get the model key. |
92 | 93 | $key = key($attributes); |
93 | 94 |
|
94 | | - // Get the username from the AD model |
| 95 | + // Get the username from the AD model. |
95 | 96 | $username = $user->{$attributes[$key]}; |
96 | 97 |
|
97 | 98 | // Make sure we retrieve the first username |
98 | | - // result if it's an array |
| 99 | + // result if it's an array. |
99 | 100 | if (is_array($username)) { |
100 | 101 | $username = Arr::get($username, 0); |
101 | 102 | } |
102 | 103 |
|
103 | | - // Try to retrieve the model from the model key and AD username |
| 104 | + // Try to retrieve the model from the model key and AD username. |
104 | 105 | $model = $this->createModel()->newQuery()->where([$key => $username])->first(); |
105 | 106 |
|
106 | | - // Create the model instance of it isn't found |
| 107 | + // Create the model instance of it isn't found. |
107 | 108 | if(!$model) $model = $this->createModel(); |
108 | 109 |
|
109 | 110 | // Set the username and password in case |
110 | | - // of changes in active directory |
| 111 | + // of changes in active directory. |
111 | 112 | $model->{$key} = $username; |
112 | 113 |
|
113 | | - // Sync the users password |
| 114 | + // Sync the users password. |
114 | 115 | $model = $this->syncModelPassword($model, $password); |
115 | 116 |
|
116 | 117 | // Synchronize other active directory |
117 | | - // attributes on the model |
| 118 | + // attributes on the model. |
118 | 119 | $model = $this->syncModelFromAdldap($user, $model); |
119 | 120 |
|
120 | 121 | if($this->getBindUserToModel()) { |
@@ -166,6 +167,16 @@ protected function syncModelFromAdldap(User $user, Authenticatable $model) |
166 | 167 | */ |
167 | 168 | protected function syncModelPassword(Authenticatable $model, $password) |
168 | 169 | { |
| 170 | + if ($model instanceof Model && $model->hasSetMutator('password')) { |
| 171 | + // If the model has a set mutator for the password then |
| 172 | + // we'll assume that the dev is using they're |
| 173 | + // own encryption method for passwords. |
| 174 | + $model->password = $password; |
| 175 | + |
| 176 | + return $model; |
| 177 | + } |
| 178 | + |
| 179 | + // Always encrypt the model password by default. |
169 | 180 | $model->password = bcrypt($password); |
170 | 181 |
|
171 | 182 | return $model; |
|
0 commit comments