3
3
namespace Adldap \Laravel \Commands ;
4
4
5
5
use Adldap \Models \User ;
6
+ use Adldap \Laravel \Facades \Resolver ;
6
7
use Adldap \Laravel \Events \Importing ;
7
8
use Adldap \Laravel \Events \Synchronized ;
8
9
use Adldap \Laravel \Events \Synchronizing ;
9
- use Illuminate \Support \Str ;
10
10
use Illuminate \Support \Facades \Event ;
11
11
use Illuminate \Support \Facades \Config ;
12
12
use Illuminate \Database \Eloquent \Model ;
@@ -27,25 +27,16 @@ class Import
27
27
*/
28
28
protected $ model ;
29
29
30
- /**
31
- * The LDAP users credentials.
32
- *
33
- * @var array
34
- */
35
- protected $ credentials ;
36
-
37
30
/**
38
31
* Constructor.
39
32
*
40
33
* @param User $user
41
34
* @param Model $model
42
- * @param array $credentials
43
35
*/
44
- public function __construct (User $ user , Model $ model, array $ credentials = [] )
36
+ public function __construct (User $ user , Model $ model )
45
37
{
46
- $ this ->user = $ this -> transformUsername ( $ user) ;
38
+ $ this ->user = $ user ;
47
39
$ this ->model = $ model ;
48
- $ this ->credentials = $ credentials ;
49
40
}
50
41
51
42
/**
@@ -58,7 +49,7 @@ public function handle()
58
49
// Here we'll try to locate our local user model from
59
50
// the LDAP users model. If one isn't located,
60
51
// we'll create a new one for them.
61
- $ model = $ this ->findByCredentials () ?: $ this ->model ->newInstance ();
52
+ $ model = $ this ->findById () ?: $ this ->model ->newInstance ();
62
53
63
54
if (! $ model ->exists ) {
64
55
Event::fire (new Importing ($ this ->user , $ model ));
@@ -78,12 +69,8 @@ public function handle()
78
69
*
79
70
* @return Model|null
80
71
*/
81
- protected function findByCredentials ()
72
+ protected function findById ()
82
73
{
83
- if (empty ($ this ->credentials )) {
84
- return ;
85
- }
86
-
87
74
$ query = $ this ->model ->newQuery ();
88
75
89
76
if ($ query ->getMacro ('withTrashed ' )) {
@@ -93,15 +80,11 @@ protected function findByCredentials()
93
80
$ query ->withTrashed ();
94
81
}
95
82
96
- foreach ($ this ->credentials as $ key => $ value ) {
97
- if (! Str::contains ($ key , 'password ' )) {
98
- // We need to lowercase all values so we locate the
99
- // proper model. This avoids case sensitivity.
100
- $ query ->where ($ key , strtolower ($ value ));
101
- }
102
- }
103
-
104
- return $ query ->first ();
83
+ return $ query ->where (
84
+ Resolver::getDatabaseIdentifierColumn (),
85
+ '= ' ,
86
+ Resolver::getLdapUserIdentifier ($ this ->user )
87
+ )->first ();
105
88
}
106
89
107
90
/**
@@ -113,6 +96,12 @@ protected function findByCredentials()
113
96
*/
114
97
protected function sync (Model $ model )
115
98
{
99
+ // Set the users identifier automatically.
100
+ $ model ->setAttribute (
101
+ Resolver::getDatabaseIdentifierColumn (),
102
+ Resolver::getLdapUserIdentifier ($ this ->user )
103
+ );
104
+
116
105
foreach ($ this ->getLdapSyncAttributes () as $ modelField => $ ldapField ) {
117
106
// If the field is a loaded class and contains a `handle()` method,
118
107
// we need to construct the attribute handler.
@@ -133,24 +122,6 @@ protected function sync(Model $model)
133
122
}
134
123
}
135
124
136
- /**
137
- * Transforms the username of the given user to avoid case sensitivity issues.
138
- *
139
- * We want to transform the username on the user model so it persists through attribute handlers.
140
- *
141
- * @param User $user
142
- *
143
- * @return User
144
- */
145
- protected function transformUsername (User $ user )
146
- {
147
- $ attribute = $ this ->getLdapDiscoveryUsername ();
148
-
149
- $ user ->setFirstAttribute ($ attribute , strtolower ($ user ->getFirstAttribute ($ attribute )));
150
-
151
- return $ user ;
152
- }
153
-
154
125
/**
155
126
* Determines if the given handler value is a class that contains the 'handle' method.
156
127
*
@@ -175,14 +146,4 @@ protected function getLdapSyncAttributes()
175
146
'name ' => 'cn ' ,
176
147
]);
177
148
}
178
-
179
- /**
180
- * Returns the configured LDAP discovery username attribute.
181
- *
182
- * @return string
183
- */
184
- protected function getLdapDiscoveryUsername ()
185
- {
186
- return Config::get ('ldap_auth.usernames.ldap.discover ' , 'userprincipalname ' );
187
- }
188
149
}
0 commit comments