1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- namespace App\Http\Models;
- use Illuminate\Database\Eloquent\Relations\BelongsToMany;
- use Illuminate\Database\Eloquent\Relations\HasOne;
- use Illuminate\Database\Eloquent\SoftDeletes;
- use Illuminate\Notifications\Notifiable;
- use Illuminate\Foundation\Auth\User as AuthenticTable;
- class User extends AuthenticTable
- {
- use Notifiable, SoftDeletes;
- protected $table = 'users';
- protected $guarded = [];
- public function role(): HasOne
- {
- return $this->hasOne(
- Role::class,
- 'id',
- 'role_id'
- );
- }
- public function getStatusWithCssAttribute()
- {
- return ($this->status ?? 0) ?
- sprintf('<button type="button" class="btn btn-xs btn-primary">%s</button>', '启用') :
- sprintf('<button type="button" class="btn btn-xs btn-danger">%s</button>', '禁用');
- }
- public function sites(): BelongsToMany
- {
- return $this->belongsToMany(
- Site::class,
- 'user_has_sites',
- 'user_id',
- 'site_id'
- );
- }
- public function permissions()
- {
- return $this->belongsToMany(
- Permission::class,
- 'user_has_permissions',
- 'user_id',
- 'permission_id'
- );
- }
- protected static $permissions; //静态缓存
- public function hasAuth($auth): bool
- {
- if (!empty($this->is_super)) return true;
- if (self::$permissions === null) {
- $role = $this->role ?? null;
- if (!$role) return false;
- self::$permissions = $role->permissions->where('type', 2)->pluck('rule')->toArray();
- }
- if (in_array($auth, self::$permissions)) {
- return true;
- }
- return false;
- }
- public function username()
- {
- return $this->username ?? '';
- }
- public static function getUserList()
- {
- $userList = User::query()->pluck('nickname', 'id')->toArray()??[];
- return $userList;
- }
- }
|