Added support for image and date fields; added methods getUserByDn() and getAllUsers...
[TYPO3CMS/Extensions/ap_ldap_auth.git] / Classes / Domain / Repository / LDAPFeUserRepository.php
1 <?php
2 namespace AP\ApLdapAuth\Domain\Repository;
3
4 use AP\ApLdap\Exception\LDAPException,
5 TYPO3\CMS\Core\Utility\GeneralUtility,
6 AP\ApLdapAuth\Utility\LDAPConfigUtility;
7
8 /**
9 * Repository for LDAP frontend users
10 *
11 * @package TYPO3
12 * @subpackage tx_apldapauth
13 * @author Alexander Pankow <info@alexander-pankow.de>
14 */
15 class LDAPFEUserRepository extends \AP\ApLdapAuth\Persistence\LdapRepository {
16
17 /**
18 * @param int $configId
19 * @param string $filter
20 * @return array
21 */
22 public function getAllUsers($configId = 0, $filter = '') {
23 if ($configId > 0)
24 $ldapConnections = array($this->getLDAPConnection($configId));
25 else
26 $ldapConnections = $this->getLDAPConnections();
27
28 $users = array();
29 foreach ($ldapConnections as $ldapConnection) {
30 if (empty($filter))
31 $filter = str_replace('<username>', '*', $ldapConnection->getConfig()->getFeUsersFilter());
32 $baseDn = $ldapConnection->getConfig()->getFeUsersBaseDn();
33 $search = $ldapConnection->search($baseDn, $filter);
34 while ($entry = $search->getNextEntry()) {
35 $dn = $entry->getDN();
36 foreach ($entry->getAttributes() as $attribute) {
37 $users[$dn][$attribute] = $entry->getValues($attribute);
38 }
39 }
40 }
41
42 return $users;
43 }
44
45 /**
46 * @param string $dn
47 * @param int $configId
48 * @return array|boolean
49 */
50 public function getUserByDn($dn, $configId = 0) {
51 if ($configId > 0)
52 $ldapConnections = array($this->getLDAPConnection($configId));
53 else
54 $ldapConnections = $this->getLDAPConnections();
55
56 $user = false;
57 foreach ($ldapConnections as $ldapConnection) {
58 try {
59 $entry = $ldapConnection->search($dn, '(objectClass=cosdayUser)')->getFirstEntry();
60 } catch (LDAPException $e) {
61 continue;
62 }
63
64 foreach ($entry->getAttributes() as $attribute)
65 $user[$attribute] = $entry->getValues($attribute);
66 }
67
68 return $user;
69 }
70
71 /**
72 * Check if user exists
73 *
74 * @param $username
75 * @param $password
76 * @return array|bool
77 */
78 public function checkUser($username, $password) {
79 $result = false;
80 foreach ($this->getLDAPConnections() as $ldapConnection) {
81 $filter = str_replace('<username>', $username, $ldapConnection->getConfig()->getFeUsersFilter());
82 $baseDn = $ldapConnection->getConfig()->getFeUsersBaseDn();
83 $search = $ldapConnection->search($baseDn, $filter)->getFirstEntry();
84
85 // try to bind as found user
86 if ($search->countEntries() > 0) {
87 $entry = $search->getLastEntry();
88 $ldapUser = array();
89 foreach ($search->getAttributes() as $attribute) {
90 $attribute = strtolower($attribute);
91 $imageField = LDAPConfigUtility::getImageAttribute($ldapConnection->getConfig()->getFeUsersMapping());
92
93 if (empty($imageField) || $attribute != $imageField)
94 $ldapUser[$attribute] = $search->getValues($attribute);
95 else if (!isset($ldapUser[$attribute]))
96 $ldapUser[$attribute] = $search->getBinaryValues($attribute);
97 }
98 $ldapUser['dn'] = $username = $search->getDN($entry);
99 try {
100 if ($ldapConnection->bind($username, $password)) {
101 $result = array(
102 'ldapUser' => $ldapUser,
103 'config' => $ldapConnection->getConfig()
104 );
105 }
106 } catch (LDAPException $e) {
107 GeneralUtility::sysLog($e->getMessage(), 'ap_ldap_auth', GeneralUtility::SYSLOG_SEVERITY_ERROR);
108 }
109 }
110 }
111
112 return $result;
113 }
114 }