Changeset View
Changeset View
Standalone View
Standalone View
src/app/User.php
<?php | <?php | ||||
namespace App; | namespace App; | ||||
use App\Entitlement; | use App\Entitlement; | ||||
use App\UserAlias; | use App\UserAlias; | ||||
use App\Sku; | use App\Sku; | ||||
use App\Traits\UuidIntKeyTrait; | |||||
use App\Traits\BelongsToTenantTrait; | use App\Traits\BelongsToTenantTrait; | ||||
use App\Traits\UserConfigTrait; | use App\Traits\EntitleableTrait; | ||||
use App\Traits\UserAliasesTrait; | use App\Traits\UserAliasesTrait; | ||||
use App\Traits\UserConfigTrait; | |||||
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\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 int $status | * @property int $status | ||||
* @property int $tenant_id | * @property int $tenant_id | ||||
*/ | */ | ||||
class User extends Authenticatable | class User extends Authenticatable | ||||
{ | { | ||||
use UuidIntKeyTrait; | |||||
use BelongsToTenantTrait; | use BelongsToTenantTrait; | ||||
use EntitleableTrait; | |||||
use HasApiTokens; | |||||
use NullableFields; | use NullableFields; | ||||
use UserConfigTrait; | use UserConfigTrait; | ||||
use UserAliasesTrait; | use UserAliasesTrait; | ||||
use UuidIntKeyTrait; | |||||
use SettingsTrait; | use SettingsTrait; | ||||
use SoftDeletes; | use SoftDeletes; | ||||
use HasApiTokens; | |||||
// 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 | ||||
public const STATUS_ACTIVE = 1 << 1; | public const STATUS_ACTIVE = 1 << 1; | ||||
// user has been suspended | // user has been suspended | ||||
public const STATUS_SUSPENDED = 1 << 2; | public const STATUS_SUSPENDED = 1 << 2; | ||||
// user has been deleted | // user has been deleted | ||||
▲ Show 20 Lines • Show All 161 Lines • ▼ Show 20 Lines | public function canDelete($object): bool | ||||
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 $wallet && ($wallet->user_id == $this->id || $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 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 | ||||
Show All 25 Lines | public function canRead($object): bool | ||||
} | } | ||||
if (!method_exists($object, 'wallet')) { | if (!method_exists($object, 'wallet')) { | ||||
return false; | return false; | ||||
} | } | ||||
$wallet = $object->wallet(); | $wallet = $object->wallet(); | ||||
return $wallet && ($this->wallets->contains($wallet) || $this->accounts->contains($wallet)); | return $wallet && ($wallet->user_id == $this->id || $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 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 | ||||
▲ Show 20 Lines • Show All 69 Lines • ▼ Show 20 Lines | public function domains(): array | ||||
$domains[] = $entitlement->entitleable; | $domains[] = $entitlement->entitleable; | ||||
} | } | ||||
} | } | ||||
return $domains; | return $domains; | ||||
} | } | ||||
/** | /** | ||||
* The user entitlement. | |||||
* | |||||
* @return \Illuminate\Database\Eloquent\Relations\MorphOne | |||||
*/ | |||||
public function entitlement() | |||||
{ | |||||
return $this->morphOne('App\Entitlement', 'entitleable'); | |||||
} | |||||
/** | |||||
* Entitlements for this user. | |||||
* | |||||
* Note that these are entitlements that apply to the user account, and not entitlements that | |||||
* this user owns. | |||||
* | |||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany | |||||
*/ | |||||
public function entitlements() | |||||
{ | |||||
return $this->hasMany('App\Entitlement', 'entitleable_id', 'id') | |||||
->where('entitleable_type', User::class); | |||||
} | |||||
/** | |||||
* Find whether an email address exists as a user (including deleted users). | * Find whether an email address exists as a user (including deleted users). | ||||
* | * | ||||
* @param string $email Email address | * @param string $email Email address | ||||
* @param bool $return_user Return User instance instead of boolean | * @param bool $return_user Return User instance instead of boolean | ||||
* | * | ||||
* @return \App\User|bool True or User model object if found, False otherwise | * @return \App\User|bool True or User model object if found, False otherwise | ||||
*/ | */ | ||||
public static function emailExists(string $email, bool $return_user = false) | public static function emailExists(string $email, bool $return_user = false) | ||||
▲ Show 20 Lines • Show All 315 Lines • ▼ Show 20 Lines | class User extends Authenticatable | ||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany | * @return \Illuminate\Database\Eloquent\Relations\HasMany | ||||
*/ | */ | ||||
public function verificationcodes() | public function verificationcodes() | ||||
{ | { | ||||
return $this->hasMany('App\VerificationCode', 'user_id', 'id'); | return $this->hasMany('App\VerificationCode', 'user_id', 'id'); | ||||
} | } | ||||
/** | /** | ||||
* Returns the wallet by which the user is controlled | |||||
* | |||||
* @return ?\App\Wallet A wallet object | |||||
*/ | |||||
public function wallet(): ?Wallet | |||||
{ | |||||
$entitlement = $this->entitlement()->withTrashed()->orderBy('created_at', 'desc')->first(); | |||||
// TODO: No entitlement should not happen, but in tests we have | |||||
// such cases, so we fallback to the user's wallet in this case | |||||
return $entitlement ? $entitlement->wallet : $this->wallets()->first(); | |||||
} | |||||
/** | |||||
* Wallets this user owns. | * Wallets this user owns. | ||||
* | * | ||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany | * @return \Illuminate\Database\Eloquent\Relations\HasMany | ||||
*/ | */ | ||||
public function wallets() | public function wallets() | ||||
{ | { | ||||
return $this->hasMany('App\Wallet'); | return $this->hasMany('App\Wallet'); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 181 Lines • Show Last 20 Lines |