/tine20/Tinebase/EmailUser/Ldap.php

https://github.com/testruby/Tine-2.0-Open-Source-Groupware-and-CRM · PHP · 204 lines · 114 code · 28 blank · 62 comment · 22 complexity · 17489d49bcd71269191971b9dc3b3650 MD5 · raw file

  1. <?php
  2. /**
  3. * Tine 2.0
  4. *
  5. * @package Tinebase
  6. * @subpackage EmailUser
  7. * @license http://www.gnu.org/licenses/agpl.html AGPL Version 3
  8. * @copyright Copyright (c) 2009-2010 Metaways Infosystems GmbH (http://www.metaways.de)
  9. * @author Philipp Schuele <p.schuele@metaways.de>
  10. *
  11. */
  12. /**
  13. * class Tinebase_EmailUser
  14. *
  15. * Email User Settings Managing for dbmail (+ ...) attributes in ldap backend
  16. *
  17. * @package Tinebase
  18. * @subpackage EmailLdap
  19. */
  20. class Tinebase_EmailUser_Ldap extends Tinebase_User_Plugin_LdapAbstract
  21. {
  22. /**
  23. * user properties mapping
  24. * -> we need to use lowercase for ldap fields because ldap_fetch returns lowercase keys
  25. *
  26. * @var array
  27. */
  28. protected $_propertyMapping = array(
  29. 'emailAddress' => 'mail',
  30. );
  31. /**
  32. * objectclasses required for users
  33. *
  34. * @var array
  35. */
  36. protected $_requiredObjectClass = array(
  37. 'inetOrgPerson'
  38. );
  39. /**
  40. * the constructor
  41. *
  42. */
  43. public function __construct(array $_options = array())
  44. {
  45. parent::__construct($_options);
  46. $ldapOptions = Tinebase_User::getBackendConfiguration();
  47. $config = Tinebase_EmailUser::getConfig($this->_backendType);
  48. $this->_options = array_merge($this->_options, $config);
  49. if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . print_r($this->_options, true));
  50. }
  51. /******************* protected functions *********************/
  52. /**
  53. * Check if we should append domain name or not
  54. *
  55. * @param string $_userName
  56. * @return string
  57. */
  58. protected function _appendDomain($_userName)
  59. {
  60. if (array_key_exists('domain', $this->_config) && ! empty($this->_config['domain'])) {
  61. $_userName .= '@' . $this->_config['domain'];
  62. }
  63. return $_userName;
  64. }
  65. /**
  66. * Returns a user object with raw data from ldap
  67. *
  68. * @param array $_userData
  69. * @param string $_accountClass
  70. * @return Tinebase_Record_Abstract
  71. *
  72. * @todo add generic function for this in Tinebase_User_Ldap or Tinebase_Ldap?
  73. */
  74. protected function _ldap2User(Tinebase_Model_User $_user, array &$_ldapEntry)
  75. {
  76. #if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . print_r($_ldapEntry, true));
  77. if ($this->_backendType == Tinebase_Config::SMTP) {
  78. $accountArray = array(
  79. 'emailForwardOnly' => false,
  80. 'emailAliases' => array(),
  81. 'emailForwards' => array()
  82. );
  83. } else {
  84. $accountArray = array();
  85. }
  86. foreach ($_ldapEntry as $key => $value) {
  87. if (is_int($key)) {
  88. continue;
  89. }
  90. $keyMapping = array_search($key, $this->_propertyMapping);
  91. if ($keyMapping !== FALSE) {
  92. switch($keyMapping) {
  93. case 'emailMailQuota':
  94. // convert to megabytes
  95. $accountArray[$keyMapping] = convertToMegabytes($value[0]);
  96. break;
  97. case 'emailAliases':
  98. case 'emailForwards':
  99. $accountArray[$keyMapping] = $value;
  100. break;
  101. case 'emailForwardOnly':
  102. $accountArray[$keyMapping] = ($value[0] == 'forwardonly') ? true : false;
  103. break;
  104. default:
  105. $accountArray[$keyMapping] = $value[0];
  106. break;
  107. }
  108. }
  109. }
  110. if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . print_r($accountArray, true));
  111. if ($this->_backendType == Tinebase_Config::SMTP) {
  112. $_user->smtpUser = new Tinebase_Model_EmailUser($accountArray);
  113. $_user->emailUser = Tinebase_EmailUser::merge(isset($_user->emailUser) ? $_user->emailUser : null, $_user->smtpUser);
  114. } else {
  115. $_user->imapUser = new Tinebase_Model_EmailUser($accountArray);
  116. $_user->emailUser = Tinebase_EmailUser::merge(clone $_user->imapUser, isset($_user->emailUser) ? $_user->emailUser : null);
  117. }
  118. }
  119. /**
  120. * convert object with user data to ldap data array
  121. *
  122. * @param Tinebase_Model_FullUser $_user
  123. * @param array $_ldapData the data to be written to ldap
  124. * @param array $_ldapEntry the data currently stored in ldap
  125. */
  126. protected function _user2Ldap(Tinebase_Model_FullUser $_user, array &$_ldapData, array &$_ldapEntry = array())
  127. {
  128. if ($this->_backendType == Tinebase_Config::SMTP) {
  129. if (empty($_user->smtpUser)) {
  130. return;
  131. }
  132. $mailSettings = $_user->smtpUser;
  133. } else {
  134. if (empty($_user->imapUser)) {
  135. return;
  136. }
  137. $mailSettings = $_user->imapUser;
  138. }
  139. foreach ($this->_propertyMapping as $objectProperty => $ldapAttribute) {
  140. $value = empty($mailSettings->{$objectProperty}) ? array() : $mailSettings->{$objectProperty};
  141. switch($objectProperty) {
  142. case 'emailMailQuota':
  143. // convert to bytes
  144. $_ldapData[$ldapAttribute] = !empty($mailSettings->{$objectProperty}) ? convertToBytes($mailSettings->{$objectProperty} . 'M') : array();
  145. break;
  146. case 'emailUID':
  147. $_ldapData[$ldapAttribute] = $this->_appendDomain($_user->accountLoginName);
  148. break;
  149. case 'emailGID':
  150. $_ldapData[$ldapAttribute] = $this->_config['emailGID'];
  151. break;
  152. case 'emailForwardOnly':
  153. $_ldapData[$ldapAttribute] = ($mailSettings->{$objectProperty} == true) ? 'forwardonly' : array();
  154. break;
  155. case 'emailAddress';
  156. $_ldapData[$ldapAttribute] = $_user->accountEmailAddress;
  157. break;
  158. default:
  159. $_ldapData[$ldapAttribute] = $mailSettings->{$objectProperty};
  160. break;
  161. }
  162. }
  163. if (array_key_exists('emailForwards', $this->_propertyMapping) && empty($_ldapData[$this->_propertyMapping['emailForwards']])) {
  164. $_ldapData[$this->_propertyMapping['emailForwardOnly']] = array();
  165. }
  166. // check if user has all required object classes. This is needed
  167. // when updating users which where created using different requirements
  168. foreach ($this->_requiredObjectClass as $className) {
  169. if (! in_array($className, $_ldapData['objectclass'])) {
  170. // merge all required classes at once
  171. $_ldapData['objectclass'] = array_unique(array_merge($_ldapData['objectclass'], $this->_requiredObjectClass));
  172. break;
  173. }
  174. }
  175. }
  176. }