Files
exchange-2.0/app/Models/User.php
T

135 lines
3.5 KiB
PHP

<?php
namespace App\Models;
use App\Classes\General\Interfaces\KeyValueInterface;
use App\Classes\General\Interfaces\Voucherifiable;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Permission\Traits\HasRoles;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\MustVerifyEmail;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Laravel\Sanctum\HasApiTokens;
class User extends AbstractModel implements
JWTSubject,
AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract,
Voucherifiable,
KeyValueInterface
{
use HasRoles, Notifiable, Authenticatable, Authorizable, CanResetPassword, MustVerifyEmail, SoftDeletes, HasApiTokens;
/**
* Get the identifier that will be stored in the subject claim of the JWT.
*
* @return mixed
*/
public function getJWTIdentifier()
{
return $this->getKey();
}
/**
* Return a key value array, containing any custom claims to be added to the JWT.
*
* @return array
*/
public function getJWTCustomClaims()
{
return [];
}
public function emailVerification(): HasMany {
return $this->hasMany(UserEmailVerification::class, 'email', 'email');
}
/**
* @return HasMany
*/
public function passwordReset(): HasMany {
return $this->hasMany(PasswordReset::class, 'user_id', 'id');
}
/**
* @return belongsToMany
*/
public function company(): belongsToMany
{
return $this->belongsToMany(Company::class, (new Employee())->getTable(), 'user_id', 'company_id');
}
// /**
// * @return HasMany
// */
// public function redemptions(): HasMany
// {
// return $this->HasMany(VoucherRedemption::class, 'user_id', 'id');
// }
/**
* @return HasMany
*/
public function milestoneProgress(): HasMany
{
return $this->HasMany(MilestoneProgress::class, 'user_id', 'id');
}
/**
* @return MorphMany
*/
public function voucherifyEntities(): MorphMany
{
return $this->morphMany(VoucherEntityMapping::class, 'owner');
}
/**
* @return HasMany
*/
public function rewards(): HasMany
{
return $this->HasMany(UserReward::class, 'user_id', 'id');
}
public function hasCustomAttribute(string $key, $value = null): bool
{
$query = $this->attributesKVP()->where('key', $key);
if ($value !== null) {
$query->where('value', $value);
}
return $query->exists();
}
public function attributesKVP(): MorphMany
{
return $this->morphMany(KeyValuePair::class, 'owner');
}
/**
* @return BelongsToMany
*/
public function affiliates(): BelongsToMany
{
return $this->belongsToMany(Affiliate::class, 'user_affiliates', 'user_id', 'affiliate_id')
->withPivot('clicked_at', 'registered_at')
->withTimestamps();
}
}