Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F117876046
D5376.1775335623.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
9 KB
Referenced Files
None
Subscribers
None
D5376.1775335623.diff
View Options
diff --git a/src/app/Policy/Greylist.php b/src/app/Policy/Greylist.php
--- a/src/app/Policy/Greylist.php
+++ b/src/app/Policy/Greylist.php
@@ -243,7 +243,7 @@
protected function recipientFromRequest()
{
- $recipients = Utils::findObjectsByRecipientAddress($this->request['recipient']);
+ $recipients = PolicyUtils::findObjectsByRecipientAddress($this->request['recipient']);
if (count($recipients) > 1) {
\Log::warning(
@@ -253,11 +253,9 @@
if (count($recipients) >= 1) {
foreach ($recipients as $recipient) {
- if ($recipient) {
- $this->recipientID = $recipient->id;
- $this->recipientType = $recipient::class;
- break;
- }
+ $this->recipientID = $recipient->id;
+ $this->recipientType = $recipient::class;
+ break;
}
} else {
$recipient = null;
diff --git a/src/app/Policy/SPF.php b/src/app/Policy/SPF.php
--- a/src/app/Policy/SPF.php
+++ b/src/app/Policy/SPF.php
@@ -3,6 +3,7 @@
namespace App\Policy;
use App\Policy\SPF\Cache;
+use App\Policy\Utils as PolicyUtils;
use App\Utils;
use SPFLib\Check\Environment;
use SPFLib\Check\Result;
@@ -111,7 +112,7 @@
// inbound mail to a local recipient address.
$objects = null;
if (array_key_exists('recipient', $data)) {
- $objects = Utils::findObjectsByRecipientAddress($data['recipient']);
+ $objects = PolicyUtils::findObjectsByRecipientAddress($data['recipient']);
}
if (!empty($objects)) {
diff --git a/src/app/Policy/Utils.php b/src/app/Policy/Utils.php
--- a/src/app/Policy/Utils.php
+++ b/src/app/Policy/Utils.php
@@ -2,10 +2,102 @@
namespace App\Policy;
+use App\Domain;
+use App\Resource;
+use App\SharedFolder;
use App\User;
+use App\Utils as AppUtils;
class Utils
{
+ /**
+ * Find objects that are the recipient for the specified email address.
+ *
+ * @param string $address Email address
+ */
+ public static function findObjectsByRecipientAddress($address): array
+ {
+ [$local, $domainName] = AppUtils::normalizeAddress($address, true);
+
+ if (empty($domainName)) {
+ return [];
+ }
+
+ $address = $local . '@' . $domainName;
+
+ $domain = Domain::where('namespace', $domainName)->first();
+
+ if (!$domain) {
+ return [];
+ }
+
+ // Find user/shared-folder/resource with specified address
+ // FIXME: Groups (distribution lists) also have an email address,
+ // but they aren't mailrecipients, or are they?
+
+ $user = User::where('email', $address)->first();
+
+ if ($user) {
+ return [$user];
+ }
+
+ $folder = SharedFolder::where('email', $address)->first();
+
+ if ($folder) {
+ return [$folder];
+ }
+
+ $resource = Resource::where('email', $address)->first();
+
+ if ($resource) {
+ return [$resource];
+ }
+
+ // Find aliases with specified address
+ // FIXME: Folders and users can share aliases, should we merge the result (and return both)?
+
+ $users = User::select('users.*')->distinct()
+ ->join('user_aliases', 'user_aliases.user_id', '=', 'users.id')
+ ->where('alias', $address)
+ ->get();
+
+ if (count($users) > 0) {
+ return $users->all();
+ }
+
+ $folders = SharedFolder::select('shared_folders.*')->distinct()
+ ->join('shared_folder_aliases', 'shared_folder_aliases.shared_folder_id', '=', 'shared_folders.id')
+ ->where('alias', $address)
+ ->get();
+
+ if (count($folders) > 0) {
+ return $folders->all();
+ }
+
+ // Use catchall@ alias if exists
+ // FIXME: Folders and users can share aliases, should we merge the result (and return both)?
+
+ $users = User::select('users.*')->distinct()
+ ->join('user_aliases', 'user_aliases.user_id', '=', 'users.id')
+ ->where('alias', "catchall@{$domain->namespace}")
+ ->get();
+
+ if (count($users) > 0) {
+ return $users->all();
+ }
+
+ $folders = SharedFolder::select('shared_folders.*')->distinct()
+ ->join('shared_folder_aliases', 'shared_folder_aliases.shared_folder_id', '=', 'shared_folders.id')
+ ->where('alias', "catchall@{$domain->namespace}")
+ ->get();
+
+ if (count($folders) > 0) {
+ return $folders->all();
+ }
+
+ return [];
+ }
+
/**
* Get user setting with a fallback to account policy
*
diff --git a/src/app/Utils.php b/src/app/Utils.php
--- a/src/app/Utils.php
+++ b/src/app/Utils.php
@@ -204,58 +204,6 @@
return $result;
}
- /**
- * Find an object that is the recipient for the specified address.
- *
- * @param string $address
- *
- * @return array
- */
- public static function findObjectsByRecipientAddress($address)
- {
- $address = self::normalizeAddress($address);
-
- [$local, $domainName] = explode('@', $address);
-
- $domain = Domain::where('namespace', $domainName)->first();
-
- if (!$domain) {
- return [];
- }
-
- $user = User::where('email', $address)->first();
-
- if ($user) {
- return [$user];
- }
-
- $userAliases = UserAlias::where('alias', $address)->get();
-
- if (count($userAliases) > 0) {
- $users = [];
-
- foreach ($userAliases as $userAlias) {
- $users[] = $userAlias->user;
- }
-
- return $users;
- }
-
- $userAliases = UserAlias::where('alias', "catchall@{$domain->namespace}")->get();
-
- if (count($userAliases) > 0) {
- $users = [];
-
- foreach ($userAliases as $userAlias) {
- $users[] = $userAlias->user;
- }
-
- return $users;
- }
-
- return [];
- }
-
/**
* Retrieve the network ID and Type from a client address
*
diff --git a/src/tests/Feature/Policy/UtilsTest.php b/src/tests/Feature/Policy/UtilsTest.php
new file mode 100644
--- /dev/null
+++ b/src/tests/Feature/Policy/UtilsTest.php
@@ -0,0 +1,105 @@
+<?php
+
+namespace Tests\Feature\Policy;
+
+use App\Domain;
+use App\Policy\Utils;
+use Tests\TestCase;
+
+class UtilsTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+
+ $this->deleteTestUser('UserAccountA@UserAccount.com');
+ $this->deleteTestDomain('UserAccount.com');
+ $this->deleteTestSharedFolder('folder-test@kolabnow.com');
+ $this->deleteTestResource('resource-test@kolabnow.com');
+ }
+
+ protected function tearDown(): void
+ {
+ $this->deleteTestUser('UserAccountA@UserAccount.com');
+ $this->deleteTestDomain('UserAccount.com');
+ $this->deleteTestSharedFolder('folder-test@kolabnow.com');
+ $this->deleteTestResource('resource-test@kolabnow.com');
+
+ parent::tearDown();
+ }
+
+ /**
+ * Test Utils::findObjectsByRecipientAddress()
+ */
+ public function testFindObjectsByRecipientAddress(): void
+ {
+ $result = Utils::findObjectsByRecipientAddress('unknown');
+ $this->assertCount(0, $result);
+
+ $result = Utils::findObjectsByRecipientAddress('unknown@unknow.org');
+ $this->assertCount(0, $result);
+
+ $result = Utils::findObjectsByRecipientAddress('unknown@kolab.org');
+ $this->assertCount(0, $result);
+
+ // Users
+
+ $user = $this->getTestUser('UserAccountA@UserAccount.com');
+ $domain = $this->getTestDomain('UserAccount.com', [
+ 'status' => Domain::STATUS_NEW | Domain::STATUS_ACTIVE,
+ 'type' => Domain::TYPE_PUBLIC,
+ ]);
+
+ $result = Utils::findObjectsByRecipientAddress('UserAccountA@UserAccount.com');
+
+ $this->assertCount(1, $result);
+ $this->assertSame($user->email, $result[0]->email);
+
+ $user->setAliases(['test@UserAccount.com']);
+
+ $result = Utils::findObjectsByRecipientAddress('test@UserAccount.com');
+
+ $this->assertCount(1, $result);
+ $this->assertSame($user->email, $result[0]->email);
+
+ $user->setAliases(['catchall@UserAccount.com']);
+
+ $result = Utils::findObjectsByRecipientAddress('unknown@UserAccount.com');
+
+ $this->assertCount(1, $result);
+ $this->assertSame($user->email, $result[0]->email);
+
+ // Shared folders
+
+ $folder = $this->getTestSharedFolder('folder-test@kolabnow.com');
+ $folder->setAliases(['test@kolabnow.com']);
+
+ $result = Utils::findObjectsByRecipientAddress('folder-test@kolabnow.com');
+
+ $this->assertCount(1, $result);
+ $this->assertSame($folder->email, $result[0]->email);
+
+ $result = Utils::findObjectsByRecipientAddress('test@kolabnow.com');
+
+ $this->assertCount(1, $result);
+ $this->assertSame($folder->email, $result[0]->email);
+
+ $folder->setAliases(['catchall@kolabnow.com']);
+
+ $result = Utils::findObjectsByRecipientAddress('unknown@kolabnow.com');
+
+ $this->assertCount(1, $result);
+ $this->assertSame($folder->email, $result[0]->email);
+
+ // Resources
+
+ $resource = $this->getTestResource('resource-test@kolabnow.com');
+
+ $result = Utils::findObjectsByRecipientAddress('resource-test@kolabnow.com');
+
+ $this->assertCount(1, $result);
+ $this->assertSame($resource->email, $result[0]->email);
+
+ // TODO: Test multiple entries result
+ }
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Apr 4, 8:47 PM (18 h, 41 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
18830548
Default Alt Text
D5376.1775335623.diff (9 KB)
Attached To
Mode
D5376: Include shared folders and resources when looking for a mail recipient
Attached
Detach File
Event Timeline