Changeset View
Changeset View
Standalone View
Standalone View
src/app/User.php
<?php | <?php | ||||
namespace App; | namespace App; | ||||
use App\UserAlias; | |||||
use App\Traits\UserAliasesTrait; | |||||
use App\Traits\UserSettingsTrait; | |||||
use Illuminate\Notifications\Notifiable; | use Illuminate\Notifications\Notifiable; | ||||
use Illuminate\Contracts\Auth\MustVerifyEmail; | use Illuminate\Contracts\Auth\MustVerifyEmail; | ||||
use Illuminate\Database\Eloquent\SoftDeletes; | use Illuminate\Database\Eloquent\SoftDeletes; | ||||
use Illuminate\Foundation\Auth\User as Authenticatable; | use Illuminate\Foundation\Auth\User as Authenticatable; | ||||
use Iatstuti\Database\Support\NullableFields; | use Iatstuti\Database\Support\NullableFields; | ||||
use Tymon\JWTAuth\Contracts\JWTSubject; | use Tymon\JWTAuth\Contracts\JWTSubject; | ||||
use App\Traits\UserSettingsTrait; | |||||
/** | /** | ||||
* The eloquent definition of a User. | * The eloquent definition of a User. | ||||
* | * | ||||
* @property integer $id | * @property string $email | ||||
* @property integer $status | * @property int $id | ||||
* @property string $name | |||||
* @property string $password | |||||
* @property int $status | |||||
*/ | */ | ||||
class User extends Authenticatable implements JWTSubject | class User extends Authenticatable implements JWTSubject | ||||
{ | { | ||||
use Notifiable; | use Notifiable; | ||||
use NullableFields; | use NullableFields; | ||||
use UserAliasesTrait; | |||||
use UserSettingsTrait; | use UserSettingsTrait; | ||||
use SoftDeletes; | use SoftDeletes; | ||||
// a new user, default on creation | // a new user, default on creation | ||||
public const STATUS_NEW = 1 << 0; | public const STATUS_NEW = 1 << 0; | ||||
// it's been activated | // it's been activated | ||||
public const STATUS_ACTIVE = 1 << 1; | public const STATUS_ACTIVE = 1 << 1; | ||||
// user has been suspended | // user has been suspended | ||||
▲ Show 20 Lines • Show All 60 Lines • ▼ Show 20 Lines | public function accounts() | ||||
'App\Wallet', // The foreign object definition | 'App\Wallet', // The foreign object definition | ||||
'user_accounts', // The table name | 'user_accounts', // The table name | ||||
'user_id', // The local foreign key | 'user_id', // The local foreign key | ||||
'wallet_id' // The remote foreign key | 'wallet_id' // The remote foreign key | ||||
); | ); | ||||
} | } | ||||
/** | /** | ||||
* Email aliases of this user. | |||||
* | |||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany | |||||
*/ | |||||
public function aliases() | |||||
{ | |||||
return $this->hasMany('App\UserAlias', 'user_id'); | |||||
} | |||||
/** | |||||
* Assign a package to a user. The user should not have any existing entitlements. | * Assign a package to a user. The user should not have any existing entitlements. | ||||
* | * | ||||
* @param \App\Package $package The package to assign. | * @param \App\Package $package The package to assign. | ||||
* @param \App\User|null $user Assign the package to another user. | * @param \App\User|null $user Assign the package to another user. | ||||
* | * | ||||
* @return \App\User | * @return \App\User | ||||
*/ | */ | ||||
public function assignPackage($package, $user = null) | public function assignPackage($package, $user = null) | ||||
Show All 16 Lines | public function assignPackage($package, $user = null) | ||||
] | ] | ||||
); | ); | ||||
} | } | ||||
} | } | ||||
return $user; | return $user; | ||||
} | } | ||||
/** | |||||
* Returns user controlling the current user (or self when it's the account owner) | |||||
* | |||||
* @return \App\User A user object | |||||
*/ | |||||
public function controller(): User | |||||
{ | |||||
// FIXME: This is most likely not the best way to do this | |||||
$entitlement = \App\Entitlement::where([ | |||||
'entitleable_id' => $this->id, | |||||
'entitleable_type' => User::class | |||||
])->first(); | |||||
if ($entitlement && $entitlement->owner_id != $this->id) { | |||||
return $entitlement->owner; | |||||
} | |||||
return $this; | |||||
} | |||||
public function assignPlan($plan, $domain = null) | public function assignPlan($plan, $domain = null) | ||||
{ | { | ||||
$this->setSetting('plan_id', $plan->id); | $this->setSetting('plan_id', $plan->id); | ||||
foreach ($plan->packages as $package) { | foreach ($plan->packages as $package) { | ||||
if ($package->isDomain()) { | if ($package->isDomain()) { | ||||
$domain->assignPackage($package, $this); | $domain->assignPackage($package, $this); | ||||
} else { | } else { | ||||
▲ Show 20 Lines • Show All 72 Lines • ▼ Show 20 Lines | class User extends Authenticatable implements JWTSubject | ||||
} | } | ||||
/** | /** | ||||
* Helper to find user by email address, whether it is | * Helper to find user by email address, whether it is | ||||
* main email address, alias or external email | * main email address, alias or external email | ||||
* | * | ||||
* @param string $email Email address | * @param string $email Email address | ||||
* | * | ||||
* @return \App\User User model object | * @return \App\User User model object if found | ||||
*/ | */ | ||||
public static function findByEmail(string $email): ?User | public static function findByEmail(string $email): ?User | ||||
{ | { | ||||
if (strpos($email, '@') === false) { | if (strpos($email, '@') === false) { | ||||
return null; | return null; | ||||
} | } | ||||
$user = self::where('email', $email)->first(); | $email = \strtolower($email); | ||||
// TODO: Aliases, External email | $user = self::where('email', $email)->first(); | ||||
if ($user) { | |||||
return $user; | return $user; | ||||
} | } | ||||
$alias = UserAlias::where('alias', $email)->first(); | |||||
if ($alias) { | |||||
return $alias->user; | |||||
} | |||||
// TODO: External email | |||||
return null; | |||||
} | |||||
public function getJWTIdentifier() | public function getJWTIdentifier() | ||||
{ | { | ||||
return $this->getKey(); | return $this->getKey(); | ||||
} | } | ||||
public function getJWTCustomClaims() | public function getJWTCustomClaims() | ||||
{ | { | ||||
return []; | return []; | ||||
▲ Show 20 Lines • Show All 159 Lines • Show Last 20 Lines |