diff --git a/modules/users/users/contracts/schemas.py b/modules/users/users/contracts/schemas.py
index 4c44459cd..3497b7a8c 100644
--- a/modules/users/users/contracts/schemas.py
+++ b/modules/users/users/contracts/schemas.py
@@ -4,9 +4,10 @@
import uuid
from datetime import datetime
+from typing import Annotated
from fastapi_users.schemas import CreateUpdateDictModel
-from pydantic import ConfigDict, EmailStr
+from pydantic import ConfigDict, EmailStr, StringConstraints
from sqlmodel import Field, SQLModel
MAX_BULK_INVITE_BODY_ADDRESSES = 1000
@@ -21,11 +22,18 @@
paragraph of context, short enough that it cannot be used to stuff arbitrary
content into outbound mail."""
-MAX_FULL_NAME_LENGTH = 255
+MAX_FULL_NAME_LENGTH = 200
"""Ceiling on a user's display name across profile, invite and admin forms.
-Matches the ``VARCHAR(255)`` column on ``users_user.full_name`` so oversized
-inputs fail cleanly with 422 instead of a database truncation error."""
+Shared by client inputs and server schemas (safely inside the ``VARCHAR(255)``
+column on ``users_user.full_name``) so oversized inputs fail cleanly with 422
+instead of a database truncation error."""
+
+FullNameStr = Annotated[
+ str,
+ StringConstraints(strip_whitespace=True, min_length=1, max_length=MAX_FULL_NAME_LENGTH),
+]
+"""Trimmed, non-blank display name up to :data:`MAX_FULL_NAME_LENGTH` chars."""
USER_STATES = ("active", "unverified", "invited", "disabled")
"""What a row in the users table *is*, as one word.
@@ -63,19 +71,19 @@ class UserCreate(CreateUpdateDictModel, SQLModel):
is_active: bool | None = True
is_superuser: bool | None = False
is_verified: bool | None = False
- full_name: str | None = Field(default=None, max_length=MAX_FULL_NAME_LENGTH)
+ full_name: FullNameStr | None = None
class UserUpdate(CreateUpdateDictModel, SQLModel):
password: str | None = None
email: EmailStr | None = None
- full_name: str | None = Field(default=None, max_length=MAX_FULL_NAME_LENGTH)
+ full_name: FullNameStr | None = None
# Admin + invite + self profile
class UserInvite(SQLModel):
email: EmailStr
- full_name: str | None = Field(default=None, max_length=MAX_FULL_NAME_LENGTH)
+ full_name: FullNameStr | None = None
role_names: list[str] = []
@@ -127,13 +135,13 @@ class BulkInviteResponse(SQLModel):
class UserAdminCreate(SQLModel):
email: EmailStr
password: str
- full_name: str | None = Field(default=None, max_length=MAX_FULL_NAME_LENGTH)
+ full_name: FullNameStr | None = None
role_names: list[str] = []
class UserDetailsUpdate(SQLModel):
email: EmailStr | None = None
- full_name: str | None = Field(default=None, max_length=MAX_FULL_NAME_LENGTH)
+ full_name: FullNameStr | None = None
class UserListItem(SQLModel):
@@ -186,7 +194,10 @@ class LoginRequest(SQLModel):
class AcceptInviteRequest(SQLModel):
token: str
password: str
- full_name: str | None = Field(default=None, max_length=MAX_FULL_NAME_LENGTH)
+ full_name: (
+ Annotated[str, StringConstraints(strip_whitespace=True, max_length=MAX_FULL_NAME_LENGTH)]
+ | None
+ ) = None
"""What the invitee calls themselves. Optional because an admin may have
filled it in when minting the invite, in which case the form pre-fills and
a blank submit must not wipe it."""
@@ -197,7 +208,7 @@ class PasswordResetLink(SQLModel):
class SelfProfileUpdate(SQLModel):
- full_name: str | None = Field(default=None, max_length=MAX_FULL_NAME_LENGTH)
+ full_name: FullNameStr | None = None
class SelfPasswordChange(SQLModel):
diff --git a/modules/users/users/core/directory.py b/modules/users/users/core/directory.py
index 78a42ed81..6b7f3bd5b 100644
--- a/modules/users/users/core/directory.py
+++ b/modules/users/users/core/directory.py
@@ -27,7 +27,7 @@
search_rows,
to_directory_user,
)
-from sqlalchemy import String, cast, func, select
+from sqlalchemy import String, cast, exists, func, select
from users.models import Role, User, UserRole
@@ -108,6 +108,7 @@ async def search(
limit: int,
include_ids: Collection[str] | None = None,
exclude_ids: Collection[str] = (),
+ exclude_roles: Collection[str] | None = None,
active_only: bool = False,
exact_email: bool = False,
match_email: bool = True,
@@ -115,6 +116,13 @@ async def search(
stmt = select(*_COLUMNS).order_by(User.email)
if exclude_ids:
stmt = stmt.where(User.id.not_in(list(parse_user_ids(exclude_ids))))
+ if exclude_roles:
+ excluded_role_subq = (
+ select(UserRole.user_id)
+ .join(Role, Role.id == UserRole.role_id)
+ .where(UserRole.user_id == User.id, Role.name.in_(list(exclude_roles)))
+ )
+ stmt = stmt.where(~exists(excluded_role_subq))
if active_only:
stmt = stmt.where(User.is_active.is_(True), User.disabled_at.is_(None))
if exact_email:
diff --git a/modules/users/users/pages/AcceptInvite.tsx b/modules/users/users/pages/AcceptInvite.tsx
index 73017996a..72104c336 100644
--- a/modules/users/users/pages/AcceptInvite.tsx
+++ b/modules/users/users/pages/AcceptInvite.tsx
@@ -41,11 +41,14 @@ function AcceptInvite({ token, invite, branding }: Props) {
const [confirm, setConfirm] = useState('');
const [mismatch, setMismatch] = useState(false);
const [error, setError] = useState
(null);
+ const [fullNameError, setFullNameError] = useState(null);
const [loading, setLoading] = useState(false);
+ const nameErrorId = 'accept-invite-full-name-error';
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
setError(null);
+ setFullNameError(null);
if (password !== confirm) {
setMismatch(true);
return;
@@ -69,6 +72,8 @@ function AcceptInvite({ token, invite, branding }: Props) {
router.visit(homeUrl);
} else if (res.detail === 'INVITE_BAD_TOKEN') {
setError(t(keys.users.accept_invite.error_bad_token));
+ } else if (res.fieldErrors.full_name) {
+ setFullNameError(res.fieldErrors.full_name);
} else {
setError(res.detail || t(keys.users.accept_invite.error_failed));
}
@@ -118,14 +123,26 @@ function AcceptInvite({ token, invite, branding }: Props) {
{t(keys.users.accept_invite.heading)}
diff --git a/modules/users/users/pages/Users/components/DetailsCard.tsx b/modules/users/users/pages/Users/components/DetailsCard.tsx
index aecd94d57..fc7bedf8a 100644
--- a/modules/users/users/pages/Users/components/DetailsCard.tsx
+++ b/modules/users/users/pages/Users/components/DetailsCard.tsx
@@ -2,9 +2,12 @@ import { Link } from '@inertiajs/react';
import { keys, useT } from '@simple-module-py/i18n';
import { SectionTitle } from '@simple-module-py/ui/components/SectionTitle';
import { Card, CardContent } from '@simple-module-py/ui/components/ui/card';
+import { FieldError } from '@simple-module-py/ui/components/ui/field';
import { Input } from '@simple-module-py/ui/components/ui/input';
import { Label } from '@simple-module-py/ui/components/ui/label';
+import type { FieldErrors } from '@simple-module-py/ui/lib/api';
import { TOUCH_TARGET_CLASS } from '@simple-module-py/ui/lib/utils';
+import { useId } from 'react';
import { type Role, RolePicker } from './RolePicker';
interface Props {
@@ -17,7 +20,9 @@ interface Props {
onToggleRole: (roleName: string) => void;
userId: string;
hasPermissionsModule: boolean;
+ disabled?: boolean;
error?: string | null;
+ fieldErrors?: FieldErrors;
}
/**
@@ -37,9 +42,16 @@ export function DetailsCard({
onToggleRole,
userId,
hasPermissionsModule,
+ disabled,
error,
+ fieldErrors,
}: Props) {
const { t } = useT();
+ const nameErrorId = useId();
+ const emailErrorId = useId();
+ const fullNameError = fieldErrors?.full_name;
+ const emailError = fieldErrors?.email;
+
return (