Changeset View
Changeset View
Standalone View
Standalone View
src/app/User.php
Show First 20 Lines • Show All 275 Lines • ▼ Show 20 Lines | class User extends Authenticatable implements JWTSubject | ||||
/** | /** | ||||
* List the domains to which this user is entitled. | * List the domains to which this user is entitled. | ||||
* | * | ||||
* @return Domain[] | * @return Domain[] | ||||
*/ | */ | ||||
public function domains() | public function domains() | ||||
{ | { | ||||
$dbdomains = Domain::whereRaw( | $domains = Domain::whereRaw(sprintf('(type & %s)', Domain::TYPE_PUBLIC)) | ||||
sprintf( | ->whereRaw(sprintf('(status & %s)', Domain::STATUS_ACTIVE)) | ||||
'(type & %s) AND (status & %s)', | ->get() | ||||
Domain::TYPE_PUBLIC, | ->all(); | ||||
Domain::STATUS_ACTIVE | |||||
) | |||||
)->get(); | |||||
$domains = []; | |||||
foreach ($dbdomains as $dbdomain) { | |||||
$domains[] = $dbdomain; | |||||
} | |||||
foreach ($this->wallets as $wallet) { | foreach ($this->wallets as $wallet) { | ||||
$entitlements = $wallet->entitlements()->where('entitleable_type', Domain::class)->get(); | $entitlements = $wallet->entitlements()->where('entitleable_type', Domain::class)->get(); | ||||
foreach ($entitlements as $entitlement) { | foreach ($entitlements as $entitlement) { | ||||
$domain = $entitlement->entitleable; | $domains[] = $entitlement->entitleable; | ||||
\Log::info("Found domain for {$this->email}: {$domain->namespace} (owned)"); | |||||
$domains[] = $domain; | |||||
} | } | ||||
} | } | ||||
foreach ($this->accounts as $wallet) { | foreach ($this->accounts as $wallet) { | ||||
$entitlements = $wallet->entitlements()->where('entitleable_type', Domain::class)->get(); | $entitlements = $wallet->entitlements()->where('entitleable_type', Domain::class)->get(); | ||||
foreach ($entitlements as $entitlement) { | foreach ($entitlements as $entitlement) { | ||||
$domain = $entitlement->entitleable; | $domains[] = $entitlement->entitleable; | ||||
\Log::info("Found domain {$this->email}: {$domain->namespace} (charged)"); | |||||
$domains[] = $domain; | |||||
} | } | ||||
} | } | ||||
return $domains; | return $domains; | ||||
} | } | ||||
/** | /** | ||||
* The user entitlement. | * The user entitlement. | ||||
▲ Show 20 Lines • Show All 88 Lines • ▼ Show 20 Lines | class User extends Authenticatable implements JWTSubject | ||||
public function getJWTCustomClaims() | public function getJWTCustomClaims() | ||||
{ | { | ||||
return []; | return []; | ||||
} | } | ||||
/** | /** | ||||
* Return groups controlled by the current user. | * Return groups controlled by the current user. | ||||
* | * | ||||
* @param bool $with_accounts Include groups assigned to wallets | |||||
* the current user controls but not owns. | |||||
* | |||||
* @return \Illuminate\Database\Eloquent\Builder Query builder | * @return \Illuminate\Database\Eloquent\Builder Query builder | ||||
*/ | */ | ||||
public function groups() | public function groups($with_accounts = true) | ||||
{ | { | ||||
$wallets = $this->wallets()->pluck('id')->all(); | $wallets = $this->wallets()->pluck('id')->all(); | ||||
$groupIds = \App\Entitlement::whereIn('entitlements.wallet_id', $wallets) | if ($with_accounts) { | ||||
->where('entitlements.entitleable_type', Group::class) | $wallets = array_merge($wallets, $this->accounts()->pluck('wallet_id')->all()); | ||||
->pluck('entitleable_id') | } | ||||
->all(); | |||||
return Group::whereIn('id', $groupIds); | 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); | |||||
} | } | ||||
/** | /** | ||||
* Check if user has an entitlement for the specified SKU. | * Check if user has an entitlement for the specified SKU. | ||||
* | * | ||||
* @param string $title The SKU title | * @param string $title The SKU title | ||||
* | * | ||||
* @return bool True if specified SKU entitlement exists | * @return bool True if specified SKU entitlement exists | ||||
▲ Show 20 Lines • Show All 285 Lines • Show Last 20 Lines |