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 Mockery;
6
 
7
class Sanctum
8
{
9
    /**
10
     * The personal access client model class name.
11
     *
12
     * @var string
13
     */
14
    public static $personalAccessTokenModel = 'Laravel\\Sanctum\\PersonalAccessToken';
15
 
16
    /**
17
     * A callback that can get the token from the request.
18
     *
19
     * @var callable|null
20
     */
21
    public static $accessTokenRetrievalCallback;
22
 
23
    /**
24
     * A callback that can add to the validation of the access token.
25
     *
26
     * @var callable|null
27
     */
28
    public static $accessTokenAuthenticationCallback;
29
 
30
    /**
31
     * Indicates if Sanctum's migrations will be run.
32
     *
33
     * @var bool
34
     */
35
    public static $runsMigrations = true;
36
 
37
    /**
38
     * Get the current application URL from the "APP_URL" environment variable - with port.
39
     *
40
     * @return string
41
     */
42
    public static function currentApplicationUrlWithPort()
43
    {
44
        $appUrl = config('app.url');
45
 
46
        return $appUrl ? ','.parse_url($appUrl, PHP_URL_HOST).(parse_url($appUrl, PHP_URL_PORT) ? ':'.parse_url($appUrl, PHP_URL_PORT) : '') : '';
47
    }
48
 
49
    /**
50
     * Set the current user for the application with the given abilities.
51
     *
52
     * @param  \Illuminate\Contracts\Auth\Authenticatable|\Laravel\Sanctum\HasApiTokens  $user
53
     * @param  array  $abilities
54
     * @param  string  $guard
55
     * @return \Illuminate\Contracts\Auth\Authenticatable
56
     */
57
    public static function actingAs($user, $abilities = [], $guard = 'sanctum')
58
    {
59
        $token = Mockery::mock(self::personalAccessTokenModel())->shouldIgnoreMissing(false);
60
 
61
        if (in_array('*', $abilities)) {
62
            $token->shouldReceive('can')->withAnyArgs()->andReturn(true);
63
        } else {
64
            foreach ($abilities as $ability) {
65
                $token->shouldReceive('can')->with($ability)->andReturn(true);
66
            }
67
        }
68
 
69
        $user->withAccessToken($token);
70
 
71
        if (isset($user->wasRecentlyCreated) && $user->wasRecentlyCreated) {
72
            $user->wasRecentlyCreated = false;
73
        }
74
 
75
        app('auth')->guard($guard)->setUser($user);
76
 
77
        app('auth')->shouldUse($guard);
78
 
79
        return $user;
80
    }
81
 
82
    /**
83
     * Set the personal access token model name.
84
     *
85
     * @param  string  $model
86
     * @return void
87
     */
88
    public static function usePersonalAccessTokenModel($model)
89
    {
90
        static::$personalAccessTokenModel = $model;
91
    }
92
 
93
    /**
94
     * Specify a callback that should be used to fetch the access token from the request.
95
     *
96
     * @param  callable  $callback
97
     * @return void
98
     */
99
    public static function getAccessTokenFromRequestUsing(callable $callback)
100
    {
101
        static::$accessTokenRetrievalCallback = $callback;
102
    }
103
 
104
    /**
105
     * Specify a callback that should be used to authenticate access tokens.
106
     *
107
     * @param  callable  $callback
108
     * @return void
109
     */
110
    public static function authenticateAccessTokensUsing(callable $callback)
111
    {
112
        static::$accessTokenAuthenticationCallback = $callback;
113
    }
114
 
115
    /**
116
     * Determine if Sanctum's migrations should be run.
117
     *
118
     * @return bool
119
     */
120
    public static function shouldRunMigrations()
121
    {
122
        return static::$runsMigrations;
123
    }
124
 
125
    /**
126
     * Configure Sanctum to not register its migrations.
127
     *
128
     * @return static
129
     */
130
    public static function ignoreMigrations()
131
    {
132
        static::$runsMigrations = false;
133
 
134
        return new static;
135
    }
136
 
137
    /**
138
     * Get the token model class name.
139
     *
140
     * @return string
141
     */
142
    public static function personalAccessTokenModel()
143
    {
144
        return static::$personalAccessTokenModel;
145
    }
146
}