Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Laravel\Sanctum;
4
 
5
use Illuminate\Database\Eloquent\Model;
6
use Laravel\Sanctum\Contracts\HasAbilities;
7
 
8
class PersonalAccessToken extends Model implements HasAbilities
9
{
10
    /**
11
     * The attributes that should be cast to native types.
12
     *
13
     * @var array
14
     */
15
    protected $casts = [
16
        'abilities' => 'json',
17
        'last_used_at' => 'datetime',
18
        'expires_at' => 'datetime',
19
    ];
20
 
21
    /**
22
     * The attributes that are mass assignable.
23
     *
24
     * @var array
25
     */
26
    protected $fillable = [
27
        'name',
28
        'token',
29
        'abilities',
30
        'expires_at',
31
    ];
32
 
33
    /**
34
     * The attributes that should be hidden for serialization.
35
     *
36
     * @var array
37
     */
38
    protected $hidden = [
39
        'token',
40
    ];
41
 
42
    /**
43
     * Get the tokenable model that the access token belongs to.
44
     *
45
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
46
     */
47
    public function tokenable()
48
    {
49
        return $this->morphTo('tokenable');
50
    }
51
 
52
    /**
53
     * Find the token instance matching the given token.
54
     *
55
     * @param  string  $token
56
     * @return static|null
57
     */
58
    public static function findToken($token)
59
    {
60
        if (strpos($token, '|') === false) {
61
            return static::where('token', hash('sha256', $token))->first();
62
        }
63
 
64
        [$id, $token] = explode('|', $token, 2);
65
 
66
        if ($instance = static::find($id)) {
67
            return hash_equals($instance->token, hash('sha256', $token)) ? $instance : null;
68
        }
69
    }
70
 
71
    /**
72
     * Determine if the token has a given ability.
73
     *
74
     * @param  string  $ability
75
     * @return bool
76
     */
77
    public function can($ability)
78
    {
79
        return in_array('*', $this->abilities) ||
80
               array_key_exists($ability, array_flip($this->abilities));
81
    }
82
 
83
    /**
84
     * Determine if the token is missing a given ability.
85
     *
86
     * @param  string  $ability
87
     * @return bool
88
     */
89
    public function cant($ability)
90
    {
91
        return ! $this->can($ability);
92
    }
93
}