136 lines
3.3 KiB
PHP
136 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Laravel\Sanctum\HasApiTokens;
|
|
|
|
class User extends Authenticatable
|
|
{
|
|
use HasApiTokens, HasFactory, Notifiable, SoftDeletes;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'name',
|
|
'email',
|
|
'role',
|
|
'dealer_id',
|
|
'password',
|
|
'role_id'
|
|
];
|
|
|
|
/**
|
|
* 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',
|
|
];
|
|
|
|
/**
|
|
* Get all of the transactions for the User
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function transactions()
|
|
{
|
|
return $this->hasMany(Transaction::class, 'user_id', 'id');
|
|
}
|
|
|
|
/**
|
|
* Get all of the sa_transactions for the User
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function sa_transactions()
|
|
{
|
|
return $this->hasMany(Transaction::class, 'user_sa_id', 'id');
|
|
}
|
|
|
|
/**
|
|
* Get the dealer associated with the User
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne
|
|
*/
|
|
public function dealer()
|
|
{
|
|
return $this->hasOne(Dealer::class, 'id', 'dealer_id');
|
|
}
|
|
|
|
/**
|
|
* Get the role associated with the User
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function role()
|
|
{
|
|
return $this->belongsTo(Role::class, 'role_id');
|
|
}
|
|
|
|
/**
|
|
* Check if user has a specific role
|
|
*
|
|
* @param string $roleName
|
|
* @return bool
|
|
*/
|
|
public function hasRole($roleName)
|
|
{
|
|
// If role_id is 0 or null, user has no role
|
|
if (!$this->role_id) {
|
|
return false;
|
|
}
|
|
|
|
// For admin role, we can check if user has admin privileges
|
|
if (strtolower($roleName) === 'admin') {
|
|
return $this->isAdmin();
|
|
}
|
|
|
|
// Load role if not already loaded
|
|
if (!$this->relationLoaded('role')) {
|
|
$this->load('role');
|
|
}
|
|
|
|
return $this->role && strtolower($this->role->name) === strtolower($roleName);
|
|
}
|
|
|
|
/**
|
|
* Check if user is admin by checking admin privileges
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function isAdmin()
|
|
{
|
|
// Check if user has admin privileges by checking if they can access admin area
|
|
try {
|
|
$adminPrivilege = \App\Models\Privilege::join('menus', 'menus.id', '=', 'privileges.menu_id')
|
|
->where('menus.link', 'adminarea')
|
|
->where('privileges.role_id', $this->role_id)
|
|
->where('privileges.view', 1)
|
|
->first();
|
|
|
|
return $adminPrivilege !== null;
|
|
} catch (\Exception $e) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|