Users.php 1.66 KB
Newer Older
冯超鹏's avatar
冯超鹏 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php

namespace App\Laravue\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Passport\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;

/**
 * Class User
 *
 * @property string $name
 * @property string $email
 * @property string $password
 * @property Role[] $roles
冯超鹏's avatar
冯超鹏 committed
17
 *protected
冯超鹏's avatar
冯超鹏 committed
18 19 20 21 22 23
 * @method static User create(array $user)
 * @package App
 */
class Users extends Authenticatable
{
    use Notifiable, HasRoles, HasApiTokens;
冯超鹏's avatar
冯超鹏 committed
24

25
    protected $table = "users";
冯超鹏's avatar
冯超鹏 committed
26 27 28
    public $timestamps = false;

    public function getTypeAllToArray($userdata = []) {
冯超鹏's avatar
冯超鹏 committed
29 30
        $userdata['password'] = sha1($userdata['password']);
        $userdata['add_time'] = time();
冯超鹏's avatar
冯超鹏 committed
31 32
        return $this->insertGetId($userdata);
    }
冯超鹏's avatar
冯超鹏 committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87


    protected $fillable = [
        'email', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    /**
     * Set permissions guard to API by default
     * @var string
     */
    protected $guard_name = 'api';

    /**
     * @inheritdoc
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * @inheritdoc
     */
    public function getJWTCustomClaims()
    {
        return [];
    }

    /**
     * @return bool
     * 验证是否管理员
     */
    public function isAdmin(): bool
    {
        foreach ($this->roles  as $role) {
            if ($role->isAdmin()) {
                return true;
            }
        }

        return false;
    }

冯超鹏's avatar
冯超鹏 committed
88
}