Changeset View
Changeset View
Standalone View
Standalone View
src/app/User.php
Show First 20 Lines • Show All 117 Lines • ▼ Show 20 Line(s) | |||||
* @return \App\User | * @return \App\User | ||||
*/ | */ | ||||
public function assignPackage($package, $user = null) | public function assignPackage($package, $user = null) | ||||
{ | { | ||||
if (!$user) { | if (!$user) { | ||||
$user = $this; | $user = $this; | ||||
} | } | ||||
$wallet_id = $this->wallets()->first()->id; | return $user->assignPackageAndWallet($package, $this->wallets()->first()); | ||||
foreach ($package->skus as $sku) { | |||||
for ($i = $sku->pivot->qty; $i > 0; $i--) { | |||||
\App\Entitlement::create( | |||||
[ | |||||
'wallet_id' => $wallet_id, | |||||
'sku_id' => $sku->id, | |||||
'cost' => $sku->pivot->cost(), | |||||
'fee' => $sku->pivot->fee(), | |||||
'entitleable_id' => $user->id, | |||||
'entitleable_type' => User::class | |||||
] | |||||
); | |||||
} | |||||
} | |||||
return $user; | |||||
} | } | ||||
/** | /** | ||||
* Assign a package plan to a user. | * Assign a package plan to a user. | ||||
* | * | ||||
* @param \App\Plan $plan The plan to assign | * @param \App\Plan $plan The plan to assign | ||||
* @param \App\Domain $domain Optional domain object | * @param \App\Domain $domain Optional domain object | ||||
* | * | ||||
Show All 10 Lines | |||||
$this->assignPackage($package); | $this->assignPackage($package); | ||||
} | } | ||||
} | } | ||||
return $this; | return $this; | ||||
} | } | ||||
/** | /** | ||||
* Assign a Sku to a user. | |||||
* | |||||
* @param \App\Sku $sku The sku to assign. | |||||
* @param int $count Count of entitlements to add | |||||
* | |||||
* @return \App\User Self | |||||
* @throws \Exception | |||||
*/ | |||||
public function assignSku(Sku $sku, int $count = 1): User | |||||
{ | |||||
// TODO: I guess wallet could be parametrized in future | |||||
$wallet = $this->wallet(); | |||||
$exists = $this->entitlements()->where('sku_id', $sku->id)->count(); | |||||
while ($count > 0) { | |||||
\App\Entitlement::create([ | |||||
'wallet_id' => $wallet->id, | |||||
'sku_id' => $sku->id, | |||||
'cost' => $exists >= $sku->units_free ? $sku->cost : 0, | |||||
'fee' => $exists >= $sku->units_free ? $sku->fee : 0, | |||||
'entitleable_id' => $this->id, | |||||
'entitleable_type' => User::class | |||||
]); | |||||
$exists++; | |||||
$count--; | |||||
} | |||||
return $this; | |||||
} | |||||
/** | |||||
* Check if current user can delete another object. | * Check if current user can delete another object. | ||||
* | * | ||||
* @param mixed $object A user|domain|wallet|group object | * @param mixed $object A user|domain|wallet|group object | ||||
* | * | ||||
* @return bool True if he can, False otherwise | * @return bool True if he can, False otherwise | ||||
*/ | */ | ||||
public function canDelete($object): bool | public function canDelete($object): bool | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 222 Lines • ▼ Show 20 Line(s) | |||||
return Group::select(['groups.*', 'entitlements.wallet_id']) | return Group::select(['groups.*', 'entitlements.wallet_id']) | ||||
->distinct() | ->distinct() | ||||
->join('entitlements', 'entitlements.entitleable_id', '=', 'groups.id') | ->join('entitlements', 'entitlements.entitleable_id', '=', 'groups.id') | ||||
->whereIn('entitlements.wallet_id', $wallets) | ->whereIn('entitlements.wallet_id', $wallets) | ||||
->where('entitlements.entitleable_type', Group::class); | ->where('entitlements.entitleable_type', Group::class); | ||||
} | } | ||||
/** | /** | ||||
* Check if user has an entitlement for the specified SKU. | |||||
* | |||||
* @param string $title The SKU title | |||||
* | |||||
* @return bool True if specified SKU entitlement exists | |||||
*/ | |||||
public function hasSku(string $title): bool | |||||
{ | |||||
$sku = Sku::withObjectTenantContext($this)->where('title', $title)->first(); | |||||
if (!$sku) { | |||||
return false; | |||||
} | |||||
return $this->entitlements()->where('sku_id', $sku->id)->count() > 0; | |||||
} | |||||
/** | |||||
* Returns whether this domain is active. | * Returns whether this domain is active. | ||||
* | * | ||||
* @return bool | * @return bool | ||||
*/ | */ | ||||
public function isActive(): bool | public function isActive(): bool | ||||
{ | { | ||||
return ($this->status & self::STATUS_ACTIVE) > 0; | return ($this->status & self::STATUS_ACTIVE) > 0; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 65 Lines • ▼ Show 20 Line(s) | |||||
if (empty($name) && $fallback) { | if (empty($name) && $fallback) { | ||||
return trim(\trans('app.siteuser', ['site' => \App\Tenant::getConfig($this->tenant_id, 'app.name')])); | return trim(\trans('app.siteuser', ['site' => \App\Tenant::getConfig($this->tenant_id, 'app.name')])); | ||||
} | } | ||||
return $name; | return $name; | ||||
} | } | ||||
/** | /** | ||||
* Remove a number of entitlements for the SKU. | |||||
* | |||||
* @param \App\Sku $sku The SKU | |||||
* @param int $count The number of entitlements to remove | |||||
* | |||||
* @return User Self | |||||
*/ | |||||
public function removeSku(Sku $sku, int $count = 1): User | |||||
{ | |||||
$entitlements = $this->entitlements() | |||||
->where('sku_id', $sku->id) | |||||
->orderBy('cost', 'desc') | |||||
->orderBy('created_at') | |||||
->get(); | |||||
$entitlements_count = count($entitlements); | |||||
foreach ($entitlements as $entitlement) { | |||||
if ($entitlements_count <= $sku->units_free) { | |||||
continue; | |||||
} | |||||
if ($count > 0) { | |||||
$entitlement->delete(); | |||||
$entitlements_count--; | |||||
$count--; | |||||
} | |||||
} | |||||
return $this; | |||||
} | |||||
/** | |||||
* Return resources controlled by the current user. | * Return resources controlled by the current user. | ||||
* | * | ||||
* @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) | ||||
▲ Show 20 Lines • Show All 330 Lines • Show Last 20 Lines |