<?php
namespace App\Modules\Admin\Entities\Rabc;
use App\Scopes\DeleteScope;
use App\Traits\Instance;
use App\Traits\MysqlTable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Storage;
use Illuminate\Database\Eloquent\Builder;
use Tymon\JWTAuth\Contracts\JWTSubject;
class Admin extends Authenticatable implements JWTSubject
{
use Instance;
use MysqlTable;
protected $primaryKey = 'admin_id';
protected $is_delete = 0; protected $delete_field = 'is_delete';
public function getIsDelete()
{
return $this->is_delete;
}
public function getDeleteField()
{
return $this->delete_field;
}
public $timestamps = false;
protected $hidden = ['password'];
protected $guarded = [];
protected static function booted()
{
$static = new static;
static::addGlobalScope('delete', function(Builder $builder) use ($static){
if ($static->is_delete == 0) $builder->where($static->delete_field, $static->is_delete);
});
}
public function getAdminByName(string $admin_name)
{
return $this->where('admin_name', $admin_name)->first();
}
public function setPasswordAttribute($key)
{
if (empty($key)) unset($this->attributes['password']);
else $this->attributes['password'] = hash_encryption($key);
}
public function getJWTIdentifier()
{
return $this->getKey();
}
public function getJWTCustomClaims()
{
return ['role' => 'admin'];
}
public function adminInfo()
{
return $this->hasOne(AdminInfo::class, $this->primaryKey, $this->primaryKey);
}
public function roles()
{
return $this->belongsToMany(AdminRole::class, AdminWithRole::class, 'admin_id', 'role_id')->withPivot(['admin_id', 'role_id']);
}
/**
* @Function assignRole
*
* @param $roles
*
* @return bool
* @author : cnpscy <[2278757482@qq.com]>
* @chineseAnnotation:给用户分配角色
* @englishAnnotation:
*/
public function assignRole($roles)
{
return $this->roles()->save($roles);
}
/**
* @Function deleteRole
*
* @param $roles
*
* @return mixed
* @author : cnpscy <[2278757482@qq.com]>
* @chineseAnnotation:取消用户分配的角色,取消而不是删除
* @englishAnnotation:
*/
public function deleteRole($roles)
{
return $this->roles()->detach($roles);
}
public function getAdminHeadAttribute($key)
{
if (empty($key)) return $key;
return Storage::url($key);
}
public function setAdminHeadAttribute($key)
{
if (!empty($key)) {
$this->attributes['admin_head'] = str_replace(Storage::url('/'), '', $key);
}
}
}