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\Traits\UserAliasesTrait; | use App\Traits\UserAliasesTrait; | ||||
use App\Traits\UserSettingsTrait; | use App\Traits\UserSettingsTrait; | ||||
use App\Wallet; | |||||
use Illuminate\Notifications\Notifiable; | use Illuminate\Notifications\Notifiable; | ||||
use Illuminate\Contracts\Auth\MustVerifyEmail; | use Illuminate\Contracts\Auth\MustVerifyEmail; | ||||
use Illuminate\Database\Eloquent\SoftDeletes; | use Illuminate\Database\Eloquent\SoftDeletes; | ||||
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 Tymon\JWTAuth\Contracts\JWTSubject; | use Tymon\JWTAuth\Contracts\JWTSubject; | ||||
/** | /** | ||||
▲ Show 20 Lines • Show All 136 Lines • ▼ Show 20 Lines | class User extends Authenticatable implements JWTSubject | ||||
public function controller(): User | public function controller(): User | ||||
{ | { | ||||
// FIXME: This is most likely not the best way to do this | // FIXME: This is most likely not the best way to do this | ||||
$entitlement = \App\Entitlement::where([ | $entitlement = \App\Entitlement::where([ | ||||
'entitleable_id' => $this->id, | 'entitleable_id' => $this->id, | ||||
'entitleable_type' => User::class | 'entitleable_type' => User::class | ||||
])->first(); | ])->first(); | ||||
if ($entitlement && $entitlement->owner_id != $this->id) { | // TODO: No entitlement should not happen, but in tests we have | ||||
return $entitlement->owner; | // such cases, so we fallback to the user itself | ||||
if ($entitlement && $entitlement->wallet->owner_id != $this->id) { | |||||
return $entitlement->wallet->owner; | |||||
} | } | ||||
return $this; | return $this; | ||||
} | } | ||||
public function assignPlan($plan, $domain = null) | public function assignPlan($plan, $domain = null) | ||||
{ | { | ||||
$this->setSetting('plan_id', $plan->id); | $this->setSetting('plan_id', $plan->id); | ||||
▲ Show 20 Lines • Show All 186 Lines • ▼ Show 20 Lines | class User extends Authenticatable implements JWTSubject | ||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany | * @return \Illuminate\Database\Eloquent\Relations\HasMany | ||||
*/ | */ | ||||
public function settings() | public function settings() | ||||
{ | { | ||||
return $this->hasMany('App\UserSetting', 'user_id'); | return $this->hasMany('App\UserSetting', 'user_id'); | ||||
} | } | ||||
/** | /** | ||||
* Return users controlled by the current user. | |||||
* | |||||
* @return \Illuminate\Database\Eloquent\Builder Query builder | |||||
*/ | |||||
public function users() | |||||
{ | |||||
$wallets = $this->wallets()->pluck('id')->all(); | |||||
return $this->select('users.*', 'entitlements.wallet_id') | |||||
vanmeeuwen: This should really be:
```
return $this->select(['users.*', 'entitlements.wallet_id'])
```… | |||||
->distinct() | |||||
->leftJoin('entitlements', 'entitlements.entitleable_id', '=', 'users.id') | |||||
->whereIn('entitlements.wallet_id', $wallets) | |||||
->where('entitlements.entitleable_type', 'App\User'); | |||||
} | |||||
/** | |||||
* 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() | ||||
{ | { | ||||
return $this->hasMany('App\VerificationCode', 'user_id', 'id'); | return $this->hasMany('App\VerificationCode', 'user_id', 'id'); | ||||
} | } | ||||
/** | /** | ||||
* Returns the wallet by which the current user is controlled | |||||
* | |||||
* @return \App\Wallet A wallet object | |||||
*/ | |||||
public function wallet(): Wallet | |||||
{ | |||||
// FIXME: This is most likely not the best way to do this | |||||
$entitlement = \App\Entitlement::where([ | |||||
'entitleable_id' => $this->id, | |||||
'entitleable_type' => User::class | |||||
])->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 | |||||
if ($entitlement) { | |||||
return $entitlement->wallet; | |||||
} | |||||
return $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 67 Lines • Show Last 20 Lines |
This should really be:
because Query\Builder::select() only supports 0-1 parameters (see phpstan output).