Added a handler for external pop3 accounts.
authorGunnar Wrobel <p@rdus.de>
Tue, 14 Apr 2009 08:07:21 +0000 (10:07 +0200)
committerGunnar Wrobel <p@rdus.de>
Tue, 14 Apr 2009 17:21:17 +0000 (19:21 +0200)
framework/Kolab_Server/lib/Horde/Kolab/Server/Object/Kolabpop3account.php [new file with mode: 0644]
framework/Kolab_Server/lib/Horde/Kolab/Server/Structure/Kolab.php
framework/Kolab_Server/test/Horde/Kolab/Server/Kolabpop3accountTest.php [new file with mode: 0644]

diff --git a/framework/Kolab_Server/lib/Horde/Kolab/Server/Object/Kolabpop3account.php b/framework/Kolab_Server/lib/Horde/Kolab/Server/Object/Kolabpop3account.php
new file mode 100644 (file)
index 0000000..34dfdd2
--- /dev/null
@@ -0,0 +1,218 @@
+<?php
+/**
+ * Represents external pop3 account information.
+ *
+ * PHP version 5
+ *
+ * @category Kolab
+ * @package  Kolab_Server
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Kolab_Server
+ */
+
+/**
+ * This class provides a representation of pop3 mail accounts.
+ *
+ * Copyright 2008-2009 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Kolab
+ * @package  Kolab_Server
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Kolab_Server
+ */
+class Horde_Kolab_Server_Object_Kolabpop3account extends Horde_Kolab_Server_Object
+{
+    /** Define attributes specific to this object type */
+
+    /** The common name */
+    const ATTRIBUTE_CN = 'cn';
+
+    /** Server the account resides on */
+    const ATTRIBUTE_SERVER = 'externalPop3AccountServer';
+
+    /** User name for the account */
+    const ATTRIBUTE_LOGINNAME = 'externalPop3AccountLoginName';
+
+    /** Password for the account */
+    const ATTRIBUTE_PASSWORD = 'externalPop3EncryptedAccountPassword';
+
+    /** Description of the account */
+    const ATTRIBUTE_DESCRIPTION = 'externalPop3AccountDescription';
+
+    /** Mail address of the account */
+    const ATTRIBUTE_MAIL = 'externalPop3AccountMail';
+
+    /** Port to connect to */
+    const ATTRIBUTE_PORT = 'externalPop3AccountPort';
+
+    /** Use SSL when fetching mail from the account? */
+    const ATTRIBUTE_USESSL = 'externalPop3AccountUseSSL';
+
+    /** Use TLS when fetching mail from the account? */
+    const ATTRIBUTE_USETLS = 'externalPop3AccountUseTLS';
+
+    /** Login method for the external account */
+    const ATTRIBUTE_LOGINMETHOD = 'externalPop3AccountLoginMethod';
+
+    /** Validate the server certificate when connecting via SSL/TLS? */
+    const ATTRIBUTE_CHECKCERTIFICATE = 'externalPop3AccountCheckServerCertificate';
+
+    /** Should the fetched mail be deleted on the external account or not? */
+    const ATTRIBUTE_KEEPMAILONSERVER = 'externalPop3AccountKeepMailOnServer';
+
+    /** The uid of the owner of this account */
+    const ATTRIBUTE_OWNERUID = 'externalPop3AccountOwnerUid';
+
+    /** The specific object class of this object type */
+    const OBJECTCLASS_KOLABEXTERNALPOP3ACCOUNT = 'kolabExternalPop3Account';
+
+    /**
+     * A structure to initialize the attribute structure for this class.
+     *
+     * @var array
+     */
+    static public $init_attributes = array(
+        'defined' => array(
+            self::ATTRIBUTE_CN,
+            self::ATTRIBUTE_SERVER,
+            self::ATTRIBUTE_LOGINNAME,
+            self::ATTRIBUTE_PASSWORD,
+            self::ATTRIBUTE_DESCRIPTION,
+            self::ATTRIBUTE_MAIL,
+            self::ATTRIBUTE_PORT,
+            self::ATTRIBUTE_USESSL,
+            self::ATTRIBUTE_USETLS,
+            self::ATTRIBUTE_LOGINMETHOD,
+            self::ATTRIBUTE_CHECKCERTIFICATE,
+            self::ATTRIBUTE_KEEPMAILONSERVER,
+        ),
+        'derived' => array(
+            self::ATTRIBUTE_OWNERUID => array(
+                'method' => 'getParentUid',
+            ),
+        ),
+        'required' => array(
+            self::ATTRIBUTE_CN,
+            self::ATTRIBUTE_SERVER,
+            self::ATTRIBUTE_LOGINNAME,
+            self::ATTRIBUTE_PASSWORD,
+        ),
+        'object_classes' => array(
+            self::OBJECTCLASS_KOLABEXTERNALPOP3ACCOUNT,
+        ),
+    );
+
+    /**
+     * Generates an ID for the given information.
+     *
+     * @param array $info The data of the object.
+     *
+     * @static
+     *
+     * @return string|PEAR_Error The ID.
+     */
+    public function generateId(&$info)
+    {
+        if (!isset($info[self::ATTRIBUTE_OWNERUID])) {
+            throw new Horde_Kolab_Server_Exception(_("No parent object provided!"),
+                                                   Horde_Kolab_Server_Exception::INVALID_INFORMATION);
+        }
+
+        if (is_array($info[self::ATTRIBUTE_OWNERUID])) {
+            $uid = $info[self::ATTRIBUTE_OWNERUID][0];
+        } else {
+            $uid = $info[self::ATTRIBUTE_OWNERUID];
+        }
+
+        $object = $this->server->fetch($uid);
+        if (!$object->exists()) {
+            throw new Horde_Kolab_Server_Exception(sprintf(_("The parent object %s does not exist!"),
+                                                           $uid),
+                                                   Horde_Kolab_Server_Exception::INVALID_INFORMATION);
+        }
+        if (!isset($info[self::ATTRIBUTE_SERVER])) {
+            throw new Horde_Kolab_Server_Exception(_("No server name given!"),
+                                                   Horde_Kolab_Server_Exception::INVALID_INFORMATION);
+        }
+        if (!isset($info[self::ATTRIBUTE_LOGINNAME])) {
+            throw new Horde_Kolab_Server_Exception(_("No login name given!"),
+                                                   Horde_Kolab_Server_Exception::INVALID_INFORMATION);
+        }
+
+        if (is_array($info[self::ATTRIBUTE_SERVER])) {
+            $server = $info[self::ATTRIBUTE_SERVER][0];
+        } else {
+            $server = $info[self::ATTRIBUTE_SERVER];
+        }
+        if (is_array($info[self::ATTRIBUTE_LOGINNAME])) {
+            $user = $info[self::ATTRIBUTE_LOGINNAME][0];
+        } else {
+            $user = $info[self::ATTRIBUTE_LOGINNAME];
+        }
+
+        $cn = $user . '@' . $server;
+
+        $base = substr($uid, 0, strpos($uid, $this->server->getBaseUid()) - 1);
+
+        return self::ATTRIBUTE_CN . '=' . $this->server->structure->quoteForUid($cn) . ',' . $base;
+    }
+
+    /**
+     * Saves object information. This may either create a new entry or modify an
+     * existing entry.
+     *
+     * Please note that fields with multiple allowed values require the callee
+     * to provide the full set of values for the field. Any old values that are
+     * not resubmitted will be considered to be deleted.
+     *
+     * @param array $info The information about the object.
+     *
+     * @return boolean|PEAR_Error True on success.
+     */
+    public function save($info)
+    {
+        if (!$this->exists() && empty($info[self::ATTRIBUTE_CN])
+            && !empty($info[self::ATTRIBUTE_SERVER])
+            && !empty($info[self::ATTRIBUTE_LOGINNAME])) {
+            $info[self::ATTRIBUTE_CN] = $info[self::ATTRIBUTE_LOGINNAME] . '@' . $info[self::ATTRIBUTE_SERVER];
+        }
+
+        return parent::save($info);
+    }
+
+    /**
+     * Returns the set of search operations supported by this object type.
+     *
+     * @return array An array of supported search operations.
+     */
+    static public function getSearchOperations()
+    {
+        $searches = array(
+            'pop3AccountsForMail',
+        );
+        return $searches;
+    }
+
+    /**
+     * Returns the UIDs of the pop3 accounts for the user with the given mail
+     * address.
+     *
+     * @param Horde_Kolab_Server $server The server to query.
+     * @param string             $mail   Search objects with this mail alias.
+     *
+     * @return mixed The UIDs or false if there was no result.
+     *
+     * @throws Horde_Kolab_Server_Exception
+     */
+    static public function pop3AccountsForMail($server, $mail)
+    {
+        $uid = $server->uidForMail($mail, Horde_Kolab_Server_Object::RESULT_SINGLE);
+        return self::objectsForUid($server, $uid, self::OBJECTCLASS_KOLABEXTERNALPOP3ACCOUNT);
+    }
+
+}
\ No newline at end of file
index 4e364e3..96b7576 100644 (file)
@@ -42,6 +42,8 @@ class Horde_Kolab_Server_Structure_Kolab extends Horde_Kolab_Server_Structure_Ld
             'Horde_Kolab_Server_Object_Organizationalperson',
             'Horde_Kolab_Server_Object_Inetorgperson',
             'Horde_Kolab_Server_Object_Kolabinetorgperson',
+            'Horde_Kolab_Server_Object_Kolabgermanbankarrangement',
+            'Horde_Kolab_Server_Object_Kolabpop3account',
             'Horde_Kolab_Server_Object_Kolabgroupofnames',
             'Horde_Kolab_Server_Object_Kolabsharedfolder',
             'Horde_Kolab_Server_Object_Kolab_Address',
@@ -73,6 +75,10 @@ class Horde_Kolab_Server_Structure_Kolab extends Horde_Kolab_Server_Structure_Ld
             if (in_array('kolabgroupofnames', $oc)) {
                 return 'Horde_Kolab_Server_Object_Kolabgroupofnames';
             }
+            // Is it an external pop3 account?
+            if (in_array('kolabexternalpop3account', $oc)) {
+                return 'Horde_Kolab_Server_Object_Kolabpop3account';
+            }
             // Is it a shared Folder?
             if (in_array('kolabsharedfolder', $oc)) {
                 return 'Horde_Kolab_Server_Object_Kolabsharedfolder';
diff --git a/framework/Kolab_Server/test/Horde/Kolab/Server/Kolabpop3accountTest.php b/framework/Kolab_Server/test/Horde/Kolab/Server/Kolabpop3accountTest.php
new file mode 100644 (file)
index 0000000..af5ebe7
--- /dev/null
@@ -0,0 +1,230 @@
+<?php
+/**
+ * Test the kolabExternalPop3Account object.
+ *
+ * PHP version 5
+ *
+ * @category Kolab
+ * @package  Kolab_Server
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Kolab_Server
+ */
+
+/**
+ * The Autoloader allows us to omit "require/include" statements.
+ */
+require_once 'Horde/Autoloader.php';
+
+/**
+ * Test the kolabExternalPop3Account object.
+ *
+ * Copyright 2009 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Kolab
+ * @package  Kolab_Server
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Kolab_Server
+ */
+class Horde_Kolab_Server_Kolabpop3accountTest extends Horde_Kolab_Test_Server
+{
+    /**
+     * Objects used within this test
+     *
+     * @var array
+     */
+    private $objects = array(
+        /* Default bank account owner */
+        array(
+            'type' => 'Horde_Kolab_Server_Object_Kolabinetorgperson',
+            Horde_Kolab_Server_Object_Kolabinetorgperson::ATTRIBUTE_GIVENNAME    => 'Frank',
+            Horde_Kolab_Server_Object_Kolabinetorgperson::ATTRIBUTE_SN           => 'Mustermann',
+            Horde_Kolab_Server_Object_Kolabinetorgperson::ATTRIBUTE_USERPASSWORD => 'Kolab_Server_OrgPersonTest_123',
+        ),
+        /* Default account */
+        array(
+            'type' => 'Horde_Kolab_Server_Object_Kolabpop3account',
+            Horde_Kolab_Server_Object_Kolabpop3account::ATTRIBUTE_SERVER   => 'pop3.example.com',
+            Horde_Kolab_Server_Object_Kolabpop3account::ATTRIBUTE_LOGINNAME => 'frank',
+            Horde_Kolab_Server_Object_Kolabpop3account::ATTRIBUTE_PASSWORD => ' must3r',
+        ),
+    );
+
+    /**
+     * Provide different server types.
+     *
+     * @return array The different server types.
+     */
+    public function &provideServers()
+    {
+        $servers = array();
+        /**
+         * We always use the test server
+         */
+        $servers[] = array($this->prepareEmptyKolabServer());
+        if (false) {
+            $real = $this->prepareLdapKolabServer();
+            if (!empty($real)) {
+                $servers[] = array($real);
+            }
+        }
+        return $servers;
+    }
+
+    /**
+     * Test ID generation for a person.
+     *
+     * @dataProvider provideServers
+     *
+     * @return NULL
+     */
+    public function testGenerateId($server)
+    {
+        $person = $this->assertAdd($server, $this->objects[0],
+                                   array(Horde_Kolab_Server_Object_Kolabinetorgperson::ATTRIBUTE_SID => ''));
+        $account_data = $this->objects[1];
+        $account_data[Horde_Kolab_Server_Object_Kolabpop3account::ATTRIBUTE_OWNERUID] = $person->getUid();
+        $a = new Horde_Kolab_Server_Object_Kolabpop3account($server, null, $account_data);
+        $this->assertContains(Horde_Kolab_Server_Object_Kolabpop3account::ATTRIBUTE_CN . '=' . $this->objects[1][Horde_Kolab_Server_Object_Kolabpop3account::ATTRIBUTE_LOGINNAME] . '@' . $this->objects[1][Horde_Kolab_Server_Object_Kolabpop3account::ATTRIBUTE_SERVER],
+                              $a->get(Horde_Kolab_Server_Object_Kolabpop3account::ATTRIBUTE_UID));
+    }
+
+    /**
+     * Test adding an invalid Account.
+     *
+     * @dataProvider provideServers
+     * @expectedException Horde_Kolab_Server_Exception
+     *
+     * @return NULL
+     */
+    public function testAddInvalidAccount($server)
+    {
+        $result = $server->add($this->objects[1]);
+    }
+
+    /**
+     * Test handling easy attributes.
+     *
+     * @dataProvider provideServers
+     *
+     * @return NULL
+     */
+    public function testEasyAttributes($server)
+    {
+        $person = $this->assertAdd($server, $this->objects[0],
+                                   array(Horde_Kolab_Server_Object_Kolabinetorgperson::ATTRIBUTE_SID => ''));
+        $account_data = $this->objects[1];
+        $account_data[Horde_Kolab_Server_Object_Kolabpop3account::ATTRIBUTE_OWNERUID] = $person->getUid();
+        $account = $this->assertAdd($server, $account_data,
+                                    array(Horde_Kolab_Server_Object_Kolabpop3account::ATTRIBUTE_OWNERUID => $person->getUid()));
+        $this->assertEasyAttributes($account, $server,
+                                    array(
+                                        Horde_Kolab_Server_Object_Kolabpop3account::ATTRIBUTE_PASSWORD => array(
+                                            'something',
+                                            'somewhere',
+                                            null,
+                                            array('a', 'b'),
+                                            '',
+                                        ),
+                                        Horde_Kolab_Server_Object_Kolabpop3account::ATTRIBUTE_DESCRIPTION => array(
+                                            'something',
+                                            'somewhere',
+                                            null,
+                                            array('a', 'b'),
+                                            '',
+                                        ),
+                                        Horde_Kolab_Server_Object_Kolabpop3account::ATTRIBUTE_MAIL => array(
+                                            'something',
+                                            'somewhere',
+                                            null,
+                                            array('a', 'b'),
+                                            '',
+                                        ),
+                                        Horde_Kolab_Server_Object_Kolabpop3account::ATTRIBUTE_PORT => array(
+                                            '110',
+                                            '111',
+                                            null,
+                                            array('20', '21'),
+                                            '',
+                                        ),
+                                        Horde_Kolab_Server_Object_Kolabpop3account::ATTRIBUTE_USESSL => array(
+                                            'TRUE',
+                                            'FALSE',
+                                            null,
+                                            array('TRUE', 'FALSE'),
+                                            '',
+                                        ),
+                                        Horde_Kolab_Server_Object_Kolabpop3account::ATTRIBUTE_USETLS => array(
+                                            'TRUE',
+                                            'FALSE',
+                                            null,
+                                            array('TRUE', 'FALSE'),
+                                            '',
+                                        ),
+                                        Horde_Kolab_Server_Object_Kolabpop3account::ATTRIBUTE_LOGINMETHOD => array(
+                                            'something',
+                                            'somewhere',
+                                            null,
+                                            array('a', 'b'),
+                                            '',
+                                        ),
+                                        Horde_Kolab_Server_Object_Kolabpop3account::ATTRIBUTE_CHECKCERTIFICATE => array(
+                                            'TRUE',
+                                            'FALSE',
+                                            null,
+                                            array('TRUE', 'FALSE'),
+                                            '',
+                                        ),
+                                        Horde_Kolab_Server_Object_Kolabpop3account::ATTRIBUTE_KEEPMAILONSERVER => array(
+                                            'TRUE',
+                                            'FALSE',
+                                            null,
+                                            array('TRUE', 'FALSE'),
+                                            '',
+                                        ),
+                                    )
+        );
+    }
+
+    /**
+     * Test modifying the attributes required for the UID of the account. This
+     * should lead to renaming object.
+     *
+     * @dataProvider provideServers
+     *
+     * @return NULL
+     */
+    public function testModifyUidElements($server)
+    {
+        $person = $this->assertAdd($server, $this->objects[0],
+                                   array(Horde_Kolab_Server_Object_Kolabinetorgperson::ATTRIBUTE_SID => ''));
+        $account_data = $this->objects[1];
+        $account_data[Horde_Kolab_Server_Object_Kolabpop3account::ATTRIBUTE_OWNERUID] = $person->getUid();
+        $account = $server->add($account_data);
+        $this->assertNoError($account);
+
+        $account = $server->fetch($account->getUid());
+        $this->assertNoError($account);
+
+        $this->assertEquals($this->objects[1][Horde_Kolab_Server_Object_Kolabpop3account::ATTRIBUTE_SERVER],
+                            $account->get(Horde_Kolab_Server_Object_Kolabpop3account::ATTRIBUTE_SERVER));
+
+        $result = $account->save(array(Horde_Kolab_Server_Object_Kolabpop3account::ATTRIBUTE_SERVER => 'pop3s.example.com'));
+        $this->assertNoError($result);
+
+        $account = $server->fetch($account->getUid());
+        $this->assertNoError($account);
+
+        $this->assertEquals($account->get(Horde_Kolab_Server_Object_Kolabpop3account::ATTRIBUTE_SERVER),
+                            'pop3s.example.com');
+
+        $this->assertContains('pop3s.example.com', $account->getUid());
+
+        $result = $server->delete($account->getUid());
+        $this->assertNoError($result);
+    }
+}