1 | <?php |
||||
2 | /** |
||||
3 | * Created by PhpStorm. |
||||
4 | * User: hugh.li |
||||
5 | * Date: 2022/6/14 |
||||
6 | * Time: 17:41. |
||||
7 | */ |
||||
8 | |||||
9 | namespace HughCube\Laravel\OTS\Sanctum; |
||||
10 | |||||
11 | use Aliyun\OTS\Consts\ColumnTypeConst; |
||||
12 | use Aliyun\OTS\Consts\RowExistenceExpectationConst; |
||||
13 | use Aliyun\OTS\OTSClientException; |
||||
14 | use Aliyun\OTS\OTSServerException; |
||||
15 | use Carbon\Carbon; |
||||
16 | use Exception; |
||||
17 | use HughCube\Base\Base; |
||||
18 | use HughCube\Laravel\OTS\Connection; |
||||
19 | use HughCube\Laravel\OTS\Ots; |
||||
20 | |||||
21 | /** |
||||
22 | * @property string $tokenable_type |
||||
23 | * @property int $tokenable_id |
||||
24 | * @property string $name |
||||
25 | * @property string $token |
||||
26 | * @property array $abilities |
||||
27 | * @property Carbon|null $last_used_at |
||||
28 | * @property Carbon|null $created_at |
||||
29 | * @property Carbon|null $updated_at |
||||
30 | */ |
||||
31 | class PersonalAccessToken extends \Laravel\Sanctum\PersonalAccessToken |
||||
32 | { |
||||
33 | /** |
||||
34 | * @throws Exception |
||||
35 | */ |
||||
36 | protected static function getOts(): Connection |
||||
37 | { |
||||
38 | return Ots::connection(); |
||||
39 | } |
||||
40 | |||||
41 | public static function getOtsTable(): string |
||||
42 | { |
||||
43 | return 'personal_access_tokens'; |
||||
44 | } |
||||
45 | |||||
46 | public static function getApp(): string |
||||
47 | { |
||||
48 | return config('app.name'); |
||||
49 | } |
||||
50 | |||||
51 | /** |
||||
52 | * 最后一次使用时间在15天之前. |
||||
53 | * |
||||
54 | * @param bool $isValid 验证的当前值 |
||||
55 | * |
||||
56 | * @return bool |
||||
57 | */ |
||||
58 | public function isValidAccessToken(bool $isValid = true): bool |
||||
0 ignored issues
–
show
|
|||||
59 | { |
||||
60 | if (!$this->last_used_at instanceof Carbon) { |
||||
61 | return true; |
||||
62 | } |
||||
63 | |||||
64 | return $this->last_used_at->gt(Carbon::now()->subSeconds(3600 * 24 * 15)); |
||||
65 | } |
||||
66 | |||||
67 | /** |
||||
68 | * @throws OTSServerException |
||||
69 | * @throws OTSClientException |
||||
70 | * @throws Exception |
||||
71 | */ |
||||
72 | public static function findToken($token): ?PersonalAccessToken |
||||
73 | { |
||||
74 | $request = [ |
||||
75 | 'table_name' => static::getOtsTable(), |
||||
76 | 'primary_key' => [ |
||||
77 | ['token', hash('sha256', $token)], |
||||
78 | ['app', static::getApp()], |
||||
79 | ], |
||||
80 | 'max_versions' => 1, |
||||
81 | ]; |
||||
82 | |||||
83 | $row = Ots::parseRow(static::getOts()->getRow($request)); |
||||
0 ignored issues
–
show
The method
getRow() does not exist on HughCube\Laravel\OTS\Connection . Since you implemented __call , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
84 | |||||
85 | /** @var static $model */ |
||||
86 | $model = static::query()->newModelInstance(); |
||||
87 | $model->forceFill($row); |
||||
88 | $model->exists = true; |
||||
89 | |||||
90 | if (is_string($model->abilities)) { |
||||
0 ignored issues
–
show
|
|||||
91 | $abilities = json_decode($model->abilities, true); |
||||
92 | if (JSON_ERROR_NONE !== json_last_error()) { |
||||
93 | $abilities = []; |
||||
94 | } |
||||
95 | $model->abilities = $abilities; |
||||
96 | } |
||||
97 | |||||
98 | return $model; |
||||
99 | } |
||||
100 | |||||
101 | /** |
||||
102 | * @throws OTSServerException |
||||
103 | * @throws OTSClientException |
||||
104 | * @throws Exception |
||||
105 | */ |
||||
106 | public function save(array $options = []): bool |
||||
107 | { |
||||
108 | $this->created_at = $this->created_at ?? Carbon::now(); |
||||
109 | $this->updated_at = $this->updated_at ?? Carbon::now(); |
||||
110 | |||||
111 | if (!$this->exists) { |
||||
112 | return $this->saveInstall(); |
||||
113 | } |
||||
114 | |||||
115 | $this->updated_at = Carbon::now(); |
||||
116 | $request = [ |
||||
117 | 'table_name' => $this->table, |
||||
118 | 'condition' => RowExistenceExpectationConst::CONST_EXPECT_EXIST, |
||||
119 | 'primary_key' => [ |
||||
120 | ['token', $this->token], |
||||
121 | ['app', $this->getApp()], |
||||
122 | ], |
||||
123 | 'update_of_attribute_columns' => [ |
||||
124 | 'PUT' => [ |
||||
125 | ['tokenable_type', $this->tokenable_type, ColumnTypeConst::CONST_STRING], |
||||
126 | ['tokenable_id', Base::digitalToString($this->tokenable_id), ColumnTypeConst::CONST_STRING], |
||||
127 | ['name', $this->name, ColumnTypeConst::CONST_STRING], |
||||
128 | ['abilities', json_encode($this->abilities), ColumnTypeConst::CONST_STRING], |
||||
129 | ['last_used_at', $this->last_used_at->getTimestamp(), ColumnTypeConst::CONST_DOUBLE], |
||||
0 ignored issues
–
show
The method
getTimestamp() does not exist on null .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||
130 | ['created_at', $this->created_at->getTimestamp(), ColumnTypeConst::CONST_DOUBLE], |
||||
131 | ['updated_at', $this->updated_at->getTimestamp(), ColumnTypeConst::CONST_DOUBLE], |
||||
132 | ], |
||||
133 | ], |
||||
134 | ]; |
||||
135 | |||||
136 | static::getOts()->updateRow($request); |
||||
0 ignored issues
–
show
The method
updateRow() does not exist on HughCube\Laravel\OTS\Connection . Since you implemented __call , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
137 | |||||
138 | return true; |
||||
139 | } |
||||
140 | |||||
141 | /** |
||||
142 | * @throws OTSServerException |
||||
143 | * @throws OTSClientException |
||||
144 | * @throws Exception |
||||
145 | */ |
||||
146 | protected function saveInstall(): bool |
||||
147 | { |
||||
148 | $this->last_used_at = $this->last_used_at ?? Carbon::now(); |
||||
149 | |||||
150 | $request = [ |
||||
151 | 'table_name' => $this->table, |
||||
152 | 'condition' => RowExistenceExpectationConst::CONST_EXPECT_NOT_EXIST, |
||||
153 | 'primary_key' => [ |
||||
154 | ['token', $this->token], |
||||
155 | ['app', $this->getApp()], |
||||
156 | ], |
||||
157 | 'attribute_columns' => [ |
||||
158 | ['tokenable_type', $this->tokenable_type, ColumnTypeConst::CONST_STRING], |
||||
159 | ['tokenable_id', Base::digitalToString($this->tokenable_id), ColumnTypeConst::CONST_STRING], |
||||
160 | ['name', $this->name, ColumnTypeConst::CONST_STRING], |
||||
161 | ['abilities', json_encode($this->abilities), ColumnTypeConst::CONST_STRING], |
||||
162 | ['last_used_at', $this->last_used_at->getTimestamp(), ColumnTypeConst::CONST_DOUBLE], |
||||
163 | ['created_at', $this->created_at->getTimestamp(), ColumnTypeConst::CONST_DOUBLE], |
||||
0 ignored issues
–
show
The method
getTimestamp() does not exist on null .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||
164 | ['updated_at', $this->updated_at->getTimestamp(), ColumnTypeConst::CONST_DOUBLE], |
||||
0 ignored issues
–
show
The method
getTimestamp() does not exist on null .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||
165 | ], |
||||
166 | ]; |
||||
167 | |||||
168 | static::getOts()->putRow($request); |
||||
0 ignored issues
–
show
The method
putRow() does not exist on HughCube\Laravel\OTS\Connection . Since you implemented __call , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
169 | $this->exists = true; |
||||
170 | |||||
171 | return true; |
||||
172 | } |
||||
173 | } |
||||
174 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.