seed_mini_test_phone.php 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /** Initialize three separate local test accounts with one demo grant each. */
  3. declare(strict_types=1);
  4. require dirname(__DIR__) . '/vendor/autoload.php';
  5. \Dotenv\Dotenv::createUnsafeMutable(dirname(__DIR__))->load();
  6. \support\App::loadAllConfig(['route']);
  7. date_default_timezone_set('Asia/Shanghai');
  8. use app\miniapi\auth\TestPhoneIdentityProvider;
  9. use app\miniapi\model\MiniUser;
  10. use app\miniapi\model\MiniGrant;
  11. use app\miniapi\service\Access;
  12. use app\miniapi\service\IdentityManagement;
  13. if (!TestPhoneIdentityProvider::enabled()) {
  14. throw new RuntimeException('Requires APP_ENV=local and MINI_TEST_PHONE_LOGIN=true');
  15. }
  16. $targets = [
  17. ['enterprise_admin', TestPhoneIdentityProvider::PHONES['enterprise_admin'], '【测试0910】科技园配送测试'],
  18. ['driver', TestPhoneIdentityProvider::PHONES['driver'], '【测试】地图配送员-KJY'],
  19. ['agent', TestPhoneIdentityProvider::PHONES['agent'], '测试商贸'],
  20. ];
  21. $apply = in_array('--apply', $argv, true);
  22. $result = (new MiniUser())->getConnection()->transaction(function () use ($targets, $apply) {
  23. $accounts = [];
  24. foreach ($targets as [$role, $phone, $name]) {
  25. $model = Access::resourceModel($role);
  26. $rows = $model::query()->where('name', $name)->where('status', 1)->get();
  27. if ($rows->count() !== 1) {
  28. throw new RuntimeException('Expected one active demo resource: ' . $name);
  29. }
  30. $accounts[] = ['role' => $role, 'phone' => $phone, 'resource_id' => (int) $rows[0]->id, 'name' => $name];
  31. }
  32. if (!$apply) {
  33. return ['mode' => 'inspect', 'accounts' => $accounts];
  34. }
  35. foreach ($accounts as &$account) {
  36. $user = MiniUser::query()->firstOrCreate(
  37. ['appid' => TestPhoneIdentityProvider::APP_ID, 'openid' => $account['phone']],
  38. ['nickname' => '本地' . $account['name'], 'status' => 1, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')]
  39. );
  40. if ((int) $user->status !== 1) {
  41. throw new RuntimeException('Test account disabled: ' . $account['phone']);
  42. }
  43. $account['user_id'] = (int) $user->id;
  44. }
  45. unset($account);
  46. $enterpriseUser = $accounts[0]['user_id'];
  47. foreach (array_slice($accounts, 1) as $account) {
  48. $oldGrant = MiniGrant::query()->where([
  49. 'user_id' => $enterpriseUser,
  50. 'role' => $account['role'],
  51. 'resource_id' => $account['resource_id'],
  52. ])->where('status', 1)->first();
  53. if ($oldGrant) {
  54. (new IdentityManagement())->grant(1, [
  55. 'user_id' => $enterpriseUser,
  56. 'role' => $account['role'],
  57. 'resource_id' => $account['resource_id'],
  58. 'status' => 0,
  59. ]);
  60. }
  61. }
  62. foreach ($accounts as $account) {
  63. $key = ['user_id' => $account['user_id'], 'role' => $account['role'], 'resource_id' => $account['resource_id']];
  64. $otherGrant = MiniGrant::query()->where('user_id', $account['user_id'])->where('status', 1)
  65. ->where(function ($query) use ($account) {
  66. $query->where('role', '!=', $account['role'])->orWhere('resource_id', '!=', $account['resource_id']);
  67. })->exists();
  68. if ($otherGrant) {
  69. throw new RuntimeException('Test account has another active grant: ' . $account['phone']);
  70. }
  71. $existing = MiniGrant::query()->where($key)->first();
  72. if ($existing && (int) $existing->status !== 1) {
  73. throw new RuntimeException('Test grant disabled; do not silently reactivate: ' . $account['phone']);
  74. }
  75. if (!$existing) {
  76. (new IdentityManagement())->grant(1, $key);
  77. }
  78. }
  79. return ['mode' => 'applied', 'accounts' => $accounts];
  80. });
  81. echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . PHP_EOL;