Changeset View
Changeset View
Standalone View
Standalone View
src/app/User.php
Show All 14 Lines | |||||
/** | /** | ||||
* 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 int $status | * @property int $status | ||||
* @property int $tenant_id | |||||
*/ | */ | ||||
class User extends Authenticatable implements JWTSubject | class User extends Authenticatable implements JWTSubject | ||||
{ | { | ||||
use NullableFields; | use NullableFields; | ||||
use UserAliasesTrait; | use UserAliasesTrait; | ||||
use SettingsTrait; | use SettingsTrait; | ||||
use SoftDeletes; | use SoftDeletes; | ||||
Show All 20 Lines | class User extends Authenticatable implements JWTSubject | ||||
* | * | ||||
* @var array | * @var array | ||||
*/ | */ | ||||
protected $fillable = [ | protected $fillable = [ | ||||
'id', | 'id', | ||||
'email', | 'email', | ||||
'password', | 'password', | ||||
'password_ldap', | 'password_ldap', | ||||
'status' | 'status', | ||||
]; | ]; | ||||
/** | /** | ||||
* The attributes that should be hidden for arrays. | * The attributes that should be hidden for arrays. | ||||
* | * | ||||
* @var array | * @var array | ||||
*/ | */ | ||||
protected $hidden = [ | protected $hidden = [ | ||||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | public function assignPackage($package, $user = null) | ||||
foreach ($package->skus as $sku) { | foreach ($package->skus as $sku) { | ||||
for ($i = $sku->pivot->qty; $i > 0; $i--) { | for ($i = $sku->pivot->qty; $i > 0; $i--) { | ||||
\App\Entitlement::create( | \App\Entitlement::create( | ||||
[ | [ | ||||
'wallet_id' => $wallet_id, | 'wallet_id' => $wallet_id, | ||||
'sku_id' => $sku->id, | 'sku_id' => $sku->id, | ||||
'cost' => $sku->pivot->cost(), | 'cost' => $sku->pivot->cost(), | ||||
'fee' => $sku->pivot->fee(), | |||||
'entitleable_id' => $user->id, | 'entitleable_id' => $user->id, | ||||
'entitleable_type' => User::class | 'entitleable_type' => User::class | ||||
] | ] | ||||
); | ); | ||||
} | } | ||||
} | } | ||||
return $user; | return $user; | ||||
Show All 37 Lines | public function assignSku(Sku $sku, int $count = 1): User | ||||
$wallet = $this->wallet(); | $wallet = $this->wallet(); | ||||
$exists = $this->entitlements()->where('sku_id', $sku->id)->count(); | $exists = $this->entitlements()->where('sku_id', $sku->id)->count(); | ||||
while ($count > 0) { | while ($count > 0) { | ||||
\App\Entitlement::create([ | \App\Entitlement::create([ | ||||
'wallet_id' => $wallet->id, | 'wallet_id' => $wallet->id, | ||||
'sku_id' => $sku->id, | 'sku_id' => $sku->id, | ||||
'cost' => $exists >= $sku->units_free ? $sku->cost : 0, | 'cost' => $exists >= $sku->units_free ? $sku->cost : 0, | ||||
'fee' => $exists >= $sku->units_free ? $sku->fee : 0, | |||||
'entitleable_id' => $this->id, | 'entitleable_id' => $this->id, | ||||
'entitleable_type' => User::class | 'entitleable_type' => User::class | ||||
]); | ]); | ||||
$exists++; | $exists++; | ||||
$count--; | $count--; | ||||
} | } | ||||
return $this; | return $this; | ||||
} | } | ||||
/** | /** | ||||
* Check if current user can delete another object. | * Check if current user can delete another object. | ||||
* | * | ||||
* @param \App\User|\App\Domain $object A user|domain 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 | ||||
{ | { | ||||
if (!method_exists($object, 'wallet')) { | if (!method_exists($object, 'wallet')) { | ||||
return false; | return false; | ||||
} | } | ||||
$wallet = $object->wallet(); | $wallet = $object->wallet(); | ||||
// TODO: For now controller can delete/update the account owner, | // TODO: For now controller can delete/update the account owner, | ||||
// this may change in future, controllers are not 0-regression feature | // this may change in future, controllers are not 0-regression feature | ||||
return $this->wallets->contains($wallet) || $this->accounts->contains($wallet); | return $this->wallets->contains($wallet) || $this->accounts->contains($wallet); | ||||
} | } | ||||
/** | /** | ||||
* Check if current user can read data of another object. | * Check if current user can read data of another object. | ||||
* | * | ||||
* @param \App\User|\App\Domain|\App\Wallet $object A user|domain|wallet 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 canRead($object): bool | public function canRead($object): bool | ||||
{ | { | ||||
if ($this->role == "admin") { | if ($this->role == 'admin') { | ||||
return true; | return true; | ||||
} | } | ||||
if ($object instanceof User && $this->id == $object->id) { | if ($object instanceof User && $this->id == $object->id) { | ||||
return true; | return true; | ||||
} | } | ||||
if ($this->role == 'reseller') { | |||||
if ($object instanceof User && $object->role == 'admin') { | |||||
return false; | |||||
} | |||||
if ($object instanceof Wallet && !empty($object->owner)) { | |||||
$object = $object->owner; | |||||
} | |||||
return isset($object->tenant_id) && $object->tenant_id == $this->tenant_id; | |||||
} | |||||
if ($object instanceof Wallet) { | if ($object instanceof Wallet) { | ||||
return $object->user_id == $this->id || $object->controllers->contains($this); | return $object->user_id == $this->id || $object->controllers->contains($this); | ||||
} | } | ||||
if (!method_exists($object, 'wallet')) { | if (!method_exists($object, 'wallet')) { | ||||
return false; | return false; | ||||
} | } | ||||
$wallet = $object->wallet(); | $wallet = $object->wallet(); | ||||
return $this->wallets->contains($wallet) || $this->accounts->contains($wallet); | return $wallet && ($this->wallets->contains($wallet) || $this->accounts->contains($wallet)); | ||||
} | } | ||||
/** | /** | ||||
* Check if current user can update data of another object. | * Check if current user can update data of another object. | ||||
* | * | ||||
* @param \App\User|\App\Domain $object A user|domain 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 canUpdate($object): bool | public function canUpdate($object): bool | ||||
{ | { | ||||
if (!method_exists($object, 'wallet')) { | if ($object instanceof User && $this->id == $object->id) { | ||||
return false; | return true; | ||||
} | } | ||||
if ($object instanceof User && $this->id == $object->id) { | if ($this->role == 'admin') { | ||||
return true; | return true; | ||||
} | } | ||||
if ($this->role == 'reseller') { | |||||
if ($object instanceof User && $object->role == 'admin') { | |||||
return false; | |||||
} | |||||
if ($object instanceof Wallet && !empty($object->owner)) { | |||||
$object = $object->owner; | |||||
} | |||||
return isset($object->tenant_id) && $object->tenant_id == $this->tenant_id; | |||||
} | |||||
return $this->canDelete($object); | return $this->canDelete($object); | ||||
} | } | ||||
/** | /** | ||||
* Return the \App\Domain for this user. | * Return the \App\Domain for this user. | ||||
* | * | ||||
* @return \App\Domain|null | * @return \App\Domain|null | ||||
*/ | */ | ||||
public function domain() | public function domain() | ||||
{ | { | ||||
list($local, $domainName) = explode('@', $this->email); | list($local, $domainName) = explode('@', $this->email); | ||||
$domain = \App\Domain::withTrashed()->where('namespace', $domainName)->first(); | $domain = \App\Domain::withTrashed()->where('namespace', $domainName)->first(); | ||||
return $domain; | return $domain; | ||||
} | } | ||||
/** | /** | ||||
* List the domains to which this user is entitled. | * List the domains to which this user is entitled. | ||||
* Note: Active public domains are also returned (for the user tenant). | |||||
* | * | ||||
* @return Domain[] | * @return Domain[] List of Domain objects | ||||
*/ | */ | ||||
public function domains() | public function domains(): array | ||||
{ | { | ||||
$domains = Domain::whereRaw(sprintf('(type & %s)', Domain::TYPE_PUBLIC)) | if ($this->tenant_id) { | ||||
$domains = Domain::where('tenant_id', $this->tenant_id); | |||||
} else { | |||||
$domains = Domain::withEnvTenant(); | |||||
} | |||||
$domains = $domains->whereRaw(sprintf('(type & %s)', Domain::TYPE_PUBLIC)) | |||||
->whereRaw(sprintf('(status & %s)', Domain::STATUS_ACTIVE)) | ->whereRaw(sprintf('(status & %s)', Domain::STATUS_ACTIVE)) | ||||
->get() | ->get() | ||||
->all(); | ->all(); | ||||
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) { | ||||
$domains[] = $entitlement->entitleable; | $domains[] = $entitlement->entitleable; | ||||
▲ Show 20 Lines • Show All 282 Lines • ▼ Show 20 Lines | public function suspend(): void | ||||
return; | return; | ||||
} | } | ||||
$this->status |= User::STATUS_SUSPENDED; | $this->status |= User::STATUS_SUSPENDED; | ||||
$this->save(); | $this->save(); | ||||
} | } | ||||
/** | /** | ||||
* The tenant for this user account. | |||||
* | |||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo | |||||
*/ | |||||
public function tenant() | |||||
{ | |||||
return $this->belongsTo('App\Tenant', 'tenant_id', 'id'); | |||||
} | |||||
/** | |||||
* Unsuspend this domain. | * Unsuspend this domain. | ||||
* | * | ||||
* @return void | * @return void | ||||
*/ | */ | ||||
public function unsuspend(): void | public function unsuspend(): void | ||||
{ | { | ||||
if (!$this->isSuspended()) { | if (!$this->isSuspended()) { | ||||
return; | return; | ||||
▲ Show 20 Lines • Show All 124 Lines • Show Last 20 Lines |