Files
star-cloud/app/Models/System/User.php
sky121113 f3b2c3e018
All checks were successful
star-cloud-deploy-demo / deploy-demo (push) Successful in 54s
[FIX] 遷移機台授權為獨立模組:修復變數命名、補齊多語系並強化多租戶數據隔離
2026-03-30 15:30:46 +08:00

111 lines
2.4 KiB
PHP

<?php
namespace App\Models\System;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use App\Traits\TenantScoped;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable, HasRoles, TenantScoped, SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'company_id',
'username',
'name',
'email',
'password',
'phone',
'avatar',
'role',
'status',
'is_admin',
];
/**
* 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',
'is_admin' => 'boolean',
];
/**
* Get the login logs for the user.
*/
public function loginLogs()
{
return $this->hasMany(UserLoginLog::class);
}
/**
* Get the company that owns the user.
*/
public function company()
{
return $this->belongsTo(Company::class);
}
/**
* Get the machines assigned to the user.
*/
public function machines()
{
return $this->belongsToMany(\App\Models\Machine\Machine::class);
}
/**
* Check if the user is a system administrator.
*/
public function isSystemAdmin(): bool
{
return is_null($this->company_id);
}
/**
* Check if the user belongs to a tenant.
*/
public function isTenant(): bool
{
return !is_null($this->company_id);
}
/**
* Get the URL for the user's avatar.
*/
public function getAvatarUrlAttribute(): string
{
if ($this->avatar) {
return \Illuminate\Support\Facades\Storage::disk('public')->url($this->avatar);
}
// Return a default UI Avatar if no avatar is set
return "https://ui-avatars.com/api/?name=" . urlencode($this->name) . "&color=7F9CF5&background=EBF4FF";
}
}