2
0

UsersDesensitize.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\UserInfo;
  5. use Illuminate\Support\Facades\App;
  6. use Illuminate\Support\Str;
  7. class UsersDesensitize extends Command
  8. {
  9. /**
  10. * 对用户表进行脱敏,改写password email 两个字段数据.以test & admin开头的不会被改写
  11. * php artisan users:desensitize --test
  12. * @var string
  13. */
  14. protected $signature = 'users:desensitize {--test}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'desensitize users information';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return int
  34. */
  35. public function handle()
  36. {
  37. if (App::environment('product')) {
  38. $this->error('environment is product');
  39. return 1;
  40. }
  41. $this->info('environment is ' . App::environment());
  42. if ($this->option('test')) {
  43. $this->info('test mode');
  44. } else {
  45. $this->error('this is not test mode');
  46. }
  47. if (!$this->confirm("desensitize all users information?")) {
  48. return 0;
  49. }
  50. $users = UserInfo::cursor();
  51. $total = UserInfo::count();
  52. $desensitized = 0;
  53. $jumped = 0;
  54. foreach ($users as $key => $user) {
  55. if (
  56. mb_substr($user->username, 0, 4) === 'test' ||
  57. $user->username === 'admin' ||
  58. $user->username === 'visuddhinanda'
  59. ) {
  60. $this->info('test user jump' . $user->username);
  61. $jumped++;
  62. continue;
  63. }
  64. $desensitized++;
  65. if (!$this->option('test')) {
  66. $curr = UserInfo::find($user->id);
  67. $curr->password = Str::uuid() . '*';
  68. $curr->email = Str::uuid() . '@email.com';
  69. $curr->username = mb_substr($curr->username, 0, 2) . mt_rand(1000, 9999) . '****';
  70. $curr->nickname = mb_substr($curr->nickname, 0, 1) . '**';
  71. $curr->save();
  72. }
  73. $this->info('desensitized ' . $user->username);
  74. }
  75. $this->info("all done total={$total} desensitized={$desensitized} jumped={$jumped}");
  76. return 0;
  77. }
  78. }