Changeset View
Changeset View
Standalone View
Standalone View
src/app/User.php
<?php | <?php | ||||
namespace App; | namespace App; | ||||
use App\Entitlement; | |||||
use App\UserAlias; | use App\UserAlias; | ||||
use App\Sku; | |||||
use App\Traits\BelongsToTenantTrait; | use App\Traits\BelongsToTenantTrait; | ||||
use App\Traits\EntitleableTrait; | use App\Traits\EntitleableTrait; | ||||
use App\Traits\UserAliasesTrait; | use App\Traits\UserAliasesTrait; | ||||
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\Wallet; | use App\Wallet; | ||||
use Illuminate\Database\Eloquent\SoftDeletes; | use Illuminate\Database\Eloquent\SoftDeletes; | ||||
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. | ||||
▲ Show 20 Lines • Show All 236 Lines • ▼ Show 20 Line(s) | |||||
/** | /** | ||||
* 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 Domain[] List of Domain objects | * @return \Illuminate\Database\Eloquent\Builder Query builder | ||||
*/ | */ | ||||
public function domains($with_accounts = true, $with_public = true): array | public function domains($with_accounts = true, $with_public = true) | ||||
{ | { | ||||
$domains = []; | $domains = $this->entitleables(Domain::class, $with_accounts); | ||||
if ($with_public) { | if ($with_public) { | ||||
if ($this->tenant_id) { | $domains->orWhere(function ($query) { | ||||
$domains = Domain::where('tenant_id', $this->tenant_id); | if (!$this->tenant_id) { | ||||
$query->where('tenant_id', $this->tenant_id); | |||||
} else { | } else { | ||||
$domains = Domain::withEnvTenantContext(); | $query->withEnvTenantContext(); | ||||
} | |||||
$domains = $domains->whereRaw(sprintf('(type & %s)', Domain::TYPE_PUBLIC)) | |||||
->whereRaw(sprintf('(status & %s)', Domain::STATUS_ACTIVE)) | |||||
->get() | |||||
->all(); | |||||
} | } | ||||
foreach ($this->wallets as $wallet) { | $query->whereRaw(sprintf('(domains.type & %s)', Domain::TYPE_PUBLIC)) | ||||
$entitlements = $wallet->entitlements()->where('entitleable_type', Domain::class)->get(); | ->whereRaw(sprintf('(domains.status & %s)', Domain::STATUS_ACTIVE)); | ||||
foreach ($entitlements as $entitlement) { | }); | ||||
$domains[] = $entitlement->entitleable; | |||||
} | |||||
} | |||||
if ($with_accounts) { | |||||
foreach ($this->accounts as $wallet) { | |||||
$entitlements = $wallet->entitlements()->where('entitleable_type', Domain::class)->get(); | |||||
foreach ($entitlements as $entitlement) { | |||||
$domains[] = $entitlement->entitleable; | |||||
} | |||||
} | |||||
} | } | ||||
return $domains; | return $domains; | ||||
} | } | ||||
/** | /** | ||||
* Find whether an email address exists as a user (including deleted users). | * Find whether an email address exists as a user (including deleted users). | ||||
* | * | ||||
Show All 15 Lines | |||||
if ($user) { | if ($user) { | ||||
return $return_user ? $user : true; | return $return_user ? $user : true; | ||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
/** | /** | ||||
* Return entitleable objects of a specified type controlled by the current user. | |||||
* | |||||
* @param string $class Object class | |||||
* @param bool $with_accounts Include objects assigned to wallets | |||||
* the current user controls, but not owns. | |||||
* | |||||
* @return \Illuminate\Database\Eloquent\Builder Query builder | |||||
*/ | |||||
private function entitleables(string $class, bool $with_accounts = true) | |||||
{ | |||||
$wallets = $this->wallets()->pluck('id')->all(); | |||||
if ($with_accounts) { | |||||
$wallets = array_merge($wallets, $this->accounts()->pluck('wallet_id')->all()); | |||||
} | |||||
$object = new $class(); | |||||
$table = $object->getTable(); | |||||
return $object->select("{$table}.*") | |||||
->whereExists(function ($query) use ($table, $wallets, $class) { | |||||
$query->select(DB::raw(1)) | |||||
->from('entitlements') | |||||
->whereColumn('entitleable_id', "{$table}.id") | |||||
->whereIn('entitlements.wallet_id', $wallets) | |||||
->where('entitlements.entitleable_type', $class); | |||||
}); | |||||
} | |||||
/** | |||||
* 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 an external email. | * main email address, alias or an external email. | ||||
* | * | ||||
* If there's more than one alias NULL will be returned. | * If there's more than one alias NULL will be returned. | ||||
* | * | ||||
* @param string $email Email address | * @param string $email Email address | ||||
* @param bool $external Search also for an external email | * @param bool $external Search also for an external email | ||||
* | * | ||||
Show All 29 Lines | |||||
* | * | ||||
* @param bool $with_accounts Include groups assigned to wallets | * @param bool $with_accounts Include groups 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 | ||||
*/ | */ | ||||
public function groups($with_accounts = true) | public function groups($with_accounts = true) | ||||
{ | { | ||||
$wallets = $this->wallets()->pluck('id')->all(); | return $this->entitleables(Group::class, $with_accounts); | ||||
if ($with_accounts) { | |||||
$wallets = array_merge($wallets, $this->accounts()->pluck('wallet_id')->all()); | |||||
} | |||||
return Group::select(['groups.*', 'entitlements.wallet_id']) | |||||
->distinct() | |||||
->join('entitlements', 'entitlements.entitleable_id', '=', 'groups.id') | |||||
->whereIn('entitlements.wallet_id', $wallets) | |||||
->where('entitlements.entitleable_type', Group::class); | |||||
} | } | ||||
/** | /** | ||||
* Returns whether this domain is active. | * Returns whether this domain is active. | ||||
* | * | ||||
* @return bool | * @return bool | ||||
*/ | */ | ||||
public function isActive(): bool | public function isActive(): bool | ||||
▲ Show 20 Lines • Show All 77 Lines • ▼ Show 20 Line(s) | |||||
* | * | ||||
* @param bool $with_accounts Include resources assigned to wallets | * @param bool $with_accounts Include resources 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 | ||||
*/ | */ | ||||
public function resources($with_accounts = true) | public function resources($with_accounts = true) | ||||
{ | { | ||||
$wallets = $this->wallets()->pluck('id')->all(); | return $this->entitleables(\App\Resource::class, $with_accounts); | ||||
if ($with_accounts) { | |||||
$wallets = array_merge($wallets, $this->accounts()->pluck('wallet_id')->all()); | |||||
} | |||||
return \App\Resource::select(['resources.*', 'entitlements.wallet_id']) | |||||
->distinct() | |||||
->join('entitlements', 'entitlements.entitleable_id', '=', 'resources.id') | |||||
->whereIn('entitlements.wallet_id', $wallets) | |||||
->where('entitlements.entitleable_type', \App\Resource::class); | |||||
} | } | ||||
/** | /** | ||||
* Return shared folders controlled by the current user. | * Return shared folders controlled by the current user. | ||||
* | * | ||||
* @param bool $with_accounts Include folders assigned to wallets | * @param bool $with_accounts Include folders 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 | ||||
*/ | */ | ||||
public function sharedFolders($with_accounts = true) | public function sharedFolders($with_accounts = true) | ||||
{ | { | ||||
$wallets = $this->wallets()->pluck('id')->all(); | return $this->entitleables(\App\SharedFolder::class, $with_accounts); | ||||
if ($with_accounts) { | |||||
$wallets = array_merge($wallets, $this->accounts()->pluck('wallet_id')->all()); | |||||
} | |||||
return \App\SharedFolder::select(['shared_folders.*', 'entitlements.wallet_id']) | |||||
->distinct() | |||||
->join('entitlements', 'entitlements.entitleable_id', '=', 'shared_folders.id') | |||||
->whereIn('entitlements.wallet_id', $wallets) | |||||
->where('entitlements.entitleable_type', \App\SharedFolder::class); | |||||
} | } | ||||
public function senderPolicyFrameworkWhitelist($clientName) | public function senderPolicyFrameworkWhitelist($clientName) | ||||
{ | { | ||||
$setting = $this->getSetting('spf_whitelist'); | $setting = $this->getSetting('spf_whitelist'); | ||||
if (!$setting) { | if (!$setting) { | ||||
return false; | return false; | ||||
▲ Show 20 Lines • Show All 66 Lines • ▼ Show 20 Line(s) | |||||
* | * | ||||
* @param bool $with_accounts Include users assigned to wallets | * @param bool $with_accounts Include users 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 | ||||
*/ | */ | ||||
public function users($with_accounts = true) | public function users($with_accounts = true) | ||||
{ | { | ||||
$wallets = $this->wallets()->pluck('id')->all(); | return $this->entitleables(User::class, $with_accounts); | ||||
if ($with_accounts) { | |||||
$wallets = array_merge($wallets, $this->accounts()->pluck('wallet_id')->all()); | |||||
} | |||||
return $this->select(['users.*', 'entitlements.wallet_id']) | |||||
->distinct() | |||||
->leftJoin('entitlements', 'entitlements.entitleable_id', '=', 'users.id') | |||||
->whereIn('entitlements.wallet_id', $wallets) | |||||
->where('entitlements.entitleable_type', User::class); | |||||
} | } | ||||
/** | /** | ||||
* Verification codes for this user. | * Verification codes for this user. | ||||
* | * | ||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany | * @return \Illuminate\Database\Eloquent\Relations\HasMany | ||||
*/ | */ | ||||
public function verificationcodes() | public function verificationcodes() | ||||
▲ Show 20 Lines • Show All 194 Lines • Show Last 20 Lines |