| Total Complexity | 79 |
| Total Lines | 637 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like UserHandlerTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UserHandlerTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | trait UserHandlerTrait { |
||
| 32 | abstract protected function completeTask(); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Handle user build cache tree |
||
| 36 | * @param array $arguments Useful arguments for the task |
||
| 37 | * @return void |
||
| 38 | */ |
||
| 39 | private function handleUserBuildCacheTree($arguments) { |
||
| 41 | } |
||
| 42 | |||
| 43 | |||
| 44 | /** |
||
| 45 | * Generate user keys |
||
| 46 | * @param array $taskData Données de la tâche |
||
| 47 | * @param array $arguments Arguments nécessaires pour la création des clés |
||
| 48 | * @return void |
||
| 49 | */ |
||
| 50 | private function generateUserKeys($arguments) { |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | |||
| 79 | /** |
||
| 80 | * Process a subtask for generating user keys. |
||
| 81 | * @param array $subtask The subtask to process. |
||
| 82 | * @param array $arguments Arguments for the task. |
||
| 83 | * @return void |
||
| 84 | */ |
||
| 85 | private function processGenerateUserKeysSubtask(array $subtask, array $arguments) { |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | |||
| 160 | /** |
||
| 161 | * Generate new user keys - step 0 |
||
| 162 | * @param array $arguments Arguments for the task |
||
| 163 | * @return void |
||
| 164 | */ |
||
| 165 | private function generateNewUserStep0($arguments) { |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | |||
| 174 | /** |
||
| 175 | * Generate new user keys |
||
| 176 | * @param array $taskData Task data |
||
| 177 | * @param array $arguments Arguments for the task |
||
| 178 | */ |
||
| 179 | private function generateNewUserStep20($taskData, $arguments) { |
||
| 180 | // get user private key |
||
| 181 | $ownerInfo = isset($arguments['owner_id']) && isset($arguments['creator_pwd']) |
||
| 182 | ? $this->getOwnerInfos($arguments['owner_id'], $arguments['creator_pwd']) |
||
| 183 | : null; |
||
| 184 | $userInfo = $this->getOwnerInfos( |
||
| 185 | $arguments['new_user_id'], |
||
| 186 | empty($arguments['new_user_pwd']) === false ? $arguments['new_user_pwd'] : $arguments['new_user_code'], |
||
| 187 | ((int) $arguments['only_personal_items'] ?? 0) === 1 ? 1 : 0, |
||
| 188 | $arguments['new_user_private_key'] ?? '' |
||
| 189 | ); |
||
| 190 | |||
| 191 | // Start transaction for better performance |
||
| 192 | DB::startTransaction(); |
||
| 193 | |||
| 194 | // Loop on items |
||
| 195 | $rows = DB::query( |
||
| 196 | 'SELECT id, pw, perso |
||
| 197 | FROM ' . prefixTable('items') . ' |
||
| 198 | ORDER BY id ASC |
||
| 199 | LIMIT %i, %i', |
||
| 200 | $taskData['index'], |
||
| 201 | $taskData['nb'] |
||
| 202 | ); |
||
| 203 | |||
| 204 | foreach ($rows as $record) { |
||
| 205 | // Get itemKey from current user |
||
| 206 | $itemShareKey = DB::queryFirstRow( |
||
| 207 | 'SELECT share_key, increment_id |
||
| 208 | FROM ' . prefixTable('sharekeys_items') . ' |
||
| 209 | WHERE object_id = %i AND user_id = %i', |
||
| 210 | $record['id'], |
||
| 211 | (int) $arguments['owner_id'] |
||
| 212 | ); |
||
| 213 | |||
| 214 | // do we have any input? (#3481) |
||
| 215 | if ($itemShareKey === null || count($itemShareKey) === 0) { |
||
| 216 | continue; |
||
| 217 | } |
||
| 218 | |||
| 219 | // Decrypt itemkey with expected private key |
||
| 220 | $itemKey = decryptUserObjectKey( |
||
| 221 | $itemShareKey['share_key'], |
||
| 222 | $ownerInfo['private_key'] |
||
| 223 | ); |
||
| 224 | |||
| 225 | // Prevent to change key if its key is empty |
||
| 226 | if (empty($itemKey) === true) { |
||
| 227 | $share_key_for_item = ''; |
||
| 228 | } else { |
||
| 229 | // Encrypt Item key |
||
| 230 | $share_key_for_item = encryptUserObjectKey($itemKey, $userInfo['public_key']); |
||
| 231 | } |
||
| 232 | |||
| 233 | // Save the new sharekey correctly encrypted in DB |
||
| 234 | insertOrUpdateSharekey( |
||
| 235 | prefixTable('sharekeys_items'), |
||
| 236 | (int) $record['id'], |
||
| 237 | (int) $arguments['new_user_id'], |
||
| 238 | $share_key_for_item |
||
| 239 | ); |
||
| 240 | } |
||
| 241 | |||
| 242 | // Commit transaction |
||
| 243 | DB::commit(); |
||
| 244 | } |
||
| 245 | |||
| 246 | |||
| 247 | /** |
||
| 248 | * Generate new user keys - step 30 |
||
| 249 | * @param array $taskData Task data |
||
| 250 | * @param array $arguments Arguments for the task |
||
| 251 | * @return void |
||
| 252 | */ |
||
| 253 | private function generateNewUserStep30($taskData, $arguments) { |
||
| 254 | // get user private key |
||
| 255 | $ownerInfo = isset($arguments['owner_id']) && isset($arguments['creator_pwd']) |
||
| 256 | ? $this->getOwnerInfos($arguments['owner_id'], $arguments['creator_pwd']) |
||
| 257 | : null; |
||
| 258 | $userInfo = $this->getOwnerInfos( |
||
| 259 | $arguments['new_user_id'], |
||
| 260 | $arguments['new_user_pwd'], |
||
| 261 | ($arguments['only_personal_items'] ?? 0) === 1 ? 1 : 0, |
||
| 262 | $arguments['new_user_private_key'] ?? '' |
||
| 263 | ); |
||
| 264 | |||
| 265 | // Start transaction for better performance |
||
| 266 | DB::startTransaction(); |
||
| 267 | |||
| 268 | // Loop on logs |
||
| 269 | $rows = DB::query( |
||
| 270 | 'SELECT increment_id |
||
| 271 | FROM ' . prefixTable('log_items') . ' |
||
| 272 | WHERE raison LIKE "at_pw :%" AND encryption_type = "teampass_aes" |
||
| 273 | ORDER BY increment_id ASC |
||
| 274 | LIMIT ' . $taskData['index'] . ', ' . $taskData['nb'] |
||
| 275 | ); |
||
| 276 | foreach ($rows as $record) { |
||
| 277 | // Get itemKey from current user |
||
| 278 | $currentUserKey = DB::queryFirstRow( |
||
| 279 | 'SELECT share_key |
||
| 280 | FROM ' . prefixTable('sharekeys_logs') . ' |
||
| 281 | WHERE object_id = %i AND user_id = %i', |
||
| 282 | $record['increment_id'], |
||
| 283 | $arguments['owner_id'] |
||
| 284 | ); |
||
| 285 | |||
| 286 | // do we have any input? (#3481) |
||
| 287 | if ($currentUserKey === null || count($currentUserKey) === 0) { |
||
| 288 | continue; |
||
| 289 | } |
||
| 290 | |||
| 291 | // Decrypt itemkey with admin key |
||
| 292 | $itemKey = decryptUserObjectKey($currentUserKey['share_key'], $ownerInfo['private_key']); |
||
| 293 | |||
| 294 | // Encrypt Item key |
||
| 295 | $share_key_for_item = encryptUserObjectKey($itemKey, $userInfo['public_key']); |
||
| 296 | |||
| 297 | // Save the key in DB |
||
| 298 | insertOrUpdateSharekey( |
||
| 299 | prefixTable('sharekeys_logs'), |
||
| 300 | (int) $record['increment_id'], |
||
| 301 | (int) $arguments['new_user_id'], |
||
| 302 | $share_key_for_item |
||
| 303 | ); |
||
| 304 | } |
||
| 305 | |||
| 306 | // Commit transaction |
||
| 307 | DB::commit(); |
||
| 308 | } |
||
| 309 | |||
| 310 | |||
| 311 | /** |
||
| 312 | * Generate new user keys - step 40 |
||
| 313 | * @param array $taskData Task data |
||
| 314 | * @param array $arguments Arguments for the task |
||
| 315 | * @return void |
||
| 316 | */ |
||
| 317 | private function generateNewUserStep40($taskData, $arguments) { |
||
| 318 | // get user private key |
||
| 319 | $ownerInfo = isset($arguments['owner_id']) && isset($arguments['creator_pwd']) |
||
| 320 | ? $this->getOwnerInfos($arguments['owner_id'], $arguments['creator_pwd']) |
||
| 321 | : null; |
||
| 322 | $userInfo = $this->getOwnerInfos( |
||
| 323 | $arguments['new_user_id'], |
||
| 324 | $arguments['new_user_pwd'], |
||
| 325 | ($arguments['only_personal_items'] ?? 0) === 1 ? 1 : 0, |
||
| 326 | $arguments['new_user_private_key'] ?? '' |
||
| 327 | ); |
||
| 328 | |||
| 329 | // Start transaction for better performance |
||
| 330 | DB::startTransaction(); |
||
| 331 | |||
| 332 | // Loop on fields |
||
| 333 | $rows = DB::query( |
||
| 334 | 'SELECT id |
||
| 335 | FROM ' . prefixTable('categories_items') . ' |
||
| 336 | WHERE encryption_type = "teampass_aes" |
||
| 337 | ORDER BY id ASC |
||
| 338 | LIMIT %i, %i', |
||
| 339 | $taskData['index'], |
||
| 340 | $taskData['nb'] |
||
| 341 | ); |
||
| 342 | foreach ($rows as $record) { |
||
| 343 | // Get itemKey from current user |
||
| 344 | $currentUserKey = DB::queryFirstRow( |
||
| 345 | 'SELECT share_key |
||
| 346 | FROM ' . prefixTable('sharekeys_fields') . ' |
||
| 347 | WHERE object_id = %i AND user_id = %i', |
||
| 348 | $record['id'], |
||
| 349 | $arguments['owner_id'] |
||
| 350 | ); |
||
| 351 | |||
| 352 | // do we have any input? |
||
| 353 | if ($currentUserKey === null || count($currentUserKey) === 0) { |
||
| 354 | continue; |
||
| 355 | } |
||
| 356 | |||
| 357 | // Decrypt itemkey with admin key |
||
| 358 | $itemKey = decryptUserObjectKey($currentUserKey['share_key'], $ownerInfo['private_key']); |
||
| 359 | |||
| 360 | // Encrypt Item key |
||
| 361 | $share_key_for_item = encryptUserObjectKey($itemKey, $userInfo['public_key']); |
||
| 362 | |||
| 363 | // Save the key in DB |
||
| 364 | insertOrUpdateSharekey( |
||
| 365 | prefixTable('sharekeys_fields'), |
||
| 366 | (int) $record['id'], |
||
| 367 | (int) $arguments['new_user_id'], |
||
| 368 | $share_key_for_item |
||
| 369 | ); |
||
| 370 | } |
||
| 371 | |||
| 372 | // Commit transaction |
||
| 373 | DB::commit(); |
||
| 374 | } |
||
| 375 | |||
| 376 | |||
| 377 | /** |
||
| 378 | * Generate new user keys - step 50 |
||
| 379 | * @param array $taskData Task data |
||
| 380 | * @param array $arguments Arguments for the task |
||
| 381 | * @return void |
||
| 382 | */ |
||
| 383 | private function generateNewUserStep50($taskData, $arguments) { |
||
| 384 | // get user private key |
||
| 385 | $ownerInfo = isset($arguments['owner_id']) && isset($arguments['creator_pwd']) |
||
| 386 | ? $this->getOwnerInfos($arguments['owner_id'], $arguments['creator_pwd']) |
||
| 387 | : null; |
||
| 388 | $userInfo = $this->getOwnerInfos( |
||
| 389 | $arguments['new_user_id'], |
||
| 390 | $arguments['new_user_pwd'], |
||
| 391 | ($arguments['only_personal_items'] ?? 0) === 1 ? 1 : 0, |
||
| 392 | $arguments['new_user_private_key'] ?? '' |
||
| 393 | ); |
||
| 394 | |||
| 395 | // Start transaction for better performance |
||
| 396 | DB::startTransaction(); |
||
| 397 | |||
| 398 | // Loop on suggestions |
||
| 399 | $rows = DB::query( |
||
| 400 | 'SELECT id |
||
| 401 | FROM ' . prefixTable('suggestion') . ' |
||
| 402 | ORDER BY id ASC |
||
| 403 | LIMIT %i, %i', |
||
| 404 | $taskData['index'], |
||
| 405 | $taskData['nb'] |
||
| 406 | ); |
||
| 407 | foreach ($rows as $record) { |
||
| 408 | // Get itemKey from current user |
||
| 409 | $currentUserKey = DB::queryFirstRow( |
||
| 410 | 'SELECT share_key |
||
| 411 | FROM ' . prefixTable('sharekeys_suggestions') . ' |
||
| 412 | WHERE object_id = %i AND user_id = %i', |
||
| 413 | $record['id'], |
||
| 414 | $arguments['owner_id'] |
||
| 415 | ); |
||
| 416 | |||
| 417 | // do we have any input? (#3481) |
||
| 418 | if ($currentUserKey === null || count($currentUserKey) === 0) { |
||
| 419 | continue; |
||
| 420 | } |
||
| 421 | |||
| 422 | // Decrypt itemkey with admin key |
||
| 423 | $itemKey = decryptUserObjectKey($currentUserKey['share_key'], $ownerInfo['private_key']); |
||
| 424 | |||
| 425 | // Encrypt Item key |
||
| 426 | $share_key_for_item = encryptUserObjectKey($itemKey, $userInfo['public_key']); |
||
| 427 | |||
| 428 | // Save the key in DB |
||
| 429 | insertOrUpdateSharekey( |
||
| 430 | prefixTable('sharekeys_suggestions'), |
||
| 431 | (int) $record['id'], |
||
| 432 | (int) $arguments['new_user_id'], |
||
| 433 | $share_key_for_item |
||
| 434 | ); |
||
| 435 | } |
||
| 436 | |||
| 437 | // Commit transaction |
||
| 438 | DB::commit(); |
||
| 439 | } |
||
| 440 | |||
| 441 | |||
| 442 | /** |
||
| 443 | * Generate new user keys - step 60 |
||
| 444 | * @param array $taskData Task data |
||
| 445 | * @param array $arguments Arguments for the task |
||
| 446 | * @return void |
||
| 447 | */ |
||
| 448 | private function generateNewUserStep60($taskData, $arguments) { |
||
| 449 | // get user private key |
||
| 450 | $ownerInfo = isset($arguments['owner_id']) && isset($arguments['creator_pwd']) |
||
| 451 | ? $this->getOwnerInfos($arguments['owner_id'], $arguments['creator_pwd']) |
||
| 452 | : null; |
||
| 453 | $userInfo = $this->getOwnerInfos( |
||
| 454 | $arguments['new_user_id'], |
||
| 455 | $arguments['new_user_pwd'], |
||
| 456 | ($arguments['only_personal_items'] ?? 0) === 1 ? 1 : 0, |
||
| 457 | $arguments['new_user_private_key'] ?? '' |
||
| 458 | ); |
||
| 459 | |||
| 460 | // Start transaction for better performance |
||
| 461 | DB::startTransaction(); |
||
| 462 | |||
| 463 | // Loop on files |
||
| 464 | $rows = DB::query( |
||
| 465 | 'SELECT f.id AS id, i.perso AS perso |
||
| 466 | FROM ' . prefixTable('files') . ' AS f |
||
| 467 | INNER JOIN ' . prefixTable('items') . ' AS i ON i.id = f.id_item |
||
| 468 | WHERE f.status = "' . TP_ENCRYPTION_NAME . '" |
||
| 469 | LIMIT %i, %i', |
||
| 470 | $taskData['index'], |
||
| 471 | $taskData['nb'] |
||
| 472 | ); //aes_encryption |
||
| 473 | foreach ($rows as $record) { |
||
| 474 | // Get itemKey from current user |
||
| 475 | $currentUserKey = DB::queryFirstRow( |
||
| 476 | 'SELECT share_key, increment_id |
||
| 477 | FROM ' . prefixTable('sharekeys_files') . ' |
||
| 478 | WHERE object_id = %i AND user_id = %i', |
||
| 479 | $record['id'], |
||
| 480 | (int) $record['perso'] === 0 ? $arguments['owner_id'] : $arguments['new_user_id'] |
||
| 481 | ); |
||
| 482 | |||
| 483 | // do we have any input? (#3481) |
||
| 484 | if ($currentUserKey === null || count($currentUserKey) === 0) { |
||
| 485 | continue; |
||
| 486 | } |
||
| 487 | |||
| 488 | // Decrypt itemkey with user key |
||
| 489 | $itemKey = decryptUserObjectKey( |
||
| 490 | $currentUserKey['share_key'], |
||
| 491 | //$ownerInfo['private_key'] |
||
| 492 | (int) $record['perso'] === 0 ? $ownerInfo['private_key'] : $userInfo['private_key'] |
||
| 493 | ); |
||
| 494 | |||
| 495 | // Prevent to change key if its key is empty |
||
| 496 | if (empty($itemKey) === true) { |
||
| 497 | continue; |
||
| 498 | } |
||
| 499 | |||
| 500 | // Encrypt Item key |
||
| 501 | $share_key_for_item = encryptUserObjectKey($itemKey, $userInfo['public_key']); |
||
| 502 | |||
| 503 | // Save the key in DB |
||
| 504 | insertOrUpdateSharekey( |
||
| 505 | prefixTable('sharekeys_files'), |
||
| 506 | (int) $record['id'], |
||
| 507 | (int) $arguments['new_user_id'], |
||
| 508 | $share_key_for_item |
||
| 509 | ); |
||
| 510 | } |
||
| 511 | |||
| 512 | // Commit transaction |
||
| 513 | DB::commit(); |
||
| 514 | } |
||
| 515 | |||
| 516 | |||
| 517 | /** |
||
| 518 | * Generate new user keys - step 99 |
||
| 519 | * @param array $arguments Arguments for the task |
||
| 520 | */ |
||
| 521 | private function generateNewUserStep99($arguments) { |
||
| 628 | ); |
||
| 629 | } |
||
| 630 | } |
||
| 631 | |||
| 632 | |||
| 633 | /** |
||
| 634 | * Get owner info |
||
| 635 | * @param int $owner_id Owner ID |
||
| 636 | * @param string $owner_pwd Owner password |
||
| 637 | * @param int $only_personal_items 1 if only personal items, 0 else |
||
| 638 | * @param string $owner_pwd Owner password |
||
| 639 | * @return array Owner information |
||
| 640 | */ |
||
| 641 | private function getOwnerInfos(int $owner_id, string $owner_pwd, ?int $only_personal_items = 0, ?string $owner_private_key = ''): array { |
||
| 668 | ]; |
||
| 669 | } |
||
| 670 | } |
||
| 671 | } |