123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php
- use app\model\Employee;
- use app\model\Grant;
- use app\model\Permission;
- use think\facade\Db;
- use think\migration\Migrator;
- class ChangePermission extends Migrator
- {
- /**
- * Change Method.
- *
- * Write your reversible migrations using this method.
- *
- * More information on writing migrations is available here:
- * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
- *
- * The following commands can be used in this method and Phinx will
- * automatically reverse them when rolling back:
- *
- * createTable
- * renameTable
- * addColumn
- * renameColumn
- * addIndex
- * addForeignKey
- *
- * Remember to call "create()" or "update()" and NOT "save()" when working
- * with the Table class.
- */
- public function change()
- {
- Db::startTrans();
- $auth = ['statistics/table', 'statistics/new_table_data'];
- // 客户跟进数据统计 统计部堡表 权限删除
- $p = Permission::where(['uri' => 'statistics/customer'])->find();
- $r = explode(',', $p->relation);
- foreach ($auth as $v) {
- $k = array_search($v, $r);
- unset($r[$k]);
- }
- $p->relation = implode(',', $r);
- $p->save();
- // 客户跟进权限添加 statistics/table statistics/new_table_data
- $p1 = Permission::create([
- 'pid' => $p->id,
- 'auth_name' => '客户跟进数据统计',
- 'uri' => 'statistics/table',
- 'is_menu' => 0,
- 'relation' => 'statistics/table'
- ]);
- $p2 = Permission::create([
- 'pid' => $p->id,
- 'auth_name' => '统计部报表',
- 'uri' => 'statistics/new_table_data',
- 'is_menu' => 0,
- 'relation' => 'statistics/new_table_data'
- ]);
- // 超级权限
- $superG = Grant::find(1);
- $superGp = (array)$superG->permission;
- $superGp[] = $p1->id;
- $superGp[] = $p2->id;
- // 要添加权限的企业
- $c = [941, 1847, 1977]; // 馨居尚组织跟id 菡萏怡景组织跟id 臻品软装组织跟id
- foreach ($c as $root_id) {
- // 权限所属人添加权限
- $gl = Grant::where(['root_id' => $root_id])->select();
- foreach ($gl as $g) {
- $permission = (array)$g->permission;
- if (!in_array($p->id, $permission)) {
- $permission[] = $p1->id;
- $permission[] = $p2->id;
- $g->permission = $permission;
- $g->save();
- }
- }
- // 创建所属企业的超级管理员
- $rSp = Grant::create([
- 'name' => '超级管理员',
- 'permission' => $superGp,
- 'root_id' => $root_id,
- 'type' => 'm'
- ]);
- // 更改所属企业的超级管理员权限
- Employee::where(['root_id' => $root_id, 'grant_id' => 1])->update(['grant_id' => $rSp->id]);
- }
- Db::commit();
- }
- }
|