]> source.dussan.org Git - nextcloud-server.git/commitdiff
Move Configuration to PSR-4
authorJoas Schilling <nickvergessen@owncloud.com>
Thu, 12 May 2016 14:37:49 +0000 (16:37 +0200)
committerJoas Schilling <nickvergessen@owncloud.com>
Wed, 25 May 2016 14:04:59 +0000 (16:04 +0200)
apps/user_ldap/ajax/getNewServerConfigPrefix.php
apps/user_ldap/ajax/wizard.php
apps/user_ldap/lib/Command/CreateEmptyConfig.php
apps/user_ldap/lib/Command/SetConfig.php
apps/user_ldap/lib/Command/ShowConfig.php
apps/user_ldap/lib/Configuration.php [new file with mode: 0644]
apps/user_ldap/lib/Wizard.php
apps/user_ldap/lib/configuration.php [deleted file]
apps/user_ldap/settings.php
apps/user_ldap/tests/WizardTest.php
apps/user_ldap/tests/configuration.php

index 3820c05d9d6c8ec9da2e70afde735f8db2e335bb..18407126fbf0038cb2b60a3ab71afaac18502f55 100644 (file)
@@ -34,12 +34,12 @@ $nk = 's'.str_pad($ln+1, 2, '0', STR_PAD_LEFT);
 
 $resultData = array('configPrefix' => $nk);
 
-$newConfig = new \OCA\user_ldap\lib\Configuration($nk, false);
+$newConfig = new \OCA\User_LDAP\Configuration($nk, false);
 if(isset($_POST['copyConfig'])) {
-       $originalConfig = new \OCA\user_ldap\lib\Configuration($_POST['copyConfig']);
+       $originalConfig = new \OCA\User_LDAP\Configuration($_POST['copyConfig']);
        $newConfig->setConfiguration($originalConfig->getConfiguration());
 } else {
-       $configuration = new \OCA\user_ldap\lib\Configuration($nk, false);
+       $configuration = new \OCA\User_LDAP\Configuration($nk, false);
        $newConfig->setConfiguration($configuration->getDefaults());
        $resultData['defaults'] = $configuration->getDefaults();
 }
index 1d0f6000666ed17174d96e2c79111adc70480039..6a67e2a1fd0a550550e6781e3d16e2ad5b41301d 100644 (file)
@@ -42,7 +42,7 @@ if(!isset($_POST['ldap_serverconfig_chooser'])) {
 $prefix = (string)$_POST['ldap_serverconfig_chooser'];
 
 $ldapWrapper = new \OCA\User_LDAP\LDAP();
-$configuration = new \OCA\user_ldap\lib\Configuration($prefix);
+$configuration = new \OCA\User_LDAP\Configuration($prefix);
 
 $con = new \OCA\User_LDAP\Connection($ldapWrapper, '', null);
 $con->setConfiguration($configuration->getConfiguration());
index 779dbbc6fd5e2ebe635a8bfe481658b3af28d4bd..c735e51f49118fb2cb6d2c3469f15cbf65c4f525 100644 (file)
@@ -27,7 +27,7 @@ use Symfony\Component\Console\Command\Command;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Output\OutputInterface;
 use \OCA\User_LDAP\Helper;
-use \OCA\user_ldap\lib\Configuration;
+use \OCA\User_LDAP\Configuration;
 
 class CreateEmptyConfig extends Command {
        /** @var \OCA\User_LDAP\Helper */
index d9308c0afd4e39d0e6e7aa1527635d915ab0f22e..d466e1eb7699523a8a0f2d1b8d0132fdb7cbfe16 100644 (file)
@@ -28,7 +28,7 @@ use Symfony\Component\Console\Input\InputArgument;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Output\OutputInterface;
 use OCA\User_LDAP\Helper;
-use \OCA\user_ldap\lib\Configuration;
+use OCA\User_LDAP\Configuration;
 
 class SetConfig extends Command {
 
index 91904ca838033cbe22c7dfb1749d4d4f49485edd..dbd18216f81d4c411577a167bc591a2304a80172 100644 (file)
@@ -28,8 +28,8 @@ use Symfony\Component\Console\Input\InputArgument;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
-use \OCA\User_LDAP\Helper;
-use \OCA\user_ldap\lib\Configuration;
+use OCA\User_LDAP\Helper;
+use OCA\User_LDAP\Configuration;
 
 class ShowConfig extends Command {
        /** @var \OCA\User_LDAP\Helper */
diff --git a/apps/user_ldap/lib/Configuration.php b/apps/user_ldap/lib/Configuration.php
new file mode 100644 (file)
index 0000000..476483e
--- /dev/null
@@ -0,0 +1,511 @@
+<?php
+/**
+ * @author Alex Weirig <alex.weirig@technolink.lu>
+ * @author Alexander Bergolth <leo@strike.wu.ac.at>
+ * @author Arthur Schiwon <blizzz@owncloud.com>
+ * @author Jörn Friedrich Dreyer <jfd@butonic.de>
+ * @author Lennart Rosam <hello@takuto.de>
+ * @author Lukas Reschke <lukas@owncloud.com>
+ * @author Morris Jobke <hey@morrisjobke.de>
+ * @author Robin McCorkell <robin@mccorkell.me.uk>
+ *
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OCA\User_LDAP;
+
+/**
+ * @property int ldapPagingSize holds an integer
+ */
+class Configuration {
+
+       protected $configPrefix = null;
+       protected $configRead = false;
+
+       //settings
+       protected $config = array(
+               'ldapHost' => null,
+               'ldapPort' => null,
+               'ldapBackupHost' => null,
+               'ldapBackupPort' => null,
+               'ldapBase' => null,
+               'ldapBaseUsers' => null,
+               'ldapBaseGroups' => null,
+               'ldapAgentName' => null,
+               'ldapAgentPassword' => null,
+               'ldapTLS' => null,
+               'turnOffCertCheck' => null,
+               'ldapIgnoreNamingRules' => null,
+               'ldapUserDisplayName' => null,
+               'ldapUserDisplayName2' => null,
+               'ldapUserFilterObjectclass' => null,
+               'ldapUserFilterGroups' => null,
+               'ldapUserFilter' => null,
+               'ldapUserFilterMode' => null,
+               'ldapGroupFilter' => null,
+               'ldapGroupFilterMode' => null,
+               'ldapGroupFilterObjectclass' => null,
+               'ldapGroupFilterGroups' => null,
+               'ldapGroupDisplayName' => null,
+               'ldapGroupMemberAssocAttr' => null,
+               'ldapLoginFilter' => null,
+               'ldapLoginFilterMode' => null,
+               'ldapLoginFilterEmail' => null,
+               'ldapLoginFilterUsername' => null,
+               'ldapLoginFilterAttributes' => null,
+               'ldapQuotaAttribute' => null,
+               'ldapQuotaDefault' => null,
+               'ldapEmailAttribute' => null,
+               'ldapCacheTTL' => null,
+               'ldapUuidUserAttribute' => 'auto',
+               'ldapUuidGroupAttribute' => 'auto',
+               'ldapOverrideMainServer' => false,
+               'ldapConfigurationActive' => false,
+               'ldapAttributesForUserSearch' => null,
+               'ldapAttributesForGroupSearch' => null,
+               'ldapExperiencedAdmin' => false,
+               'homeFolderNamingRule' => null,
+               'hasPagedResultSupport' => false,
+               'hasMemberOfFilterSupport' => false,
+               'useMemberOfToDetectMembership' => true,
+               'ldapExpertUsernameAttr' => null,
+               'ldapExpertUUIDUserAttr' => null,
+               'ldapExpertUUIDGroupAttr' => null,
+               'lastJpegPhotoLookup' => null,
+               'ldapNestedGroups' => false,
+               'ldapPagingSize' => null,
+               'ldapDynamicGroupMemberURL' => null,
+       );
+
+       /**
+        * @param string $configPrefix
+        * @param bool $autoRead
+        */
+       public function __construct($configPrefix, $autoRead = true) {
+               $this->configPrefix = $configPrefix;
+               if($autoRead) {
+                       $this->readConfiguration();
+               }
+       }
+
+       /**
+        * @param string $name
+        * @return mixed|void
+        */
+       public function __get($name) {
+               if(isset($this->config[$name])) {
+                       return $this->config[$name];
+               }
+       }
+
+       /**
+        * @param string $name
+        * @param mixed $value
+        */
+       public function __set($name, $value) {
+               $this->setConfiguration(array($name => $value));
+       }
+
+       /**
+        * @return array
+        */
+       public function getConfiguration() {
+               return $this->config;
+       }
+
+       /**
+        * set LDAP configuration with values delivered by an array, not read
+        * from configuration. It does not save the configuration! To do so, you
+        * must call saveConfiguration afterwards.
+        * @param array $config array that holds the config parameters in an associated
+        * array
+        * @param array &$applied optional; array where the set fields will be given to
+        * @return false|null
+        */
+       public function setConfiguration($config, &$applied = null) {
+               if(!is_array($config)) {
+                       return false;
+               }
+
+               $cta = $this->getConfigTranslationArray();
+               foreach($config as $inputKey => $val) {
+                       if(strpos($inputKey, '_') !== false && array_key_exists($inputKey, $cta)) {
+                               $key = $cta[$inputKey];
+                       } elseif(array_key_exists($inputKey, $this->config)) {
+                               $key = $inputKey;
+                       } else {
+                               continue;
+                       }
+
+                       $setMethod = 'setValue';
+                       switch($key) {
+                               case 'ldapAgentPassword':
+                                       $setMethod = 'setRawValue';
+                                       break;
+                               case 'homeFolderNamingRule':
+                                       $trimmedVal = trim($val);
+                                       if(!empty($trimmedVal) && strpos($val, 'attr:') === false) {
+                                               $val = 'attr:'.$trimmedVal;
+                                       }
+                                       break;
+                               case 'ldapBase':
+                               case 'ldapBaseUsers':
+                               case 'ldapBaseGroups':
+                               case 'ldapAttributesForUserSearch':
+                               case 'ldapAttributesForGroupSearch':
+                               case 'ldapUserFilterObjectclass':
+                               case 'ldapUserFilterGroups':
+                               case 'ldapGroupFilterObjectclass':
+                               case 'ldapGroupFilterGroups':
+                               case 'ldapLoginFilterAttributes':
+                                       $setMethod = 'setMultiLine';
+                                       break;
+                       }
+                       $this->$setMethod($key, $val);
+                       if(is_array($applied)) {
+                               $applied[] = $inputKey;
+                       }
+               }
+
+       }
+
+       public function readConfiguration() {
+               if(!$this->configRead && !is_null($this->configPrefix)) {
+                       $cta = array_flip($this->getConfigTranslationArray());
+                       foreach($this->config as $key => $val) {
+                               if(!isset($cta[$key])) {
+                                       //some are determined
+                                       continue;
+                               }
+                               $dbKey = $cta[$key];
+                               switch($key) {
+                                       case 'ldapBase':
+                                       case 'ldapBaseUsers':
+                                       case 'ldapBaseGroups':
+                                       case 'ldapAttributesForUserSearch':
+                                       case 'ldapAttributesForGroupSearch':
+                                       case 'ldapUserFilterObjectclass':
+                                       case 'ldapUserFilterGroups':
+                                       case 'ldapGroupFilterObjectclass':
+                                       case 'ldapGroupFilterGroups':
+                                       case 'ldapLoginFilterAttributes':
+                                               $readMethod = 'getMultiLine';
+                                               break;
+                                       case 'ldapIgnoreNamingRules':
+                                               $readMethod = 'getSystemValue';
+                                               $dbKey = $key;
+                                               break;
+                                       case 'ldapAgentPassword':
+                                               $readMethod = 'getPwd';
+                                               break;
+                                       case 'ldapUserDisplayName2':
+                                       case 'ldapGroupDisplayName':
+                                               $readMethod = 'getLcValue';
+                                               break;
+                                       case 'ldapUserDisplayName':
+                                       default:
+                                               // user display name does not lower case because
+                                               // we rely on an upper case N as indicator whether to
+                                               // auto-detect it or not. FIXME
+                                               $readMethod = 'getValue';
+                                               break;
+                               }
+                               $this->config[$key] = $this->$readMethod($dbKey);
+                       }
+                       $this->configRead = true;
+               }
+       }
+
+       /**
+        * saves the current Configuration in the database
+        */
+       public function saveConfiguration() {
+               $cta = array_flip($this->getConfigTranslationArray());
+               foreach($this->config as $key => $value) {
+                       switch ($key) {
+                               case 'ldapAgentPassword':
+                                       $value = base64_encode($value);
+                                       break;
+                               case 'ldapBase':
+                               case 'ldapBaseUsers':
+                               case 'ldapBaseGroups':
+                               case 'ldapAttributesForUserSearch':
+                               case 'ldapAttributesForGroupSearch':
+                               case 'ldapUserFilterObjectclass':
+                               case 'ldapUserFilterGroups':
+                               case 'ldapGroupFilterObjectclass':
+                               case 'ldapGroupFilterGroups':
+                               case 'ldapLoginFilterAttributes':
+                                       if(is_array($value)) {
+                                               $value = implode("\n", $value);
+                                       }
+                                       break;
+                               //following options are not stored but detected, skip them
+                               case 'ldapIgnoreNamingRules':
+                               case 'hasPagedResultSupport':
+                               case 'ldapUuidUserAttribute':
+                               case 'ldapUuidGroupAttribute':
+                                       continue 2;
+                       }
+                       if(is_null($value)) {
+                               $value = '';
+                       }
+                       $this->saveValue($cta[$key], $value);
+               }
+       }
+
+       /**
+        * @param string $varName
+        * @return array|string
+        */
+       protected function getMultiLine($varName) {
+               $value = $this->getValue($varName);
+               if(empty($value)) {
+                       $value = '';
+               } else {
+                       $value = preg_split('/\r\n|\r|\n/', $value);
+               }
+
+               return $value;
+       }
+
+       /**
+        * Sets multi-line values as arrays
+        * 
+        * @param string $varName name of config-key
+        * @param array|string $value to set
+        */
+       protected function setMultiLine($varName, $value) {
+               if(empty($value)) {
+                       $value = '';
+               } else if (!is_array($value)) {
+                       $value = preg_split('/\r\n|\r|\n|;/', $value);
+                       if($value === false) {
+                               $value = '';
+                       }
+               }
+
+               if(!is_array($value)) {
+                       $finalValue = trim($value);
+               } else {
+                       $finalValue = [];
+                       foreach($value as $key => $val) {
+                               if(is_string($val)) {
+                                       $val = trim($val);
+                                       if(!empty($val)) {
+                                               //accidental line breaks are not wanted and can cause
+                                               // odd behaviour. Thus, away with them.
+                                               $finalValue[] = $val;
+                                       }
+                               } else {
+                                       $finalValue[] = $val;
+                               }
+                       }
+               }
+
+               $this->setRawValue($varName, $finalValue);
+       }
+
+       /**
+        * @param string $varName
+        * @return string
+        */
+       protected function getPwd($varName) {
+               return base64_decode($this->getValue($varName));
+       }
+
+       /**
+        * @param string $varName
+        * @return string
+        */
+       protected function getLcValue($varName) {
+               return mb_strtolower($this->getValue($varName), 'UTF-8');
+       }
+
+       /**
+        * @param string $varName
+        * @return string
+        */
+       protected function getSystemValue($varName) {
+               //FIXME: if another system value is added, softcode the default value
+               return \OCP\Config::getSystemValue($varName, false);
+       }
+
+       /**
+        * @param string $varName
+        * @return string
+        */
+       protected function getValue($varName) {
+               static $defaults;
+               if(is_null($defaults)) {
+                       $defaults = $this->getDefaults();
+               }
+               return \OCP\Config::getAppValue('user_ldap',
+                                                                               $this->configPrefix.$varName,
+                                                                               $defaults[$varName]);
+       }
+
+       /**
+        * Sets a scalar value.
+        * 
+        * @param string $varName name of config key
+        * @param mixed $value to set
+        */
+       protected function setValue($varName, $value) {
+               if(is_string($value)) {
+                       $value = trim($value);
+               }
+               $this->config[$varName] = $value;
+       }
+
+       /**
+        * Sets a scalar value without trimming.
+        *
+        * @param string $varName name of config key
+        * @param mixed $value to set
+        */
+       protected function setRawValue($varName, $value) {
+               $this->config[$varName] = $value;
+       }
+
+       /**
+        * @param string $varName
+        * @param string $value
+        * @return bool
+        */
+       protected function saveValue($varName, $value) {
+               return \OCP\Config::setAppValue('user_ldap',
+                                                                               $this->configPrefix.$varName,
+                                                                               $value);
+       }
+
+       /**
+        * @return array an associative array with the default values. Keys are correspond
+        * to config-value entries in the database table
+        */
+       public function getDefaults() {
+               return array(
+                       'ldap_host'                         => '',
+                       'ldap_port'                         => '',
+                       'ldap_backup_host'                  => '',
+                       'ldap_backup_port'                  => '',
+                       'ldap_override_main_server'         => '',
+                       'ldap_dn'                           => '',
+                       'ldap_agent_password'               => '',
+                       'ldap_base'                         => '',
+                       'ldap_base_users'                   => '',
+                       'ldap_base_groups'                  => '',
+                       'ldap_userlist_filter'              => '',
+                       'ldap_user_filter_mode'             => 0,
+                       'ldap_userfilter_objectclass'       => '',
+                       'ldap_userfilter_groups'            => '',
+                       'ldap_login_filter'                 => '',
+                       'ldap_login_filter_mode'            => 0,
+                       'ldap_loginfilter_email'            => 0,
+                       'ldap_loginfilter_username'         => 1,
+                       'ldap_loginfilter_attributes'       => '',
+                       'ldap_group_filter'                 => '',
+                       'ldap_group_filter_mode'            => 0,
+                       'ldap_groupfilter_objectclass'      => '',
+                       'ldap_groupfilter_groups'           => '',
+                       'ldap_display_name'                 => 'displayName',
+                       'ldap_user_display_name_2'                      => '',
+                       'ldap_group_display_name'           => 'cn',
+                       'ldap_tls'                          => 0,
+                       'ldap_quota_def'                    => '',
+                       'ldap_quota_attr'                   => '',
+                       'ldap_email_attr'                   => '',
+                       'ldap_group_member_assoc_attribute' => 'uniqueMember',
+                       'ldap_cache_ttl'                    => 600,
+                       'ldap_uuid_user_attribute'          => 'auto',
+                       'ldap_uuid_group_attribute'         => 'auto',
+                       'home_folder_naming_rule'           => '',
+                       'ldap_turn_off_cert_check'          => 0,
+                       'ldap_configuration_active'         => 0,
+                       'ldap_attributes_for_user_search'   => '',
+                       'ldap_attributes_for_group_search'  => '',
+                       'ldap_expert_username_attr'         => '',
+                       'ldap_expert_uuid_user_attr'        => '',
+                       'ldap_expert_uuid_group_attr'       => '',
+                       'has_memberof_filter_support'       => 0,
+                       'use_memberof_to_detect_membership' => 1,
+                       'last_jpegPhoto_lookup'             => 0,
+                       'ldap_nested_groups'                => 0,
+                       'ldap_paging_size'                  => 500,
+                       'ldap_experienced_admin'            => 0,
+                       'ldap_dynamic_group_member_url'     => '',
+               );
+       }
+
+       /**
+        * @return array that maps internal variable names to database fields
+        */
+       public function getConfigTranslationArray() {
+               //TODO: merge them into one representation
+               static $array = array(
+                       'ldap_host'                         => 'ldapHost',
+                       'ldap_port'                         => 'ldapPort',
+                       'ldap_backup_host'                  => 'ldapBackupHost',
+                       'ldap_backup_port'                  => 'ldapBackupPort',
+                       'ldap_override_main_server'         => 'ldapOverrideMainServer',
+                       'ldap_dn'                           => 'ldapAgentName',
+                       'ldap_agent_password'               => 'ldapAgentPassword',
+                       'ldap_base'                         => 'ldapBase',
+                       'ldap_base_users'                   => 'ldapBaseUsers',
+                       'ldap_base_groups'                  => 'ldapBaseGroups',
+                       'ldap_userfilter_objectclass'       => 'ldapUserFilterObjectclass',
+                       'ldap_userfilter_groups'            => 'ldapUserFilterGroups',
+                       'ldap_userlist_filter'              => 'ldapUserFilter',
+                       'ldap_user_filter_mode'             => 'ldapUserFilterMode',
+                       'ldap_login_filter'                 => 'ldapLoginFilter',
+                       'ldap_login_filter_mode'            => 'ldapLoginFilterMode',
+                       'ldap_loginfilter_email'            => 'ldapLoginFilterEmail',
+                       'ldap_loginfilter_username'         => 'ldapLoginFilterUsername',
+                       'ldap_loginfilter_attributes'       => 'ldapLoginFilterAttributes',
+                       'ldap_group_filter'                 => 'ldapGroupFilter',
+                       'ldap_group_filter_mode'            => 'ldapGroupFilterMode',
+                       'ldap_groupfilter_objectclass'      => 'ldapGroupFilterObjectclass',
+                       'ldap_groupfilter_groups'           => 'ldapGroupFilterGroups',
+                       'ldap_display_name'                 => 'ldapUserDisplayName',
+                       'ldap_user_display_name_2'                      => 'ldapUserDisplayName2',
+                       'ldap_group_display_name'           => 'ldapGroupDisplayName',
+                       'ldap_tls'                          => 'ldapTLS',
+                       'ldap_quota_def'                    => 'ldapQuotaDefault',
+                       'ldap_quota_attr'                   => 'ldapQuotaAttribute',
+                       'ldap_email_attr'                   => 'ldapEmailAttribute',
+                       'ldap_group_member_assoc_attribute' => 'ldapGroupMemberAssocAttr',
+                       'ldap_cache_ttl'                    => 'ldapCacheTTL',
+                       'home_folder_naming_rule'           => 'homeFolderNamingRule',
+                       'ldap_turn_off_cert_check'          => 'turnOffCertCheck',
+                       'ldap_configuration_active'         => 'ldapConfigurationActive',
+                       'ldap_attributes_for_user_search'   => 'ldapAttributesForUserSearch',
+                       'ldap_attributes_for_group_search'  => 'ldapAttributesForGroupSearch',
+                       'ldap_expert_username_attr'         => 'ldapExpertUsernameAttr',
+                       'ldap_expert_uuid_user_attr'        => 'ldapExpertUUIDUserAttr',
+                       'ldap_expert_uuid_group_attr'       => 'ldapExpertUUIDGroupAttr',
+                       'has_memberof_filter_support'       => 'hasMemberOfFilterSupport',
+                       'use_memberof_to_detect_membership' => 'useMemberOfToDetectMembership',
+                       'last_jpegPhoto_lookup'             => 'lastJpegPhotoLookup',
+                       'ldap_nested_groups'                => 'ldapNestedGroups',
+                       'ldap_paging_size'                  => 'ldapPagingSize',
+                       'ldap_experienced_admin'            => 'ldapExperiencedAdmin',
+                       'ldap_dynamic_group_member_url'     => 'ldapDynamicGroupMemberURL',
+               );
+               return $array;
+       }
+
+}
index c565b5bdc439f9e5e8b09c6eb40e7cca2b3051c6..c63bf903d82a396556eeccf3881aaf734a85e66d 100644 (file)
@@ -33,7 +33,6 @@ namespace OCA\User_LDAP;
 
 use OC\ServerNotAvailableException;
 use OCA\user_ldap\lib\Access;
-use OCA\user_ldap\lib\Configuration;
 
 class Wizard extends LDAPUtility {
        static protected $l;
diff --git a/apps/user_ldap/lib/configuration.php b/apps/user_ldap/lib/configuration.php
deleted file mode 100644 (file)
index 418a2bf..0000000
+++ /dev/null
@@ -1,511 +0,0 @@
-<?php
-/**
- * @author Alex Weirig <alex.weirig@technolink.lu>
- * @author Alexander Bergolth <leo@strike.wu.ac.at>
- * @author Arthur Schiwon <blizzz@owncloud.com>
- * @author Jörn Friedrich Dreyer <jfd@butonic.de>
- * @author Lennart Rosam <hello@takuto.de>
- * @author Lukas Reschke <lukas@owncloud.com>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Robin McCorkell <robin@mccorkell.me.uk>
- *
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- * @license AGPL-3.0
- *
- * This code is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License, version 3,
- * along with this program.  If not, see <http://www.gnu.org/licenses/>
- *
- */
-
-namespace OCA\user_ldap\lib;
-
-/**
- * @property int ldapPagingSize holds an integer
- */
-class Configuration {
-
-       protected $configPrefix = null;
-       protected $configRead = false;
-
-       //settings
-       protected $config = array(
-               'ldapHost' => null,
-               'ldapPort' => null,
-               'ldapBackupHost' => null,
-               'ldapBackupPort' => null,
-               'ldapBase' => null,
-               'ldapBaseUsers' => null,
-               'ldapBaseGroups' => null,
-               'ldapAgentName' => null,
-               'ldapAgentPassword' => null,
-               'ldapTLS' => null,
-               'turnOffCertCheck' => null,
-               'ldapIgnoreNamingRules' => null,
-               'ldapUserDisplayName' => null,
-               'ldapUserDisplayName2' => null,
-               'ldapUserFilterObjectclass' => null,
-               'ldapUserFilterGroups' => null,
-               'ldapUserFilter' => null,
-               'ldapUserFilterMode' => null,
-               'ldapGroupFilter' => null,
-               'ldapGroupFilterMode' => null,
-               'ldapGroupFilterObjectclass' => null,
-               'ldapGroupFilterGroups' => null,
-               'ldapGroupDisplayName' => null,
-               'ldapGroupMemberAssocAttr' => null,
-               'ldapLoginFilter' => null,
-               'ldapLoginFilterMode' => null,
-               'ldapLoginFilterEmail' => null,
-               'ldapLoginFilterUsername' => null,
-               'ldapLoginFilterAttributes' => null,
-               'ldapQuotaAttribute' => null,
-               'ldapQuotaDefault' => null,
-               'ldapEmailAttribute' => null,
-               'ldapCacheTTL' => null,
-               'ldapUuidUserAttribute' => 'auto',
-               'ldapUuidGroupAttribute' => 'auto',
-               'ldapOverrideMainServer' => false,
-               'ldapConfigurationActive' => false,
-               'ldapAttributesForUserSearch' => null,
-               'ldapAttributesForGroupSearch' => null,
-               'ldapExperiencedAdmin' => false,
-               'homeFolderNamingRule' => null,
-               'hasPagedResultSupport' => false,
-               'hasMemberOfFilterSupport' => false,
-               'useMemberOfToDetectMembership' => true,
-               'ldapExpertUsernameAttr' => null,
-               'ldapExpertUUIDUserAttr' => null,
-               'ldapExpertUUIDGroupAttr' => null,
-               'lastJpegPhotoLookup' => null,
-               'ldapNestedGroups' => false,
-               'ldapPagingSize' => null,
-               'ldapDynamicGroupMemberURL' => null,
-       );
-
-       /**
-        * @param string $configPrefix
-        * @param bool $autoRead
-        */
-       public function __construct($configPrefix, $autoRead = true) {
-               $this->configPrefix = $configPrefix;
-               if($autoRead) {
-                       $this->readConfiguration();
-               }
-       }
-
-       /**
-        * @param string $name
-        * @return mixed|void
-        */
-       public function __get($name) {
-               if(isset($this->config[$name])) {
-                       return $this->config[$name];
-               }
-       }
-
-       /**
-        * @param string $name
-        * @param mixed $value
-        */
-       public function __set($name, $value) {
-               $this->setConfiguration(array($name => $value));
-       }
-
-       /**
-        * @return array
-        */
-       public function getConfiguration() {
-               return $this->config;
-       }
-
-       /**
-        * set LDAP configuration with values delivered by an array, not read
-        * from configuration. It does not save the configuration! To do so, you
-        * must call saveConfiguration afterwards.
-        * @param array $config array that holds the config parameters in an associated
-        * array
-        * @param array &$applied optional; array where the set fields will be given to
-        * @return false|null
-        */
-       public function setConfiguration($config, &$applied = null) {
-               if(!is_array($config)) {
-                       return false;
-               }
-
-               $cta = $this->getConfigTranslationArray();
-               foreach($config as $inputKey => $val) {
-                       if(strpos($inputKey, '_') !== false && array_key_exists($inputKey, $cta)) {
-                               $key = $cta[$inputKey];
-                       } elseif(array_key_exists($inputKey, $this->config)) {
-                               $key = $inputKey;
-                       } else {
-                               continue;
-                       }
-
-                       $setMethod = 'setValue';
-                       switch($key) {
-                               case 'ldapAgentPassword':
-                                       $setMethod = 'setRawValue';
-                                       break;
-                               case 'homeFolderNamingRule':
-                                       $trimmedVal = trim($val);
-                                       if(!empty($trimmedVal) && strpos($val, 'attr:') === false) {
-                                               $val = 'attr:'.$trimmedVal;
-                                       }
-                                       break;
-                               case 'ldapBase':
-                               case 'ldapBaseUsers':
-                               case 'ldapBaseGroups':
-                               case 'ldapAttributesForUserSearch':
-                               case 'ldapAttributesForGroupSearch':
-                               case 'ldapUserFilterObjectclass':
-                               case 'ldapUserFilterGroups':
-                               case 'ldapGroupFilterObjectclass':
-                               case 'ldapGroupFilterGroups':
-                               case 'ldapLoginFilterAttributes':
-                                       $setMethod = 'setMultiLine';
-                                       break;
-                       }
-                       $this->$setMethod($key, $val);
-                       if(is_array($applied)) {
-                               $applied[] = $inputKey;
-                       }
-               }
-
-       }
-
-       public function readConfiguration() {
-               if(!$this->configRead && !is_null($this->configPrefix)) {
-                       $cta = array_flip($this->getConfigTranslationArray());
-                       foreach($this->config as $key => $val) {
-                               if(!isset($cta[$key])) {
-                                       //some are determined
-                                       continue;
-                               }
-                               $dbKey = $cta[$key];
-                               switch($key) {
-                                       case 'ldapBase':
-                                       case 'ldapBaseUsers':
-                                       case 'ldapBaseGroups':
-                                       case 'ldapAttributesForUserSearch':
-                                       case 'ldapAttributesForGroupSearch':
-                                       case 'ldapUserFilterObjectclass':
-                                       case 'ldapUserFilterGroups':
-                                       case 'ldapGroupFilterObjectclass':
-                                       case 'ldapGroupFilterGroups':
-                                       case 'ldapLoginFilterAttributes':
-                                               $readMethod = 'getMultiLine';
-                                               break;
-                                       case 'ldapIgnoreNamingRules':
-                                               $readMethod = 'getSystemValue';
-                                               $dbKey = $key;
-                                               break;
-                                       case 'ldapAgentPassword':
-                                               $readMethod = 'getPwd';
-                                               break;
-                                       case 'ldapUserDisplayName2':
-                                       case 'ldapGroupDisplayName':
-                                               $readMethod = 'getLcValue';
-                                               break;
-                                       case 'ldapUserDisplayName':
-                                       default:
-                                               // user display name does not lower case because
-                                               // we rely on an upper case N as indicator whether to
-                                               // auto-detect it or not. FIXME
-                                               $readMethod = 'getValue';
-                                               break;
-                               }
-                               $this->config[$key] = $this->$readMethod($dbKey);
-                       }
-                       $this->configRead = true;
-               }
-       }
-
-       /**
-        * saves the current Configuration in the database
-        */
-       public function saveConfiguration() {
-               $cta = array_flip($this->getConfigTranslationArray());
-               foreach($this->config as $key => $value) {
-                       switch ($key) {
-                               case 'ldapAgentPassword':
-                                       $value = base64_encode($value);
-                                       break;
-                               case 'ldapBase':
-                               case 'ldapBaseUsers':
-                               case 'ldapBaseGroups':
-                               case 'ldapAttributesForUserSearch':
-                               case 'ldapAttributesForGroupSearch':
-                               case 'ldapUserFilterObjectclass':
-                               case 'ldapUserFilterGroups':
-                               case 'ldapGroupFilterObjectclass':
-                               case 'ldapGroupFilterGroups':
-                               case 'ldapLoginFilterAttributes':
-                                       if(is_array($value)) {
-                                               $value = implode("\n", $value);
-                                       }
-                                       break;
-                               //following options are not stored but detected, skip them
-                               case 'ldapIgnoreNamingRules':
-                               case 'hasPagedResultSupport':
-                               case 'ldapUuidUserAttribute':
-                               case 'ldapUuidGroupAttribute':
-                                       continue 2;
-                       }
-                       if(is_null($value)) {
-                               $value = '';
-                       }
-                       $this->saveValue($cta[$key], $value);
-               }
-       }
-
-       /**
-        * @param string $varName
-        * @return array|string
-        */
-       protected function getMultiLine($varName) {
-               $value = $this->getValue($varName);
-               if(empty($value)) {
-                       $value = '';
-               } else {
-                       $value = preg_split('/\r\n|\r|\n/', $value);
-               }
-
-               return $value;
-       }
-
-       /**
-        * Sets multi-line values as arrays
-        * 
-        * @param string $varName name of config-key
-        * @param array|string $value to set
-        */
-       protected function setMultiLine($varName, $value) {
-               if(empty($value)) {
-                       $value = '';
-               } else if (!is_array($value)) {
-                       $value = preg_split('/\r\n|\r|\n|;/', $value);
-                       if($value === false) {
-                               $value = '';
-                       }
-               }
-
-               if(!is_array($value)) {
-                       $finalValue = trim($value);
-               } else {
-                       $finalValue = [];
-                       foreach($value as $key => $val) {
-                               if(is_string($val)) {
-                                       $val = trim($val);
-                                       if(!empty($val)) {
-                                               //accidental line breaks are not wanted and can cause
-                                               // odd behaviour. Thus, away with them.
-                                               $finalValue[] = $val;
-                                       }
-                               } else {
-                                       $finalValue[] = $val;
-                               }
-                       }
-               }
-
-               $this->setRawValue($varName, $finalValue);
-       }
-
-       /**
-        * @param string $varName
-        * @return string
-        */
-       protected function getPwd($varName) {
-               return base64_decode($this->getValue($varName));
-       }
-
-       /**
-        * @param string $varName
-        * @return string
-        */
-       protected function getLcValue($varName) {
-               return mb_strtolower($this->getValue($varName), 'UTF-8');
-       }
-
-       /**
-        * @param string $varName
-        * @return string
-        */
-       protected function getSystemValue($varName) {
-               //FIXME: if another system value is added, softcode the default value
-               return \OCP\Config::getSystemValue($varName, false);
-       }
-
-       /**
-        * @param string $varName
-        * @return string
-        */
-       protected function getValue($varName) {
-               static $defaults;
-               if(is_null($defaults)) {
-                       $defaults = $this->getDefaults();
-               }
-               return \OCP\Config::getAppValue('user_ldap',
-                                                                               $this->configPrefix.$varName,
-                                                                               $defaults[$varName]);
-       }
-
-       /**
-        * Sets a scalar value.
-        * 
-        * @param string $varName name of config key
-        * @param mixed $value to set
-        */
-       protected function setValue($varName, $value) {
-               if(is_string($value)) {
-                       $value = trim($value);
-               }
-               $this->config[$varName] = $value;
-       }
-
-       /**
-        * Sets a scalar value without trimming.
-        *
-        * @param string $varName name of config key
-        * @param mixed $value to set
-        */
-       protected function setRawValue($varName, $value) {
-               $this->config[$varName] = $value;
-       }
-
-       /**
-        * @param string $varName
-        * @param string $value
-        * @return bool
-        */
-       protected function saveValue($varName, $value) {
-               return \OCP\Config::setAppValue('user_ldap',
-                                                                               $this->configPrefix.$varName,
-                                                                               $value);
-       }
-
-       /**
-        * @return array an associative array with the default values. Keys are correspond
-        * to config-value entries in the database table
-        */
-       public function getDefaults() {
-               return array(
-                       'ldap_host'                         => '',
-                       'ldap_port'                         => '',
-                       'ldap_backup_host'                  => '',
-                       'ldap_backup_port'                  => '',
-                       'ldap_override_main_server'         => '',
-                       'ldap_dn'                           => '',
-                       'ldap_agent_password'               => '',
-                       'ldap_base'                         => '',
-                       'ldap_base_users'                   => '',
-                       'ldap_base_groups'                  => '',
-                       'ldap_userlist_filter'              => '',
-                       'ldap_user_filter_mode'             => 0,
-                       'ldap_userfilter_objectclass'       => '',
-                       'ldap_userfilter_groups'            => '',
-                       'ldap_login_filter'                 => '',
-                       'ldap_login_filter_mode'            => 0,
-                       'ldap_loginfilter_email'            => 0,
-                       'ldap_loginfilter_username'         => 1,
-                       'ldap_loginfilter_attributes'       => '',
-                       'ldap_group_filter'                 => '',
-                       'ldap_group_filter_mode'            => 0,
-                       'ldap_groupfilter_objectclass'      => '',
-                       'ldap_groupfilter_groups'           => '',
-                       'ldap_display_name'                 => 'displayName',
-                       'ldap_user_display_name_2'                      => '',
-                       'ldap_group_display_name'           => 'cn',
-                       'ldap_tls'                          => 0,
-                       'ldap_quota_def'                    => '',
-                       'ldap_quota_attr'                   => '',
-                       'ldap_email_attr'                   => '',
-                       'ldap_group_member_assoc_attribute' => 'uniqueMember',
-                       'ldap_cache_ttl'                    => 600,
-                       'ldap_uuid_user_attribute'          => 'auto',
-                       'ldap_uuid_group_attribute'         => 'auto',
-                       'home_folder_naming_rule'           => '',
-                       'ldap_turn_off_cert_check'          => 0,
-                       'ldap_configuration_active'         => 0,
-                       'ldap_attributes_for_user_search'   => '',
-                       'ldap_attributes_for_group_search'  => '',
-                       'ldap_expert_username_attr'         => '',
-                       'ldap_expert_uuid_user_attr'        => '',
-                       'ldap_expert_uuid_group_attr'       => '',
-                       'has_memberof_filter_support'       => 0,
-                       'use_memberof_to_detect_membership' => 1,
-                       'last_jpegPhoto_lookup'             => 0,
-                       'ldap_nested_groups'                => 0,
-                       'ldap_paging_size'                  => 500,
-                       'ldap_experienced_admin'            => 0,
-                       'ldap_dynamic_group_member_url'     => '',
-               );
-       }
-
-       /**
-        * @return array that maps internal variable names to database fields
-        */
-       public function getConfigTranslationArray() {
-               //TODO: merge them into one representation
-               static $array = array(
-                       'ldap_host'                         => 'ldapHost',
-                       'ldap_port'                         => 'ldapPort',
-                       'ldap_backup_host'                  => 'ldapBackupHost',
-                       'ldap_backup_port'                  => 'ldapBackupPort',
-                       'ldap_override_main_server'         => 'ldapOverrideMainServer',
-                       'ldap_dn'                           => 'ldapAgentName',
-                       'ldap_agent_password'               => 'ldapAgentPassword',
-                       'ldap_base'                         => 'ldapBase',
-                       'ldap_base_users'                   => 'ldapBaseUsers',
-                       'ldap_base_groups'                  => 'ldapBaseGroups',
-                       'ldap_userfilter_objectclass'       => 'ldapUserFilterObjectclass',
-                       'ldap_userfilter_groups'            => 'ldapUserFilterGroups',
-                       'ldap_userlist_filter'              => 'ldapUserFilter',
-                       'ldap_user_filter_mode'             => 'ldapUserFilterMode',
-                       'ldap_login_filter'                 => 'ldapLoginFilter',
-                       'ldap_login_filter_mode'            => 'ldapLoginFilterMode',
-                       'ldap_loginfilter_email'            => 'ldapLoginFilterEmail',
-                       'ldap_loginfilter_username'         => 'ldapLoginFilterUsername',
-                       'ldap_loginfilter_attributes'       => 'ldapLoginFilterAttributes',
-                       'ldap_group_filter'                 => 'ldapGroupFilter',
-                       'ldap_group_filter_mode'            => 'ldapGroupFilterMode',
-                       'ldap_groupfilter_objectclass'      => 'ldapGroupFilterObjectclass',
-                       'ldap_groupfilter_groups'           => 'ldapGroupFilterGroups',
-                       'ldap_display_name'                 => 'ldapUserDisplayName',
-                       'ldap_user_display_name_2'                      => 'ldapUserDisplayName2',
-                       'ldap_group_display_name'           => 'ldapGroupDisplayName',
-                       'ldap_tls'                          => 'ldapTLS',
-                       'ldap_quota_def'                    => 'ldapQuotaDefault',
-                       'ldap_quota_attr'                   => 'ldapQuotaAttribute',
-                       'ldap_email_attr'                   => 'ldapEmailAttribute',
-                       'ldap_group_member_assoc_attribute' => 'ldapGroupMemberAssocAttr',
-                       'ldap_cache_ttl'                    => 'ldapCacheTTL',
-                       'home_folder_naming_rule'           => 'homeFolderNamingRule',
-                       'ldap_turn_off_cert_check'          => 'turnOffCertCheck',
-                       'ldap_configuration_active'         => 'ldapConfigurationActive',
-                       'ldap_attributes_for_user_search'   => 'ldapAttributesForUserSearch',
-                       'ldap_attributes_for_group_search'  => 'ldapAttributesForGroupSearch',
-                       'ldap_expert_username_attr'         => 'ldapExpertUsernameAttr',
-                       'ldap_expert_uuid_user_attr'        => 'ldapExpertUUIDUserAttr',
-                       'ldap_expert_uuid_group_attr'       => 'ldapExpertUUIDGroupAttr',
-                       'has_memberof_filter_support'       => 'hasMemberOfFilterSupport',
-                       'use_memberof_to_detect_membership' => 'useMemberOfToDetectMembership',
-                       'last_jpegPhoto_lookup'             => 'lastJpegPhotoLookup',
-                       'ldap_nested_groups'                => 'ldapNestedGroups',
-                       'ldap_paging_size'                  => 'ldapPagingSize',
-                       'ldap_experienced_admin'            => 'ldapExperiencedAdmin',
-                       'ldap_dynamic_group_member_url'     => 'ldapDynamicGroupMemberURL',
-               );
-               return $array;
-       }
-
-}
index 3bb9689d29010ee47000dd10714e0061bfa564b6..40d02dfaa1488f1e3b6e308900faa6268f36ce3e 100644 (file)
@@ -67,7 +67,7 @@ $tmpl->assign('toc', $toc);
 $tmpl->assign('settingControls', $sControls);
 
 // assign default values
-$config = new \OCA\user_ldap\lib\Configuration('', false);
+$config = new \OCA\User_LDAP\Configuration('', false);
 $defaults = $config->getDefaults();
 foreach($defaults as $key => $default) {
        $tmpl->assign($key.'_default', $default);
index f0767ea635a47bddfd195c71e9885b9a398fbc46..d962e057911003c4b8b2ea1e80b5eaebecda0348 100644 (file)
@@ -29,7 +29,7 @@ use \OCA\User_LDAP\Wizard;
 
 // use \OCA\User_LDAP\User_LDAP as UserLDAP;
 // use \OCA\user_ldap\lib\Access;
-// use \OCA\user_ldap\lib\Configuration;
+// use \OCA\User_LDAP\Configuration;
 // use \OCA\User_LDAP\ILDAPWrapper;
 
 /**
@@ -59,12 +59,12 @@ class WizardTest extends \Test\TestCase {
                static $accMethods;
 
                if(is_null($confMethods)) {
-                       $confMethods = get_class_methods('\OCA\user_ldap\lib\Configuration');
+                       $confMethods = get_class_methods('\OCA\User_LDAP\Configuration');
                        $connMethods = get_class_methods('\OCA\User_LDAP\Connection');
                        $accMethods  = get_class_methods('\OCA\user_ldap\lib\Access');
                }
                $lw   = $this->getMock('\OCA\User_LDAP\ILDAPWrapper');
-               $conf = $this->getMock('\OCA\user_ldap\lib\Configuration',
+               $conf = $this->getMock('\OCA\User_LDAP\Configuration',
                                                           $confMethods,
                                                           array($lw, null, null));
 
index 3b87223b75bf4ddb6c9c0e95021c6ca9f161968e..d4f1ce142bddc4d06d204013bbdbb4b253ed508d 100644 (file)
@@ -89,7 +89,7 @@ class Test_Configuration extends \Test\TestCase {
         * @dataProvider configurationDataProvider
         */
        public function testSetValue($key, $input, $expected) {
-               $configuration = new \OCA\user_ldap\lib\Configuration('t01', false);
+               $configuration = new \OCA\User_LDAP\Configuration('t01', false);
 
                $settingsInput = array(
                        'ldapBaseUsers' => array(