3
3
namespace Adldap \Laravel \Resolvers ;
4
4
5
5
use Adldap \Models \User ;
6
+ use Adldap \Laravel \Auth \DatabaseUserProvider ;
7
+ use Adldap \Laravel \Auth \NoDatabaseUserProvider ;
6
8
use Adldap \Connections \ProviderInterface ;
7
9
use Illuminate \Support \Facades \Config ;
8
10
use Illuminate \Contracts \Auth \Authenticatable ;
@@ -41,27 +43,38 @@ public function byCredentials(array $credentials = [])
41
43
return ;
42
44
}
43
45
44
- return $ this ->query ()
45
- ->whereEquals ($ this ->getLdapUsername (), $ credentials [$ this ->getEloquentUsername ()])
46
- ->first ();
46
+ $ field = $ this ->getLdapDiscoveryAttribute ();
47
+
48
+ switch ($ this ->getUserProvider ()) {
49
+ case NoDatabaseUserProvider::class:
50
+ $ username = $ credentials [$ this ->getLdapDiscoveryAttribute ()];
51
+ break ;
52
+ default :
53
+ $ username = $ credentials [$ this ->getEloquentUsernameAttribute ()];
54
+ break ;
55
+ }
56
+
57
+ return $ this ->query ()->whereEquals ($ field , $ username )->first ();
47
58
}
48
59
49
60
/**
50
61
* {@inheritdoc}
51
62
*/
52
63
public function byModel (Authenticatable $ model )
53
64
{
54
- return $ this ->query ()
55
- ->whereEquals ($ this ->getLdapUsername (), $ model ->{$ this ->getEloquentUsername ()})
56
- ->first ();
65
+ $ field = $ this ->getLdapDiscoveryAttribute ();
66
+
67
+ $ username = $ model ->{$ this ->getEloquentUsernameAttribute ()};
68
+
69
+ return $ this ->query ()->whereEquals ($ field , $ username )->first ();
57
70
}
58
71
59
72
/**
60
73
* {@inheritdoc}
61
74
*/
62
75
public function authenticate (User $ user , array $ credentials = [])
63
76
{
64
- $ attribute = $ user ->getAttribute ($ this ->getLdapAuthUsername ());
77
+ $ attribute = $ user ->getAttribute ($ this ->getLdapAuthAttribute ());
65
78
66
79
$ username = is_array ($ attribute ) ? array_first ($ attribute ) : $ attribute ;
67
80
@@ -89,23 +102,23 @@ public function query()
89
102
/**
90
103
* {@inheritdoc}
91
104
*/
92
- public function getLdapUsername ()
105
+ public function getLdapDiscoveryAttribute ()
93
106
{
94
107
return Config::get ('adldap_auth.usernames.ldap.discover ' , 'userprincipalname ' );
95
108
}
96
109
97
110
/**
98
111
* {@inheritdoc}
99
112
*/
100
- public function getLdapAuthUsername ()
113
+ public function getLdapAuthAttribute ()
101
114
{
102
115
return Config::get ('adldap_auth.usernames.ldap.authenticate ' , 'userprincipalname ' );
103
116
}
104
117
105
118
/**
106
119
* {@inheritdoc}
107
120
*/
108
- public function getEloquentUsername ()
121
+ public function getEloquentUsernameAttribute ()
109
122
{
110
123
return Config::get ('adldap_auth.usernames.eloquent ' , 'email ' );
111
124
}
@@ -119,4 +132,14 @@ protected function getScopes()
119
132
{
120
133
return Config::get ('adldap_auth.scopes ' , []);
121
134
}
135
+
136
+ /**
137
+ * Returns the configured LDAP user provider.
138
+ *
139
+ * @return string
140
+ */
141
+ protected function getUserProvider ()
142
+ {
143
+ return Config::get ('adldap_auth.provider ' , DatabaseUserProvider::class);
144
+ }
122
145
}
0 commit comments