2024_08_14_093112_create_permission_tables.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. use Illuminate\Database\Migrations\Migration;
  3. use Illuminate\Database\Schema\Blueprint;
  4. use Illuminate\Support\Facades\Schema;
  5. return new class extends Migration {
  6. /**
  7. * Run the migrations.
  8. */
  9. public function up(): void
  10. {
  11. $teams = config('permission.teams');
  12. $tableNames = config('permission.table_names');
  13. $columnNames = config('permission.column_names');
  14. $pivotRole = $columnNames['role_pivot_key'] ?? 'role_id';
  15. $pivotPermission = $columnNames['permission_pivot_key'] ?? 'permission_id';
  16. if (empty($tableNames)) {
  17. throw new \Exception('Error: config/permission.php not loaded. Run [php artisan config:clear] and try again.');
  18. }
  19. if ($teams && empty($columnNames['team_foreign_key'] ?? null)) {
  20. throw new \Exception('Error: team_foreign_key on config/permission.php not loaded. Run [php artisan config:clear] and try again.');
  21. }
  22. Schema::create($tableNames['permissions'], function (Blueprint $table) {
  23. //$table->engine('InnoDB');
  24. $table->bigIncrements('id'); // permission id
  25. $table->string('name')->comment('权限名称'); // For MyISAM use string('name', 225); // (or 166 for InnoDB with Redundant/Compact row format)
  26. $table->string('guard_name')->default('web'); // For MyISAM use string('guard_name', 25);
  27. $table->timestamps();
  28. $table->unique(['name', 'guard_name']);
  29. });
  30. Schema::create($tableNames['roles'], function (Blueprint $table) use ($teams, $columnNames) {
  31. //$table->engine('InnoDB');
  32. $table->bigIncrements('id'); // role id
  33. if ($teams || config('permission.testing')) { // permission.testing is a fix for sqlite testing
  34. $table->unsignedBigInteger($columnNames['team_foreign_key'])->nullable();
  35. $table->index($columnNames['team_foreign_key'], 'roles_team_foreign_key_index');
  36. }
  37. $table->string('name')->comment('角色名称'); // For MyISAM use string('name', 225); // (or 166 for InnoDB with Redundant/Compact row format)
  38. $table->string('guard_name')->default('web'); // For MyISAM use string('guard_name', 25);
  39. $table->string('code', 100)->default('')->comment('角色权限标识');
  40. $table->integer('sort')->default(0)->comment('显示顺序');
  41. $table->tinyInteger('data_scope')->default(1)->comment('数据范围(1:全部数据 2:指定部门数据 3:所属部门数据 4:所属部门及下级数据)');
  42. $table->json('data_scope_dept_ids')->nullable()->comment('数据范围(指定部门)');
  43. $table->tinyInteger('status')->default(0)->comment('角色状态');
  44. $table->tinyInteger('type')->comment('角色类型');
  45. $table->text('remark')->nullable()->comment('备注');
  46. $table->string('creator', 64)->nullable()->comment('创建者');
  47. $table->string('updater', 64)->nullable()->comment('更新者');
  48. $table->timestamps();
  49. $table->softDeletes();
  50. if ($teams || config('permission.testing')) {
  51. $table->unique([$columnNames['team_foreign_key'], 'name', 'guard_name']);
  52. } else {
  53. $table->unique(['name', 'guard_name']);
  54. }
  55. });
  56. Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames, $pivotPermission, $teams) {
  57. $table->unsignedBigInteger($pivotPermission);
  58. $table->string('model_type');
  59. $table->unsignedBigInteger($columnNames['model_morph_key']);
  60. $table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_permissions_model_id_model_type_index');
  61. $table->foreign($pivotPermission)
  62. ->references('id') // permission id
  63. ->on($tableNames['permissions'])
  64. ->onDelete('cascade');
  65. if ($teams) {
  66. $table->unsignedBigInteger($columnNames['team_foreign_key']);
  67. $table->index($columnNames['team_foreign_key'], 'model_has_permissions_team_foreign_key_index');
  68. $table->primary([$columnNames['team_foreign_key'], $pivotPermission, $columnNames['model_morph_key'], 'model_type'],
  69. 'model_has_permissions_permission_model_type_primary');
  70. } else {
  71. $table->primary([$pivotPermission, $columnNames['model_morph_key'], 'model_type'],
  72. 'model_has_permissions_permission_model_type_primary');
  73. }
  74. });
  75. Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames, $pivotRole, $teams) {
  76. $table->unsignedBigInteger($pivotRole);
  77. $table->string('model_type');
  78. $table->unsignedBigInteger($columnNames['model_morph_key']);
  79. $table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_roles_model_id_model_type_index');
  80. $table->foreign($pivotRole)
  81. ->references('id') // role id
  82. ->on($tableNames['roles'])
  83. ->onDelete('cascade');
  84. if ($teams) {
  85. $table->unsignedBigInteger($columnNames['team_foreign_key']);
  86. $table->index($columnNames['team_foreign_key'], 'model_has_roles_team_foreign_key_index');
  87. $table->primary([$columnNames['team_foreign_key'], $pivotRole, $columnNames['model_morph_key'], 'model_type'],
  88. 'model_has_roles_role_model_type_primary');
  89. } else {
  90. $table->primary([$pivotRole, $columnNames['model_morph_key'], 'model_type'],
  91. 'model_has_roles_role_model_type_primary');
  92. }
  93. });
  94. Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames, $pivotRole, $pivotPermission) {
  95. $table->unsignedBigInteger($pivotPermission);
  96. $table->unsignedBigInteger($pivotRole);
  97. $table->foreign($pivotPermission)
  98. ->references('id') // permission id
  99. ->on($tableNames['permissions'])
  100. ->onDelete('cascade');
  101. $table->foreign($pivotRole)
  102. ->references('id') // role id
  103. ->on($tableNames['roles'])
  104. ->onDelete('cascade');
  105. $table->primary([$pivotPermission, $pivotRole], 'role_has_permissions_permission_id_role_id_primary');
  106. });
  107. app('cache')
  108. ->store(config('permission.cache.store') != 'default' ? config('permission.cache.store') : null)
  109. ->forget(config('permission.cache.key'));
  110. }
  111. /**
  112. * Reverse the migrations.
  113. */
  114. public function down(): void
  115. {
  116. $tableNames = config('permission.table_names');
  117. if (empty($tableNames)) {
  118. throw new \Exception('Error: config/permission.php not found and defaults could not be merged. Please publish the package configuration before proceeding, or drop the tables manually.');
  119. }
  120. Schema::drop($tableNames['role_has_permissions']);
  121. Schema::drop($tableNames['model_has_roles']);
  122. Schema::drop($tableNames['model_has_permissions']);
  123. Schema::drop($tableNames['roles']);
  124. Schema::drop($tableNames['permissions']);
  125. }
  126. };