Skip to content

Commit aaf66fe

Browse files
committed
refactor: moved duplicate code into private method
1 parent ab519da commit aaf66fe

File tree

1 file changed

+28
-63
lines changed

1 file changed

+28
-63
lines changed

phpmyfaq/src/phpMyFAQ/Auth/AuthLdap.php

Lines changed: 28 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@
3232
*/
3333
class AuthLdap extends Auth implements AuthDriverInterface
3434
{
35-
/** @var LdapCore */
36-
private $ldap = null;
35+
/** @var LdapCore|null */
36+
private ?LdapCore $ldap = null;
3737

3838
/** @var string[] Array of LDAP servers */
39-
private $ldapServer;
39+
private array $ldapServer;
4040

4141
/** @var int Active LDAP server */
42-
private $activeServer = 0;
42+
private int $activeServer = 0;
4343

4444
/** @var bool */
4545
private $multipleServers;
@@ -61,17 +61,7 @@ public function __construct(Configuration $config)
6161
}
6262

6363
$this->ldap = new LdapCore($this->config);
64-
$this->ldap->connect(
65-
$this->ldapServer[$this->activeServer]['ldap_server'],
66-
$this->ldapServer[$this->activeServer]['ldap_port'],
67-
$this->ldapServer[$this->activeServer]['ldap_base'],
68-
$this->ldapServer[$this->activeServer]['ldap_user'],
69-
$this->ldapServer[$this->activeServer]['ldap_password']
70-
);
71-
72-
if ($this->ldap->error) {
73-
$this->errors[] = $this->ldap->error;
74-
}
64+
$this->connect($this->activeServer);
7565
}
7666

7767
/**
@@ -83,17 +73,7 @@ public function create($login, $password, $domain = ''): bool
8373
$user = new User($this->config);
8474
$result = $user->createUser($login, '', $domain);
8575

86-
$this->ldap->connect(
87-
$this->ldapServer[$this->activeServer]['ldap_server'],
88-
$this->ldapServer[$this->activeServer]['ldap_port'],
89-
$this->ldapServer[$this->activeServer]['ldap_base'],
90-
$this->ldapServer[$this->activeServer]['ldap_user'],
91-
$this->ldapServer[$this->activeServer]['ldap_password']
92-
);
93-
94-
if ($this->ldap->error) {
95-
$this->errors[] = $this->ldap->error;
96-
}
76+
$this->connect($this->activeServer);
9777

9878
$user->setStatus('active');
9979

@@ -126,29 +106,20 @@ public function delete($login): bool
126106

127107
/**
128108
* @inheritDoc
109+
* @throws Exception
129110
*/
130111
public function checkCredentials($login, $password, array $optionalData = null): bool
131112
{
132113
if ('' === trim($password)) {
133114
$this->errors[] = User::ERROR_USER_INCORRECT_PASSWORD;
134-
135115
return false;
136116
}
137117

138118
// Get active LDAP server for current user
139119
if ($this->multipleServers) {
140120
// Try all LDAP servers
141121
foreach ($this->ldapServer as $key => $value) {
142-
$this->ldap->connect(
143-
$this->ldapServer[$key]['ldap_server'],
144-
$this->ldapServer[$key]['ldap_port'],
145-
$this->ldapServer[$key]['ldap_base'],
146-
$this->ldapServer[$key]['ldap_user'],
147-
$this->ldapServer[$key]['ldap_password']
148-
);
149-
if ($this->ldap->error) {
150-
$this->errors[] = $this->ldap->error;
151-
}
122+
$this->connect($key);
152123

153124
if (false !== $this->ldap->getDn($login)) {
154125
$this->activeServer = (int)$key;
@@ -163,16 +134,7 @@ public function checkCredentials($login, $password, array $optionalData = null):
163134
$bindLogin = $optionalData['domain'] . '\\' . $login;
164135
}
165136
} else {
166-
$this->ldap->connect(
167-
$this->ldapServer[$this->activeServer]['ldap_server'],
168-
$this->ldapServer[$this->activeServer]['ldap_port'],
169-
$this->ldapServer[$this->activeServer]['ldap_base'],
170-
$this->ldapServer[$this->activeServer]['ldap_user'],
171-
$this->ldapServer[$this->activeServer]['ldap_password']
172-
);
173-
if ($this->ldap->error) {
174-
$this->errors[] = $this->ldap->error;
175-
}
137+
$this->connect($this->activeServer);
176138

177139
$bindLogin = $this->ldap->getDn($login);
178140
}
@@ -205,16 +167,7 @@ public function isValidLogin($login, array $optionalData = null): int
205167
if ($this->multipleServers) {
206168
// Try all LDAP servers
207169
foreach ($this->ldapServer as $key => $value) {
208-
$this->ldap->connect(
209-
$this->ldapServer[$key]['ldap_server'],
210-
$this->ldapServer[$key]['ldap_port'],
211-
$this->ldapServer[$key]['ldap_base'],
212-
$this->ldapServer[$key]['ldap_user'],
213-
$this->ldapServer[$key]['ldap_password']
214-
);
215-
if ($this->ldap->error) {
216-
$this->errors[] = $this->ldap->error;
217-
}
170+
$this->connect($key);
218171

219172
if (false !== $this->ldap->getDn($login)) {
220173
$this->activeServer = (int)$key;
@@ -223,14 +176,26 @@ public function isValidLogin($login, array $optionalData = null): int
223176
}
224177
}
225178

179+
$this->connect($this->activeServer);
180+
181+
return strlen($this->ldap->getCompleteName($login));
182+
}
183+
184+
/**
185+
* @param int $activeServer
186+
*/
187+
private function connect(int $activeServer = 0): void
188+
{
226189
$this->ldap->connect(
227-
$this->ldapServer[$this->activeServer]['ldap_server'],
228-
$this->ldapServer[$this->activeServer]['ldap_port'],
229-
$this->ldapServer[$this->activeServer]['ldap_base'],
230-
$this->ldapServer[$this->activeServer]['ldap_user'],
231-
$this->ldapServer[$this->activeServer]['ldap_password']
190+
$this->ldapServer[$activeServer]['ldap_server'],
191+
$this->ldapServer[$activeServer]['ldap_port'],
192+
$this->ldapServer[$activeServer]['ldap_base'],
193+
$this->ldapServer[$activeServer]['ldap_user'],
194+
$this->ldapServer[$activeServer]['ldap_password']
232195
);
233196

234-
return strlen($this->ldap->getCompleteName($login));
197+
if ($this->ldap->error) {
198+
$this->errors[] = $this->ldap->error;
199+
}
235200
}
236201
}

0 commit comments

Comments
 (0)