1919
2020namespace phpMyFAQ \Auth ;
2121
22+ use Closure ;
2223use phpMyFAQ \Auth ;
2324use phpMyFAQ \Configuration ;
2425use phpMyFAQ \Core \Exception ;
3536class AuthSso extends Auth implements AuthDriverInterface
3637{
3738 private readonly Request $ request ;
39+ private readonly ?Closure $ ldapFactory ;
40+ private readonly ?Closure $ userFactory ;
3841
3942 /**
4043 * @inheritDoc
4144 */
42- public function __construct (Configuration $ configuration )
43- {
45+ public function __construct (
46+ Configuration $ configuration ,
47+ ?Request $ request = null ,
48+ ?Closure $ ldapFactory = null ,
49+ ?Closure $ userFactory = null ,
50+ ) {
4451 parent ::__construct ($ configuration );
4552
46- $ this ->request = Request::createFromGlobals ();
53+ $ this ->request = $ request ?? Request::createFromGlobals ();
54+ $ this ->ldapFactory = $ ldapFactory ;
55+ $ this ->userFactory = $ userFactory ;
4756 }
4857
4958 /**
@@ -54,12 +63,12 @@ public function create(string $login, #[SensitiveParameter] string $password, st
5463 {
5564 if ($ this ->configuration ->isLdapActive ()) {
5665 // LDAP/AD + SSO
57- $ authLdap = new AuthLdap ( $ this ->configuration );
66+ $ authLdap = $ this ->createLdapAuth ( );
5867 return $ authLdap ->create ($ login , '' , $ domain );
5968 }
6069
6170 // SSO without LDAP/AD
62- $ user = new User ( $ this ->configuration );
71+ $ user = $ this ->createUser ( );
6372 $ result = $ user ->createUser ($ login , '' , $ domain );
6473 $ user ->setStatus ('active ' );
6574 $ user ->setAuthSource (AuthenticationSourceType::AUTH_SSO ->value );
@@ -121,4 +130,22 @@ public function isValidLogin(string $login, ?array $optionalData = null): int
121130 {
122131 return $ this ->request ->server ->get ('PHP_AUTH_USER ' ) !== null ? 1 : 0 ;
123132 }
133+
134+ private function createLdapAuth (): AuthLdap
135+ {
136+ if ($ this ->ldapFactory instanceof Closure) {
137+ return ($ this ->ldapFactory )();
138+ }
139+
140+ return new AuthLdap ($ this ->configuration );
141+ }
142+
143+ private function createUser (): User
144+ {
145+ if ($ this ->userFactory instanceof Closure) {
146+ return ($ this ->userFactory )();
147+ }
148+
149+ return new User ($ this ->configuration );
150+ }
124151}
0 commit comments