From 9f9fdf03dd54ef6ccd8b1da52fce90584894232b Mon Sep 17 00:00:00 2001 From: robertbinning Date: Wed, 6 Nov 2024 12:07:28 -0600 Subject: [PATCH] show user roles in Users table --- backend/managers/UsersManager.py | 7 ++++--- backend/schemas.py | 3 +++ frontend/src/users.tsx | 20 ++++++++++++-------- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/backend/managers/UsersManager.py b/backend/managers/UsersManager.py index 2c9559f0..1d297cb7 100644 --- a/backend/managers/UsersManager.py +++ b/backend/managers/UsersManager.py @@ -46,7 +46,7 @@ async def retrieve_user(self, id): async with db_session_context() as session: result = await session.execute(select(User).filter(User.id == id)) user = result.scalar_one_or_none() - return UserSchema(id=user.id, name=user.name, email=user.email) if user else None + return UserSchema(id=user.id, name=user.name, email=user.email, role=user.role) if user else None async def retrieve_users(self, offset=0, limit=100, sort_by=None, sort_order='asc', filters=None): async with db_session_context() as session: @@ -59,7 +59,7 @@ async def retrieve_users(self, offset=0, limit=100, sort_by=None, sort_order='as else: query = query.filter(getattr(User, key) == value) - if sort_by and sort_by in ['id', 'name', 'email']: + if sort_by and sort_by in ['id', 'name', 'email', 'role']: order_column = getattr(User, sort_by) query = query.order_by(order_column.desc() if sort_order.lower() == 'desc' else order_column) @@ -69,7 +69,8 @@ async def retrieve_users(self, offset=0, limit=100, sort_by=None, sort_order='as users = [UserSchema( id=user.id, name=user.name, - email=user.email + email=user.email, + role=user.role ) for user in result.scalars().all()] # Get total count diff --git a/backend/schemas.py b/backend/schemas.py index 7ded2faf..d3ad7802 100644 --- a/backend/schemas.py +++ b/backend/schemas.py @@ -47,6 +47,9 @@ class UserCreateSchema(UserBaseSchema): class UserSchema(UserBaseSchema): id: str + name: str + email: str + role: str # Asset schemas class AssetBaseSchema(BaseModel): diff --git a/frontend/src/users.tsx b/frontend/src/users.tsx index f4fd5f98..cf11d9dd 100644 --- a/frontend/src/users.tsx +++ b/frontend/src/users.tsx @@ -1,16 +1,15 @@ import { useMediaQuery, Theme } from "@mui/material"; -import { Create, Edit, EditButton, DeleteButton, List, SimpleList, Show, ShowButton, SimpleForm, SimpleShowLayout, Datagrid, TextField, TextInput, EmailField, useRecordContext } from "react-admin"; +import { Create, Edit, EditButton, DeleteButton, List, SimpleList, Show, ShowButton, SimpleForm, SimpleShowLayout, Datagrid, TextField, TextInput, EmailField, SelectInput, useRecordContext } from "react-admin"; const UserTitle = () => { const record = useRecordContext(); return Users {record ? `- ${record.name}` : ""}; }; -interface UserRecord { - id: string; - name: string; - email: string; -} +const roleChoices = [ + { id: 'user', name: 'User' }, + { id: 'admin', name: 'Admin' }, +]; export const UserList = () => { const isSmall = useMediaQuery((theme) => theme.breakpoints.down("sm")); @@ -18,13 +17,15 @@ export const UserList = () => { {isSmall ? ( record.name} - secondaryText={(record: UserRecord) => record.email} + primaryText={(record) => record.name} + secondaryText={(record) => record.email} + tertiaryText={(record) => record.role} /> ) : ( + @@ -40,6 +41,7 @@ export const UserShow = () => ( + ); @@ -49,6 +51,7 @@ export const UserEdit = () => ( + ); @@ -58,6 +61,7 @@ export const UserCreate = () => ( + );