1
0

20230429025637_add_permission_for_haoxiaoyun.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. use app\model\Employee;
  3. use app\model\Grant;
  4. use app\model\Permission;
  5. use think\migration\Migrator;
  6. use think\migration\db\Column;
  7. class AddPermissionForHaoxiaoyun 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. $np = Permission::where(['uri' => 'haoxiaoyun/setting'])->find();
  33. if ($np == null) {
  34. $p = Permission::where([['auth_name', '=', '企业管理']])->find();
  35. $np = Permission::create([
  36. 'pid' => $p->id,
  37. 'auth_name' => '外呼设置',
  38. 'uri' => 'haoxiaoyun/setting',
  39. 'relation' => 'haoxiaoyun/setting,haoxiaoyun/settingSave,haoxiaoyun/balence',
  40. 'is_menu' => 1
  41. ]);
  42. }
  43. // 复制超级管理员权限
  44. $s = Grant::find(1);
  45. $pm = (array)$s->permission;
  46. $pm[] = $np->id;
  47. $c = [643];
  48. foreach ($c as $root_id) {
  49. $super = Grant::where(['type' => 'm', 'root_id' => $root_id])->find();
  50. if($super){
  51. $spm = (array)$super->permission;
  52. $spm[] = $np->id;
  53. $super->permission = $spm;
  54. $super->save();
  55. continue;
  56. }
  57. $g = Grant::create([
  58. 'name' => '超级管理员',
  59. 'permission' => $pm,
  60. 'type' => 'm',
  61. 'root_id' => $root_id
  62. ]);
  63. Employee::where(['root_id' => $root_id, 'top_one' => 1])->update([
  64. 'grant_id' => $g->id
  65. ]);
  66. }
  67. }
  68. }