-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathAccessToken.php
More file actions
99 lines (84 loc) · 2.09 KB
/
AccessToken.php
File metadata and controls
99 lines (84 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
88
89
90
91
92
93
94
95
96
97
98
99
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter Shield.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Shield\Entities;
use CodeIgniter\Entity\Entity;
use CodeIgniter\I18n\Time;
/**
* Class AccessToken
*
* Represents a single Personal Access Token, used
* for authenticating users for an API.
*
* @property string|Time|null $expires
* @property string|Time|null $last_used_at
*/
class AccessToken extends Entity
{
private ?User $user = null;
/**
* @var array<string, string>
*/
protected $casts = [
'id' => '?integer',
'last_used_at' => 'datetime',
'extra' => 'array',
'expires' => 'datetime',
];
/**
* @var array<string, string>
*/
protected $datamap = [
'scopes' => 'extra',
];
/**
* Returns the user associated with this token.
*/
public function user(): ?User
{
if ($this->user === null) {
$users = auth()->getProvider();
$this->user = $users->findById($this->user_id);
}
return $this->user;
}
/**
* Determines whether this token grants
* permission to the $scope
*/
public function can(string $scope): bool
{
if ($this->extra === []) {
return false;
}
// Wildcard present
if (in_array('*', $this->extra, true)) {
return true;
}
// Check stored scopes
return in_array($scope, $this->extra, true);
}
/**
* Determines whether this token does NOT
* grant permission to $scope.
*/
public function cant(string $scope): bool
{
if ($this->extra === []) {
return true;
}
// Wildcard present
if (in_array('*', $this->extra, true)) {
return false;
}
// Check stored scopes
return ! in_array($scope, $this->extra, true);
}
}