|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Controllers\Admin;
|
|
|
|
|
|
|
|
|
|
use App\Services\Auth;
|
|
|
|
|
use App\Services\AdminDataMasker;
|
|
|
|
|
use App\Services\AdminPermission;
|
|
|
|
|
use App\Services\Config;
|
|
|
|
|
use App\Services\Mail;
|
|
|
|
|
use App\Services\LoginSessionRevocationService;
|
|
|
|
|
use App\Services\RedisClient;
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
use App\Controllers\AdminController;
|
|
|
|
|
use App\Utils\Hash;
|
|
|
|
|
|
|
|
|
|
class UserController extends AdminController
|
|
|
|
|
{
|
|
|
|
|
private const TRUSTED_LOGIN_DEVICE_PREFIX = 'trusted_login_device:';
|
|
|
|
|
|
|
|
|
|
public function index($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
$table_config = [
|
|
|
|
|
'total_column' => [
|
|
|
|
|
'op' => '操作', 'id' => 'ID', 'user_name' => '用户名', 'remark' => '备注',
|
|
|
|
|
'email' => '邮箱', 'money' => '余额', 'im_type' => '联络方式类型', 'im_value' => '联络方式详情',
|
|
|
|
|
'enable' => '是否启用', 'reg_date' => '注册时间', 'reg_ip' => '注册 IP', 'ref_by' => '邀请人 ID',
|
|
|
|
|
'ref_by_name' => '邀请人账户', 'aff_money' => '推广返利余额',
|
|
|
|
|
'trusted_login_device_count' => '可信设备数',
|
|
|
|
|
],
|
|
|
|
|
'default_show_column' => ['op', 'id', 'user_name', 'remark', 'email', 'ref_by_name', 'aff_money', 'enable'],
|
|
|
|
|
'ajax_url' => 'user/ajax',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return $this->view()->assign('table_config', $table_config)->display('admin/user/index.tpl');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function createNewUser($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
if (!AdminPermission::isSuperAdmin()) {
|
|
|
|
|
return $this->echoJson($response, ['ret' => 0, 'msg' => '你没有创建用户权限']);
|
|
|
|
|
}
|
|
|
|
|
$raw = $request->getParam('userEmail', '');
|
|
|
|
|
$email = is_string($raw) ? strtolower(trim($raw)) : '';
|
|
|
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL) || strlen($email) > 254) {
|
|
|
|
|
return $this->echoJson($response, ['ret' => 0, 'msg' => '邮箱格式无效']);
|
|
|
|
|
}
|
|
|
|
|
if (User::where('email', $email)->exists()) {
|
|
|
|
|
return $this->echoJson($response, ['ret' => 0, 'msg' => '邮箱已经被注册']);
|
|
|
|
|
}
|
|
|
|
|
$password = 'A!a1' . bin2hex(random_bytes(12));
|
|
|
|
|
$user = new User();
|
|
|
|
|
$user->user_name = strstr($email, '@', true);
|
|
|
|
|
$user->email = $email;
|
|
|
|
|
$user->pass = Hash::passwordHash($password);
|
|
|
|
|
$user->enable = 1;
|
|
|
|
|
$user->is_admin = 0;
|
|
|
|
|
$user->money = 0;
|
|
|
|
|
$user->ref_by = 0;
|
|
|
|
|
$user->invite_num = max(0, (int) Config::get('inviteNum', 0));
|
|
|
|
|
$user->reg_date = date('Y-m-d H:i:s');
|
|
|
|
|
$user->reg_ip = $_SERVER['REMOTE_ADDR'] ?? '';
|
|
|
|
|
$user->theme = 'venix';
|
|
|
|
|
if (!$user->save()) {
|
|
|
|
|
return $this->echoJson($response, ['ret' => 0, 'msg' => '创建失败']);
|
|
|
|
|
}
|
|
|
|
|
$sent = Mail::send($email, Config::get('appName') . ' - 新用户注册通知', 'news/monitor.tpl', [
|
|
|
|
|
'user' => $user, 'text' => '管理员为您创建了账户,邮箱:' . $email . ',初始密码:' . $password,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return $this->echoJson($response, [
|
|
|
|
|
'ret' => 1, 'msg' => '用户已创建,随机初始密码:' . $password,
|
|
|
|
|
'email_error' => $sent ? 'success' : '邮件发送失败,请查看邮件记录',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function edit($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
if (!AdminPermission::isSuperAdmin()) {
|
|
|
|
|
return $this->echoJson($response, ['ret' => 0, 'msg' => '你没有编辑权限']);
|
|
|
|
|
}
|
|
|
|
|
$user = User::find((int) $args['id']);
|
|
|
|
|
if ($user === null) {
|
|
|
|
|
return $response->withStatus(404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->view()->assign('edit_user', $user)->display('admin/user/edit.tpl');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function update($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
if (!AdminPermission::isSuperAdmin()) {
|
|
|
|
|
return $this->echoJson($response, ['ret' => 0, 'msg' => '你没有修改权限']);
|
|
|
|
|
}
|
|
|
|
|
$user = User::find((int) $args['id']);
|
|
|
|
|
if ($user === null) {
|
|
|
|
|
return $this->echoJson($response, ['ret' => 0, 'msg' => '用户不存在']);
|
|
|
|
|
}
|
|
|
|
|
$email = $request->getParam('email', $user->email);
|
|
|
|
|
if (!is_string($email) || !filter_var($email, FILTER_VALIDATE_EMAIL) || strlen($email) > 254) {
|
|
|
|
|
return $this->echoJson($response, ['ret' => 0, 'msg' => '邮箱格式无效']);
|
|
|
|
|
}
|
|
|
|
|
$email = strtolower(trim($email));
|
|
|
|
|
if (User::where('email', $email)->where('id', '<>', $user->id)->exists()) {
|
|
|
|
|
return $this->echoJson($response, ['ret' => 0, 'msg' => '邮箱已被其他账户使用']);
|
|
|
|
|
}
|
|
|
|
|
$name = $request->getParam('user_name', $user->user_name);
|
|
|
|
|
if (!is_string($name) || trim($name) === '' || mb_strlen($name) > 128) {
|
|
|
|
|
return $this->echoJson($response, ['ret' => 0, 'msg' => '用户名需为 1 至 128 个字符']);
|
|
|
|
|
}
|
|
|
|
|
$name = (new \voku\helper\AntiXSS())->xss_clean(trim($name));
|
|
|
|
|
if ($name === '') {
|
|
|
|
|
return $this->echoJson($response, ['ret' => 0, 'msg' => '用户名不能为空']);
|
|
|
|
|
}
|
|
|
|
|
$user->user_name = $name;
|
|
|
|
|
$password = $request->getParam('pass', '');
|
|
|
|
|
if ($password !== '') {
|
|
|
|
|
$error = Hash::newPasswordError($password);
|
|
|
|
|
if ($error !== null) {
|
|
|
|
|
return $this->echoJson($response, ['ret' => 0, 'msg' => $error]);
|
|
|
|
|
}
|
|
|
|
|
$user->pass = Hash::passwordHash($password);
|
|
|
|
|
}
|
|
|
|
|
foreach (['remark', 'im_value'] as $field) {
|
|
|
|
|
$value = $request->getParam($field, $user->$field);
|
|
|
|
|
if (is_scalar($value) || $value === null) {
|
|
|
|
|
$user->$field = $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$user->email = $email;
|
|
|
|
|
$user->im_type = (int) $request->getParam('im_type', $user->im_type);
|
|
|
|
|
$user->invite_num = max(0, (int) $request->getParam('invite_num', $user->invite_num));
|
|
|
|
|
$user->enable = (int) $request->getParam('enable', $user->enable) === 1 ? 1 : 0;
|
|
|
|
|
$user->is_admin = (int) $request->getParam('is_admin', $user->is_admin) === 1;
|
|
|
|
|
$user->ref_by = max(0, (int) $request->getParam('ref_by', $user->ref_by));
|
|
|
|
|
$money = $request->getParam('money', $user->money);
|
|
|
|
|
if (!is_scalar($money) || !is_numeric($money) || !is_finite((float) $money)
|
|
|
|
|
|| (float) $money < 0 || (float) $money > 9999999999.99) {
|
|
|
|
|
return $this->echoJson($response, ['ret' => 0, 'msg' => '余额格式无效']);
|
|
|
|
|
}
|
|
|
|
|
$user->money = number_format((float) $money, 2, '.', '');
|
|
|
|
|
if (!$user->save()) {
|
|
|
|
|
return $this->echoJson($response, ['ret' => 0, 'msg' => '修改失败']);
|
|
|
|
|
}
|
|
|
|
|
if ($password !== '' || (int) $user->enable === 0) {
|
|
|
|
|
LoginSessionRevocationService::revokeUser($user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->echoJson($response, ['ret' => 1, 'msg' => '修改成功']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function delete($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
if (AdminPermission::isSuperAdmin()) {
|
|
|
|
|
$id = (int) $request->getParam('id');
|
|
|
|
|
$user = User::find($id);
|
|
|
|
|
if ($user === null || (bool) $user->is_admin) {
|
|
|
|
|
$rs['ret'] = 0;
|
|
|
|
|
$rs['msg'] = "用户不存在或不能删除管理员";
|
|
|
|
|
return $response->getBody()->write(json_encode($rs));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (!$user->kill_user()) {
|
|
|
|
|
throw new \RuntimeException('用户删除结果无效');
|
|
|
|
|
}
|
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
|
error_log('Admin user deletion failed: ' . $exception->getMessage());
|
|
|
|
|
$rs['ret'] = 0;
|
|
|
|
|
$rs['msg'] = "删除失败,请检查默认库、日志库及服务端错误日志";
|
|
|
|
|
return $response->getBody()->write(json_encode($rs));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$rs['ret'] = 1;
|
|
|
|
|
$rs['msg'] = "删除成功";
|
|
|
|
|
return $response->getBody()->write(json_encode($rs));
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
$rs['ret'] = 0;
|
|
|
|
|
$rs['msg'] = "你没有删除权限。";
|
|
|
|
|
return $response->getBody()->write(json_encode($rs));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function send($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
$subjectFromFrontEnd = $request->getParam('subject');
|
|
|
|
|
$contentFromFrontEnd = $request->getParam('content');
|
|
|
|
|
|
|
|
|
|
if (empty($subjectFromFrontEnd) || empty($contentFromFrontEnd)) {
|
|
|
|
|
return json_encode(['ret' => 0, 'msg' => '不能为空']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$id = $request->getParam('id');
|
|
|
|
|
$user = User::query()->where('id', $id)->first();
|
|
|
|
|
|
|
|
|
|
if (AdminPermission::isSuperAdmin() && $user) {
|
|
|
|
|
$to = $user->email;
|
|
|
|
|
$subject = Config::get('appName') . ' - ' . $subjectFromFrontEnd;
|
|
|
|
|
$text = $contentFromFrontEnd;
|
|
|
|
|
try {
|
|
|
|
|
$sent = Mail::send($to, $subject, 'news/monitor.tpl', [
|
|
|
|
|
"user" => $user, "text" => $text
|
|
|
|
|
], []);
|
|
|
|
|
} catch (\Throwable $e){
|
|
|
|
|
return json_encode(['ret' => 0, 'msg' => $e->getMessage()]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return json_encode($sent
|
|
|
|
|
? ['ret' => 1, 'msg' => '发送成功']
|
|
|
|
|
: ['ret' => 0, 'msg' => '发送失败,请查看邮件记录']);
|
|
|
|
|
} else {
|
|
|
|
|
return json_encode(['ret' => 0, 'msg' => "用户未找到或无权限"]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function ajax($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
$isSuperAdmin = AdminPermission::isSuperAdmin();
|
|
|
|
|
$columns = $request->getParam('columns', []);
|
|
|
|
|
$orders = $request->getParam('order', []);
|
|
|
|
|
$first = is_array($orders) && isset($orders[0]) && is_array($orders[0]) ? $orders[0] : [];
|
|
|
|
|
$columnIndex = filter_var($first['column'] ?? null, FILTER_VALIDATE_INT);
|
|
|
|
|
$requested = $columnIndex !== false && is_array($columns) && isset($columns[$columnIndex]['data'])
|
|
|
|
|
? (string) $columns[$columnIndex]['data'] : 'id';
|
|
|
|
|
$sortable = ['id', 'money', 'im_type', 'enable', 'reg_date', 'ref_by', 'aff_money'];
|
|
|
|
|
if ($isSuperAdmin) {
|
|
|
|
|
$sortable = array_merge($sortable, ['user_name', 'remark', 'email', 'im_value', 'reg_ip']);
|
|
|
|
|
}
|
|
|
|
|
$orderBy = in_array($requested, $sortable, true) ? $requested : 'id';
|
|
|
|
|
$direction = ($first['dir'] ?? '') === 'asc' ? 'asc' : 'desc';
|
|
|
|
|
$start = max(0, min(1000000, (int) $request->getParam('start', 0)));
|
|
|
|
|
$length = max(1, min(100, (int) $request->getParam('length', 10)));
|
|
|
|
|
$searchInput = $request->getParam('search', []);
|
|
|
|
|
$search = is_array($searchInput) && isset($searchInput['value']) && is_scalar($searchInput['value'])
|
|
|
|
|
? substr(trim((string) $searchInput['value']), 0, 200) : '';
|
|
|
|
|
$query = User::query();
|
|
|
|
|
if ($search !== '') {
|
|
|
|
|
if ($isSuperAdmin) {
|
|
|
|
|
$this->applyUserIdentityKeywordFilter($query, $search);
|
|
|
|
|
} elseif (ctype_digit($search)) {
|
|
|
|
|
$query->where('id', (int) $search);
|
|
|
|
|
} else {
|
|
|
|
|
$query->whereRaw('1 = 0');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$filtered = (clone $query)->count();
|
|
|
|
|
$users = $query->orderBy($orderBy, $direction)->skip($start)->limit($length)->get();
|
|
|
|
|
$ids = [];
|
|
|
|
|
$referrerIds = [];
|
|
|
|
|
foreach ($users as $user) {
|
|
|
|
|
$ids[] = (int) $user->id;
|
|
|
|
|
$referrerId = (int) ($user->ref_by ?? 0);
|
|
|
|
|
if ($referrerId > 0) {
|
|
|
|
|
$referrerIds[$referrerId] = $referrerId;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$referrerNames = [];
|
|
|
|
|
$referrerIds = array_values($referrerIds);
|
|
|
|
|
if ($referrerIds !== []) {
|
|
|
|
|
foreach (User::whereIn('id', $referrerIds)->get() as $referrer) {
|
|
|
|
|
$referrerNames[(int) $referrer->id] = AdminDataMasker::account($referrer->user_name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$deviceCounts = $this->trustedLoginDeviceCounts($ids);
|
|
|
|
|
$data = [];
|
|
|
|
|
foreach ($users as $user) {
|
|
|
|
|
$id = (int) $user->id;
|
|
|
|
|
$data[] = [
|
|
|
|
|
'op' => $isSuperAdmin
|
|
|
|
|
? '<a class="btn btn-brand" href="/admin/user/' . $id . '/edit">编辑</a>'
|
|
|
|
|
: '<span class="text-muted">只读</span>',
|
|
|
|
|
'id' => $id,
|
|
|
|
|
'user_name' => AdminDataMasker::account($user->user_name),
|
|
|
|
|
'remark' => AdminDataMasker::sensitiveText($user->remark),
|
|
|
|
|
'email' => AdminDataMasker::email($user->email),
|
|
|
|
|
'money' => $user->money,
|
|
|
|
|
'im_type' => [1 => '微信', 2 => 'QQ', 3 => '其他'][(int) $user->im_type] ?? '其他',
|
|
|
|
|
'im_value' => AdminDataMasker::account($user->im_value),
|
|
|
|
|
'enable' => (int) $user->enable === 1 ? '可用' : '禁用',
|
|
|
|
|
'reg_date' => $user->reg_date,
|
|
|
|
|
'reg_ip' => AdminDataMasker::ip($user->reg_ip),
|
|
|
|
|
'ref_by' => (int) $user->ref_by,
|
|
|
|
|
'ref_by_name' => $referrerNames[(int) $user->ref_by] ?? '',
|
|
|
|
|
'aff_money' => number_format((float) ($user->aff_money ?? 0), 2, '.', ''),
|
|
|
|
|
'trusted_login_device_count' => $deviceCounts[$id] ?? 0,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $response->withJson([
|
|
|
|
|
'draw' => max(0, (int) $request->getParam('draw', 0)),
|
|
|
|
|
'recordsTotal' => User::count(), 'recordsFiltered' => $filtered, 'data' => $data,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function clearTrustedLoginDevices($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
if (!$this->isSuperAdmin()) {
|
|
|
|
|
return $response->withJson(['ret' => 0, 'msg' => '你没有权限']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$id = isset($args['id']) ? (int) $args['id'] : 0;
|
|
|
|
|
$user = User::find($id);
|
|
|
|
|
if ($user == null) {
|
|
|
|
|
return $response->withJson(['ret' => 0, 'msg' => '用户不存在']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$redis = new RedisClient();
|
|
|
|
|
$keys = $this->trustedLoginDeviceKeys($id, $redis);
|
|
|
|
|
if (empty($keys)) {
|
|
|
|
|
return $response->withJson(['ret' => 0, 'msg' => '该用户没有可清理的安全验证缓存']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($keys as $key) {
|
|
|
|
|
$redis->del($key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $response->withJson([
|
|
|
|
|
'ret' => 1,
|
|
|
|
|
'msg' => '已清除该用户 ' . count($keys) . ' 个安全设备登录验证缓存',
|
|
|
|
|
'count' => count($keys),
|
|
|
|
|
]);
|
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
|
return $response->withJson(['ret' => 0, 'msg' => '安全验证缓存清理失败']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function clearLoginSessions($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
if (!$this->isSuperAdmin()) {
|
|
|
|
|
return $response->withJson(['ret' => 0, 'msg' => '你没有权限']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$id = isset($args['id']) ? (int) $args['id'] : 0;
|
|
|
|
|
$user = User::find($id);
|
|
|
|
|
if ($user === null) {
|
|
|
|
|
return $response->withJson(['ret' => 0, 'msg' => '用户不存在']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$admin = Auth::getUser();
|
|
|
|
|
try {
|
|
|
|
|
$version = LoginSessionRevocationService::revokeUser($user);
|
|
|
|
|
$logoutCurrent = (int) $admin->id === $id;
|
|
|
|
|
if ($logoutCurrent) {
|
|
|
|
|
try {
|
|
|
|
|
Auth::logout();
|
|
|
|
|
} catch (\Throwable $logoutException) {
|
|
|
|
|
error_log('Current admin logout after user revocation failed: ' . $logoutException->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
error_log(
|
|
|
|
|
'User login sessions revoked by admin #' . (int) $admin->id
|
|
|
|
|
. ': user #' . $id . ', version ' . $version
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return $response->withJson([
|
|
|
|
|
'ret' => 1,
|
|
|
|
|
'msg' => '已注销用户 #' . $id . ' 的全部登录状态',
|
|
|
|
|
'logout_current' => $logoutCurrent,
|
|
|
|
|
]);
|
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
|
error_log('User login session revocation failed for #' . $id . ': ' . $exception->getMessage());
|
|
|
|
|
return $response->withJson([
|
|
|
|
|
'ret' => 0,
|
|
|
|
|
'msg' => '用户登录状态清除失败,请确认已导入登录会话升级 SQL',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function applyUserIdentityKeywordFilter($query, $keyword)
|
|
|
|
|
{
|
|
|
|
|
$keyword = trim(substr((string) $keyword, 0, 100));
|
|
|
|
|
if ($keyword === '') {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!AdminPermission::isSuperAdmin()) {
|
|
|
|
|
if (ctype_digit($keyword)) {
|
|
|
|
|
$query->where('id', (int) $keyword);
|
|
|
|
|
} else {
|
|
|
|
|
$query->whereRaw('1 = 0');
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$query->where(function ($keywordQuery) use ($keyword) {
|
|
|
|
|
$like = '%' . $keyword . '%';
|
|
|
|
|
$keywordQuery->where('email', 'LIKE', $like)
|
|
|
|
|
->orWhere('user_name', 'LIKE', $like)
|
|
|
|
|
->orWhere('remark', 'LIKE', $like)
|
|
|
|
|
->orWhere('im_value', 'LIKE', $like);
|
|
|
|
|
if (ctype_digit($keyword)) {
|
|
|
|
|
$keywordQuery->orWhere('id', '=', (int) $keyword);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function trustedLoginDeviceCounts(array $userIds, ?RedisClient $redis = null)
|
|
|
|
|
{
|
|
|
|
|
$counts = [];
|
|
|
|
|
foreach ($userIds as $userId) {
|
|
|
|
|
$counts[(int) $userId] = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (empty($counts)) {
|
|
|
|
|
return $counts;
|
|
|
|
|
}
|
|
|
|
|
if ($redis === null) {
|
|
|
|
|
try {
|
|
|
|
|
$redis = new RedisClient();
|
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
|
return $counts;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$wanted = array_flip(array_keys($counts));
|
|
|
|
|
$keys = $this->scanRedisKeys(self::TRUSTED_LOGIN_DEVICE_PREFIX . '*', $redis);
|
|
|
|
|
foreach ($keys as $key) {
|
|
|
|
|
if (preg_match('/^trusted_login_device:(\d+):/', (string) $key, $matches)) {
|
|
|
|
|
$userId = (int) $matches[1];
|
|
|
|
|
if (isset($wanted[$userId])) {
|
|
|
|
|
$counts[$userId]++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $counts;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function trustedLoginDeviceKeys($userId, RedisClient $redis)
|
|
|
|
|
{
|
|
|
|
|
return $this->scanRedisKeys(self::TRUSTED_LOGIN_DEVICE_PREFIX . (int) $userId . ':*', $redis);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function scanRedisKeys($pattern, RedisClient $redis)
|
|
|
|
|
{
|
|
|
|
|
$client = $redis->getClient();
|
|
|
|
|
$keys = [];
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$cursor = '0';
|
|
|
|
|
do {
|
|
|
|
|
$result = $client->executeRaw(['SCAN', $cursor, 'MATCH', $pattern, 'COUNT', '200']);
|
|
|
|
|
if (!is_array($result) || count($result) < 2) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$cursor = (string) $result[0];
|
|
|
|
|
$batch = is_array($result[1]) ? $result[1] : [];
|
|
|
|
|
foreach ($batch as $key) {
|
|
|
|
|
$keys[] = (string) $key;
|
|
|
|
|
}
|
|
|
|
|
} while ($cursor !== '0');
|
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
|
try {
|
|
|
|
|
$keys = $client->keys($pattern);
|
|
|
|
|
} catch (\Exception $innerException) {
|
|
|
|
|
$keys = [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return array_values(array_unique($keys));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function isSuperAdmin()
|
|
|
|
|
{
|
|
|
|
|
return AdminPermission::isSuperAdmin();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|