summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorColin Williams <github@crwilliams.co.uk>2015-01-31 15:05:30 +0000
committerColin Williams <github@crwilliams.co.uk>2015-02-07 18:15:31 +0000
commitdb338a33cf28cfcbc906b0a88b878446b5b73c48 (patch)
tree3b621ef7623b1251954a876a831c8dbbd88705ea
parentc2ff3b2d696224800cb70513edb3ed0c84cc87a5 (diff)
Reduce complexity and duplication in LDAP::lookup()
-rw-r--r--app/Auth/Ldap.php57
1 files changed, 42 insertions, 15 deletions
diff --git a/app/Auth/Ldap.php b/app/Auth/Ldap.php
index dbf12387..e9bb5815 100644
--- a/app/Auth/Ldap.php
+++ b/app/Auth/Ldap.php
@@ -199,8 +199,8 @@ class Ldap extends Base
return array(
'username' => $username,
- 'name' => isset($info[0][LDAP_ACCOUNT_FULLNAME][0]) ? $info[0][LDAP_ACCOUNT_FULLNAME][0] : '',
- 'email' => isset($info[0][LDAP_ACCOUNT_EMAIL][0]) ? $info[0][LDAP_ACCOUNT_EMAIL][0] : '',
+ 'name' => $this->getFromInfo($info, LDAP_ACCOUNT_FULLNAME),
+ 'email' => $this->getFromInfo($info, LDAP_ACCOUNT_EMAIL),
);
}
@@ -215,16 +215,8 @@ class Ldap extends Base
*/
public function lookup($username = null, $email = null)
{
- if ($username && $email) {
- $query = '(&('.sprintf(LDAP_USER_PATTERN, $username).')('.sprintf(LDAP_ACCOUNT_EMAIL, $email).')';
- }
- else if ($username) {
- $query = sprintf(LDAP_USER_PATTERN, $username);
- }
- else if ($email) {
- $query = '('.LDAP_ACCOUNT_EMAIL.'='.$email.')';
- }
- else {
+ $query = $this->getQuery($username, $email);
+ if ($query === false) {
return false;
}
@@ -253,9 +245,44 @@ class Ldap extends Base
}
return array(
- 'username' => isset($info[0][LDAP_ACCOUNT_ID][0]) ? $info[0][LDAP_ACCOUNT_ID][0] : $username,
- 'name' => isset($info[0][LDAP_ACCOUNT_FULLNAME][0]) ? $info[0][LDAP_ACCOUNT_FULLNAME][0] : '',
- 'email' => isset($info[0][LDAP_ACCOUNT_EMAIL][0]) ? $info[0][LDAP_ACCOUNT_EMAIL][0] : $email,
+ 'username' => $this->getFromInfo($info, LDAP_ACCOUNT_ID, $username),
+ 'name' => $this->getFromInfo($info, LDAP_ACCOUNT_FULLNAME),
+ 'email' => $this->getFromInfo($info, LDAP_ACCOUNT_EMAIL, $email),
);
}
+
+ /**
+ * Get the LDAP query to find a user
+ *
+ * @param string $username Username
+ * @param string $email Email address
+ */
+ private function getQuery($username, $email)
+ {
+ if ($username && $email) {
+ return '(&('.sprintf(LDAP_USER_PATTERN, $username).')('.sprintf(LDAP_ACCOUNT_EMAIL, $email).')';
+ }
+ else if ($username) {
+ return sprintf(LDAP_USER_PATTERN, $username);
+ }
+ else if ($email) {
+ return '('.LDAP_ACCOUNT_EMAIL.'='.$email.')';
+ }
+ else {
+ return false;
+ }
+ }
+
+ /**
+ * Return a value from the LDAP info
+ *
+ * @param array $info LDAP info
+ * @param string $key Key
+ * @param string $default Default value if key not set in entry
+ * @return string
+ */
+ private function getFromInfo($info, $key, $default = '')
+ {
+ return isset($info[0][$key][0]) ? $info[0][$key][0] : $default;
+ }
}