Files
online.tbbank.gov.tm-larave…/app/Models/User.php
2025-03-23 23:46:02 +05:00

224 lines
4.7 KiB
PHP

<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Models\Branch\Branch;
use App\Models\Order\Card\CardOrder;
use App\Models\Order\Loan\LoanOrder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;
/**
* @property int $id
* @property string $username
* @property string $name
* @property string $email
* @property string $phone
* @property null|\Illuminate\Support\Carbon $email_verified_at
* @property null|\Illuminate\Support\Carbon $phone_verified_at
* @property string $password
* @property string $locale
* @property null|array $options
* @property bool $active
* @property string $remember_token
* @property \Illuminate\Support\Carbon $created_at
* @property \Illuminate\Support\Carbon $updated_at
*/
class User extends Authenticatable
{
use HasApiTokens;
use HasFactory;
use HasRoles;
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'username',
'name',
'email',
'phone',
'email_verified_at',
'phone_verified_at',
'password',
'locale',
'active',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'options' => 'array',
];
/**
* Branches associated with user
*
* @return BelongsToMany<Branch>
*/
public function branches(): BelongsToMany
{
return $this->belongsToMany(Branch::class);
}
/**
* Loan orders user created
*
* @return HasMany<LoanOrder>
*/
public function loanOrders(): HasMany
{
return $this->hasMany(LoanOrder::class);
}
/**
* Card orders user created
*
* @return HasMany<CardOrder>
*/
public function cardOrders(): HasMany
{
return $this->hasMany(CardOrder::class);
}
/**
* Alerts
*
* @return HasMany<Alert>
*/
public function alerts(): HasMany
{
return $this->hasMany(Alert::class);
}
/**
* Check if user has role.
*/
public function withoutRole(): bool
{
return $this->roles->count() === 0;
}
/**
* Check if user can access api docs
*/
public function canAccessApiDocs(): bool
{
return $this->isMe() || in_array($this->username, [
'muhammet',
]);
}
/**
* Check if user is me.
*/
public function isMe(): bool
{
return $this->email === 'nurmuhammet@mail.com';
}
/**
* Check if user is super admin.
*/
public function isSuperAdmin(): bool
{
if ($this->isMe()) {
return true;
}
return $this->hasRole('superadmin');
}
/**
* Check if user is admin.
*/
public function isAdmin(): bool
{
if ($this->isMe()) {
return true;
}
return $this->hasRole(['king', 'superadmin', 'admin']);
}
/**
* Check if user is operator.
*/
public function isOperator(): bool
{
return $this->hasRole('operator');
}
/**
* Is System User
*/
public function isSystemUser(): bool
{
return $this->isAdmin() || $this->isOperator();
}
/**
* Check if user owns loan order.
*/
public function ownsLoanOrder(LoanOrder $loanOrder): bool
{
return $this->id == intval($loanOrder->user_id);
}
/**
* Check if user owns loan order.
*/
public function ownsCardOrder(CardOrder $cardOrder): bool
{
return $this->id == intval($cardOrder->user_id);
}
/**
* Check if phone is verified
*/
public function phoneIsVerified(): bool
{
return ! is_null($this->phone_verified_at);
}
/**
* Profile page of user
*/
public function profilePage(): string
{
return sprintf('/resources/users/%s', $this->id);
}
/**
* Get option from options
*/
public function getOption(string $option): null|int|string
{
return $this->options && array_key_exists($option, $this->options) ? $this->options[$option] : '';
}
}