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 DateTimeInterface;
6
use Illuminate\Support\Str;
7
 
8
trait HasApiTokens
9
{
10
    /**
11
     * The access token the user is using for the current request.
12
     *
13
     * @var \Laravel\Sanctum\Contracts\HasAbilities
14
     */
15
    protected $accessToken;
16
 
17
    /**
18
     * Get the access tokens that belong to model.
19
     *
20
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
21
     */
22
    public function tokens()
23
    {
24
        return $this->morphMany(Sanctum::$personalAccessTokenModel, 'tokenable');
25
    }
26
 
27
    /**
28
     * Determine if the current API token has a given scope.
29
     *
30
     * @param  string  $ability
31
     * @return bool
32
     */
33
    public function tokenCan(string $ability)
34
    {
35
        return $this->accessToken && $this->accessToken->can($ability);
36
    }
37
 
38
    /**
39
     * Create a new personal access token for the user.
40
     *
41
     * @param  string  $name
42
     * @param  array  $abilities
43
     * @param  \DateTimeInterface|null  $expiresAt
44
     * @return \Laravel\Sanctum\NewAccessToken
45
     */
46
    public function createToken(string $name, array $abilities = ['*'], DateTimeInterface $expiresAt = null)
47
    {
48
        $token = $this->tokens()->create([
49
            'name' => $name,
50
            'token' => hash('sha256', $plainTextToken = Str::random(40)),
51
            'abilities' => $abilities,
52
            'expires_at' => $expiresAt,
53
        ]);
54
 
55
        return new NewAccessToken($token, $token->getKey().'|'.$plainTextToken);
56
    }
57
 
58
    /**
59
     * Get the access token currently associated with the user.
60
     *
61
     * @return \Laravel\Sanctum\Contracts\HasAbilities
62
     */
63
    public function currentAccessToken()
64
    {
65
        return $this->accessToken;
66
    }
67
 
68
    /**
69
     * Set the current access token for the user.
70
     *
71
     * @param  \Laravel\Sanctum\Contracts\HasAbilities  $accessToken
72
     * @return $this
73
     */
74
    public function withAccessToken($accessToken)
75
    {
76
        $this->accessToken = $accessToken;
77
 
78
        return $this;
79
    }
80
}