Added support for image and date fields; added methods getUserByDn() and getAllUsers... 86/35986/2 master
authorAlexander Pankow <alexander.pankow@bpanet.de>
Mon, 12 Jan 2015 11:27:07 +0000 (12:27 +0100)
committerAlexander Pankow <info@alexander-pankow.de>
Tue, 13 Jan 2015 08:58:59 +0000 (09:58 +0100)
Change-Id: I22c189fa483d2b3bd556915a1d811d91f746cfdf
Reviewed-on: http://review.typo3.org/35986
Reviewed-by: Alexander Pankow <info@alexander-pankow.de>
Tested-by: Alexander Pankow <info@alexander-pankow.de>
Classes/Domain/Repository/LDAPFeUserRepository.php
Classes/Utility/LDAPAuthUtility.php

index 1c40000..1f8d741 100644 (file)
@@ -2,8 +2,8 @@
 namespace AP\ApLdapAuth\Domain\Repository;
 
 use AP\ApLdap\Exception\LDAPException,
-       TYPO3\CMS\Core\Utility\GeneralUtility;
-use AP\ApLdapAuth\Utility\LDAPConfigUtility;
+       TYPO3\CMS\Core\Utility\GeneralUtility,
+       AP\ApLdapAuth\Utility\LDAPConfigUtility;
 
 /**
  * Repository for LDAP frontend users
@@ -15,6 +15,60 @@ use AP\ApLdapAuth\Utility\LDAPConfigUtility;
 class LDAPFEUserRepository extends \AP\ApLdapAuth\Persistence\LdapRepository {
 
        /**
+        * @param int $configId
+        * @param string $filter
+        * @return array
+        */
+       public function getAllUsers($configId = 0, $filter = '') {
+               if ($configId > 0)
+                       $ldapConnections =  array($this->getLDAPConnection($configId));
+               else
+                       $ldapConnections = $this->getLDAPConnections();
+
+               $users = array();
+               foreach ($ldapConnections as $ldapConnection) {
+                       if (empty($filter))
+                               $filter = str_replace('<username>', '*', $ldapConnection->getConfig()->getFeUsersFilter());
+                       $baseDn = $ldapConnection->getConfig()->getFeUsersBaseDn();
+                       $search = $ldapConnection->search($baseDn, $filter);
+                       while ($entry = $search->getNextEntry()) {
+                               $dn = $entry->getDN();
+                               foreach ($entry->getAttributes() as $attribute) {
+                                       $users[$dn][$attribute] = $entry->getValues($attribute);
+                               }
+                       }
+               }
+
+               return $users;
+       }
+
+       /**
+        * @param string $dn
+        * @param int $configId
+        * @return array|boolean
+        */
+       public function getUserByDn($dn, $configId = 0) {
+               if ($configId > 0)
+                       $ldapConnections =  array($this->getLDAPConnection($configId));
+               else
+                       $ldapConnections = $this->getLDAPConnections();
+
+               $user = false;
+               foreach ($ldapConnections as $ldapConnection) {
+                       try {
+                               $entry = $ldapConnection->search($dn, '(objectClass=cosdayUser)')->getFirstEntry();
+                       } catch (LDAPException $e) {
+                               continue;
+                       }
+
+                       foreach ($entry->getAttributes() as $attribute)
+                               $user[$attribute] = $entry->getValues($attribute);
+               }
+
+               return $user;
+       }
+
+       /**
         * Check if user exists
         *
         * @param $username
@@ -32,17 +86,14 @@ class LDAPFEUserRepository extends \AP\ApLdapAuth\Persistence\LdapRepository {
                        if ($search->countEntries() > 0) {
                                $entry = $search->getLastEntry();
                                $ldapUser = array();
-                               foreach ($search->getAttributes() as $attribute => $values) {
-                                       if ($values['count'] <= 0)
-                                               continue;
-
+                               foreach ($search->getAttributes() as $attribute) {
+                                       $attribute = strtolower($attribute);
                                        $imageField = LDAPConfigUtility::getImageAttribute($ldapConnection->getConfig()->getFeUsersMapping());
-                                       foreach ($values as $key => $value) {
-                                               if (empty($imageField) || $attribute != $imageField)
-                                                       $ldapUser[$attribute][$key] = $value;
-                                               else if (!isset($ldapUser[$attribute]))
-                                                       $ldapUser[$attribute] = $search->getBinaryValues($attribute);
-                                       }
+
+                                       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 {
index 31f64ed..db099a3 100644 (file)
@@ -3,6 +3,7 @@ namespace AP\ApLdapAuth\Utility;
 
 use AP\ApLdapAuth\Domain\Model\Mapping\FeUsers,
        AP\ApLdapAuth\Domain\Model\Config;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
 
 /**
  * LDAP auth utility to fetch and create users and groups from LDAP
@@ -148,7 +149,7 @@ class LDAPAuthUtility implements \TYPO3\CMS\Core\SingletonInterface {
                $typo3UserData = array(
                        'username' => $ldapUser[$usernameAttribute][0],
                        'tx_apldapauth_dn' => $ldapUser['dn'],
-                       'pid' => 0, // can be overwritten
+                       'pid' => 0,
                        'password' => $password
                );
                unset($mappings['pid']);
@@ -156,17 +157,21 @@ class LDAPAuthUtility implements \TYPO3\CMS\Core\SingletonInterface {
                /** @var $mapping FeUsers */
                foreach ($mappings as $mapping) {
                        $typo3FieldName = $mapping->getField();
-                       $ldapAttribute = $mapping->getAttribute();
-                       if (!$mapping->getIsImage()) {
-                               // text fields
-                               $typo3UserData[$typo3FieldName] = $mapping->getIsAttribute() ? $ldapUser[$ldapAttribute][0] : $mapping->getValue(); // use value of $ldapFieldName if field in $ldapUser doesn't exist
-                       } else {
+                       $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($ldapUser[$ldapAttribute][0]);
+                               $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
                        }
                }