Page MenuHomePhorge

D5397.1775285822.diff
No OneTemporary

Authored By
Unknown
Size
4 KB
Referenced Files
None
Subscribers
None

D5397.1775285822.diff

diff --git a/src/app/Domain.php b/src/app/Domain.php
--- a/src/app/Domain.php
+++ b/src/app/Domain.php
@@ -318,7 +318,6 @@
/**
* List the users of a domain, so long as the domain is not a public registration domain.
- * Note: It returns only users with a mailbox.
*
* @return Collection<User> A collection of users
*/
@@ -334,22 +333,13 @@
return collect([]);
}
- $mailboxSKU = Sku::withObjectTenantContext($this)->where('title', 'mailbox')->first();
-
- if (!$mailboxSKU) {
- \Log::error("No mailbox SKU available.");
- return collect([]);
- }
-
- return User::select()
- ->whereExists(static function ($query) use ($wallet, $mailboxSKU) {
- $query->select(DB::raw(1))
- ->from('entitlements')
- ->whereColumn('entitleable_id', 'users.id')
- ->where('entitlements.wallet_id', $wallet->id)
- ->where('entitlements.entitleable_type', User::class)
- ->where('entitlements.sku_id', $mailboxSKU->id);
- })
+ return User::whereIn('id', static function ($query) use ($wallet) {
+ $query->from('entitlements')
+ ->select('entitleable_id')
+ ->where('wallet_id', $wallet->id)
+ ->where('entitleable_type', User::class)
+ ->whereNull('deleted_at');
+ })
->get();
}
diff --git a/src/app/Traits/EntitleableTrait.php b/src/app/Traits/EntitleableTrait.php
--- a/src/app/Traits/EntitleableTrait.php
+++ b/src/app/Traits/EntitleableTrait.php
@@ -11,6 +11,7 @@
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Relations\HasMany;
+use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Str;
/**
@@ -321,19 +322,30 @@
*/
public function wallet(): ?Wallet
{
- $entitlement = $this->entitlements()->withTrashed()->orderBy('created_at', 'desc')->first();
-
- if ($entitlement) {
- return $entitlement->wallet;
- }
-
- // TODO: No entitlement should not happen, but in tests we have
- // such cases, so we fallback to the user's wallet in this case
- if ($this instanceof User) {
- return $this->wallets()->first();
+ // Note: Using $this->entitlements() here results in more complicated query
+ $entitlement = Entitlement::withTrashed()
+ ->where('entitleable_id', $this->id)
+ ->where('entitleable_type', self::class)
+ ->orderByDesc('created_at')
+ ->limit(1);
+
+ // Note: We use joinSub() because whereIn() does not allow LIMIT in the subquery
+ // @phpstan-ignore-next-line
+ $wallet = Wallet::select('wallets.*')
+ ->joinSub($entitlement, 'e', static function (JoinClause $join) {
+ $join->on('wallets.id', '=', 'e.wallet_id');
+ })
+ ->first();
+
+ // A new user account does not have entitlements. Without this fallback to
+ // the user wallet e.g. assignSku() will fail. It is not a problem for
+ // assignPackage() so typical account creation works w/o this. Some tests fail, though.
+ // TODO: In future we should throw an exception here instead.
+ if (!$wallet && $this instanceof User) {
+ $wallet = $this->wallets()->first();
}
- return null;
+ return $wallet;
}
/**
diff --git a/src/tests/Feature/UserTest.php b/src/tests/Feature/UserTest.php
--- a/src/tests/Feature/UserTest.php
+++ b/src/tests/Feature/UserTest.php
@@ -1693,20 +1693,24 @@
}
/**
- * Tests for User::wallets()
+ * Tests for User::wallet() and User::wallets()
*/
public function testWallets(): void
{
$john = $this->getTestUser('john@kolab.org');
$ned = $this->getTestUser('ned@kolab.org');
+ $account_wallet = $john->wallets->first();
$this->assertSame(1, $john->wallets()->count());
$this->assertCount(1, $john->wallets);
- $this->assertInstanceOf(Wallet::class, $john->wallets->first());
+ $this->assertInstanceOf(Wallet::class, $account_wallet);
$this->assertSame(1, $ned->wallets()->count());
$this->assertCount(1, $ned->wallets);
$this->assertInstanceOf(Wallet::class, $ned->wallets->first());
+
+ $this->assertSame($account_wallet->id, $john->wallet()->id);
+ $this->assertSame($account_wallet->id, $ned->wallet()->id);
}
/**

File Metadata

Mime Type
text/plain
Expires
Sat, Apr 4, 6:57 AM (9 h, 52 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
18827230
Default Alt Text
D5397.1775285822.diff (4 KB)

Event Timeline