/** * Get the data for TYPO3 database * * @param array $ldapUser * @return array */ public function getTypo3UserData(array $ldapUser) { $mappings = $this->currentConfig->getFeUsersMapping(); // generate random password $charSet = 'abdeghjmnpqrstuvxyzABDEGHJLMNPQRSTVWXYZ23456789@#$%'; $password = ''; for ($i = 0; $i < 16; $i++) { $password .= $charSet[rand() % strlen($charSet)]; } $usernameAttribute = LDAPConfigUtility::getUsernameAttribute($this->currentConfig->getFeUsersFilter()); $typo3UserData = array('username' => $ldapUser[$usernameAttribute][0], 'tx_apldapauth_dn' => $ldapUser['dn'], 'pid' => 0, 'password' => $password); unset($mappings['pid']); /** @var $mapping FeUsers */ foreach ($mappings as $mapping) { $typo3FieldName = $mapping->getField(); $ldapAttribute = strtolower($mapping->getAttribute()); $value = $mapping->getIsAttribute() ? $ldapUser[$ldapAttribute][0] : $mapping->getValue(); if ($mapping->getIsImage()) { // image $fileName = 'tx_apldapauth_' . md5($typo3UserData['tx_apldapauth_dn']) . '.jpg'; $imageFilePath = PATH_site . 'uploads/pics/' . $fileName; $image = imagecreatefromstring($value); $imageSaveSuccess = imagejpeg($image, $imageFilePath); if ($imageSaveSuccess) { $typo3UserData[$typo3FieldName] = $fileName; } } else { if ($mapping->getIsDatetime()) { // date fields $typo3UserData[$typo3FieldName] = strtotime($value); } else { // text fields $typo3UserData[$typo3FieldName] = $value; // use value of $ldapFieldName if field in $ldapUser doesn't exist } } } return $typo3UserData; }
/** * Check if user exists and we can bind to the user * * @param $username * @param $password * @return array|bool */ public function checkUser($username, $password) { $result = false; foreach ($this->getLDAPConnections() as $ldapConnection) { $filter = $this->getFeUsersFilter($ldapConnection, $username); $baseDn = $ldapConnection->getConfig()->getFeUsersBaseDn(); $search = $ldapConnection->search($baseDn, $filter)->getFirstEntry(); // try to bind as found user if ($search->countEntries() > 0) { $entry = $search->getLastEntry(); $ldapUser = array(); foreach ($search->getAttributes() as $attribute) { $attribute = strtolower($attribute); $imageField = LDAPConfigUtility::getImageAttribute($ldapConnection->getConfig()->getFeUsersMapping()); if (empty($imageField) || $attribute != $imageField) { $ldapUser[$attribute] = $search->getValues($attribute); } else { if (!isset($ldapUser[$attribute])) { $ldapUser[$attribute] = $search->getBinaryValues($attribute); } } } $ldapUser['dn'] = $username = $search->getDN($entry); try { if ($ldapConnection->bind($username, $password)) { $result = array('ldapUser' => $ldapUser, 'config' => $ldapConnection->getConfig()); } } catch (LDAPException $e) { GeneralUtility::sysLog($e->getMessage(), 'ap_ldap_auth', GeneralUtility::SYSLOG_SEVERITY_ERROR); } } } return $result; }