diff --git a/src/app/User.php b/src/app/User.php index 0ee328e9..06e207fe 100644 --- a/src/app/User.php +++ b/src/app/User.php @@ -1,207 +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) + public static function findByEmail(string $email): ?User { if (strpos($email, '@') === false) { - return; + return null; } $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/UserTest.php b/src/tests/Feature/UserTest.php index 791b05c7..fe754e69 100644 --- a/src/tests/Feature/UserTest.php +++ b/src/tests/Feature/UserTest.php @@ -1,70 +1,63 @@ 'UserAccountA@UserAccount.com' - ] - ); + $userA = User::firstOrCreate(['email' => 'UserAccountA@UserAccount.com']); $this->assertTrue($userA->wallets()->count() == 1); $userA->wallets()->each( function ($wallet) { - $userB = User::firstOrCreate( - [ - 'email' => 'UserAccountB@UserAccount.com' - ] - ); + $userB = User::firstOrCreate(['email' => 'UserAccountB@UserAccount.com']); $wallet->addController($userB); } ); - $userB = User::firstOrCreate( - [ - 'email' => 'UserAccountB@UserAccount.com' - ] - ); + $userB = User::firstOrCreate(['email' => 'UserAccountB@UserAccount.com']); $this->assertTrue($userB->accounts()->get()[0]->id === $userA->wallets()->get()[0]->id); } - public function testUserDomains() + public function testUserDomains(): void { - $user = User::firstOrCreate( - [ - 'email' => 'john@kolab.org' - ] - ); + $user = User::firstOrCreate(['email' => 'john@kolab.org']); $domains = []; foreach ($user->domains() as $domain) { $domains[] = $domain->namespace; } $this->assertTrue(in_array('kolabnow.com', $domains)); $this->assertTrue(in_array('kolab.org', $domains)); } - public function testFindByEmail() + /** + * Tests for User::findByEmail() + */ + public function testFindByEmail(): void { - $this->markTestIncomplete('TODO'); + $user = User::firstOrCreate(['email' => 'john@kolab.org']); + + $result = User::findByEmail('john'); + $this->assertNull($result); + + $result = User::findByEmail('john@kolab.org'); + $this->assertInstanceOf(User::class, $result); + $this->assertSame($user->id, $result->id); + + // TODO: Make sure searching is case-insensitive + // TODO: Alias, eternal email } }