diff --git a/lib/Froxlor/Api/Commands/EmailForwarders.php b/lib/Froxlor/Api/Commands/EmailForwarders.php index fcfff35a..af344a67 100644 --- a/lib/Froxlor/Api/Commands/EmailForwarders.php +++ b/lib/Froxlor/Api/Commands/EmailForwarders.php @@ -109,11 +109,11 @@ class EmailForwarders extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\Re /** * You cannot directly get an email forwarder. - * You need to call Emails.get() + * Try EmailForwarders.listing() */ public function get() { - throw new \Exception('You cannot directly get an email forwarder. You need to call Emails.get()', 303); + throw new \Exception('You cannot directly get an email forwarder. Try EmailForwarders.listing()', 303); } /** @@ -126,21 +126,91 @@ class EmailForwarders extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\Re } /** - * You cannot directly list email forwarders. - * You need to call Emails.listing() + * List email forwarders for a given email address + * + * @param int $id + * optional, the email-address-id + * @param string $emailaddr + * optional, the email-address to delete the forwarder from + * @param int $customerid + * optional, admin-only, the customer-id + * @param string $loginname + * optional, admin-only, the loginname + * + * @access admin,customer + * @throws \Exception + * @return string json-encoded array count|list */ public function listing() { - throw new \Exception('You cannot directly list email forwarders. You need to call Emails.listing()', 303); + if ($this->isAdmin() == false && Settings::IsInList('panel.customer_hide_options', 'email')) { + throw new \Exception("You cannot access this resource", 405); + } + + // parameter + $id = $this->getParam('id', true, 0); + $ea_optional = ($id <= 0 ? false : true); + $emailaddr = $this->getParam('emailaddr', $ea_optional, ''); + + // validation + $result = $this->apiCall('Emails.get', array( + 'id' => $id, + 'emailaddr' => $emailaddr + )); + $id = $result['id']; + + $result['destination'] = explode(' ', $result['destination']); + $destination = array(); + foreach ($result['destination'] as $index => $address) { + $destination[] = [ + 'id' => $index, + 'address' => $address + ]; + } + + return $this->response(200, "successfull", [ + 'count' => count($destination), + 'list' => $destination + ]); } /** - * You cannot directly count email forwarders. - * You need to call Emails.listingCount() + * count email forwarders for a given email address + * + * @param int $id + * optional, the email-address-id + * @param string $emailaddr + * optional, the email-address to delete the forwarder from + * @param int $customerid + * optional, admin-only, the customer-id + * @param string $loginname + * optional, admin-only, the loginname + * + * @access admin,customer + * @throws \Exception + * @return string json-encoded array */ public function listingCount() { - throw new \Exception('You cannot directly count email forwarders. You need to call Emails.listingCount()', 303); + if ($this->isAdmin() == false && Settings::IsInList('panel.customer_hide_options', 'email')) { + throw new \Exception("You cannot access this resource", 405); + } + + // parameter + $id = $this->getParam('id', true, 0); + $ea_optional = ($id <= 0 ? false : true); + $emailaddr = $this->getParam('emailaddr', $ea_optional, ''); + + // validation + $result = $this->apiCall('Emails.get', array( + 'id' => $id, + 'emailaddr' => $emailaddr + )); + $id = $result['id']; + + $result['destination'] = explode(' ', $result['destination']); + + return $this->response(200, "successfull", count($result['destination'])); } /** diff --git a/tests/Emails/EmailsTest.php b/tests/Emails/EmailsTest.php index 3e254bbb..c8879414 100644 --- a/tests/Emails/EmailsTest.php +++ b/tests/Emails/EmailsTest.php @@ -191,6 +191,38 @@ class MailsTest extends TestCase $this->assertEquals('other@domain.tld other2@domain.tld', $result['destination']); } + /** + * + * @depends testCustomerEmailForwardersAddAnother + */ + public function testCustomerEmailForwardersListing() + { + global $admin_userdata; + + Settings::Set('panel.customer_hide_options', '', true); + + // get customer + $json_result = Customers::getLocal($admin_userdata, array( + 'loginname' => 'test1' + ))->get(); + $customer_userdata = json_decode($json_result, true)['data']; + + $data = [ + 'emailaddr' => 'info@test2.local' + ]; + $json_result = EmailForwarders::getLocal($customer_userdata, $data)->listing(); + $result = json_decode($json_result, true)['data']; + $this->assertEquals(2, $result['count']); + $this->assertEquals(0, $result['list'][0]['id']); + $this->assertEquals('other@domain.tld', $result['list'][0]['address']); + $this->assertEquals(1, $result['list'][1]['id']); + $this->assertEquals('other2@domain.tld', $result['list'][1]['address']); + + $json_result = EmailForwarders::getLocal($customer_userdata, $data)->listingCount(); + $result = json_decode($json_result, true)['data']; + $this->assertEquals(2, $result); + } + /** * * @depends testCustomerEmailForwardersDeleteEmailHidden @@ -292,13 +324,6 @@ class MailsTest extends TestCase EmailForwarders::getLocal($admin_userdata)->update(); } - public function testAdminEmailForwadersUndefinedListing() - { - global $admin_userdata; - $this->expectExceptionCode(303); - EmailForwarders::getLocal($admin_userdata)->listing(); - } - /** * * @depends testCustomerEmailForwardersAddAnother