Changeset View
Changeset View
Standalone View
Standalone View
src/app/User.php
<?php | <?php | ||||
namespace App; | namespace App; | ||||
use App\UserAlias; | use App\Traits\AliasesTrait; | ||||
use App\Traits\BelongsToTenantTrait; | use App\Traits\BelongsToTenantTrait; | ||||
use App\Traits\EntitleableTrait; | use App\Traits\EntitleableTrait; | ||||
use App\Traits\UserAliasesTrait; | use App\Traits\EmailPropertyTrait; | ||||
use App\Traits\UserConfigTrait; | use App\Traits\UserConfigTrait; | ||||
use App\Traits\UuidIntKeyTrait; | use App\Traits\UuidIntKeyTrait; | ||||
use App\Traits\SettingsTrait; | use App\Traits\SettingsTrait; | ||||
use App\Traits\StatusPropertyTrait; | use App\Traits\StatusPropertyTrait; | ||||
use App\Wallet; | |||||
use Illuminate\Database\Eloquent\SoftDeletes; | use Illuminate\Database\Eloquent\SoftDeletes; | ||||
use Illuminate\Support\Facades\DB; | use Illuminate\Support\Facades\DB; | ||||
use Illuminate\Support\Facades\Hash; | use Illuminate\Support\Facades\Hash; | ||||
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 Laravel\Passport\HasApiTokens; | use Laravel\Passport\HasApiTokens; | ||||
use League\OAuth2\Server\Exception\OAuthServerException; | use League\OAuth2\Server\Exception\OAuthServerException; | ||||
/** | /** | ||||
* The eloquent definition of a User. | * The eloquent definition of a User. | ||||
* | * | ||||
* @property string $email | * @property string $email | ||||
* @property int $id | * @property int $id | ||||
* @property string $password | * @property string $password | ||||
* @property string $password_ldap | * @property string $password_ldap | ||||
* @property int $status | * @property int $status | ||||
* @property int $tenant_id | * @property int $tenant_id | ||||
*/ | */ | ||||
class User extends Authenticatable | class User extends Authenticatable | ||||
{ | { | ||||
use AliasesTrait; | |||||
use BelongsToTenantTrait; | use BelongsToTenantTrait; | ||||
use EntitleableTrait; | use EntitleableTrait; | ||||
use EmailPropertyTrait; | |||||
use HasApiTokens; | use HasApiTokens; | ||||
use NullableFields; | use NullableFields; | ||||
use UserConfigTrait; | use UserConfigTrait; | ||||
use UserAliasesTrait; | |||||
use UuidIntKeyTrait; | use UuidIntKeyTrait; | ||||
use SettingsTrait; | use SettingsTrait; | ||||
use SoftDeletes; | use SoftDeletes; | ||||
use StatusPropertyTrait; | use StatusPropertyTrait; | ||||
// 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 | ||||
▲ Show 20 Lines • Show All 51 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 20 Lines • Show All 134 Lines • ▼ Show 20 Lines | public function degrade(): void | ||||
return; | return; | ||||
} | } | ||||
$this->status |= User::STATUS_DEGRADED; | $this->status |= User::STATUS_DEGRADED; | ||||
$this->save(); | $this->save(); | ||||
} | } | ||||
/** | /** | ||||
* Return the \App\Domain for this user. | |||||
* | |||||
* @return \App\Domain|null | |||||
*/ | |||||
public function domain() | |||||
{ | |||||
list($local, $domainName) = explode('@', $this->email); | |||||
$domain = \App\Domain::withTrashed()->where('namespace', $domainName)->first(); | |||||
return $domain; | |||||
} | |||||
/** | |||||
* List the domains to which this user is entitled. | * List the domains to which this user is entitled. | ||||
* | * | ||||
* @param bool $with_accounts Include domains assigned to wallets | * @param bool $with_accounts Include domains assigned to wallets | ||||
* the current user controls but not owns. | * the current user controls but not owns. | ||||
* @param bool $with_public Include active public domains (for the user tenant). | * @param bool $with_public Include active public domains (for the user tenant). | ||||
* | * | ||||
* @return \Illuminate\Database\Eloquent\Builder Query builder | * @return \Illuminate\Database\Eloquent\Builder Query builder | ||||
*/ | */ | ||||
Show All 13 Lines | public function domains($with_accounts = true, $with_public = true) | ||||
->whereRaw(sprintf('(domains.status & %s)', Domain::STATUS_ACTIVE)); | ->whereRaw(sprintf('(domains.status & %s)', Domain::STATUS_ACTIVE)); | ||||
}); | }); | ||||
} | } | ||||
return $domains; | return $domains; | ||||
} | } | ||||
/** | /** | ||||
* Find whether an email address exists as a user (including deleted users). | |||||
* | |||||
* @param string $email Email address | |||||
* @param bool $return_user Return User instance instead of boolean | |||||
* | |||||
* @return \App\User|bool True or User model object if found, False otherwise | |||||
*/ | |||||
public static function emailExists(string $email, bool $return_user = false) | |||||
{ | |||||
if (strpos($email, '@') === false) { | |||||
return false; | |||||
} | |||||
$email = \strtolower($email); | |||||
$user = self::withTrashed()->where('email', $email)->first(); | |||||
if ($user) { | |||||
return $return_user ? $user : true; | |||||
} | |||||
return false; | |||||
} | |||||
/** | |||||
* Return entitleable objects of a specified type controlled by the current user. | * Return entitleable objects of a specified type controlled by the current user. | ||||
* | * | ||||
* @param string $class Object class | * @param string $class Object class | ||||
* @param bool $with_accounts Include objects assigned to wallets | * @param bool $with_accounts Include objects assigned to wallets | ||||
* the current user controls, but not owns. | * the current user controls, but not owns. | ||||
* | * | ||||
* @return \Illuminate\Database\Eloquent\Builder Query builder | * @return \Illuminate\Database\Eloquent\Builder Query builder | ||||
*/ | */ | ||||
Show All 38 Lines | public static function findByEmail(string $email, bool $external = false): ?User | ||||
$email = \strtolower($email); | $email = \strtolower($email); | ||||
$user = self::where('email', $email)->first(); | $user = self::where('email', $email)->first(); | ||||
if ($user) { | if ($user) { | ||||
return $user; | return $user; | ||||
} | } | ||||
$aliases = UserAlias::where('alias', $email)->get(); | $aliases = \App\UserAlias::where('alias', $email)->get(); | ||||
if (count($aliases) == 1) { | if (count($aliases) == 1) { | ||||
return $aliases->first()->user; | return $aliases->first()->user; | ||||
} | } | ||||
// TODO: External email | // TODO: External email | ||||
return null; | return null; | ||||
▲ Show 20 Lines • Show All 361 Lines • Show Last 20 Lines |