@frigear-nu/users 3.1.0 → 3.1.1
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 +15 -3
- 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 +4 -3
- 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 +21 -9
- 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
package/dist/module.d.mts
CHANGED
|
@@ -39,6 +39,8 @@ type AdminUserDto = {
|
|
|
39
39
|
name: string;
|
|
40
40
|
email: string;
|
|
41
41
|
avatar: string | null;
|
|
42
|
+
/** Only in the detail and edit responses, and only for a caller with `users:pii`. */
|
|
43
|
+
phone?: string | null;
|
|
42
44
|
emailVerifiedAt: string | null;
|
|
43
45
|
lastLoginAt: string | null;
|
|
44
46
|
createdAt: string;
|
|
@@ -49,6 +51,9 @@ type ProfileDto = {
|
|
|
49
51
|
name: string;
|
|
50
52
|
email: string;
|
|
51
53
|
avatar: string | null;
|
|
54
|
+
phone: string | null;
|
|
55
|
+
/** The user's language (`da`, `en-GB`), or `null`. Filled from the app's language at sign-in when unset. */
|
|
56
|
+
locale: string | null;
|
|
52
57
|
emailVerifiedAt: string | null;
|
|
53
58
|
};
|
|
54
59
|
|
|
@@ -59,6 +64,8 @@ declare module 'nuxt/schema' {
|
|
|
59
64
|
interface RuntimeConfig {
|
|
60
65
|
users?: {
|
|
61
66
|
verifyEmailChange: boolean;
|
|
67
|
+
/** The app's i18n locale codes (filled at `modules:done`): the languages `users.locale` accepts. Empty: any short BCP-47 tag. */
|
|
68
|
+
locales: string[];
|
|
62
69
|
};
|
|
63
70
|
}
|
|
64
71
|
interface PublicRuntimeConfig {
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { defineNuxtModule, createResolver, addServerImportsDir, hasNuxtModule } from '@nuxt/kit';
|
|
2
|
-
import { registerTailwindSource, registerLocales, registerDatabaseTable, inlineModuleRuntime, addModulePages, addModuleRoutes, resolvePagePath, addOptionalMenuSource } from '@frigear-nu/module-kit/utils';
|
|
1
|
+
import { defineNuxtModule, createResolver, addComponent, addServerImportsDir, hasNuxtModule, addPlugin } from '@nuxt/kit';
|
|
2
|
+
import { registerTailwindSource, registerLocales, registerDatabaseTable, registerSearchIndex, inlineModuleRuntime, addModulePages, addModuleRoutes, resolvePagePath, addOptionalMenuSource } from '@frigear-nu/module-kit/utils';
|
|
3
3
|
import { setAuthUserStore, registerAccess } from '@frigear-nu/auth/utils';
|
|
4
|
+
import { setNotifyRecipientStore } from '@frigear-nu/notify/utils';
|
|
4
5
|
import { usersAccess, USERS_SELF_SERVICE } from '../dist/runtime/access.js';
|
|
5
6
|
|
|
6
7
|
const module$1 = defineNuxtModule({
|
|
@@ -44,16 +45,20 @@ const module$1 = defineNuxtModule({
|
|
|
44
45
|
registerTailwindSource(nuxt, resolver.resolve("./runtime/app"));
|
|
45
46
|
registerLocales(nuxt, resolver.resolve("./runtime/i18n/lang"));
|
|
46
47
|
const settings = options.settings ?? {};
|
|
48
|
+
addComponent({ name: "UserPicker", filePath: resolver.resolve("./runtime/app/components/UserPicker.vue") });
|
|
47
49
|
registerDatabaseTable(nuxt, {
|
|
48
50
|
resolver,
|
|
49
51
|
schema: (dialect) => `./runtime/db/schema.${dialect}`
|
|
50
52
|
});
|
|
53
|
+
registerSearchIndex(nuxt, { name: "users", handler: resolver.resolve("./runtime/server/search-index") });
|
|
51
54
|
setAuthUserStore(nuxt, resolver.resolve("./runtime/server/user-store"));
|
|
55
|
+
setNotifyRecipientStore(nuxt, resolver.resolve("./runtime/server/notify-recipients"));
|
|
52
56
|
nuxt.hook("notify:email:templates", (sources) => sources.push({ dir: resolver.resolve("./runtime/emails") }));
|
|
53
57
|
addServerImportsDir(resolver.resolve("./runtime/server/utils"));
|
|
54
58
|
inlineModuleRuntime(nuxt, resolver);
|
|
55
59
|
const selfRoutes = [
|
|
56
60
|
{ method: "patch", path: "me", file: "me/profile.patch" },
|
|
61
|
+
{ method: "put", path: "me/locale", file: "me/locale.put" },
|
|
57
62
|
{ method: "put", path: "me/email", file: "me/email.put" },
|
|
58
63
|
{ method: "put", path: "me/password", file: "me/password.put" }
|
|
59
64
|
];
|
|
@@ -84,7 +89,14 @@ const module$1 = defineNuxtModule({
|
|
|
84
89
|
const { addMcpDefinitions } = await import('@frigear-nu/mcp/utils');
|
|
85
90
|
addMcpDefinitions(nuxt, resolver.resolve("./runtime/server/mcp"));
|
|
86
91
|
}
|
|
87
|
-
nuxt.options.runtimeConfig.users = { verifyEmailChange: settings.verifyEmailChange === true };
|
|
92
|
+
nuxt.options.runtimeConfig.users = { verifyEmailChange: settings.verifyEmailChange === true, locales: [] };
|
|
93
|
+
nuxt.hook("modules:done", () => {
|
|
94
|
+
const locales = nuxt.options.i18n?.locales ?? [];
|
|
95
|
+
nuxt.options.runtimeConfig.users.locales = locales.map((locale) => typeof locale === "string" ? locale : locale.code);
|
|
96
|
+
if (hasNuxtModule("@nuxtjs/i18n", nuxt)) {
|
|
97
|
+
addPlugin({ src: resolver.resolve("./runtime/app/plugins/remember-locale.client"), mode: "client" });
|
|
98
|
+
}
|
|
99
|
+
});
|
|
88
100
|
const withAdmin = !!settings.admin;
|
|
89
101
|
registerAccess(nuxt, usersAccess, withAdmin ? {} : { permissions: USERS_SELF_SERVICE });
|
|
90
102
|
const adminOptions = typeof settings.admin === "object" ? settings.admin : {};
|
package/dist/runtime/access.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export declare const usersAccess: import("@frigear-nu/auth/access").Access<"users", "list" | "profile:read" | "profile:write" | "read" | "create" | "update" | "delete" | "manage", never, never>;
|
|
1
|
+
export declare const usersAccess: import("@frigear-nu/auth/access").Access<"users", "list" | "profile:read" | "profile:write" | "read" | "create" | "update" | "delete" | "manage" | "pii", never, never>;
|
|
2
2
|
/** The self-service permissions, registered with or without the admin pages. */
|
|
3
3
|
export declare const USERS_SELF_SERVICE: readonly ["profile:read", "profile:write"];
|
package/dist/runtime/access.js
CHANGED
|
@@ -2,21 +2,26 @@ import { defineAccess } from "@frigear-nu/auth/access";
|
|
|
2
2
|
export const usersAccess = defineAccess("users", {
|
|
3
3
|
permissions: {
|
|
4
4
|
// Self-service: every signed-in user holds these (also without `@frigear-nu/authorization`), and may delegate them:
|
|
5
|
-
// an API token or OAuth access token given `profile:write` may change the user's name and
|
|
5
|
+
// an API token or OAuth access token given `profile:write` may change the user's name, avatar and phone number (`PATCH /me`). The
|
|
6
6
|
// email address and password are not part of it: they change only in a session
|
|
7
7
|
// (`requireUser(event, { sessionOnly: true })`); staff change another user's email with the session-only
|
|
8
8
|
// `users:manage`.
|
|
9
|
-
"profile:read": { label: "Read your profile", description: "Read your own name, email and
|
|
10
|
-
"profile:write": { label: "Change your profile", description: "Change your own name and
|
|
9
|
+
"profile:read": { label: "Read your profile", description: "Read your own name, email, avatar and phone number.", implicit: true },
|
|
10
|
+
"profile:write": { label: "Change your profile", description: "Change your own name, avatar and phone number.", implicit: true },
|
|
11
11
|
// Staff permissions (with the admin pages only), granted through roles (`admin: ['*']` covers them). Reads also work
|
|
12
12
|
// for API tokens and OAuth access tokens given them. Deleting a user and anything that touches how someone signs in
|
|
13
13
|
// (their email, sign-in links) is session only. Roles themselves are `@frigear-nu/authorization`'s.
|
|
14
14
|
"read": { label: "View users", description: "See a user's profile, sign-in methods and what other modules show on the user page." },
|
|
15
15
|
"list": { label: "List users", description: "Search the user directory and see the sign-up numbers." },
|
|
16
|
-
|
|
16
|
+
// Granted to `admin` by name; give it to other staff roles in the host's `authorization.roles`. It covers an optional
|
|
17
|
+
// phone number on the new user (changing it later is `users:pii`).
|
|
18
|
+
"create": { label: "Add users", description: "Create users and email them an invite link.", grantTo: ["admin"] },
|
|
17
19
|
"update": { label: "Edit users", description: "Change a user's name." },
|
|
18
20
|
"delete": { label: "Delete users", description: "Delete users and their sign-in methods.", sessionOnly: true },
|
|
19
|
-
"manage": { label: "Manage sign-in of users", description: "Change or verify a user's email address and email them a sign-in link.", sessionOnly: true }
|
|
21
|
+
"manage": { label: "Manage sign-in of users", description: "Change or verify a user's email address and email them a sign-in link.", sessionOnly: true },
|
|
22
|
+
// Sensitive (`:pii`): never covered by `users:*` or `*`, only granted by name. Other modules show the phone number
|
|
23
|
+
// under their own `:pii` permission (`crew:pii`, `shifts:pii`).
|
|
24
|
+
"pii": { label: "See users' phone numbers", description: "See and change the phone number of other users.", grantTo: ["admin"] }
|
|
20
25
|
}
|
|
21
26
|
});
|
|
22
27
|
export const USERS_SELF_SERVICE = ["profile:read", "profile:write"];
|
|
@@ -1,8 +1,20 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import type { UserCreateResult } from '../utils/admin-user.js';
|
|
2
|
+
type __VLS_Props = {
|
|
3
|
+
/** Prefills the form (the picker's search term, or what a host knows of the person). */
|
|
4
|
+
initial?: {
|
|
5
|
+
name?: string;
|
|
6
|
+
email?: string;
|
|
7
|
+
phone?: string;
|
|
8
|
+
};
|
|
9
|
+
/** Show "Mark the email address as verified" (`users:manage`). The admin list does, the picker does not. */
|
|
10
|
+
verify?: boolean;
|
|
11
|
+
/** On a taken email, offer to select the user who has it (`{ existing }`). */
|
|
12
|
+
offerExisting?: boolean;
|
|
13
|
+
};
|
|
14
|
+
declare const __VLS_export: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
|
|
15
|
+
close: (result: UserCreateResult) => any;
|
|
16
|
+
}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{
|
|
17
|
+
onClose?: ((result: UserCreateResult) => any) | undefined;
|
|
18
|
+
}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
|
|
7
19
|
declare const _default: typeof __VLS_export;
|
|
8
20
|
export default _default;
|
|
@@ -1,28 +1,54 @@
|
|
|
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
|
-
import { $fetch, reactive,
|
|
5
|
+
import { $fetch, reactive, ref, useErrorToast, useModuleT, useNuxtApp, useToast } from "#imports";
|
|
5
6
|
defineOptions({ name: "UsersUserCreateSlideover" });
|
|
7
|
+
const props = defineProps({
|
|
8
|
+
initial: { type: Object, required: false },
|
|
9
|
+
verify: { type: Boolean, required: false, default: true },
|
|
10
|
+
offerExisting: { type: Boolean, required: false, default: false }
|
|
11
|
+
});
|
|
6
12
|
const emit = defineEmits(["close"]);
|
|
7
13
|
const t = useModuleT("users");
|
|
8
|
-
const
|
|
14
|
+
const toast = useToast();
|
|
15
|
+
const showError = useErrorToast();
|
|
16
|
+
const busy = ref(false);
|
|
17
|
+
const locale = useNuxtApp().$i18n?.locale?.value || void 0;
|
|
9
18
|
const schema = z.object({
|
|
10
19
|
name: z.string().trim().min(1, t("admin.nameRequired", "Enter a name.")).max(120),
|
|
11
20
|
email: z.string().trim().email(t("admin.emailInvalid", "Enter a valid email address.")),
|
|
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 + - ( ).")).or(z.literal("")),
|
|
12
22
|
emailVerified: z.boolean(),
|
|
13
23
|
sendWelcomeEmail: z.boolean()
|
|
14
24
|
});
|
|
15
|
-
const state = reactive({ name: "", email: "", emailVerified: false, sendWelcomeEmail: true });
|
|
25
|
+
const state = reactive({ name: props.initial?.name ?? "", email: props.initial?.email ?? "", phone: props.initial?.phone ?? "", emailVerified: false, sendWelcomeEmail: true });
|
|
26
|
+
const existing = ref(null);
|
|
16
27
|
async function save(event) {
|
|
17
|
-
|
|
18
|
-
|
|
28
|
+
existing.value = null;
|
|
29
|
+
busy.value = true;
|
|
30
|
+
try {
|
|
31
|
+
const { phone, emailVerified, ...rest } = event.data;
|
|
32
|
+
const created = await $fetch(usersApi.admin.users, {
|
|
19
33
|
method: "POST",
|
|
20
|
-
body:
|
|
21
|
-
})
|
|
22
|
-
{
|
|
23
|
-
|
|
24
|
-
|
|
34
|
+
body: { ...rest, ...phone ? { phone } : {}, ...props.verify ? { emailVerified } : {}, ...locale ? { locale } : {} }
|
|
35
|
+
});
|
|
36
|
+
toast.add({ title: t("admin.createdNamed", "Created {name}", { name: created.name }), color: "success", icon: "i-lucide-check" });
|
|
37
|
+
emit("close", created);
|
|
38
|
+
} catch (e) {
|
|
39
|
+
const conflict = e.statusCode === 409 ? e.data?.data : void 0;
|
|
40
|
+
if (props.offerExisting && conflict?.field === "email" && typeof conflict.userId === "number") {
|
|
41
|
+
existing.value = { id: conflict.userId, name: null, email: event.data.email.trim().toLowerCase(), avatar: null };
|
|
42
|
+
} else {
|
|
43
|
+
showError(e, t("admin.createFailed", "The user could not be created"));
|
|
44
|
+
}
|
|
45
|
+
} finally {
|
|
46
|
+
busy.value = false;
|
|
47
|
+
}
|
|
25
48
|
}
|
|
49
|
+
const chooseExisting = () => {
|
|
50
|
+
if (existing.value) emit("close", { existing: existing.value });
|
|
51
|
+
};
|
|
26
52
|
</script>
|
|
27
53
|
|
|
28
54
|
<template>
|
|
@@ -41,6 +67,16 @@ async function save(event) {
|
|
|
41
67
|
class="flex flex-col gap-4"
|
|
42
68
|
@submit="save"
|
|
43
69
|
>
|
|
70
|
+
<UAlert
|
|
71
|
+
v-if="existing"
|
|
72
|
+
color="warning"
|
|
73
|
+
variant="subtle"
|
|
74
|
+
icon="i-lucide-user-check"
|
|
75
|
+
:title="t('admin.emailTaken', 'This email already has an account')"
|
|
76
|
+
:description="t('admin.emailTakenHint', '{email} belongs to an existing user. Select that user instead of creating a new one.', { email: existing.email ?? '' })"
|
|
77
|
+
:actions="[{ label: t('admin.selectExisting', 'Select existing user'), color: 'warning', variant: 'solid', onClick: chooseExisting }]"
|
|
78
|
+
data-existing-user
|
|
79
|
+
/>
|
|
44
80
|
<UFormField
|
|
45
81
|
name="name"
|
|
46
82
|
:label="t('admin.name', 'Name')"
|
|
@@ -62,13 +98,27 @@ async function save(event) {
|
|
|
62
98
|
class="w-full"
|
|
63
99
|
/>
|
|
64
100
|
</UFormField>
|
|
101
|
+
<UFormField
|
|
102
|
+
name="phone"
|
|
103
|
+
:label="t('admin.phone', 'Phone')"
|
|
104
|
+
:description="t('admin.createPhoneHelp', 'Optional. Used for text messages.')"
|
|
105
|
+
>
|
|
106
|
+
<UInput
|
|
107
|
+
v-model="state.phone"
|
|
108
|
+
type="tel"
|
|
109
|
+
autocomplete="off"
|
|
110
|
+
class="w-full"
|
|
111
|
+
/>
|
|
112
|
+
</UFormField>
|
|
65
113
|
<UCheckbox
|
|
114
|
+
v-if="verify"
|
|
66
115
|
v-model="state.emailVerified"
|
|
67
116
|
:label="t('admin.emailVerified', 'Mark the email address as verified')"
|
|
68
117
|
/>
|
|
69
118
|
<UCheckbox
|
|
70
119
|
v-model="state.sendWelcomeEmail"
|
|
71
|
-
:label="t('admin.
|
|
120
|
+
:label="t('admin.sendInvite', 'Send invite email')"
|
|
121
|
+
:description="t('admin.sendInviteHelp', 'A single-use link to sign in and verify the address, valid for several days.')"
|
|
72
122
|
/>
|
|
73
123
|
</UForm>
|
|
74
124
|
</FormSlideover>
|
|
@@ -1,8 +1,20 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import type { UserCreateResult } from '../utils/admin-user.js';
|
|
2
|
+
type __VLS_Props = {
|
|
3
|
+
/** Prefills the form (the picker's search term, or what a host knows of the person). */
|
|
4
|
+
initial?: {
|
|
5
|
+
name?: string;
|
|
6
|
+
email?: string;
|
|
7
|
+
phone?: string;
|
|
8
|
+
};
|
|
9
|
+
/** Show "Mark the email address as verified" (`users:manage`). The admin list does, the picker does not. */
|
|
10
|
+
verify?: boolean;
|
|
11
|
+
/** On a taken email, offer to select the user who has it (`{ existing }`). */
|
|
12
|
+
offerExisting?: boolean;
|
|
13
|
+
};
|
|
14
|
+
declare const __VLS_export: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
|
|
15
|
+
close: (result: UserCreateResult) => any;
|
|
16
|
+
}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{
|
|
17
|
+
onClose?: ((result: UserCreateResult) => any) | undefined;
|
|
18
|
+
}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
|
|
7
19
|
declare const _default: typeof __VLS_export;
|
|
8
20
|
export default _default;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { UserPickerCreateInitial, UserPickerOption } from '../../../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* The one user picker of the app: a search by name (and by email where the route lets the caller see emails), single
|
|
4
|
+
* (`v-model` a user id) or `multiple` (an array of ids). `endpoint` is the search route (`GET ?q=`, answering
|
|
5
|
+
* `UserPickerOption[]` or a page with `items`); it defaults to the users admin list (`users:list`), and a module whose
|
|
6
|
+
* staff lack that permission passes its own (teams: `teams:manage`, emails with `teams:pii`).
|
|
7
|
+
*
|
|
8
|
+
* With `users:create` (and `create` not `false`) a "Create user" option opens the create form, prefilled from the
|
|
9
|
+
* search; the created user is selected. A taken email offers the user who has it instead. `selected` gives the names of
|
|
10
|
+
* ids the model already holds.
|
|
11
|
+
*/
|
|
12
|
+
type __VLS_Props = {
|
|
13
|
+
endpoint?: string;
|
|
14
|
+
multiple?: boolean;
|
|
15
|
+
create?: boolean;
|
|
16
|
+
selected?: UserPickerOption[];
|
|
17
|
+
placeholder?: string;
|
|
18
|
+
disabled?: boolean;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Opens the create form prefilled with `initial` and selects the created user (or the existing one a taken email
|
|
22
|
+
* offers). Resolves with the selected user, or `null` when cancelled or not allowed. Exposed, so a host that knows the
|
|
23
|
+
* person (an inbox contact) can offer "Create user" before anything is typed.
|
|
24
|
+
*/
|
|
25
|
+
declare function openCreate(initial?: UserPickerCreateInitial): Promise<UserPickerOption | null>;
|
|
26
|
+
type __VLS_ModelProps = {
|
|
27
|
+
modelValue?: number | number[] | null | undefined;
|
|
28
|
+
};
|
|
29
|
+
type __VLS_PublicProps = __VLS_Props & __VLS_ModelProps;
|
|
30
|
+
declare const __VLS_export: import("vue").DefineComponent<__VLS_PublicProps, {
|
|
31
|
+
openCreate: typeof openCreate;
|
|
32
|
+
canCreate: import("vue").ComputedRef<boolean>;
|
|
33
|
+
}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
|
|
34
|
+
created: (user: UserPickerOption) => any;
|
|
35
|
+
picked: (user: UserPickerOption) => any;
|
|
36
|
+
"update:modelValue": (value: number | number[] | null | undefined) => any;
|
|
37
|
+
}, string, import("vue").PublicProps, Readonly<__VLS_PublicProps> & Readonly<{
|
|
38
|
+
onCreated?: ((user: UserPickerOption) => any) | undefined;
|
|
39
|
+
onPicked?: ((user: UserPickerOption) => any) | undefined;
|
|
40
|
+
"onUpdate:modelValue"?: ((value: number | number[] | null | undefined) => any) | undefined;
|
|
41
|
+
}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
|
|
42
|
+
declare const _default: typeof __VLS_export;
|
|
43
|
+
export default _default;
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
<script setup>
|
|
2
|
+
import { formBusInjectionKey, formFieldInjectionKey } from "@nuxt/ui/composables/useFormField";
|
|
3
|
+
import { authApi } from "@frigear-nu/auth/api";
|
|
4
|
+
import { usersApi } from "../../utils/api";
|
|
5
|
+
import UserCreateSlideover from "./UserCreateSlideover.vue";
|
|
6
|
+
import { computed, inject, nextTick, onBeforeUnmount, ref, useLazyFetch, useModuleT, useOverlay, watch } from "#imports";
|
|
7
|
+
defineOptions({ name: "UserPicker" });
|
|
8
|
+
const props = defineProps({
|
|
9
|
+
endpoint: { type: String, required: false, default: void 0 },
|
|
10
|
+
multiple: { type: Boolean, required: false, default: false },
|
|
11
|
+
create: { type: Boolean, required: false, default: true },
|
|
12
|
+
selected: { type: Array, required: false, default: () => [] },
|
|
13
|
+
placeholder: { type: String, required: false, default: void 0 },
|
|
14
|
+
disabled: { type: Boolean, required: false, default: false }
|
|
15
|
+
});
|
|
16
|
+
const model = defineModel({ type: null });
|
|
17
|
+
const emit = defineEmits(["picked", "created"]);
|
|
18
|
+
const t = useModuleT("users");
|
|
19
|
+
const overlay = useOverlay();
|
|
20
|
+
const { data: access } = useLazyFetch(authApi.me.access, {
|
|
21
|
+
key: "users:picker-access",
|
|
22
|
+
server: false,
|
|
23
|
+
immediate: props.create,
|
|
24
|
+
default: () => ({ permissions: [] }),
|
|
25
|
+
onResponseError: () => {
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
const canCreate = computed(() => props.create && !!access.value?.permissions?.includes("users:create"));
|
|
29
|
+
const search = ref("");
|
|
30
|
+
const renderKey = ref(0);
|
|
31
|
+
const q = ref("");
|
|
32
|
+
let debounce;
|
|
33
|
+
watch(search, (value) => {
|
|
34
|
+
clearTimeout(debounce);
|
|
35
|
+
debounce = setTimeout(() => {
|
|
36
|
+
q.value = value;
|
|
37
|
+
}, 250);
|
|
38
|
+
});
|
|
39
|
+
onBeforeUnmount(() => clearTimeout(debounce));
|
|
40
|
+
const { data: found, status, execute } = useLazyFetch(() => props.endpoint ?? usersApi.admin.users, {
|
|
41
|
+
query: computed(() => ({ q: q.value.trim(), pageSize: 20 })),
|
|
42
|
+
immediate: false,
|
|
43
|
+
watch: false,
|
|
44
|
+
default: () => [],
|
|
45
|
+
onResponseError: () => {
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
watch(q, (term) => {
|
|
49
|
+
if (term.trim()) void execute();
|
|
50
|
+
});
|
|
51
|
+
const known = ref(/* @__PURE__ */ new Map());
|
|
52
|
+
const remember = (users) => {
|
|
53
|
+
for (const user of users) known.value.set(user.id, user);
|
|
54
|
+
};
|
|
55
|
+
watch(() => props.selected, (users) => remember(users ?? []), { immediate: true });
|
|
56
|
+
const results = computed(() => {
|
|
57
|
+
if (!q.value.trim()) return [];
|
|
58
|
+
const data = found.value;
|
|
59
|
+
return Array.isArray(data) ? data : data?.items ?? [];
|
|
60
|
+
});
|
|
61
|
+
watch(results, remember);
|
|
62
|
+
const toItem = (user) => ({
|
|
63
|
+
label: user.name || user.email || `#${user.id}`,
|
|
64
|
+
description: user.email ?? void 0,
|
|
65
|
+
avatar: user.avatar ? { src: user.avatar } : void 0,
|
|
66
|
+
value: user.id
|
|
67
|
+
});
|
|
68
|
+
const optionOf = (id) => known.value.get(id) ?? { id, name: null };
|
|
69
|
+
const items = computed(() => results.value.map(toItem));
|
|
70
|
+
const current = computed({
|
|
71
|
+
get: () => {
|
|
72
|
+
if (props.multiple) return (Array.isArray(model.value) ? model.value : []).map((id) => toItem(optionOf(id)));
|
|
73
|
+
return typeof model.value === "number" ? toItem(optionOf(model.value)) : void 0;
|
|
74
|
+
},
|
|
75
|
+
set: (value) => {
|
|
76
|
+
if (props.multiple) {
|
|
77
|
+
const before = Array.isArray(model.value) ? model.value : [];
|
|
78
|
+
const ids = (Array.isArray(value) ? value : []).map((item) => item.value);
|
|
79
|
+
model.value = ids;
|
|
80
|
+
for (const id2 of ids.filter((id3) => !before.includes(id3))) emit("picked", optionOf(id2));
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const id = value && !Array.isArray(value) ? value.value : void 0;
|
|
84
|
+
model.value = id;
|
|
85
|
+
if (typeof id === "number") emit("picked", optionOf(id));
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
const formBus = inject(formBusInjectionKey, void 0);
|
|
89
|
+
const formField = inject(formFieldInjectionKey, void 0);
|
|
90
|
+
const notifyFormField = () => nextTick(() => {
|
|
91
|
+
const name = formField?.value?.name;
|
|
92
|
+
if (formBus && name) formBus.emit({ type: "change", name });
|
|
93
|
+
});
|
|
94
|
+
const select = (user) => {
|
|
95
|
+
remember([user]);
|
|
96
|
+
if (props.multiple) {
|
|
97
|
+
const ids = Array.isArray(model.value) ? model.value : [];
|
|
98
|
+
if (!ids.includes(user.id)) model.value = [...ids, user.id];
|
|
99
|
+
} else {
|
|
100
|
+
model.value = user.id;
|
|
101
|
+
}
|
|
102
|
+
emit("picked", user);
|
|
103
|
+
search.value = "";
|
|
104
|
+
renderKey.value++;
|
|
105
|
+
void notifyFormField();
|
|
106
|
+
};
|
|
107
|
+
async function openCreate(initial = {}) {
|
|
108
|
+
if (!canCreate.value) return null;
|
|
109
|
+
const result = await overlay.create(UserCreateSlideover, { destroyOnClose: true }).open({ initial, verify: false, offerExisting: true }).result;
|
|
110
|
+
if (!result) return null;
|
|
111
|
+
if ("existing" in result) {
|
|
112
|
+
select(result.existing);
|
|
113
|
+
return result.existing;
|
|
114
|
+
}
|
|
115
|
+
const user = { id: result.id, name: result.name, email: result.email, avatar: result.avatar };
|
|
116
|
+
select(user);
|
|
117
|
+
emit("created", user);
|
|
118
|
+
return user;
|
|
119
|
+
}
|
|
120
|
+
async function createUser(term) {
|
|
121
|
+
const value = term.trim();
|
|
122
|
+
await openCreate(value.includes("@") ? { email: value } : { name: value });
|
|
123
|
+
}
|
|
124
|
+
defineExpose({ openCreate, canCreate });
|
|
125
|
+
</script>
|
|
126
|
+
|
|
127
|
+
<template>
|
|
128
|
+
<UInputMenu
|
|
129
|
+
:key="renderKey"
|
|
130
|
+
v-model="current"
|
|
131
|
+
v-model:search-term="search"
|
|
132
|
+
:items="items"
|
|
133
|
+
:multiple="multiple"
|
|
134
|
+
by="value"
|
|
135
|
+
ignore-filter
|
|
136
|
+
:create-item="canCreate ? { position: 'bottom', when: 'always' } : false"
|
|
137
|
+
:loading="status === 'pending'"
|
|
138
|
+
:disabled="disabled"
|
|
139
|
+
icon="i-lucide-search"
|
|
140
|
+
:placeholder="placeholder ?? t('picker.placeholder', 'Search people')"
|
|
141
|
+
class="w-full"
|
|
142
|
+
data-user-picker
|
|
143
|
+
@create="createUser"
|
|
144
|
+
>
|
|
145
|
+
<template #create-item-label="{ item }">
|
|
146
|
+
<span class="inline-flex items-center gap-1.5">
|
|
147
|
+
<UIcon
|
|
148
|
+
name="i-lucide-user-plus"
|
|
149
|
+
class="size-4 shrink-0"
|
|
150
|
+
/>
|
|
151
|
+
{{ t("picker.create", 'Create user "{term}"', { term: item }) }}
|
|
152
|
+
</span>
|
|
153
|
+
</template>
|
|
154
|
+
<template #empty>
|
|
155
|
+
{{ q.trim() ? t("picker.empty", "Nobody matches") : t("picker.hint", "Type a name to search") }}
|
|
156
|
+
</template>
|
|
157
|
+
</UInputMenu>
|
|
158
|
+
</template>
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { UserPickerCreateInitial, UserPickerOption } from '../../../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* The one user picker of the app: a search by name (and by email where the route lets the caller see emails), single
|
|
4
|
+
* (`v-model` a user id) or `multiple` (an array of ids). `endpoint` is the search route (`GET ?q=`, answering
|
|
5
|
+
* `UserPickerOption[]` or a page with `items`); it defaults to the users admin list (`users:list`), and a module whose
|
|
6
|
+
* staff lack that permission passes its own (teams: `teams:manage`, emails with `teams:pii`).
|
|
7
|
+
*
|
|
8
|
+
* With `users:create` (and `create` not `false`) a "Create user" option opens the create form, prefilled from the
|
|
9
|
+
* search; the created user is selected. A taken email offers the user who has it instead. `selected` gives the names of
|
|
10
|
+
* ids the model already holds.
|
|
11
|
+
*/
|
|
12
|
+
type __VLS_Props = {
|
|
13
|
+
endpoint?: string;
|
|
14
|
+
multiple?: boolean;
|
|
15
|
+
create?: boolean;
|
|
16
|
+
selected?: UserPickerOption[];
|
|
17
|
+
placeholder?: string;
|
|
18
|
+
disabled?: boolean;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Opens the create form prefilled with `initial` and selects the created user (or the existing one a taken email
|
|
22
|
+
* offers). Resolves with the selected user, or `null` when cancelled or not allowed. Exposed, so a host that knows the
|
|
23
|
+
* person (an inbox contact) can offer "Create user" before anything is typed.
|
|
24
|
+
*/
|
|
25
|
+
declare function openCreate(initial?: UserPickerCreateInitial): Promise<UserPickerOption | null>;
|
|
26
|
+
type __VLS_ModelProps = {
|
|
27
|
+
modelValue?: number | number[] | null | undefined;
|
|
28
|
+
};
|
|
29
|
+
type __VLS_PublicProps = __VLS_Props & __VLS_ModelProps;
|
|
30
|
+
declare const __VLS_export: import("vue").DefineComponent<__VLS_PublicProps, {
|
|
31
|
+
openCreate: typeof openCreate;
|
|
32
|
+
canCreate: import("vue").ComputedRef<boolean>;
|
|
33
|
+
}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
|
|
34
|
+
created: (user: UserPickerOption) => any;
|
|
35
|
+
picked: (user: UserPickerOption) => any;
|
|
36
|
+
"update:modelValue": (value: number | number[] | null | undefined) => any;
|
|
37
|
+
}, string, import("vue").PublicProps, Readonly<__VLS_PublicProps> & Readonly<{
|
|
38
|
+
onCreated?: ((user: UserPickerOption) => any) | undefined;
|
|
39
|
+
onPicked?: ((user: UserPickerOption) => any) | undefined;
|
|
40
|
+
"onUpdate:modelValue"?: ((value: number | number[] | null | undefined) => any) | undefined;
|
|
41
|
+
}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
|
|
42
|
+
declare const _default: typeof __VLS_export;
|
|
43
|
+
export default _default;
|
|
@@ -12,9 +12,10 @@ export function useUserActions() {
|
|
|
12
12
|
const listPath = computed(() => localize(adminPage.replace(/\/+$/, "")));
|
|
13
13
|
const userPath = (id) => `${listPath.value}/${id}`;
|
|
14
14
|
async function create() {
|
|
15
|
-
const
|
|
16
|
-
if (
|
|
17
|
-
|
|
15
|
+
const result = await overlay.create(UserCreateSlideover, { destroyOnClose: true }).open();
|
|
16
|
+
if (!result || "existing" in result) return false;
|
|
17
|
+
await navigateTo(userPath(result.id));
|
|
18
|
+
return result;
|
|
18
19
|
}
|
|
19
20
|
async function remove(user, options = {}) {
|
|
20
21
|
const confirmed = await confirm({
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { usersApi } from "../../utils/api.js";
|
|
2
|
+
import { $fetch, defineNuxtPlugin, useUserSession, watch } from "#imports";
|
|
3
|
+
export default defineNuxtPlugin({
|
|
4
|
+
name: "users:remember-locale",
|
|
5
|
+
dependsOn: ["i18n:plugin"],
|
|
6
|
+
setup(nuxtApp) {
|
|
7
|
+
const i18n = nuxtApp.$i18n;
|
|
8
|
+
if (!i18n) return;
|
|
9
|
+
const { loggedIn, user, fetch: refreshSession } = useUserSession();
|
|
10
|
+
const tried = /* @__PURE__ */ new Set();
|
|
11
|
+
watch([loggedIn, () => user.value, () => i18n.locale.value], async ([signedIn, current, locale]) => {
|
|
12
|
+
if (!signedIn || !current || current.locale || !locale) return;
|
|
13
|
+
const key = `${String(current.id)}:${locale}`;
|
|
14
|
+
if (tried.has(key)) return;
|
|
15
|
+
tried.add(key);
|
|
16
|
+
try {
|
|
17
|
+
await $fetch(usersApi.me.locale, { method: "PUT", body: { locale, ifUnset: true } });
|
|
18
|
+
await refreshSession();
|
|
19
|
+
} catch {
|
|
20
|
+
}
|
|
21
|
+
}, { immediate: true });
|
|
22
|
+
}
|
|
23
|
+
});
|