@frigear-nu/users 3.1.0 → 3.1.2
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/dist/module.d.mts +7 -0
- package/dist/module.json +2 -2
- package/dist/module.mjs +28 -7
- package/dist/runtime/access.d.ts +1 -1
- package/dist/runtime/access.js +10 -5
- package/dist/runtime/app/components/UserCreateSlideover.d.vue.ts +18 -6
- package/dist/runtime/app/components/UserCreateSlideover.vue +61 -11
- package/dist/runtime/app/components/UserCreateSlideover.vue.d.ts +18 -6
- package/dist/runtime/app/components/UserPicker.d.vue.ts +43 -0
- package/dist/runtime/app/components/UserPicker.vue +158 -0
- package/dist/runtime/app/components/UserPicker.vue.d.ts +43 -0
- package/dist/runtime/app/composables/useUserActions.js +5 -4
- package/dist/runtime/app/plugins/remember-locale.client.d.ts +2 -0
- package/dist/runtime/app/plugins/remember-locale.client.js +23 -0
- package/dist/runtime/app/tabs/Overview.vue +25 -6
- package/dist/runtime/app/utils/admin-user.d.ts +7 -2
- package/dist/runtime/db/schema.postgresql.d.ts +51 -0
- package/dist/runtime/db/schema.postgresql.js +7 -0
- package/dist/runtime/db/schema.sqlite.d.ts +57 -0
- package/dist/runtime/db/schema.sqlite.js +7 -0
- package/dist/runtime/i18n/lang/da.json +23 -5
- package/dist/runtime/i18n/lang/en.json +21 -3
- package/dist/runtime/server/notify-recipients.d.ts +3 -0
- package/dist/runtime/server/notify-recipients.js +10 -0
- package/dist/runtime/server/routes/admin/user.get.js +7 -4
- package/dist/runtime/server/routes/admin/user.patch.js +12 -5
- package/dist/runtime/server/routes/admin/users.get.js +2 -5
- package/dist/runtime/server/routes/admin/users.post.js +22 -10
- package/dist/runtime/server/routes/me/locale.put.d.ts +2 -0
- package/dist/runtime/server/routes/me/locale.put.js +21 -0
- package/dist/runtime/server/routes/me/profile.patch.js +11 -5
- package/dist/runtime/server/schemas.d.ts +10 -0
- package/dist/runtime/server/schemas.js +6 -0
- package/dist/runtime/server/search-index.d.ts +2 -0
- package/dist/runtime/server/search-index.js +7 -0
- package/dist/runtime/server/user-store.js +10 -3
- package/dist/runtime/server/utils/dto.d.ts +10 -3
- package/dist/runtime/server/utils/dto.js +4 -1
- package/dist/runtime/server/utils/user-locale.d.ts +8 -0
- package/dist/runtime/server/utils/user-locale.js +7 -0
- package/dist/runtime/server/utils/user-phone.d.ts +6 -0
- package/dist/runtime/server/utils/user-phone.js +7 -0
- package/dist/runtime/server/utils/user-refs.d.ts +14 -0
- package/dist/runtime/server/utils/user-refs.js +40 -0
- package/dist/runtime/server/utils/user-search.d.ts +9 -0
- package/dist/runtime/server/utils/user-search.js +2 -0
- package/dist/runtime/utils/api.d.ts +9 -3
- package/dist/runtime/utils/api.js +7 -1
- package/dist/runtime/utils/locale.d.ts +10 -0
- package/dist/runtime/utils/locale.js +9 -0
- package/dist/runtime/utils/phone.d.ts +4 -0
- package/dist/runtime/utils/phone.js +2 -0
- package/package.json +10 -10
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
<script setup>
|
|
2
2
|
import { usersApi } from "../../utils/api";
|
|
3
|
+
import { USER_PHONE_MAX_LENGTH, USER_PHONE_PATTERN } from "../../utils/phone";
|
|
3
4
|
import { z } from "zod";
|
|
4
5
|
import { $fetch, computed, reactive, resolveComponent, useAction, useFormat, useModuleT, watch } from "#imports";
|
|
5
6
|
defineOptions({ name: "UsersAdminOverviewTab" });
|
|
@@ -15,11 +16,13 @@ const overviewWidgets = typeof OverviewWidgets === "string" ? void 0 : OverviewW
|
|
|
15
16
|
const schema = z.object({
|
|
16
17
|
name: z.string().trim().min(1, t("admin.nameRequired", "Enter a name.")).max(120),
|
|
17
18
|
email: z.string().trim().email(t("admin.emailInvalid", "Enter a valid email address.")),
|
|
18
|
-
emailVerified: z.boolean()
|
|
19
|
+
emailVerified: z.boolean(),
|
|
20
|
+
// The server's rule (`phoneField`): digits, spaces and `+-().`; empty clears it.
|
|
21
|
+
phone: z.string().trim().max(USER_PHONE_MAX_LENGTH).regex(USER_PHONE_PATTERN, t("admin.phoneInvalid", "Enter a phone number of digits, spaces and + - ( )."))
|
|
19
22
|
});
|
|
20
|
-
const form = reactive({ name: "", email: "", emailVerified: false });
|
|
23
|
+
const form = reactive({ name: "", email: "", emailVerified: false, phone: "" });
|
|
21
24
|
function fill(value) {
|
|
22
|
-
Object.assign(form, { name: value.name, email: value.email, emailVerified: !!value.emailVerifiedAt });
|
|
25
|
+
Object.assign(form, { name: value.name, email: value.email, emailVerified: !!value.emailVerifiedAt, phone: value.phone ?? "" });
|
|
23
26
|
}
|
|
24
27
|
fill(props.user);
|
|
25
28
|
watch(() => props.user, fill);
|
|
@@ -29,10 +32,11 @@ const accountFacts = computed(() => [
|
|
|
29
32
|
{ key: "password", label: t("admin.detail.password", "Password"), value: props.user.hasPassword ? t("admin.detail.set", "Set") : null },
|
|
30
33
|
{ key: "passkeys", label: t("admin.detail.passkeys", "Passkeys"), value: props.user.passkeys }
|
|
31
34
|
]);
|
|
32
|
-
const dirty = computed(() => form.name !== props.user.name || form.email !== props.user.email || form.emailVerified !== !!props.user.emailVerifiedAt);
|
|
35
|
+
const dirty = computed(() => form.name !== props.user.name || form.email !== props.user.email || form.emailVerified !== !!props.user.emailVerifiedAt || props.user.can.pii && form.phone.trim() !== (props.user.phone ?? ""));
|
|
33
36
|
async function save(event) {
|
|
34
|
-
const { name, email, emailVerified } = event.data;
|
|
37
|
+
const { name, email, emailVerified, phone } = event.data;
|
|
35
38
|
const emailChanged = email !== props.user.email;
|
|
39
|
+
const phoneChanged = props.user.can.pii && phone !== (props.user.phone ?? "");
|
|
36
40
|
const saved = await run(
|
|
37
41
|
() => $fetch(usersApi.admin.user(props.user.id), {
|
|
38
42
|
method: "PATCH",
|
|
@@ -41,7 +45,8 @@ async function save(event) {
|
|
|
41
45
|
...emailChanged ? { email } : {},
|
|
42
46
|
// An unchanged address keeps its state unless staff toggled it; a changed one is verified by email. Both
|
|
43
47
|
// need `users:manage`, so they are only sent when they change.
|
|
44
|
-
...!emailChanged && emailVerified !== !!props.user.emailVerifiedAt ? { emailVerified } : {}
|
|
48
|
+
...!emailChanged && emailVerified !== !!props.user.emailVerifiedAt ? { emailVerified } : {},
|
|
49
|
+
...phoneChanged ? { phone: phone || null } : {}
|
|
45
50
|
}
|
|
46
51
|
}).then(() => true),
|
|
47
52
|
{ success: emailChanged ? t("admin.detail.savedEmail", "Saved. A verification email was sent to the new address.") : t("admin.detail.saved", "Saved.") }
|
|
@@ -102,6 +107,20 @@ async function save(event) {
|
|
|
102
107
|
:disabled="!user.can.manage || form.email !== user.email"
|
|
103
108
|
/>
|
|
104
109
|
</FormRow>
|
|
110
|
+
<FormRow
|
|
111
|
+
v-if="user.phone !== void 0"
|
|
112
|
+
name="phone"
|
|
113
|
+
:label="t('admin.phone', 'Phone')"
|
|
114
|
+
:description="t('admin.detail.phoneHelp', 'Used for text messages. Leave it empty to remove it.')"
|
|
115
|
+
>
|
|
116
|
+
<UInput
|
|
117
|
+
v-model="form.phone"
|
|
118
|
+
type="tel"
|
|
119
|
+
autocomplete="off"
|
|
120
|
+
class="w-full sm:max-w-sm"
|
|
121
|
+
:disabled="!user.can.pii"
|
|
122
|
+
/>
|
|
123
|
+
</FormRow>
|
|
105
124
|
</UForm>
|
|
106
125
|
<template #footer>
|
|
107
126
|
<div class="flex justify-end">
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AdminUserDto } from '../../../types.js';
|
|
1
|
+
import type { AdminUserDto, UserPickerOption } from '../../../types.js';
|
|
2
2
|
export type UserSubscription = {
|
|
3
3
|
planKey: string | null;
|
|
4
4
|
status: string;
|
|
@@ -30,14 +30,19 @@ export type UserDetail = AdminUserDto & {
|
|
|
30
30
|
hasPassword: boolean;
|
|
31
31
|
passkeys: number;
|
|
32
32
|
subscription: UserSubscription | null;
|
|
33
|
-
/** `manage`: change or verify the email address (`users:manage`). */
|
|
33
|
+
/** `manage`: change or verify the email address (`users:manage`). `pii`: see and change the phone number (`users:pii`). */
|
|
34
34
|
can: {
|
|
35
35
|
update: boolean;
|
|
36
36
|
delete: boolean;
|
|
37
37
|
manage: boolean;
|
|
38
|
+
pii: boolean;
|
|
38
39
|
};
|
|
39
40
|
};
|
|
40
41
|
/** What the actions need of a user: the list rows and the detail both satisfy it. */
|
|
41
42
|
export type UserSubject = Pick<UserRow, 'id' | 'name' | 'emailVerifiedAt'>;
|
|
42
43
|
/** The key of the list's `useFetch`, refreshed after a user is created or deleted. */
|
|
43
44
|
export declare const ADMIN_USERS_KEY = "admin-users";
|
|
45
|
+
/** What the create form closes with: the created user, the existing user staff chose after a 409, or `false` (cancelled). */
|
|
46
|
+
export type UserCreateResult = UserRow | {
|
|
47
|
+
existing: UserPickerOption;
|
|
48
|
+
} | false;
|
|
@@ -36,6 +36,23 @@ export declare const users: import("drizzle-orm/pg-core").PgTableWithColumns<{
|
|
|
36
36
|
identity: undefined;
|
|
37
37
|
generated: undefined;
|
|
38
38
|
}, {}, {}>;
|
|
39
|
+
searchText: import("drizzle-orm/pg-core").PgColumn<{
|
|
40
|
+
name: "searchText";
|
|
41
|
+
tableName: "users";
|
|
42
|
+
dataType: "string";
|
|
43
|
+
columnType: "PgText";
|
|
44
|
+
data: string;
|
|
45
|
+
driverParam: string;
|
|
46
|
+
notNull: true;
|
|
47
|
+
hasDefault: true;
|
|
48
|
+
isPrimaryKey: false;
|
|
49
|
+
isAutoincrement: false;
|
|
50
|
+
hasRuntimeDefault: false;
|
|
51
|
+
enumValues: [string, ...string[]];
|
|
52
|
+
baseColumn: never;
|
|
53
|
+
identity: undefined;
|
|
54
|
+
generated: undefined;
|
|
55
|
+
}, {}, {}>;
|
|
39
56
|
email: import("drizzle-orm/pg-core").PgColumn<{
|
|
40
57
|
name: "email";
|
|
41
58
|
tableName: "users";
|
|
@@ -70,6 +87,40 @@ export declare const users: import("drizzle-orm/pg-core").PgTableWithColumns<{
|
|
|
70
87
|
identity: undefined;
|
|
71
88
|
generated: undefined;
|
|
72
89
|
}, {}, {}>;
|
|
90
|
+
phone: import("drizzle-orm/pg-core").PgColumn<{
|
|
91
|
+
name: "phone";
|
|
92
|
+
tableName: "users";
|
|
93
|
+
dataType: "string";
|
|
94
|
+
columnType: "PgText";
|
|
95
|
+
data: string;
|
|
96
|
+
driverParam: string;
|
|
97
|
+
notNull: false;
|
|
98
|
+
hasDefault: false;
|
|
99
|
+
isPrimaryKey: false;
|
|
100
|
+
isAutoincrement: false;
|
|
101
|
+
hasRuntimeDefault: false;
|
|
102
|
+
enumValues: [string, ...string[]];
|
|
103
|
+
baseColumn: never;
|
|
104
|
+
identity: undefined;
|
|
105
|
+
generated: undefined;
|
|
106
|
+
}, {}, {}>;
|
|
107
|
+
locale: import("drizzle-orm/pg-core").PgColumn<{
|
|
108
|
+
name: "locale";
|
|
109
|
+
tableName: "users";
|
|
110
|
+
dataType: "string";
|
|
111
|
+
columnType: "PgText";
|
|
112
|
+
data: string;
|
|
113
|
+
driverParam: string;
|
|
114
|
+
notNull: false;
|
|
115
|
+
hasDefault: false;
|
|
116
|
+
isPrimaryKey: false;
|
|
117
|
+
isAutoincrement: false;
|
|
118
|
+
hasRuntimeDefault: false;
|
|
119
|
+
enumValues: [string, ...string[]];
|
|
120
|
+
baseColumn: never;
|
|
121
|
+
identity: undefined;
|
|
122
|
+
generated: undefined;
|
|
123
|
+
}, {}, {}>;
|
|
73
124
|
acceptedTermsAt: import("drizzle-orm/pg-core").PgColumn<{
|
|
74
125
|
name: "accepted_terms_at";
|
|
75
126
|
tableName: "users";
|
|
@@ -2,8 +2,15 @@ import { pgTable, text, serial, bigint, timestamp } from "drizzle-orm/pg-core";
|
|
|
2
2
|
export const users = pgTable("users", {
|
|
3
3
|
id: serial().primaryKey(),
|
|
4
4
|
name: text().notNull(),
|
|
5
|
+
// `name` folded for search (`searchText` of `@frigear-nu/module-kit/server`), written with it: `searchUserRefs` and
|
|
6
|
+
// other modules' user searches match it.
|
|
7
|
+
searchText: text().notNull().default(""),
|
|
5
8
|
email: text().notNull().unique(),
|
|
6
9
|
avatar: text(),
|
|
10
|
+
// Personal data: shown to staff with `users:pii` only, and to other modules' staff through their own `:pii`.
|
|
11
|
+
phone: text(),
|
|
12
|
+
// The user's language (an i18n locale code such as `da`, or a short BCP-47 tag): messages to them use it.
|
|
13
|
+
locale: text(),
|
|
7
14
|
acceptedTermsAt: bigint("accepted_terms_at", { mode: "number" }),
|
|
8
15
|
createdAt: timestamp({ withTimezone: true }).notNull().$defaultFn(() => /* @__PURE__ */ new Date()),
|
|
9
16
|
updatedAt: timestamp({ withTimezone: true }).notNull().$onUpdateFn(() => /* @__PURE__ */ new Date())
|
|
@@ -38,6 +38,25 @@ export declare const users: import("drizzle-orm/sqlite-core").SQLiteTableWithCol
|
|
|
38
38
|
}, {}, {
|
|
39
39
|
length: undefined;
|
|
40
40
|
}>;
|
|
41
|
+
searchText: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
42
|
+
name: "searchText";
|
|
43
|
+
tableName: "users";
|
|
44
|
+
dataType: "string";
|
|
45
|
+
columnType: "SQLiteText";
|
|
46
|
+
data: string;
|
|
47
|
+
driverParam: string;
|
|
48
|
+
notNull: true;
|
|
49
|
+
hasDefault: true;
|
|
50
|
+
isPrimaryKey: false;
|
|
51
|
+
isAutoincrement: false;
|
|
52
|
+
hasRuntimeDefault: false;
|
|
53
|
+
enumValues: [string, ...string[]];
|
|
54
|
+
baseColumn: never;
|
|
55
|
+
identity: undefined;
|
|
56
|
+
generated: undefined;
|
|
57
|
+
}, {}, {
|
|
58
|
+
length: undefined;
|
|
59
|
+
}>;
|
|
41
60
|
email: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
42
61
|
name: "email";
|
|
43
62
|
tableName: "users";
|
|
@@ -76,6 +95,44 @@ export declare const users: import("drizzle-orm/sqlite-core").SQLiteTableWithCol
|
|
|
76
95
|
}, {}, {
|
|
77
96
|
length: undefined;
|
|
78
97
|
}>;
|
|
98
|
+
phone: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
99
|
+
name: "phone";
|
|
100
|
+
tableName: "users";
|
|
101
|
+
dataType: "string";
|
|
102
|
+
columnType: "SQLiteText";
|
|
103
|
+
data: string;
|
|
104
|
+
driverParam: string;
|
|
105
|
+
notNull: false;
|
|
106
|
+
hasDefault: false;
|
|
107
|
+
isPrimaryKey: false;
|
|
108
|
+
isAutoincrement: false;
|
|
109
|
+
hasRuntimeDefault: false;
|
|
110
|
+
enumValues: [string, ...string[]];
|
|
111
|
+
baseColumn: never;
|
|
112
|
+
identity: undefined;
|
|
113
|
+
generated: undefined;
|
|
114
|
+
}, {}, {
|
|
115
|
+
length: undefined;
|
|
116
|
+
}>;
|
|
117
|
+
locale: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
118
|
+
name: "locale";
|
|
119
|
+
tableName: "users";
|
|
120
|
+
dataType: "string";
|
|
121
|
+
columnType: "SQLiteText";
|
|
122
|
+
data: string;
|
|
123
|
+
driverParam: string;
|
|
124
|
+
notNull: false;
|
|
125
|
+
hasDefault: false;
|
|
126
|
+
isPrimaryKey: false;
|
|
127
|
+
isAutoincrement: false;
|
|
128
|
+
hasRuntimeDefault: false;
|
|
129
|
+
enumValues: [string, ...string[]];
|
|
130
|
+
baseColumn: never;
|
|
131
|
+
identity: undefined;
|
|
132
|
+
generated: undefined;
|
|
133
|
+
}, {}, {
|
|
134
|
+
length: undefined;
|
|
135
|
+
}>;
|
|
79
136
|
acceptedTermsAt: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
80
137
|
name: "accepted_terms_at";
|
|
81
138
|
tableName: "users";
|
|
@@ -2,8 +2,15 @@ import { sqliteTable, text, integer } from "drizzle-orm/sqlite-core";
|
|
|
2
2
|
export const users = sqliteTable("users", {
|
|
3
3
|
id: integer().primaryKey({ autoIncrement: true }),
|
|
4
4
|
name: text().notNull(),
|
|
5
|
+
// `name` folded for search (`searchText` of `@frigear-nu/module-kit/server`), written with it: `searchUserRefs` and
|
|
6
|
+
// other modules' user searches match it.
|
|
7
|
+
searchText: text().notNull().default(""),
|
|
5
8
|
email: text().notNull().unique(),
|
|
6
9
|
avatar: text(),
|
|
10
|
+
// Personal data: shown to staff with `users:pii` only, and to other modules' staff through their own `:pii`.
|
|
11
|
+
phone: text(),
|
|
12
|
+
// The user's language (an i18n locale code such as `da`, or a short BCP-47 tag): messages to them use it.
|
|
13
|
+
locale: text(),
|
|
7
14
|
acceptedTermsAt: integer("accepted_terms_at"),
|
|
8
15
|
createdAt: integer({ mode: "timestamp" }).notNull().$defaultFn(() => /* @__PURE__ */ new Date()),
|
|
9
16
|
updatedAt: integer({ mode: "timestamp" }).notNull().$onUpdateFn(() => /* @__PURE__ */ new Date())
|
|
@@ -32,8 +32,8 @@
|
|
|
32
32
|
"created": "Oprettet",
|
|
33
33
|
"filters": {
|
|
34
34
|
"all": "Alle",
|
|
35
|
-
"verified": "
|
|
36
|
-
"unverified": "Ikke
|
|
35
|
+
"verified": "Verificeret",
|
|
36
|
+
"unverified": "Ikke verificeret",
|
|
37
37
|
"role": "Præcis rolle",
|
|
38
38
|
"clear": "Ryd filtre",
|
|
39
39
|
"allRoles": "Alle roller"
|
|
@@ -52,7 +52,8 @@
|
|
|
52
52
|
"loadFailed": "Brugeren kunne ikke indlæses",
|
|
53
53
|
"never": "Aldrig",
|
|
54
54
|
"saveProfile": "Gem profil",
|
|
55
|
-
"notFoundHint": "Den er måske slettet."
|
|
55
|
+
"notFoundHint": "Den er måske slettet.",
|
|
56
|
+
"phoneHelp": "Bruges til sms'er. Lad feltet stå tomt for at fjerne det."
|
|
56
57
|
},
|
|
57
58
|
"tabs": {
|
|
58
59
|
"overview": "Oversigt"
|
|
@@ -74,7 +75,17 @@
|
|
|
74
75
|
"deleteTitle": "Slet {name}?",
|
|
75
76
|
"deleteBody": "Kontoen og dens loginmetoder fjernes permanent. Det kan ikke fortrydes.",
|
|
76
77
|
"deleted": "Brugeren er slettet.",
|
|
77
|
-
"roles": "Roller"
|
|
78
|
+
"roles": "Roller",
|
|
79
|
+
"phone": "Telefon",
|
|
80
|
+
"phoneInvalid": "Angiv et telefonnummer med cifre, mellemrum og + - ( ).",
|
|
81
|
+
"createdNamed": "{name} er oprettet",
|
|
82
|
+
"createFailed": "Brugeren kunne ikke oprettes",
|
|
83
|
+
"emailTaken": "Denne e-mail har allerede en konto",
|
|
84
|
+
"emailTakenHint": "{email} tilhører en eksisterende bruger. Vælg den bruger i stedet for at oprette en ny.",
|
|
85
|
+
"selectExisting": "Vælg eksisterende bruger",
|
|
86
|
+
"createPhoneHelp": "Valgfrit. Bruges til sms.",
|
|
87
|
+
"sendInvite": "Send invitation på e-mail",
|
|
88
|
+
"sendInviteHelp": "Et engangslink til at logge ind og bekræfte adressen, gyldigt i flere dage."
|
|
78
89
|
},
|
|
79
90
|
"emailChange": {
|
|
80
91
|
"verify": {
|
|
@@ -125,7 +136,14 @@
|
|
|
125
136
|
"create": "Tilføje brugere",
|
|
126
137
|
"update": "Redigere brugere",
|
|
127
138
|
"delete": "Slette brugere",
|
|
128
|
-
"manage": "Administrere brugeres login"
|
|
139
|
+
"manage": "Administrere brugeres login",
|
|
140
|
+
"pii": "Se brugeres telefonnumre"
|
|
141
|
+
},
|
|
142
|
+
"picker": {
|
|
143
|
+
"placeholder": "Søg efter personer",
|
|
144
|
+
"create": "Opret bruger \"{term}\"",
|
|
145
|
+
"empty": "Ingen matcher",
|
|
146
|
+
"hint": "Skriv et navn for at søge"
|
|
129
147
|
}
|
|
130
148
|
}
|
|
131
149
|
}
|
|
@@ -52,7 +52,8 @@
|
|
|
52
52
|
"loadFailed": "The user could not be loaded",
|
|
53
53
|
"never": "Never",
|
|
54
54
|
"saveProfile": "Save profile",
|
|
55
|
-
"notFoundHint": "It may have been deleted."
|
|
55
|
+
"notFoundHint": "It may have been deleted.",
|
|
56
|
+
"phoneHelp": "Used for text messages. Leave it empty to remove it."
|
|
56
57
|
},
|
|
57
58
|
"tabs": {
|
|
58
59
|
"overview": "Overview"
|
|
@@ -74,7 +75,17 @@
|
|
|
74
75
|
"deleteTitle": "Delete {name}?",
|
|
75
76
|
"deleteBody": "This permanently removes the account and its sign-in methods. It cannot be undone.",
|
|
76
77
|
"deleted": "User deleted.",
|
|
77
|
-
"roles": "Roles"
|
|
78
|
+
"roles": "Roles",
|
|
79
|
+
"phone": "Phone",
|
|
80
|
+
"phoneInvalid": "Enter a phone number of digits, spaces and + - ( ).",
|
|
81
|
+
"createdNamed": "Created {name}",
|
|
82
|
+
"createFailed": "The user could not be created",
|
|
83
|
+
"emailTaken": "This email already has an account",
|
|
84
|
+
"emailTakenHint": "{email} belongs to an existing user. Select that user instead of creating a new one.",
|
|
85
|
+
"selectExisting": "Select existing user",
|
|
86
|
+
"createPhoneHelp": "Optional. Used for text messages.",
|
|
87
|
+
"sendInvite": "Send invite email",
|
|
88
|
+
"sendInviteHelp": "A single-use link to sign in and verify the address, valid for several days."
|
|
78
89
|
},
|
|
79
90
|
"emailChange": {
|
|
80
91
|
"verify": {
|
|
@@ -125,7 +136,14 @@
|
|
|
125
136
|
"create": "Add users",
|
|
126
137
|
"update": "Edit users",
|
|
127
138
|
"delete": "Delete users",
|
|
128
|
-
"manage": "Manage sign-in of users"
|
|
139
|
+
"manage": "Manage sign-in of users",
|
|
140
|
+
"pii": "See users' phone numbers"
|
|
141
|
+
},
|
|
142
|
+
"picker": {
|
|
143
|
+
"placeholder": "Search people",
|
|
144
|
+
"create": "Create user \"{term}\"",
|
|
145
|
+
"empty": "Nobody matches",
|
|
146
|
+
"hint": "Type a name to search"
|
|
129
147
|
}
|
|
130
148
|
}
|
|
131
149
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { db, schema } from "@nuxthub/db";
|
|
2
|
+
import { eq } from "drizzle-orm";
|
|
3
|
+
const store = {
|
|
4
|
+
async findRecipient(userId) {
|
|
5
|
+
const [row] = await db.select({ email: schema.users.email, phone: schema.users.phone, locale: schema.users.locale }).from(schema.users).where(eq(schema.users.id, userId)).limit(1);
|
|
6
|
+
if (!row) return null;
|
|
7
|
+
return { email: row.email || null, phone: row.phone?.trim() || null, locale: row.locale || null };
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
export default store;
|
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
import { db, schema } from "@nuxthub/db";
|
|
2
2
|
import { count, eq } from "drizzle-orm";
|
|
3
|
+
import { usersAccess } from "../../../access.js";
|
|
3
4
|
export default defineEventHandler(async (event) => {
|
|
4
5
|
const { actor, target } = await loadUsersAdminTarget(event, "view");
|
|
5
6
|
const passkeys = schema.userCredentials ? (await db.select({ total: count() }).from(schema.userCredentials).where(eq(schema.userCredentials.userId, target.id)))[0]?.total ?? 0 : 0;
|
|
6
7
|
const subscription = (await latestSubscriptions([target.id])).get(target.id) ?? null;
|
|
7
|
-
const [canUpdate, canDelete, canManage] = await Promise.all([
|
|
8
|
+
const [canUpdate, canDelete, canManage, pii] = await Promise.all([
|
|
8
9
|
canUsersAdminTarget(event, "update", target),
|
|
9
10
|
canUsersAdminTarget(event, "delete", target),
|
|
10
|
-
canUsersAdminTarget(event, "manage", target)
|
|
11
|
+
canUsersAdminTarget(event, "manage", target),
|
|
12
|
+
// The phone number is personal data: shown (and editable) with `users:pii` only.
|
|
13
|
+
allows(event, usersAccess.permissions.pii, target)
|
|
11
14
|
]);
|
|
12
15
|
return {
|
|
13
|
-
...toAdminUserDto(target),
|
|
16
|
+
...toAdminUserDto(target, { pii }),
|
|
14
17
|
hasPassword: !!target.password,
|
|
15
18
|
passkeys,
|
|
16
19
|
subscription,
|
|
17
|
-
can: { update: canUpdate, delete: canDelete && target.id !== actor.id, manage: canUpdate && canManage }
|
|
20
|
+
can: { update: canUpdate, delete: canDelete && target.id !== actor.id, manage: canUpdate && canManage, pii: canUpdate && pii }
|
|
18
21
|
};
|
|
19
22
|
});
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { db, schema } from "@nuxthub/db";
|
|
2
2
|
import { eq } from "drizzle-orm";
|
|
3
3
|
import { z } from "zod";
|
|
4
|
-
import {
|
|
4
|
+
import { usersAccess } from "../../../access.js";
|
|
5
|
+
import { emailField, nameField, phoneField } from "../../schemas.js";
|
|
5
6
|
const bodySchema = z.object({
|
|
6
7
|
name: nameField().optional(),
|
|
7
8
|
email: emailField().optional(),
|
|
8
|
-
emailVerified: z.boolean().optional()
|
|
9
|
+
emailVerified: z.boolean().optional(),
|
|
10
|
+
// `null` or an empty string clears it; changing it needs `users:pii` as well.
|
|
11
|
+
phone: phoneField().optional()
|
|
9
12
|
});
|
|
10
13
|
export default defineEventHandler(async (event) => {
|
|
11
14
|
const { target } = await loadUsersAdminTarget(event, "update");
|
|
@@ -13,17 +16,21 @@ export default defineEventHandler(async (event) => {
|
|
|
13
16
|
const emailChanged = !!input.email && !sameUserEmail(input.email, target.email);
|
|
14
17
|
if (emailChanged || input.emailVerified !== void 0) await authorize(event, USERS_MANAGE_PERMISSION, target);
|
|
15
18
|
if (emailChanged) await assertEmailAvailable(input.email, { exceptUserId: target.id });
|
|
19
|
+
if (input.phone !== void 0) await authorize(event, usersAccess.permissions.pii, target);
|
|
20
|
+
const phoneChanged = input.phone !== void 0 && input.phone !== (target.phone ?? null);
|
|
21
|
+
const pii = input.phone !== void 0 || await allows(event, usersAccess.permissions.pii, target);
|
|
16
22
|
const changes = {
|
|
17
|
-
...input.name ? { name: input.name } : {},
|
|
23
|
+
...input.name ? withUserSearchText({ name: input.name }) : {},
|
|
24
|
+
...phoneChanged ? { phone: input.phone } : {},
|
|
18
25
|
// A changed address was typed in by staff, so it is unverified until its owner confirms it (unless staff
|
|
19
26
|
// explicitly marks it verified in the same request).
|
|
20
27
|
...emailChanged ? { email: input.email, emailVerifiedAt: input.emailVerified ? /* @__PURE__ */ new Date() : null } : {},
|
|
21
28
|
...!emailChanged && input.emailVerified !== void 0 ? { emailVerifiedAt: input.emailVerified ? target.emailVerifiedAt ?? /* @__PURE__ */ new Date() : null } : {}
|
|
22
29
|
};
|
|
23
|
-
if (!Object.keys(changes).length) return toAdminUserDto(target);
|
|
30
|
+
if (!Object.keys(changes).length) return toAdminUserDto(target, { pii });
|
|
24
31
|
const [updated] = await db.update(schema.users).set(changes).where(eq(schema.users.id, target.id)).returning();
|
|
25
32
|
if (emailChanged && !input.emailVerified) {
|
|
26
33
|
await sendVerificationLink(event, updated);
|
|
27
34
|
}
|
|
28
|
-
return toAdminUserDto(updated);
|
|
35
|
+
return toAdminUserDto(updated, { pii });
|
|
29
36
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { db, schema } from "@nuxthub/db";
|
|
2
2
|
import { and, asc, count, inArray, isNotNull, isNull, or, sql } from "drizzle-orm";
|
|
3
3
|
import { z } from "zod";
|
|
4
|
-
import {
|
|
4
|
+
import { likeContains, orderByQuery, pageQuery, pageRange, paginate, searchContains } from "@frigear-nu/module-kit/server";
|
|
5
5
|
const querySchema = pageQuery({
|
|
6
6
|
sort: ["id", "name", "email", "role", "createdAt", "lastLoginAt"],
|
|
7
7
|
defaultSort: "createdAt",
|
|
@@ -15,10 +15,7 @@ export default defineEventHandler(async (event) => {
|
|
|
15
15
|
const query = await getValidatedQuery(event, querySchema.parse);
|
|
16
16
|
const userRoles = usersRoleAssignments();
|
|
17
17
|
const conditions = [];
|
|
18
|
-
if (query.q)
|
|
19
|
-
const pattern = `%${escapeLike(query.q)}%`;
|
|
20
|
-
conditions.push(or(like(schema.users.name, pattern), like(schema.users.email, pattern)));
|
|
21
|
-
}
|
|
18
|
+
if (query.q) conditions.push(or(searchContains(schema.users.searchText, query.q), likeContains(schema.users.email, query.q)));
|
|
22
19
|
if (query.role && userRoles) {
|
|
23
20
|
conditions.push(sql`exists (select 1 from ${userRoles} where ${userRoles.userId} = ${schema.users.id} and ${userRoles.role} = ${query.role})`);
|
|
24
21
|
}
|
|
@@ -1,29 +1,41 @@
|
|
|
1
1
|
import { db, schema } from "@nuxthub/db";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
-
import { apiError, created } from "@frigear-nu/module-kit/server";
|
|
4
|
-
import {
|
|
5
|
-
|
|
3
|
+
import { apiError, conflict, created } from "@frigear-nu/module-kit/server";
|
|
4
|
+
import { usersAccess } from "../../../access.js";
|
|
5
|
+
import { emailField, localeField, nameField, phoneField } from "../../schemas.js";
|
|
6
|
+
const bodySchema = (locales) => z.object({
|
|
6
7
|
name: nameField(),
|
|
7
8
|
email: emailField(),
|
|
9
|
+
// Optional, allowed with `users:create`: staff type it in once. Reading or changing it later is `users:pii`. Phone
|
|
10
|
+
// numbers are not unique, so a number another user has is stored too and never answered differently.
|
|
11
|
+
phone: phoneField().optional(),
|
|
12
|
+
// The language to write to the user in: the creating staff member's current one (the admin UI sends it), or unset.
|
|
13
|
+
locale: localeField(locales).optional(),
|
|
8
14
|
// Verified accounts are signed in by the emailed link below; leave unverified to let the user verify themselves.
|
|
9
15
|
emailVerified: z.boolean().default(false),
|
|
16
|
+
// The invite: a single-use sign-in link, valid for `authentication.settings.inviteTtl` (7 days by default).
|
|
10
17
|
sendWelcomeEmail: z.boolean().default(true)
|
|
11
18
|
});
|
|
12
19
|
export default defineEventHandler(async (event) => {
|
|
13
20
|
await authorizeUsersAdmin(event, "create");
|
|
14
|
-
const input = await readValidatedBody(event, bodySchema.parse);
|
|
21
|
+
const input = await readValidatedBody(event, bodySchema(usersLocales()).parse);
|
|
15
22
|
if (input.emailVerified) await authorize(event, USERS_MANAGE_PERMISSION);
|
|
16
|
-
await
|
|
17
|
-
|
|
23
|
+
const holder = await findUserByEmail(input.email);
|
|
24
|
+
if (holder) {
|
|
25
|
+
throw conflict("A user with this email already exists.", { field: "email", userId: holder.id });
|
|
26
|
+
}
|
|
27
|
+
const [user] = await db.insert(schema.users).values(withUserSearchText({
|
|
18
28
|
name: input.name,
|
|
19
29
|
email: input.email,
|
|
30
|
+
phone: input.phone ?? null,
|
|
31
|
+
locale: input.locale ?? null,
|
|
20
32
|
emailVerifiedAt: input.emailVerified ? /* @__PURE__ */ new Date() : null
|
|
21
|
-
}).returning();
|
|
33
|
+
})).returning();
|
|
22
34
|
if (!user) {
|
|
23
35
|
throw apiError(500, "internal_error", "Failed to create the user.");
|
|
24
36
|
}
|
|
25
|
-
if (input.sendWelcomeEmail) {
|
|
26
|
-
await sendVerificationLink(event, user);
|
|
37
|
+
if (input.sendWelcomeEmail && useRuntimeConfig(event).public.authentication?.mode !== "resource") {
|
|
38
|
+
await sendVerificationLink(event, user, { invite: true });
|
|
27
39
|
}
|
|
28
|
-
return created(event, toAdminUserDto(user));
|
|
40
|
+
return created(event, toAdminUserDto(user, { pii: await allows(event, usersAccess.permissions.pii) }));
|
|
29
41
|
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { db, schema } from "@nuxthub/db";
|
|
2
|
+
import { and, eq, isNull, or } from "drizzle-orm";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { notFound } from "@frigear-nu/module-kit/server";
|
|
5
|
+
import { localeField } from "../../schemas.js";
|
|
6
|
+
const bodySchema = (locales) => z.object({
|
|
7
|
+
locale: localeField(locales).refine((value) => value !== null, { message: "Choose a language." }),
|
|
8
|
+
// Only fill an empty language: the app's language at sign-in or on a switch never replaces one the user chose.
|
|
9
|
+
ifUnset: z.boolean().optional()
|
|
10
|
+
});
|
|
11
|
+
export default defineEventHandler(async (event) => {
|
|
12
|
+
const { user, kind } = await authorize(event, "profile:write");
|
|
13
|
+
const { locale, ifUnset } = await readValidatedBody(event, bodySchema(usersLocales()).parse);
|
|
14
|
+
const userId = Number(user.id);
|
|
15
|
+
const own = eq(schema.users.id, userId);
|
|
16
|
+
await db.update(schema.users).set({ locale, updatedAt: /* @__PURE__ */ new Date() }).where(ifUnset ? and(own, or(isNull(schema.users.locale), eq(schema.users.locale, ""))) : own);
|
|
17
|
+
const [row] = await db.select().from(schema.users).where(own).limit(1);
|
|
18
|
+
if (!row) throw notFound("User not found.");
|
|
19
|
+
if (kind === "session") await setUserSession(event, { user: mapUserToSession(row) });
|
|
20
|
+
return toProfileDto(row);
|
|
21
|
+
});
|
|
@@ -2,18 +2,24 @@ import { db, schema } from "@nuxthub/db";
|
|
|
2
2
|
import { eq } from "drizzle-orm";
|
|
3
3
|
import { z } from "zod";
|
|
4
4
|
import { notFound } from "@frigear-nu/module-kit/server";
|
|
5
|
-
import { avatarField, nameField } from "../../schemas.js";
|
|
6
|
-
const bodySchema = z.object({
|
|
5
|
+
import { avatarField, localeField, nameField, phoneField } from "../../schemas.js";
|
|
6
|
+
const bodySchema = (locales) => z.object({
|
|
7
7
|
name: nameField().optional(),
|
|
8
8
|
// `null` clears the avatar; leaving it out keeps it.
|
|
9
|
-
avatar: avatarField().optional()
|
|
9
|
+
avatar: avatarField().optional(),
|
|
10
|
+
// `null` or an empty string clears the phone number; leaving it out keeps it.
|
|
11
|
+
phone: phoneField().optional(),
|
|
12
|
+
// The user's language: one of the app's i18n locales (else a short BCP-47 tag); `null` or `''` clears it.
|
|
13
|
+
locale: localeField(locales).optional()
|
|
10
14
|
});
|
|
11
15
|
export default defineEventHandler(async (event) => {
|
|
12
16
|
const { user, kind } = await authorize(event, "profile:write");
|
|
13
|
-
const { name, avatar } = await readValidatedBody(event, bodySchema.parse);
|
|
17
|
+
const { name, avatar, phone, locale } = await readValidatedBody(event, bodySchema(usersLocales()).parse);
|
|
14
18
|
const [updated] = await db.update(schema.users).set({
|
|
15
|
-
...name !== void 0 ? { name } : {},
|
|
19
|
+
...name !== void 0 ? withUserSearchText({ name }) : {},
|
|
16
20
|
...avatar !== void 0 ? { avatar } : {},
|
|
21
|
+
...phone !== void 0 ? { phone } : {},
|
|
22
|
+
...locale !== void 0 ? { locale } : {},
|
|
17
23
|
updatedAt: /* @__PURE__ */ new Date()
|
|
18
24
|
}).where(eq(schema.users.id, user.id)).returning();
|
|
19
25
|
if (!updated) throw notFound("User not found.");
|
|
@@ -5,3 +5,13 @@ export declare const emailField: () => z.ZodString;
|
|
|
5
5
|
export declare const nameField: () => z.ZodString;
|
|
6
6
|
/** An avatar: an absolute http(s) URL, or `null` to clear it. */
|
|
7
7
|
export declare const avatarField: () => z.ZodNullable<z.ZodString>;
|
|
8
|
+
/**
|
|
9
|
+
* A phone number as it is stored: trimmed, at most 40 characters of digits, spaces and `+-().`, or `null` to clear it.
|
|
10
|
+
* An empty string clears it too.
|
|
11
|
+
*/
|
|
12
|
+
export declare const phoneField: () => z.ZodNullable<z.ZodPipe<z.ZodString, z.ZodTransform<string | null, string>>>;
|
|
13
|
+
/**
|
|
14
|
+
* A language as it is stored (`normalizeUserLocale`): one of `known` (the app's i18n locale codes) when there are any,
|
|
15
|
+
* else a short BCP-47 tag; or `null` to clear it. An empty string clears it too.
|
|
16
|
+
*/
|
|
17
|
+
export declare const localeField: (known?: readonly string[]) => z.ZodNullable<z.ZodPipe<z.ZodString, z.ZodTransform<string | null, string>>>;
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
+
import { USER_PHONE_MAX_LENGTH, USER_PHONE_PATTERN } from "../utils/phone.js";
|
|
3
|
+
import { USER_LOCALE_MAX_LENGTH, normalizeUserLocale } from "../utils/locale.js";
|
|
2
4
|
export const emailField = () => z.string().trim().toLowerCase().email().max(254);
|
|
3
5
|
export const nameField = () => z.string().trim().min(1).max(120);
|
|
4
6
|
export const avatarField = () => z.string().trim().max(2048).url().refine((value) => /^https?:$/.test(new URL(value).protocol), { message: "The avatar must be an http(s) URL." }).nullable();
|
|
7
|
+
export const phoneField = () => z.string().trim().max(USER_PHONE_MAX_LENGTH).regex(USER_PHONE_PATTERN, { message: "Enter a phone number of digits, spaces and + - ( )." }).transform((value) => value || null).nullable();
|
|
8
|
+
export const localeField = (known = []) => z.string().trim().max(USER_LOCALE_MAX_LENGTH).refine((value) => !value || normalizeUserLocale(value, known) !== null, {
|
|
9
|
+
message: known.length ? `Choose one of: ${known.join(", ")}.` : 'Enter a language tag such as "da" or "en-GB".'
|
|
10
|
+
}).transform((value) => normalizeUserLocale(value, known)).nullable();
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { schema } from "@nuxthub/db";
|
|
2
|
+
import { defineSearchIndex } from "@frigear-nu/module-kit/server";
|
|
3
|
+
import { withUserSearchText } from "./utils/user-search.js";
|
|
4
|
+
export default defineSearchIndex({
|
|
5
|
+
table: schema.users,
|
|
6
|
+
columns: (user) => ({ searchText: withUserSearchText({ name: user.name ?? "" }).searchText ?? "" })
|
|
7
|
+
});
|