|
|
@@ -14,6 +14,7 @@ use App\AdminRole;
|
|
|
use App\AdminUser;
|
|
|
use App\Http\Controllers\Controller;
|
|
|
use App\Http\helper\Helper;
|
|
|
+use App\Services\GoogleAuthenticatorService;
|
|
|
use App\Utility\Rbac;
|
|
|
use Illuminate\Http\Request;
|
|
|
use Illuminate\Support\Collection;
|
|
|
@@ -392,12 +393,14 @@ class AdministratorController extends Controller
|
|
|
$channels = DB::table('QPPlatformDB.dbo.ChannelPackageName')
|
|
|
->pluck('Channel', 'Channel');
|
|
|
|
|
|
+ $ga = $this->buildGaPayload($admin);
|
|
|
|
|
|
return view('admin.administrator_update', [
|
|
|
'admin' => $admin,
|
|
|
'roles' => $roles,
|
|
|
'channels'=>$channels,
|
|
|
's_role_id_arr' => $selectRoleIdArr,
|
|
|
+ 'ga' => $ga,
|
|
|
|
|
|
]);
|
|
|
}
|
|
|
@@ -411,7 +414,17 @@ class AdministratorController extends Controller
|
|
|
return $this->json(500, '该账号已存在');
|
|
|
}
|
|
|
$post['channel'] = json_encode($post['channel']);
|
|
|
- $post = array_filter($post);
|
|
|
+ $post['ga_enabled'] = empty($post['ga_enabled']) ? 0 : 1;
|
|
|
+
|
|
|
+ if ($post['ga_enabled'] == 1 && empty($post['ga_secret'])) {
|
|
|
+ return $this->json(500, '请先生成GA密钥');
|
|
|
+ }
|
|
|
+ $post = array_filter($post, function ($value, $key) {
|
|
|
+ if ($key === 'ga_enabled') {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return !($value === null || $value === '');
|
|
|
+ }, ARRAY_FILTER_USE_BOTH);
|
|
|
|
|
|
$admin->fill($post)->save();
|
|
|
|
|
|
@@ -426,6 +439,19 @@ class AdministratorController extends Controller
|
|
|
|
|
|
}
|
|
|
|
|
|
+ public function resetGaSecret($id)
|
|
|
+ {
|
|
|
+ $admin = AdminUser::findOrFail($id);
|
|
|
+ $gaService = new GoogleAuthenticatorService();
|
|
|
+ $secret = $gaService->generateSecret(32);
|
|
|
+
|
|
|
+ $admin->ga_secret = $secret;
|
|
|
+ $admin->ga_enabled = 0;
|
|
|
+ $admin->save();
|
|
|
+
|
|
|
+ return $this->json(200, 'GA密钥已重置,请扫码后再启用', $this->buildGaPayload($admin));
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* @return mixed
|
|
|
* 删除管理员
|
|
|
@@ -547,6 +573,18 @@ class AdministratorController extends Controller
|
|
|
if ($admin->status == -1) {
|
|
|
return $this->json(500, trans('cs.login.block'));
|
|
|
}
|
|
|
+ if ((int) $admin->ga_enabled !== 1 || empty($admin->ga_secret)) {
|
|
|
+ return $this->json(500, trans('cs.login.ga_required'));
|
|
|
+ }
|
|
|
+
|
|
|
+ $gaCode = isset($post['ga_code']) ? trim((string) $post['ga_code']) : '';
|
|
|
+ if ($gaCode === '') {
|
|
|
+ return $this->json(500, trans('cs.login.notice_ga_code'));
|
|
|
+ }
|
|
|
+ $gaService = new GoogleAuthenticatorService();
|
|
|
+ if (!$gaService->verifyCode($admin->ga_secret, $gaCode)) {
|
|
|
+ return $this->json(500, trans('cs.login.wrong_ga_code'));
|
|
|
+ }
|
|
|
|
|
|
$roles = $admin->roles;
|
|
|
|
|
|
@@ -623,4 +661,25 @@ class AdministratorController extends Controller
|
|
|
return redirect('/admin/login_op');
|
|
|
}
|
|
|
|
|
|
+ protected function buildGaPayload(AdminUser $admin)
|
|
|
+ {
|
|
|
+ $secret = $admin->ga_secret ?: '';
|
|
|
+ $gaService = new GoogleAuthenticatorService();
|
|
|
+ $issuer = config('app.name', 'Admin');
|
|
|
+ $otpAuthUrl = '';
|
|
|
+ $qrCodeUrl = '';
|
|
|
+
|
|
|
+ if ($secret !== '') {
|
|
|
+ $otpAuthUrl = $gaService->getOtpAuthUrl($issuer, $admin->account, $secret);
|
|
|
+ $qrCodeUrl = $gaService->getQrCodeUrl($otpAuthUrl, 200);
|
|
|
+ }
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'secret' => $secret,
|
|
|
+ 'otpauth_url' => $otpAuthUrl,
|
|
|
+ 'qr_code_url' => $qrCodeUrl,
|
|
|
+ 'enabled' => (int) $admin->ga_enabled,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
}
|