diff --git a/src/app/User.php b/src/app/User.php index 43cc8912..0ee328e9 100644 --- a/src/app/User.php +++ b/src/app/User.php @@ -1,206 +1,207 @@ 'datetime', ]; /** * Any wallets on which this user is a controller. * * @return Wallet[] */ public function accounts() { return $this->belongsToMany( 'App\Wallet', // The foreign object definition 'user_accounts', // The table name 'user_id', // The local foreign key 'wallet_id' // The remote foreign key ); } /** * List the domains to which this user is entitled. * * @return Domain[] */ public function domains() { $domains = Domain::whereRaw( sprintf( '(type & %s) AND (status & %s)', Domain::TYPE_PUBLIC, Domain::STATUS_ACTIVE ) )->get(); foreach ($this->entitlements()->get() as $entitlement) { if ($entitlement->entitleable instanceof Domain) { $domain = Domain::find($entitlement->entitleable_id); \Log::info("Found domain {$domain->namespace}"); $domains[] = $domain; } } foreach ($this->accounts()->get() as $wallet) { foreach ($wallet->entitlements()->get() as $entitlement) { if ($entitlement->entitleable instanceof Domain) { $domain = Domain::find($entitlement->entitleable_id); \Log::info("Found domain {$domain->namespace}"); $domains[] = $domain; } } } return $domains; } public function entitlement() { return $this->morphOne('App\Entitlement', 'entitleable'); } /** * Entitlements for this user. * * @return Entitlement[] */ public function entitlements() { return $this->hasMany('App\Entitlement', 'owner_id', 'id'); } public function addEntitlement($entitlement) { + // FIXME: This contains() check looks fishy if (!$this->entitlements()->get()->contains($entitlement)) { return $this->entitlements()->save($entitlement); } } /** * Helper to find user by email address, whether it is * main email address, alias or external email * * @param string $email Email address * * @return \App\User User model object */ public static function findByEmail(string $email) { if (strpos($email, '@') === false) { return; } $user = self::where('email', $email)->first(); // TODO: Aliases, External email return $user; } public function settings() { return $this->hasMany('App\UserSetting', 'user_id'); } /** * Verification codes for this user. * * @return VerificationCode[] */ public function verificationcodes() { return $this->hasMany('App\VerificationCode', 'user_id', 'id'); } /** * Wallets this user owns. * * @return Wallet[] */ public function wallets() { return $this->hasMany('App\Wallet'); } public function getJWTIdentifier() { return $this->getKey(); } public function getJWTCustomClaims() { return []; } public function setPasswordAttribute($password) { if (!empty($password)) { $this->attributes['password'] = bcrypt($password, [ "rounds" => 12 ]); $this->attributes['password_ldap'] = '{SSHA512}' . base64_encode( pack('H*', hash('sha512', $password)) ); } } public function setPasswordLdapAttribute($password) { if (!empty($password)) { $this->attributes['password'] = bcrypt($password, [ "rounds" => 12 ]); $this->attributes['password_ldap'] = '{SSHA512}' . base64_encode( pack('H*', hash('sha512', $password)) ); } } } diff --git a/src/tests/Feature/EntitlementTest.php b/src/tests/Feature/EntitlementTest.php index 0eb56532..2b4bc164 100644 --- a/src/tests/Feature/EntitlementTest.php +++ b/src/tests/Feature/EntitlementTest.php @@ -1,113 +1,90 @@ 'entitlement-test@kolabnow.com'] - ); - - $user = User::firstOrCreate( - ['email' => 'entitled-user@custom-domain.com'] - ); - - $entitlement = Entitlement::firstOrCreate( - [ - 'owner_id' => $owner->id, - 'user_id' => $user->id - ] - ); - - $entitlement->delete(); - $user->delete(); - $owner->delete(); + User::where('email', 'entitlement-test@kolabnow.com') + ->orWhere('email', 'entitled-user@custom-domain.com') + ->delete(); } - public function testUserAddEntitlement() + /** + * Tests for User::AddEntitlement() + */ + public function testUserAddEntitlement(): void { - $sku_domain = Sku::firstOrCreate( - ['title' => 'domain'] - ); - - $sku_mailbox = Sku::firstOrCreate( - ['title' => 'mailbox'] - ); - - $owner = User::firstOrCreate( - ['email' => 'entitlement-test@kolabnow.com'] - ); - - $user = User::firstOrCreate( - ['email' => 'entitled-user@custom-domain.com'] - ); + $sku_domain = Sku::firstOrCreate(['title' => 'domain']); + $sku_mailbox = Sku::firstOrCreate(['title' => 'mailbox']); + $owner = User::firstOrCreate(['email' => 'entitlement-test@kolabnow.com']); + $user = User::firstOrCreate(['email' => 'entitled-user@custom-domain.com']); $this->assertTrue($owner->id != $user->id); $wallets = $owner->wallets()->get(); $domain = Domain::firstOrCreate( [ 'namespace' => 'custom-domain.com', 'status' => Domain::STATUS_NEW, 'type' => Domain::TYPE_EXTERNAL, ] ); - $entitlement_own_mailbox = Entitlement::firstOrCreate( + $entitlement_own_mailbox = new Entitlement( [ 'owner_id' => $owner->id, 'entitleable_id' => $owner->id, 'entitleable_type' => User::class, 'wallet_id' => $wallets[0]->id, 'sku_id' => $sku_mailbox->id, 'description' => "Owner Mailbox Entitlement Test" ] ); - $entitlement_domain = Entitlement::firstOrCreate( + $entitlement_domain = new Entitlement( [ 'owner_id' => $owner->id, 'entitleable_id' => $domain->id, 'entitleable_type' => Domain::class, 'wallet_id' => $wallets[0]->id, 'sku_id' => $sku_domain->id, 'description' => "User Domain Entitlement Test" ] ); - $entitlement_mailbox = Entitlement::firstOrCreate( + $entitlement_mailbox = new Entitlement( [ 'owner_id' => $owner->id, 'entitleable_id' => $user->id, 'entitleable_type' => User::class, 'wallet_id' => $wallets[0]->id, 'sku_id' => $sku_mailbox->id, 'description' => "User Mailbox Entitlement Test" ] ); $owner->addEntitlement($entitlement_own_mailbox); $owner->addEntitlement($entitlement_domain); $owner->addEntitlement($entitlement_mailbox); $this->assertTrue($owner->entitlements()->count() == 3); - $this->assertTrue($sku_domain->entitlements()->count() == 2); - $this->assertTrue($sku_mailbox->entitlements()->count() == 3); + $this->assertTrue($sku_domain->entitlements()->where('owner_id', $owner->id)->count() == 1); + $this->assertTrue($sku_mailbox->entitlements()->where('owner_id', $owner->id)->count() == 2); $this->assertTrue($wallets[0]->entitlements()->count() == 3); $this->assertTrue($wallets[0]->fresh()->balance < 0.00); + + // TODO: Test case of adding entitlement that already exists } }