feat: implement admin-users management with super admin restriction
- Add /admin/admin-users routes for managing admin users (super admin only) - Remove vendor role from user creation form (vendors created via company hierarchy) - Add admin-users.html and admin-user-detail.html templates - Add admin-users.js and admin-user-detail.js for frontend logic - Move database operations to admin_platform_service (list, get, create, delete, toggle status) - Update sidebar to show Admin Users section only for super admins - Add isSuperAdmin computed property to init-alpine.js - Fix /api/v1 prefix issues in JS files (apiClient already adds prefix) - Update architecture rule JS-012 to catch more variable patterns (url, endpoint, path) - Replace inline SVGs with $icon() helper in select-platform.html Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
// static/admin/js/user-create.js
|
||||
|
||||
// Create custom logger for user create
|
||||
const userCreateLog = window.LogConfig.createLogger('USER-CREATE');
|
||||
// Create custom logger for admin user create
|
||||
const userCreateLog = window.LogConfig.createLogger('ADMIN-USER-CREATE');
|
||||
|
||||
function adminUserCreate() {
|
||||
return {
|
||||
// Inherit base layout functionality from init-alpine.js
|
||||
...data(),
|
||||
|
||||
// User create page specific state
|
||||
currentPage: 'user-create',
|
||||
// Admin user create page specific state
|
||||
currentPage: 'admin-users',
|
||||
loading: false,
|
||||
formData: {
|
||||
username: '',
|
||||
@@ -17,7 +17,6 @@ function adminUserCreate() {
|
||||
password: '',
|
||||
first_name: '',
|
||||
last_name: '',
|
||||
role: 'vendor',
|
||||
is_super_admin: false,
|
||||
platform_ids: []
|
||||
},
|
||||
@@ -27,11 +26,11 @@ function adminUserCreate() {
|
||||
|
||||
// Initialize
|
||||
async init() {
|
||||
userCreateLog.info('=== USER CREATE PAGE INITIALIZING ===');
|
||||
userCreateLog.info('=== ADMIN USER CREATE PAGE INITIALIZING ===');
|
||||
|
||||
// Prevent multiple initializations
|
||||
if (window._userCreateInitialized) {
|
||||
userCreateLog.warn('User create page already initialized, skipping...');
|
||||
userCreateLog.warn('Admin user create page already initialized, skipping...');
|
||||
return;
|
||||
}
|
||||
window._userCreateInitialized = true;
|
||||
@@ -39,7 +38,7 @@ function adminUserCreate() {
|
||||
// Load platforms for admin assignment
|
||||
await this.loadPlatforms();
|
||||
|
||||
userCreateLog.info('=== USER CREATE PAGE INITIALIZATION COMPLETE ===');
|
||||
userCreateLog.info('=== ADMIN USER CREATE PAGE INITIALIZATION COMPLETE ===');
|
||||
},
|
||||
|
||||
// Load available platforms
|
||||
@@ -55,16 +54,6 @@ function adminUserCreate() {
|
||||
}
|
||||
},
|
||||
|
||||
// Handle role change
|
||||
onRoleChange() {
|
||||
userCreateLog.debug('Role changed to:', this.formData.role);
|
||||
if (this.formData.role !== 'admin') {
|
||||
// Reset admin-specific fields when switching away from admin
|
||||
this.formData.is_super_admin = false;
|
||||
this.formData.platform_ids = [];
|
||||
}
|
||||
},
|
||||
|
||||
// Validate form
|
||||
validateForm() {
|
||||
this.errors = {};
|
||||
@@ -79,8 +68,8 @@ function adminUserCreate() {
|
||||
this.errors.password = 'Password must be at least 6 characters';
|
||||
}
|
||||
|
||||
// Admin-specific validation
|
||||
if (this.formData.role === 'admin' && !this.formData.is_super_admin) {
|
||||
// Platform admin validation: must have at least one platform
|
||||
if (!this.formData.is_super_admin) {
|
||||
if (!this.formData.platform_ids || this.formData.platform_ids.length === 0) {
|
||||
this.errors.platform_ids = 'Platform admins must be assigned to at least one platform';
|
||||
}
|
||||
@@ -91,7 +80,7 @@ function adminUserCreate() {
|
||||
|
||||
// Submit form
|
||||
async handleSubmit() {
|
||||
userCreateLog.info('=== CREATING USER ===');
|
||||
userCreateLog.info('=== CREATING ADMIN USER ===');
|
||||
userCreateLog.debug('Form data:', { ...this.formData, password: '[REDACTED]' });
|
||||
|
||||
if (!this.validateForm()) {
|
||||
@@ -103,55 +92,38 @@ function adminUserCreate() {
|
||||
this.saving = true;
|
||||
|
||||
try {
|
||||
let url, payload, response;
|
||||
|
||||
if (this.formData.role === 'admin') {
|
||||
// Use admin-users endpoint for creating admin users
|
||||
url = '/api/v1/admin/admin-users';
|
||||
payload = {
|
||||
email: this.formData.email,
|
||||
username: this.formData.username,
|
||||
password: this.formData.password,
|
||||
first_name: this.formData.first_name || null,
|
||||
last_name: this.formData.last_name || null,
|
||||
is_super_admin: this.formData.is_super_admin,
|
||||
platform_ids: this.formData.is_super_admin ? [] : this.formData.platform_ids.map(id => parseInt(id))
|
||||
};
|
||||
} else {
|
||||
// Use regular users endpoint for vendor users
|
||||
url = '/admin/users';
|
||||
payload = {
|
||||
email: this.formData.email,
|
||||
username: this.formData.username,
|
||||
password: this.formData.password,
|
||||
first_name: this.formData.first_name || null,
|
||||
last_name: this.formData.last_name || null,
|
||||
role: this.formData.role
|
||||
};
|
||||
}
|
||||
// Use admin-users endpoint for creating admin users
|
||||
const url = '/admin/admin-users';
|
||||
const payload = {
|
||||
email: this.formData.email,
|
||||
username: this.formData.username,
|
||||
password: this.formData.password,
|
||||
first_name: this.formData.first_name || null,
|
||||
last_name: this.formData.last_name || null,
|
||||
is_super_admin: this.formData.is_super_admin,
|
||||
platform_ids: this.formData.is_super_admin ? [] : this.formData.platform_ids.map(id => parseInt(id))
|
||||
};
|
||||
|
||||
window.LogConfig.logApiCall('POST', url, { ...payload, password: '[REDACTED]' }, 'request');
|
||||
|
||||
const startTime = performance.now();
|
||||
response = await apiClient.post(url, payload);
|
||||
const response = await apiClient.post(url, payload);
|
||||
const duration = performance.now() - startTime;
|
||||
|
||||
window.LogConfig.logApiCall('POST', url, response, 'response');
|
||||
window.LogConfig.logPerformance('Create User', duration);
|
||||
window.LogConfig.logPerformance('Create Admin User', duration);
|
||||
|
||||
const userType = this.formData.role === 'admin'
|
||||
? (this.formData.is_super_admin ? 'Super admin' : 'Platform admin')
|
||||
: 'User';
|
||||
const userType = this.formData.is_super_admin ? 'Super admin' : 'Platform admin';
|
||||
Utils.showToast(`${userType} created successfully`, 'success');
|
||||
userCreateLog.info(`${userType} created successfully in ${duration}ms`, response);
|
||||
|
||||
// Redirect to the new user's detail page
|
||||
// Redirect to the admin users list
|
||||
setTimeout(() => {
|
||||
window.location.href = `/admin/users/${response.id}`;
|
||||
window.location.href = `/admin/admin-users/${response.id}`;
|
||||
}, 1500);
|
||||
|
||||
} catch (error) {
|
||||
window.LogConfig.logError(error, 'Create User');
|
||||
window.LogConfig.logError(error, 'Create Admin User');
|
||||
|
||||
// Handle validation errors
|
||||
if (error.details && error.details.validation_errors) {
|
||||
@@ -173,13 +145,13 @@ function adminUserCreate() {
|
||||
}
|
||||
}
|
||||
|
||||
Utils.showToast(error.message || 'Failed to create user', 'error');
|
||||
Utils.showToast(error.message || 'Failed to create admin user', 'error');
|
||||
} finally {
|
||||
this.saving = false;
|
||||
userCreateLog.info('=== USER CREATION COMPLETE ===');
|
||||
userCreateLog.info('=== ADMIN USER CREATION COMPLETE ===');
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
userCreateLog.info('User create module loaded');
|
||||
userCreateLog.info('Admin user create module loaded');
|
||||
|
||||
Reference in New Issue
Block a user