select(); $groups = \think\Db::name('auth_group')->select(); $snapshot = $backup . '/forecast-auth-backup.json'; if (!file_exists($snapshot)) { file_put_contents($snapshot, json_encode(['rules' => $rules, 'groups' => $groups], JSON_UNESCAPED_UNICODE)); chmod($snapshot, 0600); } \think\Db::transaction(function () use ($rules, $groups) { $testing = \think\Db::name('auth_rule')->where('name', 'testing')->find(); $nodes = [ 'forecast' => ['温湿度预测与实测', 1], 'forecast/index' => ['查看记录', 0], 'forecast/detail' => ['查看对比', 0], 'forecast/add' => ['录入温湿度并预测', 0], 'forecast/retry' => ['重新生成预测', 0], 'forecast/actual' => ['补录真实监测值', 0], ]; $ids = []; foreach ($nodes as $name => $info) { $id = \think\Db::name('auth_rule')->where('name', $name)->value('id'); if (!$id) { $id = \think\Db::name('auth_rule')->insertGetId([ 'name' => $name, 'title' => $info[0], 'ismenu' => $info[1], 'type' => 'file', 'pid' => $info[1] ? ($testing ? $testing['pid'] : 0) : $ids['forecast'], 'icon' => $info[1] ? 'fa fa-line-chart' : 'fa fa-circle-o', 'menutype' => 'addtabs', 'weigh' => $info[1] && $testing ? $testing['weigh'] - 1 : 0, 'status' => 'normal', 'createtime' => time(), 'updatetime' => time(), ]); } $ids[$name] = $id; } $oldIds = []; foreach ($rules as $rule) $oldIds[$rule['name']] = (string)$rule['id']; foreach ($groups as $group) { if ($group['rules'] === '*') continue; $granted = explode(',', $group['rules']); $canRead = false; foreach (['testing', 'testing/index'] as $name) if (isset($oldIds[$name]) && in_array($oldIds[$name], $granted, true)) $canRead = true; $canWrite = isset($oldIds['testing/add']) && in_array($oldIds['testing/add'], $granted, true); if (!$canRead && !$canWrite) continue; $extra = [$ids['forecast'], $ids['forecast/index'], $ids['forecast/detail']]; if ($canWrite) $extra = array_merge($extra, [$ids['forecast/add'], $ids['forecast/retry'], $ids['forecast/actual']]); \think\Db::name('auth_group')->where('id', $group['id'])->update(['rules' => implode(',', array_unique(array_merge($granted, $extra)))]); } }); echo "FORECAST_MENU_READY\n";