@@ -10,6 +10,7 @@ import { renderUsersListPage, type UsersListPageData, type User } from '../templ
1010import type { Bindings , Variables } from '../app'
1111import { getUserProfileConfig , getRegistrationFields , renderCustomProfileSection , getCustomData , saveCustomData , extractCustomFieldsFromForm , sanitizeCustomData , validateCustomData , readProfileData , writeProfileData } from '../plugins/core-plugins/user-profiles'
1212import { TenantService , VALID_MEMBER_ROLES } from '../plugins/core-plugins/multi-tenant-plugin/services/tenant-service'
13+ import { RbacService } from '../services/rbac'
1314
1415const userRoutes = new Hono < { Bindings : Bindings ; Variables : Variables } > ( )
1516
@@ -58,13 +59,10 @@ const LANGUAGES = [
5859 { value : 'zh' , label : 'Chinese' }
5960]
6061
61- // Role options for user form
62- const ROLES = [
63- { value : 'admin' , label : 'Administrator' } ,
64- { value : 'editor' , label : 'Editor' } ,
65- { value : 'author' , label : 'Author' } ,
66- { value : 'viewer' , label : 'Viewer' }
67- ]
62+ async function fetchRoleOptions ( db : any ) : Promise < Array < { value : string ; label : string } > > {
63+ const roles = await new RbacService ( db ) . getRoles ( )
64+ return roles . map ( ( r ) => ( { value : r . id , label : r . display_name } ) )
65+ }
6866
6967/**
7068 * GET /admin/profile - Show user profile page
@@ -630,8 +628,10 @@ userRoutes.get('/users/new', async (c) => {
630628 ? renderCustomProfileSection ( { fields : regFields } , { } )
631629 : undefined
632630
631+ const roles = await fetchRoleOptions ( db )
632+
633633 const pageData : UserNewPageData = {
634- roles : ROLES ,
634+ roles,
635635 registrationFieldsHtml,
636636 user : {
637637 name : user ! . email . split ( '@' ) [ 0 ] || user ! . email ,
@@ -668,9 +668,9 @@ userRoutes.post('/users/new', async (c) => {
668668 const email = formData . get ( 'email' ) ?. toString ( ) ?. trim ( ) . toLowerCase ( ) || ''
669669 const phone = sanitizeInput ( formData . get ( 'phone' ) ?. toString ( ) ) || null
670670 const bio = sanitizeInput ( formData . get ( 'bio' ) ?. toString ( ) ) || null
671- const roleInput = formData . get ( 'role' ) ?. toString ( ) || 'viewer '
672- const validRoles = [ 'admin' , 'editor' , 'author' , 'viewer' ]
673- const role = validRoles . includes ( roleInput ) ? roleInput : 'viewer'
671+ const roleInput = formData . get ( 'role' ) ?. toString ( ) || ''
672+ const validRoles = ( await new RbacService ( db ) . getRoles ( ) ) . map ( ( r ) => r . id )
673+ const role = validRoles . includes ( roleInput ) ? roleInput : ( validRoles [ 0 ] ?? 'admin' )
674674 const password = formData . get ( 'password' ) ?. toString ( ) || ''
675675 const confirmPassword = formData . get ( 'confirm_password' ) ?. toString ( ) || ''
676676 const isActive = formData . get ( 'is_active' ) === '1'
@@ -942,9 +942,11 @@ userRoutes.get('/users/:id/edit', async (c) => {
942942 const queryMessage = c . req . query ( 'message' )
943943 const queryType = c . req . query ( 'type' ) === 'error' ? 'error' : 'success'
944944
945+ const roles = await fetchRoleOptions ( db )
946+
945947 const pageData : UserEditPageData = {
946948 userToEdit : editData ,
947- roles : ROLES ,
949+ roles,
948950 hasProfilePlugin : profileConfig !== null && upRow !== null ,
949951 customProfileFieldsHtml,
950952 tenantMemberships,
@@ -985,9 +987,9 @@ userRoutes.put('/users/:id', async (c) => {
985987 const lastName = sanitizeInput ( formData . get ( 'last_name' ) ?. toString ( ) )
986988 const email = formData . get ( 'email' ) ?. toString ( ) ?. trim ( ) . toLowerCase ( ) || ''
987989 const phone = sanitizeInput ( formData . get ( 'phone' ) ?. toString ( ) ) || null
988- const roleInput = formData . get ( 'role' ) ?. toString ( ) || 'viewer '
989- const validRoles = [ 'admin' , 'editor' , 'author' , 'viewer' ]
990- const role = validRoles . includes ( roleInput ) ? roleInput : 'viewer'
990+ const roleInput = formData . get ( 'role' ) ?. toString ( ) || ''
991+ const validRoles = ( await new RbacService ( db ) . getRoles ( ) ) . map ( ( r ) => r . id )
992+ const role = validRoles . includes ( roleInput ) ? roleInput : ( validRoles [ 0 ] ?? 'admin' )
991993 const isActive = formData . get ( 'is_active' ) === '1'
992994 const emailVerified = formData . get ( 'email_verified' ) === '1'
993995
0 commit comments