@allbluecn/admin-app 0.6.19 → 0.6.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@allbluecn/admin-app",
3
- "version": "0.6.19",
3
+ "version": "0.6.21",
4
4
  "private": false,
5
5
  "license": "AGPL-3.0-or-later",
6
6
  "type": "module",
7
7
  "dependencies": {
8
- "@allbluecn/database": "0.14.15",
9
- "@allbluecn/kernel": "0.12.4",
8
+ "@allbluecn/database": "0.14.17",
9
+ "@allbluecn/kernel": "0.12.5",
10
10
  "@auth/core": "^0.41.2",
11
11
  "@base-ui/react": "^1.8.0",
12
12
  "@fontsource-variable/inter": "^5.3.0",
@@ -15,32 +15,18 @@
15
15
  // You should have received a copy of the GNU Affero General Public License
16
16
  // along with this program. If not, see <https://www.gnu.org/licenses/>.
17
17
 
18
- export type { AvatarPart, AvatarConfig, ShapeType } from "@allbluecn/kernel"
19
-
20
- export const partMax: Record<import("@allbluecn/kernel").AvatarPart, number> = {
21
- face: 15,
22
- eyes: 13,
23
- eyebrows: 15,
24
- mouth: 19,
25
- nose: 13,
26
- glasses: 14,
27
- hair: 58,
28
- accessories: 14,
29
- details: 13,
30
- beard: 16,
31
- }
32
-
33
- export const presetBgColors = [
34
- "#fff", "#fa541c", "#faad14", "#fadb14", "#a0d911",
35
- "#52c41a", "#1890ff", "#2f54eb", "#722ed1", "#eb2f96",
36
- "#bfbfbf", "#f0d5c0", "#debaba", "#babae1", "#e1bae1",
37
- "#bae1e1", "#c0d5f0", "#d5f0c0", "#e0e0e0", "#f5c6c6",
38
- ]
18
+ // 头像配置的「admin 侧入口」(2026-11-29 收敛到 @allbluecn/kernel)。
19
+ //
20
+ // 本文件原是一份 web 版 avatar-config.ts 的拷贝,且已经漂移(缺 partOrder)——
21
+ // admin 建号 / 首启 setup 与 web 注册必须生成同一口径的默认头像,
22
+ // 因此权威口径收在 kernel,这里只做再导出(保持既有 import 路径不变)。
23
+ export type { AvatarPart, AvatarConfig, ShapeType } from '@allbluecn/kernel'
39
24
 
40
- export function getRandomConfig(): import("@allbluecn/kernel").AvatarConfig {
41
- const config: Record<string, number> = {}
42
- for (const key of Object.keys(partMax) as import("@allbluecn/kernel").AvatarPart[]) {
43
- config[key] = Math.floor(Math.random() * (partMax[key] + 1))
44
- }
45
- return config as import("@allbluecn/kernel").AvatarConfig
46
- }
25
+ export {
26
+ AVATAR_PART_MAX as partMax,
27
+ AVATAR_PART_ORDER as partOrder,
28
+ AVATAR_NONE_PARTS as noneParts,
29
+ AVATAR_PRESET_BG_COLORS as presetBgColors,
30
+ DEFAULT_AVATAR_CONFIG as defaultAvatarConfig,
31
+ getRandomAvatarConfig as getRandomConfig,
32
+ } from '@allbluecn/kernel'
@@ -19,13 +19,9 @@ import { createServerFn } from '@tanstack/react-start'
19
19
  import { z } from 'zod'
20
20
  import { getPrisma } from '@allbluecn/database/get-prisma'
21
21
  import bcrypt from 'bcryptjs'
22
- import { getRandomConfig, presetBgColors } from '@/components/settings/avatar/avatar-config'
22
+ import { getRandomConfig } from '@/components/settings/avatar/avatar-config'
23
+ import { pickAvatarBgColor } from '@allbluecn/kernel'
23
24
 
24
- function pickBgColor(seed: string) {
25
- let hash = 0
26
- for (let i = 0; i < seed.length; i++) hash = seed.charCodeAt(i) + ((hash << 5) - hash)
27
- return presetBgColors[Math.abs(hash) % presetBgColors.length]
28
- }
29
25
 
30
26
  export const checkAdminExistsFn = createServerFn({ method: 'GET' }).handler(async () => {
31
27
  const prisma = await getPrisma()
@@ -69,7 +65,7 @@ export const setupAdminFn = createServerFn({ method: 'POST' })
69
65
  status: 'ACTIVE',
70
66
  avatarConfig: getRandomConfig() as object,
71
67
  bgShape: 'square',
72
- bgColor: pickBgColor(data.username),
68
+ bgColor: pickAvatarBgColor(data.username),
73
69
  },
74
70
  })
75
71
  },
@@ -0,0 +1,126 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // AllBlue - 理想之海
3
+ // Copyright (C) 2026 AllBlue Contributors
4
+ //
5
+ // This program is free software: you can redistribute it and/or modify
6
+ // it under the terms of the GNU Affero General Public License as published by
7
+ // the Free Software Foundation, either version 3 of the License, or
8
+ // (at your option) any later version.
9
+ //
10
+ // This program is distributed in the hope that it will be useful,
11
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ // GNU Affero General Public License for more details.
14
+ //
15
+ // You should have received a copy of the GNU Affero General Public License
16
+ // along with this program. If not, see <https://www.gnu.org/licenses/>.
17
+
18
+ // admin 建号必须一并生成头像(2026-11-29 补):
19
+ // 产品口径是「注册后自动生成头像」,web 注册 / OAuth 首登 / 首启 setup 都这么做,
20
+ // 唯独本入口漏了 —— 建出来的账号 avatarConfig 落到 DB 默认 `{}`,只能等该用户自己
21
+ // 首次登录时由 session 回调回填;在那之前别人看成员列表就是首字母块。
22
+ import { beforeEach, describe, expect, it, vi } from 'vitest'
23
+
24
+ vi.mock('@tanstack/react-start', () => ({
25
+ createServerFn: (_config: unknown) => {
26
+ const api: any = {
27
+ _validator: undefined,
28
+ _middlewares: [] as any[],
29
+ middleware(mws: any[]) {
30
+ this._middlewares.push(...mws)
31
+ return this
32
+ },
33
+ validator(schema: any) {
34
+ this._validator = schema
35
+ return this
36
+ },
37
+ handler(fn: Function) {
38
+ return async (args: any) => {
39
+ let data = args?.data
40
+ if (this._validator && data !== undefined) data = this._validator.parse(data)
41
+ let context: any = {}
42
+ for (const mw of this._middlewares ?? []) {
43
+ const serverFn = mw?.options?.server
44
+ if (!serverFn) continue
45
+ const result = await serverFn({
46
+ data,
47
+ context,
48
+ method: 'POST',
49
+ serverFnMeta: {},
50
+ signal: new AbortController().signal,
51
+ next: async (inner: any) => ({ context: { ...context, ...(inner?.context ?? {}) } }),
52
+ })
53
+ context = result.context
54
+ }
55
+ return fn({ data, context })
56
+ }
57
+ },
58
+ }
59
+ return api
60
+ },
61
+ createMiddleware: (opts: any) => {
62
+ const mw: any = { options: { type: opts?.type } }
63
+ mw.server = (fn: Function) => {
64
+ mw.options.server = fn
65
+ return mw
66
+ }
67
+ return mw
68
+ },
69
+ }))
70
+
71
+ vi.mock('@/server/middlewares/auth-context', () => ({
72
+ adminAuthContextMiddleware: {
73
+ options: {
74
+ server: async () => ({ context: { admin: { id: 'admin-1', role: 'SUPER_ADMIN' } } }),
75
+ },
76
+ },
77
+ }))
78
+
79
+ const db = vi.hoisted(() => ({
80
+ findUnique: vi.fn(),
81
+ create: vi.fn(),
82
+ }))
83
+
84
+ vi.mock('@allbluecn/database/get-prisma', () => ({
85
+ getPrisma: async () => ({ user: { findUnique: db.findUnique, create: db.create } }),
86
+ }))
87
+
88
+ vi.mock('bcryptjs', () => ({ default: { hash: async () => 'hashed' } }))
89
+
90
+ import { AVATAR_PART_MAX, AVATAR_PART_ORDER, AVATAR_PRESET_BG_COLORS } from '@allbluecn/kernel'
91
+ import { createUserFn } from '../create'
92
+
93
+ const input = { email: 'new@example.com', password: 'Password123', name: '新同事' }
94
+
95
+ beforeEach(() => {
96
+ vi.clearAllMocks()
97
+ db.findUnique.mockResolvedValue(null)
98
+ db.create.mockResolvedValue({ id: 'u1', email: input.email, username: input.name })
99
+ })
100
+
101
+ describe('createUserFn 建号即带头像', () => {
102
+ it('落库时写入完整 avatarConfig + 底色(不再依赖 session 回填)', async () => {
103
+ await createUserFn({ data: input } as never)
104
+
105
+ expect(db.create).toHaveBeenCalledTimes(1)
106
+ const { data } = db.create.mock.calls[0][0]
107
+
108
+ // 十个图层齐全且在合法范围内
109
+ expect(Object.keys(data.avatarConfig).sort()).toEqual([...AVATAR_PART_ORDER].sort())
110
+ for (const part of AVATAR_PART_ORDER) {
111
+ const value = data.avatarConfig[part]
112
+ expect(Number.isInteger(value), part).toBe(true)
113
+ expect(value, part).toBeGreaterThanOrEqual(0)
114
+ expect(value, part).toBeLessThanOrEqual(AVATAR_PART_MAX[part])
115
+ }
116
+
117
+ expect(data.bgShape).toBe('square')
118
+ expect(AVATAR_PRESET_BG_COLORS).toContain(data.bgColor)
119
+ })
120
+
121
+ it('邮箱已存在时不建号(头像生成不改变原有错误路径)', async () => {
122
+ db.findUnique.mockResolvedValue({ id: 'exists' })
123
+ await expect(createUserFn({ data: input } as never)).rejects.toThrow('Email already exists')
124
+ expect(db.create).not.toHaveBeenCalled()
125
+ })
126
+ })
@@ -21,6 +21,8 @@ import { getPrisma } from '@allbluecn/database/get-prisma'
21
21
  import bcrypt from 'bcryptjs'
22
22
  import { adminAuthContextMiddleware } from '@/server/middlewares/auth-context'
23
23
  import { assertCanAssignRole } from '@/server/user-admin-guard'
24
+ import { getRandomConfig } from '@/components/settings/avatar/avatar-config'
25
+ import { pickAvatarBgColor } from '@allbluecn/kernel'
24
26
 
25
27
  const createInput = z.object({
26
28
  email: z.string().email(),
@@ -42,9 +44,21 @@ export const createUserFn = createServerFn({ method: 'POST' })
42
44
  const hashedPassword = await bcrypt.hash(data.password, 12)
43
45
  // User 表无 name 列(展示名为 username),name 不能进 create data(Prisma 会抛 Unknown argument)
44
46
  const { name, ...rest } = data
47
+ // 头像:产品口径是「注册后自动生成头像」(web 注册 / OAuth 首登 / 首启 setup 都这么做)。
48
+ // 这里原先漏了,admin 建出来的账号 avatarConfig 落到 DB 默认 `{}`,只能等该用户自己
49
+ // 首次登录时由 session 回调回填——在那之前别人看成员列表就是首字母块。
50
+ const avatarConfig = getRandomConfig()
51
+ const bgColor = pickAvatarBgColor(data.name || data.email)
45
52
  try {
46
53
  return await prisma.user.create({
47
- data: { ...rest, password: hashedPassword, username: name },
54
+ data: {
55
+ ...rest,
56
+ password: hashedPassword,
57
+ username: name,
58
+ avatarConfig: avatarConfig as object,
59
+ bgShape: 'square',
60
+ bgColor,
61
+ },
48
62
  // 不回显 password 哈希
49
63
  select: { id: true, email: true, username: true, role: true, status: true },
50
64
  })