Files
online.tbbank.gov.tm-larave…/app/Models/User.php

118 lines
2.4 KiB
PHP

<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Models\Branch\Branch;
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;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
use HasRoles;
/**
* 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',
];
/**
* Branches associated with user
*/
public function branches(): BelongsToMany
{
return $this->belongsToMany(Branch::class);
}
/**
* Loan orders user created
*/
public function loanOrders(): HasMany
{
return $this->hasMany(LoanOrder::class);
}
/**
* Check if user is me.
*/
public function isMe(): bool
{
return $this->email === 'nurmuhammet@mail.com';
}
/**
* 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 === $loanOrder->user_id;
}
}