1 | <?php |
||
2 | /****************************************************************************** |
||
3 | * Wikipedia Account Creation Assistance tool * |
||
4 | * ACC Development Team. Please see team.json for a list of contributors. * |
||
5 | * * |
||
6 | * This is free and unencumbered software released into the public domain. * |
||
7 | * Please see LICENSE.md for the full licencing statement. * |
||
8 | ******************************************************************************/ |
||
9 | |||
10 | namespace Waca\DataObjects; |
||
11 | |||
12 | use DateTimeImmutable; |
||
13 | use Exception; |
||
14 | use stdClass; |
||
15 | use Waca\DataObject; |
||
16 | use Waca\Exceptions\OptimisticLockFailedException; |
||
17 | |||
18 | class OAuthIdentity extends DataObject |
||
19 | { |
||
20 | #region Fields |
||
21 | /** @var int */ |
||
22 | private $user; |
||
23 | /** @var string */ |
||
24 | private $iss; |
||
25 | /** @var int */ |
||
26 | private $sub; |
||
27 | /** @var string */ |
||
28 | private $aud; |
||
29 | /** @var int */ |
||
30 | private $exp; |
||
31 | /** @var int */ |
||
32 | private $iat; |
||
33 | /** @var string */ |
||
34 | private $username; |
||
35 | /** @var int */ |
||
36 | private $editcount; |
||
37 | /** @var int */ |
||
38 | private $confirmed_email; |
||
39 | /** @var int */ |
||
40 | private $blocked; |
||
41 | /** @var string */ |
||
42 | private $registered; |
||
43 | /** @var int */ |
||
44 | private $checkuser; |
||
45 | /** @var int */ |
||
46 | private $grantbasic; |
||
47 | /** @var int */ |
||
48 | private $grantcreateaccount; |
||
49 | /** @var int */ |
||
50 | private $granthighvolume; |
||
51 | /** @var int */ |
||
52 | private $grantcreateeditmovepage; |
||
53 | #endregion |
||
54 | |||
55 | /** |
||
56 | * Saves a data object to the database, either updating or inserting a record. |
||
57 | * @return void |
||
58 | * @throws Exception |
||
59 | * @throws OptimisticLockFailedException |
||
60 | */ |
||
61 | public function save() |
||
62 | { |
||
63 | if ($this->isNew()) { |
||
64 | $statement = $this->dbObject->prepare(<<<SQL |
||
65 | INSERT INTO oauthidentity ( |
||
66 | user, iss, sub, aud, exp, iat, username, editcount, confirmed_email, blocked, registered, checkuser, |
||
67 | grantbasic, grantcreateaccount, granthighvolume, grantcreateeditmovepage |
||
68 | ) VALUES ( |
||
69 | :user, :iss, :sub, :aud, :exp, :iat, :username, :editcount, :confirmed_email, :blocked, :registered, |
||
70 | :checkuser, :grantbasic, :grantcreateaccount, :granthighvolume, :grantcreateeditmovepage |
||
71 | ) |
||
72 | SQL |
||
73 | ); |
||
74 | |||
75 | $statement->bindValue(':user', $this->user); |
||
76 | $statement->bindValue(':iss', $this->iss); |
||
77 | $statement->bindValue(':sub', $this->sub); |
||
78 | $statement->bindValue(':aud', $this->aud); |
||
79 | $statement->bindValue(':exp', $this->exp); |
||
80 | $statement->bindValue(':iat', $this->iat); |
||
81 | $statement->bindValue(':username', $this->username); |
||
82 | $statement->bindValue(':editcount', $this->editcount); |
||
83 | $statement->bindValue(':confirmed_email', $this->confirmed_email); |
||
84 | $statement->bindValue(':blocked', $this->blocked); |
||
85 | $statement->bindValue(':registered', $this->registered); |
||
86 | $statement->bindValue(':checkuser', $this->checkuser); |
||
87 | $statement->bindValue(':grantbasic', $this->grantbasic); |
||
88 | $statement->bindValue(':grantcreateaccount', $this->grantcreateaccount); |
||
89 | $statement->bindValue(':granthighvolume', $this->granthighvolume); |
||
90 | $statement->bindValue(':grantcreateeditmovepage', $this->grantcreateeditmovepage); |
||
91 | |||
92 | if ($statement->execute()) { |
||
93 | $this->id = (int)$this->dbObject->lastInsertId(); |
||
94 | } |
||
95 | else { |
||
96 | throw new Exception($statement->errorInfo()); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
97 | } |
||
98 | } |
||
99 | else { |
||
100 | $statement = $this->dbObject->prepare(<<<SQL |
||
101 | UPDATE oauthidentity SET |
||
102 | iss = :iss |
||
103 | , sub = :sub |
||
104 | , aud = :aud |
||
105 | , exp = :exp |
||
106 | , iat = :iat |
||
107 | , username = :username |
||
108 | , editcount = :editcount |
||
109 | , confirmed_email = :confirmed_email |
||
110 | , blocked = :blocked |
||
111 | , registered = :registered |
||
112 | , checkuser = :checkuser |
||
113 | , grantbasic = :grantbasic |
||
114 | , grantcreateaccount = :grantcreateaccount |
||
115 | , granthighvolume = :granthighvolume |
||
116 | , grantcreateeditmovepage = :grantcreateeditmovepage |
||
117 | , updateversion = updateversion + 1 |
||
118 | WHERE id = :id AND updateversion = :updateversion |
||
119 | SQL |
||
120 | ); |
||
121 | |||
122 | $statement->bindValue(':iss', $this->iss); |
||
123 | $statement->bindValue(':sub', $this->sub); |
||
124 | $statement->bindValue(':aud', $this->aud); |
||
125 | $statement->bindValue(':exp', $this->exp); |
||
126 | $statement->bindValue(':iat', $this->iat); |
||
127 | $statement->bindValue(':username', $this->username); |
||
128 | $statement->bindValue(':editcount', $this->editcount); |
||
129 | $statement->bindValue(':confirmed_email', $this->confirmed_email); |
||
130 | $statement->bindValue(':blocked', $this->blocked); |
||
131 | $statement->bindValue(':registered', $this->registered); |
||
132 | $statement->bindValue(':checkuser', $this->checkuser); |
||
133 | $statement->bindValue(':grantbasic', $this->grantbasic); |
||
134 | $statement->bindValue(':grantcreateaccount', $this->grantcreateaccount); |
||
135 | $statement->bindValue(':granthighvolume', $this->granthighvolume); |
||
136 | $statement->bindValue(':grantcreateeditmovepage', $this->grantcreateeditmovepage); |
||
137 | |||
138 | $statement->bindValue(':id', $this->id); |
||
139 | $statement->bindValue(':updateversion', $this->updateversion); |
||
140 | |||
141 | if (!$statement->execute()) { |
||
142 | throw new Exception($statement->errorInfo()); |
||
143 | } |
||
144 | |||
145 | if ($statement->rowCount() !== 1) { |
||
146 | throw new OptimisticLockFailedException(); |
||
147 | } |
||
148 | |||
149 | $this->updateversion++; |
||
150 | } |
||
151 | } |
||
152 | |||
153 | #region Properties |
||
154 | |||
155 | /** |
||
156 | * @return int |
||
157 | */ |
||
158 | public function getUserId() |
||
159 | { |
||
160 | return $this->user; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * @param int $user |
||
165 | */ |
||
166 | public function setUserId($user) |
||
167 | { |
||
168 | $this->user = $user; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * @return string |
||
173 | */ |
||
174 | public function getIssuer() |
||
175 | { |
||
176 | return $this->iss; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * @return int |
||
181 | */ |
||
182 | public function getSubject() |
||
183 | { |
||
184 | return $this->sub; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @return string |
||
189 | */ |
||
190 | public function getAudience() |
||
191 | { |
||
192 | return $this->aud; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * @return int |
||
197 | */ |
||
198 | public function getExpirationTime() |
||
199 | { |
||
200 | return $this->exp; |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @return int |
||
205 | */ |
||
206 | public function getIssuedAtTime() |
||
207 | { |
||
208 | return $this->iat; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * @return string |
||
213 | */ |
||
214 | public function getUsername() |
||
215 | { |
||
216 | return $this->username; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * @return int |
||
221 | */ |
||
222 | public function getEditCount() |
||
223 | { |
||
224 | return $this->editcount; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * @return bool |
||
229 | */ |
||
230 | public function getConfirmedEmail() |
||
231 | { |
||
232 | return $this->confirmed_email == 1; |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * @return bool |
||
237 | */ |
||
238 | public function getBlocked() |
||
239 | { |
||
240 | return $this->blocked == 1; |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * @return string |
||
245 | */ |
||
246 | public function getRegistered() |
||
247 | { |
||
248 | return $this->registered; |
||
249 | } |
||
250 | |||
251 | public function getRegistrationDate() |
||
252 | { |
||
253 | return DateTimeImmutable::createFromFormat('YmdHis', $this->registered)->format('r'); |
||
254 | } |
||
255 | |||
256 | public function getAccountAge() |
||
257 | { |
||
258 | $regDate = DateTimeImmutable::createFromFormat('YmdHis', $this->registered); |
||
259 | $interval = $regDate->diff(new DateTimeImmutable(), true); |
||
260 | |||
261 | return $interval->days; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * @return bool |
||
266 | */ |
||
267 | public function getCheckuser() |
||
268 | { |
||
269 | return $this->checkuser == 1; |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * @return bool |
||
274 | */ |
||
275 | public function getGrantBasic() |
||
276 | { |
||
277 | return $this->grantbasic == 1; |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * @return bool |
||
282 | */ |
||
283 | public function getGrantCreateAccount() |
||
284 | { |
||
285 | return $this->grantcreateaccount == 1; |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * @return bool |
||
290 | */ |
||
291 | public function getGrantHighVolume() |
||
292 | { |
||
293 | return $this->granthighvolume == 1; |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * @return bool |
||
298 | */ |
||
299 | public function getGrantCreateEditMovePage() |
||
300 | { |
||
301 | return $this->grantcreateeditmovepage == 1; |
||
302 | } |
||
303 | |||
304 | #endregion Properties |
||
305 | |||
306 | /** |
||
307 | * Populates the fields of this instance from a provided JSON Web Token |
||
308 | * |
||
309 | * @param stdClass $jwt |
||
310 | */ |
||
311 | public function populate($jwt) |
||
312 | { |
||
313 | $this->iss = $jwt->iss; |
||
314 | $this->sub = $jwt->sub; |
||
315 | $this->aud = $jwt->aud; |
||
316 | $this->exp = $jwt->exp; |
||
317 | $this->iat = $jwt->iat; |
||
318 | $this->username = $jwt->username; |
||
319 | $this->editcount = $jwt->editcount; |
||
320 | $this->confirmed_email = $jwt->confirmed_email ? 1 : 0; |
||
321 | $this->blocked = $jwt->blocked ? 1 : 0; |
||
322 | $this->registered = $jwt->registered; |
||
323 | |||
324 | /* |
||
325 | * Rights we need: |
||
326 | * Account creation |
||
327 | * createaccount => createaccount |
||
328 | * Flagged users: |
||
329 | * tboverride-account => createaccount |
||
330 | * override-antispoof => N/A |
||
331 | * Welcome bot: |
||
332 | * createtalk => createeditmovepage |
||
333 | * edit => editpage/editprotected/editmycssjs/editinterface/createmoveeditpage/delete/protect |
||
334 | * Would be nice: |
||
335 | * apihighlimits => highvolume |
||
336 | * noratelimit => highvolume |
||
337 | * |
||
338 | * Hence, we're requesting these grants: |
||
339 | * useoauth (required) |
||
340 | * createaccount |
||
341 | * createeditmovepage |
||
342 | * |
||
343 | * Any antispoof conflicts will still have to be resolved manually using the normal creation form. |
||
344 | */ |
||
345 | |||
346 | $this->grantbasic = in_array('basic', $jwt->grants) ? 1 : 0; |
||
347 | $this->grantcreateaccount = in_array('createaccount', $jwt->grants) ? 1 : 0; |
||
348 | $this->grantcreateeditmovepage = in_array('createeditmovepage', $jwt->grants) ? 1 : 0; |
||
349 | $this->granthighvolume = in_array('highvolume', $jwt->grants) ? 1 : 0; |
||
350 | |||
351 | $this->checkuser = in_array('checkuser-log', $jwt->rights) ? 1 : 0; |
||
352 | } |
||
353 | } |
||
354 |