fix(iam): expose scoped account metadata and avoid duplicate notices

This commit is contained in:
2026-10-30 11:50:55 +08:00
parent 27cab30c48
commit a32e167f8b
4 changed files with 52 additions and 7 deletions
@@ -26,7 +26,7 @@ final class IamV2Controller extends BaseAdminController
return $this->data($fn())->header(['Cache-Control' => 'no-store', 'Referrer-Policy' => 'no-referrer']);
} catch (\Throwable $e) {
$code = preg_match('/^[A-Z_]{3,64}$/D', $e->getMessage()) ? $e->getMessage() : 'SERVICE_UNAVAILABLE';
return $this->fail($code, ['error' => ['code' => $code]])->header(['Cache-Control' => 'no-store']);
return $this->fail($code, ['error' => ['code' => $code]], 0, 0)->header(['Cache-Control' => 'no-store']);
}
}
private function session(bool $recent = false): array
@@ -14,7 +14,11 @@ final class IamV2Manager
public function binding(int $id, bool $fresh = false): array
{
$row = Db::name('iam_v2_binding')->where('admin_id', $id)->find();
if (!$fresh && $row && $row['checked_at'] > time() - 30) { return json_decode($row['payload'], true, 32, JSON_THROW_ON_ERROR); }
if (!$fresh && $row && $row['checked_at'] > time() - 30) {
$cached = json_decode($row['payload'], true, 32, JSON_THROW_ON_ERROR);
$cached['checked_at'] = (int) $row['checked_at'];
return $cached;
}
$result = $this->client->call('GET', '/bindings/by-external/' . $id);
if (($result['binding']['externalAccountId'] ?? (string) $id) !== (string) $id) { throw new RuntimeException('ACCOUNT_MISMATCH'); }
if ($result['binding'] ?? null) {
@@ -38,7 +42,8 @@ final class IamV2Manager
} elseif ($changed) {
IamV2Source::revokeGeneration($id, (int) $row['version'], (string) $row['employee_id']);
}
return $result + ['checked_at' => time()];
$result['checked_at'] = $values['checked_at'];
return $result;
}
public static function revoke(int $id): void
{
@@ -66,10 +71,29 @@ final class IamV2Manager
}
$total = (clone $db)->count();
$items = $db->field('id,account,name,disable,root')->page($page, $size)->select()->toArray();
foreach ($items as &$r) {
try { $r['iam'] = $this->binding((int) $r['id']); $r['iam']['state'] = $r['iam']['binding']['state'] ?? 'unlinked'; }
catch (\Throwable $e) { $r['iam'] = ['state' => 'unknown', 'error' => 'SERVICE_UNAVAILABLE']; }
// Resolve department labels only for already-scoped rows, never broaden the account query.
$departmentNames = [];
if ($items !== []) {
$links = Db::name('admin_dept')->whereIn('admin_id', array_column($items, 'id'))
->field('admin_id,dept_id')->order('dept_id', 'asc')->select()->toArray();
$ids = array_values(array_unique(array_column($links, 'dept_id')));
$labels = $ids ? Db::name('dept')->whereIn('id', $ids)->whereNull('delete_time')->column('name', 'id') : [];
foreach ($links as $link) {
if (isset($labels[$link['dept_id']])) { $departmentNames[$link['admin_id']][$link['dept_id']] = (string) $labels[$link['dept_id']]; }
}
}
foreach ($items as &$r) {
$r['dept_names'] = array_values($departmentNames[$r['id']] ?? []);
try { $r['iam'] = $this->binding((int) $r['id']); $r['iam']['state'] = $r['iam']['binding']['state'] ?? 'unlinked'; }
catch (\Throwable $e) {
$last = Db::name('iam_v2_binding')->where('admin_id', $r['id'])->value('checked_at');
$reason = in_array($e->getMessage(), ['BINDING_CONFLICT','ACCOUNT_MISMATCH','ACCOUNT_DISABLED','NOT_AUTHORIZED'], true)
? $e->getMessage() : 'SERVICE_UNAVAILABLE';
$r['iam'] = ['state' => 'unknown', 'error' => $reason, 'access' => ['allowed' => null, 'reason' => $reason],
'checked_at' => (int) $last > 0 ? (int) $last : null];
}
}
unset($r);
return ['items' => $items, 'total' => $total];
}
public static function input(array $input): array