feat(停用账号): 停用/删除即强制下线并点名成因——写入口、X-Auth-Reason 头、三面接线
- Java:AdminUserUpdateRequest 加 disabled 开关(不能停自己、归属门沿用既有闸), AccountExpiryService/AdminAuthSupport 抛停用/删除子类型,GlobalExceptionHandler 打 X-Auth-Reason: disabled|deleted 响应头;业务码保持 401 不变,不惊动三个既有消费者 - 后台管理:用户页停用/解除停用按钮 + 已停用徽章;拦截器把成因随 &auth= 带给登录页, 登录页分停用/删除两种文案;源码钉子棘轮基线按正路重生成 - 用户端:kick-handler 按 X-Auth-Reason 分支亮停用/删除标记(互斥清理), 登录页两段专属文案且各自关自动登录;跳转目标仍是 /login?expired=1 - 反假绿:摘掉成因头注入实测 2/4 红(变异自证),还原等值后复跑回绿
This commit is contained in:
@@ -17,12 +17,19 @@ export const http = axios.create({
|
||||
timeout: 30_000,
|
||||
})
|
||||
|
||||
function redirectToLogin(requestUrl?: string): void {
|
||||
/** 会话失效成因(后端 X-Auth-Reason 头):disabled=已停用、deleted=已删除;其余 401 无成因。 */
|
||||
function authReasonOf(headers: unknown): string {
|
||||
const value = (headers as Record<string, unknown> | undefined)?.['x-auth-reason']
|
||||
return value === 'disabled' || value === 'deleted' ? value : ''
|
||||
}
|
||||
|
||||
function redirectToLogin(requestUrl?: string, reason = ''): void {
|
||||
if (typeof window === 'undefined') return
|
||||
// 已在登录页或失败请求即登录端点时不再跳转,避免登录页循环与吞掉登录失败反馈。
|
||||
if (!shouldRedirectUnauthorized(window.location.pathname, requestUrl)) return
|
||||
const target = loginRedirectTarget(window.location)
|
||||
window.location.assign('/admin-vue/login?redirect=' + target)
|
||||
// 成因随 query 带给登录页点名(停用/删除),登录页据此显示对应提示
|
||||
window.location.assign('/admin-vue/login?redirect=' + target + (reason ? `&auth=${reason}` : ''))
|
||||
}
|
||||
|
||||
/** 403 只在会话引导端点上才当"进不了后台"处理;其余端点的 403 交给页面自己把原因说出来。 */
|
||||
@@ -47,7 +54,7 @@ http.interceptors.response.use(
|
||||
if (isKicked(response.data)) {
|
||||
redirectToLoginKicked()
|
||||
} else if (isUnauthorized(response.data)) {
|
||||
redirectToLogin(response.config?.url)
|
||||
redirectToLogin(response.config?.url, authReasonOf(response.headers))
|
||||
} else if (isForbidden(response.data)) {
|
||||
redirectToLoginOnForbidden(response.config?.url)
|
||||
}
|
||||
@@ -57,7 +64,7 @@ http.interceptors.response.use(
|
||||
if (isKicked(error?.response?.data)) {
|
||||
redirectToLoginKicked()
|
||||
} else if (error?.response?.status === 401 || isUnauthorized(error?.response?.data)) {
|
||||
redirectToLogin(error?.config?.url)
|
||||
redirectToLogin(error?.config?.url, authReasonOf(error?.response?.headers))
|
||||
} else if (error?.response?.status === 403 || isForbidden(error?.response?.data)) {
|
||||
redirectToLoginOnForbidden(error?.config?.url)
|
||||
}
|
||||
|
||||
@@ -29,6 +29,8 @@ export function toAdminUserItem(raw: unknown): AdminUser | null {
|
||||
if (creator) item.creatorUsername = creator
|
||||
const expiresAt = text(r.expires_at)
|
||||
if (expiresAt) item.expiresAt = expiresAt
|
||||
const disabledAt = text(r.disabled_at)
|
||||
if (disabledAt) item.disabledAt = disabledAt
|
||||
const abbr = text(r.pinyin_abbr)
|
||||
if (abbr) item.pinyinAbbr = abbr
|
||||
return item
|
||||
|
||||
@@ -24,3 +24,12 @@ export async function deleteUser(uid: number): Promise<void> {
|
||||
const { data } = await http.delete<unknown>(`/api/admin/user/${uid}`)
|
||||
unwrap<unknown>(data)
|
||||
}
|
||||
|
||||
/**
|
||||
* 停用/解除停用:PUT /api/admin/user/{uid} 只带 disabled 开关(V155)。
|
||||
* 停用是硬停点:该账号已发令牌在下一次请求就被拒(401),客户端与后台会被强制退出到登录页。
|
||||
*/
|
||||
export async function setUserDisabled(uid: number, disabled: boolean): Promise<void> {
|
||||
const { data } = await http.put<unknown>(`/api/admin/user/${uid}`, { disabled })
|
||||
unwrap<unknown>(data)
|
||||
}
|
||||
|
||||
@@ -8,11 +8,12 @@ import type { AdminUser } from '@/types/admin'
|
||||
import { roleLabel } from '@/types/admin'
|
||||
import { useAdminSessionStore } from '@/stores/admin-session'
|
||||
import { useCompanyTabStore } from '@/stores/company-tab'
|
||||
import { deleteUser, fetchUserList } from '@/api/users'
|
||||
import { deleteUser, fetchUserList, setUserDisabled } from '@/api/users'
|
||||
import { fetchCompanies } from '@/api/companies'
|
||||
import { createUserFilterState, toUserListParams, type UserFilterState } from './users-filter'
|
||||
import { totalPageCount } from './user-pagination'
|
||||
import { deleteBlockReason, deleteConfirmMessage } from './user-delete-model'
|
||||
import { disableBlockReason, disableConfirmMessage, disabledBadgeText } from './user-disable-model'
|
||||
import { listPhaseOf, userListEmptyHint } from './user-list-state'
|
||||
import { editableBy } from './user-edit-model'
|
||||
import { formatExpiryCell } from './user-expiry'
|
||||
@@ -117,6 +118,24 @@ async function removeUser(row: AdminUser) {
|
||||
}
|
||||
}
|
||||
|
||||
/** 停用/解除停用:先过身份闸,原生 confirm 说出代价(对方会被强制退出到登录页)后提交。 */
|
||||
async function toggleDisable(row: AdminUser) {
|
||||
const wantDisable = !row.disabledAt
|
||||
const reason = disableBlockReason(row, currentUserId.value)
|
||||
if (reason) {
|
||||
ElMessage.warning(reason)
|
||||
return
|
||||
}
|
||||
if (!window.confirm(disableConfirmMessage(row, wantDisable))) return
|
||||
try {
|
||||
await setUserDisabled(row.id, wantDisable)
|
||||
ElMessage.success(wantDisable ? '已停用,该账号会被强制退出到登录页' : '已解除停用')
|
||||
await loadUsers()
|
||||
} catch (error) {
|
||||
ElMessage.error(error instanceof Error ? error.message : '操作失败')
|
||||
}
|
||||
}
|
||||
|
||||
function changePage(page: number) {
|
||||
filters.page = page
|
||||
void loadUsers()
|
||||
@@ -203,6 +222,7 @@ onMounted(() => {
|
||||
<th style="width: 140px">角色</th>
|
||||
<th style="width: 160px">所属管理员</th>
|
||||
<th style="width: 160px">到期时间</th>
|
||||
<th style="width: 90px">状态</th>
|
||||
<th style="width: 180px">创建时间</th>
|
||||
<th style="width: 180px">更新时间</th>
|
||||
<th style="width: 160px">操作</th>
|
||||
@@ -218,6 +238,9 @@ onMounted(() => {
|
||||
<td :class="{ 'expiry-cell-expired': formatExpiryCell(row).expired }">
|
||||
{{ formatExpiryCell(row).text }}
|
||||
</td>
|
||||
<td class="status-cell" :class="{ 'status-cell-disabled': !!row.disabledAt }" :title="row.disabledAt ? `停用于 ${row.disabledAt}` : undefined">
|
||||
{{ disabledBadgeText(row) }}
|
||||
</td>
|
||||
<td>{{ formatDateTime(row.createdAt) }}</td>
|
||||
<td>{{ formatDateTime(row.updatedAt) }}</td>
|
||||
<td class="ops-cell">
|
||||
@@ -230,6 +253,15 @@ onMounted(() => {
|
||||
>
|
||||
编辑
|
||||
</button>
|
||||
<button
|
||||
class="btn btn-sm btn-secondary"
|
||||
type="button"
|
||||
:disabled="disableBlockReason(row, currentUserId) !== undefined || !editableOf(row)"
|
||||
:title="disableBlockReason(row, currentUserId) || (!editableOf(row) ? '该账号不可操作' : undefined)"
|
||||
@click="toggleDisable(row)"
|
||||
>
|
||||
{{ row.disabledAt ? '解除停用' : '停用' }}
|
||||
</button>
|
||||
<button
|
||||
class="btn btn-sm btn-danger"
|
||||
type="button"
|
||||
@@ -243,10 +275,10 @@ onMounted(() => {
|
||||
</tr>
|
||||
</template>
|
||||
<tr v-else-if="loading">
|
||||
<td colspan="8" class="empty-tip">加载中...</td>
|
||||
<td colspan="9" class="empty-tip">加载中...</td>
|
||||
</tr>
|
||||
<tr v-else>
|
||||
<td colspan="8" class="empty-tip">{{ userListEmptyHint() }}</td>
|
||||
<td colspan="9" class="empty-tip">{{ userListEmptyHint() }}</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
@@ -421,7 +453,7 @@ h3 {
|
||||
table-layout: fixed;
|
||||
}
|
||||
.user-table-scroll > table {
|
||||
min-width: 1000px;
|
||||
min-width: 1120px;
|
||||
}
|
||||
.table-scroll th,
|
||||
.table-scroll td {
|
||||
@@ -460,6 +492,13 @@ h3 {
|
||||
color: #c0392b;
|
||||
font-weight: 600;
|
||||
}
|
||||
.status-cell {
|
||||
color: #3d7158;
|
||||
}
|
||||
.status-cell-disabled {
|
||||
color: #91474f;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.empty-tip {
|
||||
padding: 44px 24px;
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
/** 停用/解除停用确认模型:确认文案 + 不可停原因,镜像 Java updateUser 的 disabled 闸(V155)。 */
|
||||
import type { AdminUser } from '../../types/admin'
|
||||
|
||||
/** 停用/解除的二次确认文案。停用要说出代价:对方会被立即强制退出到登录页。 */
|
||||
export function disableConfirmMessage(user: AdminUser | null | undefined, disabled: boolean): string {
|
||||
const username = user?.username || ''
|
||||
return disabled
|
||||
? `停用后 "${username}" 在客户端和后台会被立即强制退出到登录页,确定停用吗?`
|
||||
: `解除停用后 "${username}" 可以重新登录,确定解除吗?`
|
||||
}
|
||||
|
||||
/** 返回不可停用原因;可操作时返回 undefined。角色边界由编辑态(editableBy)管,这里只管身份。 */
|
||||
export function disableBlockReason(
|
||||
user: AdminUser | null | undefined,
|
||||
currentUserId?: number | null,
|
||||
): string | undefined {
|
||||
if (!user) return '用户信息缺失,无法操作'
|
||||
if (currentUserId != null && user.id === currentUserId) return '不能停用自己的账号'
|
||||
return undefined
|
||||
}
|
||||
|
||||
/** 列表「状态」列徽标文案。 */
|
||||
export function disabledBadgeText(user: Pick<AdminUser, 'disabledAt'> | null | undefined): string {
|
||||
return user?.disabledAt ? '已停用' : '正常'
|
||||
}
|
||||
@@ -21,6 +21,12 @@ const logoUrl = joinAdminPath('assets', 'logo.jpg')
|
||||
const inputType = computed(() => (showPassword.value ? 'text' : 'password'))
|
||||
/** 单设备登录:被新设备顶下线后跳回登录页(整页跳转携带 ?kicked=1) */
|
||||
const kickedNotice = computed(() => route.query.kicked === '1')
|
||||
/** 会话被服务端终止的成因(拦截器读 X-Auth-Reason 带成 ?auth=):停用/删除点名,其余不显示。 */
|
||||
const authNotice = computed(() => {
|
||||
if (route.query.auth === 'disabled') return '该账号已被管理员停用,本设备已退出登录。如有疑问请联系管理员。'
|
||||
if (route.query.auth === 'deleted') return '该账号已被删除,本设备已退出登录。'
|
||||
return ''
|
||||
})
|
||||
|
||||
function deviceId(): string {
|
||||
try {
|
||||
@@ -130,6 +136,7 @@ onMounted(() => {
|
||||
</header>
|
||||
<form novalidate @submit.prevent="submit">
|
||||
<p v-if="kickedNotice" class="kicked-msg" role="alert">该账号已在其他设备登录,本设备已下线。如非本人操作,请及时修改密码。</p>
|
||||
<p v-if="authNotice" class="kicked-msg" role="alert">{{ authNotice }}</p>
|
||||
<p v-if="errorMessage" class="error-msg" role="alert">{{ errorMessage }}</p>
|
||||
<div class="form-group">
|
||||
<label for="loginUsername">用户名</label>
|
||||
|
||||
@@ -17,6 +17,8 @@ export interface AdminUser {
|
||||
updatedAt?: string
|
||||
/** 到期时间(yyyy-MM-dd HH:mm);空串/缺省 = 永不过期(V146)。 */
|
||||
expiresAt?: string
|
||||
/** 管理员停用时刻(yyyy-MM-dd HH:mm);缺省/null = 未停用(V155)。 */
|
||||
disabledAt?: string | null
|
||||
/** 自身到期剩余整天数(floor,来自 current-user);null/缺省 = 永不过期。续费提醒按它 <5 判定。 */
|
||||
daysToExpire?: number | null
|
||||
pinyinAbbr?: string
|
||||
|
||||
Reference in New Issue
Block a user