20230329023822_change_permission.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. use app\model\Employee;
  3. use app\model\Grant;
  4. use app\model\Permission;
  5. use think\facade\Db;
  6. use think\migration\Migrator;
  7. class ChangePermission extends Migrator
  8. {
  9. /**
  10. * Change Method.
  11. *
  12. * Write your reversible migrations using this method.
  13. *
  14. * More information on writing migrations is available here:
  15. * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
  16. *
  17. * The following commands can be used in this method and Phinx will
  18. * automatically reverse them when rolling back:
  19. *
  20. * createTable
  21. * renameTable
  22. * addColumn
  23. * renameColumn
  24. * addIndex
  25. * addForeignKey
  26. *
  27. * Remember to call "create()" or "update()" and NOT "save()" when working
  28. * with the Table class.
  29. */
  30. public function change()
  31. {
  32. Db::startTrans();
  33. $auth = ['statistics/table', 'statistics/new_table_data'];
  34. // 客户跟进数据统计 统计部堡表 权限删除
  35. $p = Permission::where(['uri' => 'statistics/customer'])->find();
  36. $r = explode(',', $p->relation);
  37. foreach ($auth as $v) {
  38. $k = array_search($v, $r);
  39. unset($r[$k]);
  40. }
  41. $p->relation = implode(',', $r);
  42. $p->save();
  43. // 客户跟进权限添加 statistics/table statistics/new_table_data
  44. $p1 = Permission::create([
  45. 'pid' => $p->id,
  46. 'auth_name' => '客户跟进数据统计',
  47. 'uri' => 'statistics/table',
  48. 'is_menu' => 0,
  49. 'relation' => 'statistics/table'
  50. ]);
  51. $p2 = Permission::create([
  52. 'pid' => $p->id,
  53. 'auth_name' => '统计部报表',
  54. 'uri' => 'statistics/new_table_data',
  55. 'is_menu' => 0,
  56. 'relation' => 'statistics/new_table_data'
  57. ]);
  58. // 超级权限
  59. $superG = Grant::find(1);
  60. $superGp = (array)$superG->permission;
  61. $superGp[] = $p1->id;
  62. $superGp[] = $p2->id;
  63. // 要添加权限的企业
  64. $c = [941, 1847, 1977]; // 馨居尚组织跟id 菡萏怡景组织跟id 臻品软装组织跟id
  65. foreach ($c as $root_id) {
  66. // 权限所属人添加权限
  67. $gl = Grant::where(['root_id' => $root_id])->select();
  68. foreach ($gl as $g) {
  69. $permission = (array)$g->permission;
  70. if (!in_array($p->id, $permission)) {
  71. $permission[] = $p1->id;
  72. $permission[] = $p2->id;
  73. $g->permission = $permission;
  74. $g->save();
  75. }
  76. }
  77. // 创建所属企业的超级管理员
  78. $rSp = Grant::create([
  79. 'name' => '超级管理员',
  80. 'permission' => $superGp,
  81. 'root_id' => $root_id,
  82. 'type' => 'm'
  83. ]);
  84. // 更改所属企业的超级管理员权限
  85. Employee::where(['root_id' => $root_id, 'grant_id' => 1])->update(['grant_id' => $rSp->id]);
  86. }
  87. Db::commit();
  88. }
  89. }