Added support for image and date fields; added methods getUserByDn() and getAllUsers...
[TYPO3CMS/Extensions/ap_ldap_auth.git] / Classes / Utility / LDAPAuthUtility.php
1 <?php
2 namespace AP\ApLdapAuth\Utility;
3
4 use AP\ApLdapAuth\Domain\Model\Mapping\FeUsers,
5 AP\ApLdapAuth\Domain\Model\Config;
6 use TYPO3\CMS\Core\Utility\GeneralUtility;
7
8 /**
9 * LDAP auth utility to fetch and create users and groups from LDAP
10 *
11 * @package TYPO3
12 * @subpackage tx_apldapauth
13 * @author Alexander Pankow <info@alexander-pankow.de>
14 */
15 class LDAPAuthUtility implements \TYPO3\CMS\Core\SingletonInterface {
16
17 /**
18 * @var \AP\ApLdapAuth\Domain\Repository\LDAPFeUserRepository|null
19 */
20 protected $ldapFeUserRepository = null;
21
22 /**
23 * LDAP configuration of found user
24 *
25 * @var Config|null
26 */
27 protected $currentConfig = null;
28
29 /**
30 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
31 */
32 protected $objectManager = null;
33
34 /**
35 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
36 */
37 public function __construct(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager = null) {
38 if ($objectManager === null)
39 $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
40 else
41 $this->objectManager = $objectManager;
42 }
43
44 /**
45 * @see \AP\ApLdapAuth\Domain\Repository\LDAPFeUserRepository::checkUser()
46 * @param $username
47 * @param $password
48 * @return array|bool
49 */
50 public function checkUser($username, $password) {
51 switch (TYPO3_MODE) {
52 case 'FE':
53 $result = $this->getLdapFeUserRepository()->checkUser($username, $password);
54 if (!empty($result['config']))
55 $this->currentConfig = $result['config'];
56 return $result['ldapUser'];
57 case 'BE':
58 return false;
59 }
60
61 return false;
62 }
63
64 /**
65 * Synchronize user from LDAP directory with TYPO3 database
66 *
67 * @param array $ldapUser
68 * @return array|null
69 */
70 public function synchronizeUser(array $ldapUser) {
71 $userData = $this->getTypo3UserData($ldapUser);
72
73 // get user from database
74 $user = $this->selectUser($userData);
75
76 if (!$user) {
77 // create user
78 $user = $this->importUser($userData);
79 } else {
80 // update user
81 $user = $this->updateUser($userData);
82 }
83
84 return $user;
85 }
86
87 /**
88 * Gets user from TYPO3 database
89 *
90 * @param array $userData
91 * @return array|null
92 */
93 public function selectUser(array $userData) {
94 $user = $this->getTypo3DbConnection()->exec_SELECTgetRows(
95 '*',
96 TYPO3_MODE === 'BE' ? 'be_users' : 'fe_users',
97 'tx_apldapauth_dn = ' . $this->getTypo3DbConnection()->fullQuoteStr($userData['tx_apldapauth_dn'], TYPO3_MODE === 'BE' ? 'be_users' : 'fe_users')
98 );
99 return $user[0];
100 }
101
102 /**
103 * Import user from LDAP directory into TYPO3 database
104 *
105 * @param array $userData
106 * @return array|null
107 */
108 public function importUser(array $userData) {
109 $this->getTypo3DbConnection()->exec_INSERTquery(
110 TYPO3_MODE === 'BE' ? 'be_users' : 'fe_users',
111 $userData
112 );
113
114 return $this->selectUser($userData);
115 }
116
117 /**
118 * Updates user in TYPO3 database by data from LDAP directory
119 *
120 * @param array $userData
121 * @return array|null
122 */
123 public function updateUser(array $userData) {
124 $this->getTypo3DbConnection()->exec_UPDATEquery(
125 TYPO3_MODE === 'BE' ? 'be_users' : 'fe_users',
126 'tx_apldapauth_dn = ' . $this->getTypo3DbConnection()->fullQuoteStr($userData['tx_apldapauth_dn'], TYPO3_MODE === 'BE' ? 'be_users' : 'fe_users'),
127 $userData
128 );
129
130 return $this->selectUser($userData);
131 }
132
133 /**
134 * Get the data for TYPO3 database
135 *
136 * @param array $ldapUser
137 * @return array
138 */
139 public function getTypo3UserData(array $ldapUser) {
140 $mappings = $this->currentConfig->getFeUsersMapping();
141
142 // generate random password
143 $charSet = 'abdeghjmnpqrstuvxyzABDEGHJLMNPQRSTVWXYZ23456789@#$%';
144 $password = '';
145 for ($i = 0; $i < 16; $i++)
146 $password .= $charSet[(rand() % strlen($charSet))];
147
148 $usernameAttribute = LDAPConfigUtility::getUsernameAttribute($this->currentConfig->getFeUsersFilter());
149 $typo3UserData = array(
150 'username' => $ldapUser[$usernameAttribute][0],
151 'tx_apldapauth_dn' => $ldapUser['dn'],
152 'pid' => 0,
153 'password' => $password
154 );
155 unset($mappings['pid']);
156
157 /** @var $mapping FeUsers */
158 foreach ($mappings as $mapping) {
159 $typo3FieldName = $mapping->getField();
160 $ldapAttribute = strtolower($mapping->getAttribute());
161 $value = $mapping->getIsAttribute() ? $ldapUser[$ldapAttribute][0] : $mapping->getValue();
162 if ($mapping->getIsImage()) {
163 // image
164 $fileName = 'tx_apldapauth_' . md5($typo3UserData['tx_apldapauth_dn']) . '.jpg';
165 $imageFilePath = PATH_site . 'uploads/pics/' . $fileName;
166 $image = imagecreatefromstring($value);
167 $imageSaveSuccess = imagejpeg($image, $imageFilePath);
168 if ($imageSaveSuccess) $typo3UserData[$typo3FieldName] = $fileName;
169 } else if ($mapping->getIsDatetime()) {
170 // date fields
171 $typo3UserData[$typo3FieldName] = strtotime($value);
172 } else {
173 // text fields
174 $typo3UserData[$typo3FieldName] = $value; // use value of $ldapFieldName if field in $ldapUser doesn't exist
175 }
176 }
177
178 return $typo3UserData;
179 }
180
181 /**
182 * @return \AP\ApLdapAuth\Domain\Repository\LDAPFeUserRepository|null
183 */
184 protected function getLdapFeUserRepository() {
185 if ($this->ldapFeUserRepository === null)
186 $this->ldapFeUserRepository = $this->objectManager->get('AP\\ApLdapAuth\\Domain\\Repository\\LDAPFeUserRepository');
187 return $this->ldapFeUserRepository;
188 }
189
190 /**
191 * @return \TYPO3\CMS\Dbal\Database\DatabaseConnection
192 */
193 protected function getTypo3DbConnection() {
194 return $GLOBALS['TYPO3_DB'];
195 }
196 }