294 lines
8.8 KiB
JavaScript
294 lines
8.8 KiB
JavaScript
// [DEF:adminService:Module]
|
|
//
|
|
// @TIER: STANDARD
|
|
// @SEMANTICS: admin, users, roles, ad-mappings, api
|
|
// @PURPOSE: Service for Admin-related API calls (User and Role management).
|
|
// @LAYER: Service
|
|
// @RELATION: DEPENDS_ON -> frontend.src.lib.api
|
|
//
|
|
// @INVARIANT: All requests must include valid Admin JWT token (handled by api client).
|
|
|
|
// [SECTION: IMPORTS]
|
|
import { api } from '../lib/api';
|
|
// [/SECTION]
|
|
|
|
// [DEF:getUsers:Function]
|
|
/**
|
|
* @purpose Fetches all registered users from the backend.
|
|
* @pre User must be authenticated with Admin privileges.
|
|
* @post Returns an array of user objects.
|
|
* @returns {Promise<Array>}
|
|
* @relation CALLS -> backend.src.api.routes.admin.list_users
|
|
*/
|
|
async function getUsers() {
|
|
console.log('[getUsers][Entry]');
|
|
try {
|
|
const users = await api.requestApi('/admin/users', 'GET');
|
|
console.log('[getUsers][Coherence:OK]');
|
|
return users;
|
|
} catch (e) {
|
|
console.error('[getUsers][Coherence:Failed]', e);
|
|
throw e;
|
|
}
|
|
}
|
|
// [/DEF:getUsers:Function]
|
|
|
|
// [DEF:createUser:Function]
|
|
/**
|
|
* @purpose Creates a new local user.
|
|
* @pre User must be authenticated with Admin privileges.
|
|
* @param {Object} userData - User details (username, email, password, roles, is_active).
|
|
* @post New user record created in auth.db.
|
|
* @returns {Promise<Object>}
|
|
* @relation CALLS -> backend.src.api.routes.admin.create_user
|
|
*/
|
|
async function createUser(userData) {
|
|
console.log('[createUser][Entry]');
|
|
try {
|
|
const user = await api.postApi('/admin/users', userData);
|
|
console.log('[createUser][Coherence:OK]');
|
|
return user;
|
|
} catch (e) {
|
|
console.error('[createUser][Coherence:Failed]', e);
|
|
throw e;
|
|
}
|
|
}
|
|
// [/DEF:createUser:Function]
|
|
|
|
// [DEF:getRoles:Function]
|
|
/**
|
|
* @purpose Fetches all available system roles.
|
|
* @pre User must be authenticated with Admin privileges.
|
|
* @post Returns an array of role objects.
|
|
* @returns {Promise<Array>}
|
|
* @relation CALLS -> backend.src.api.routes.admin.list_roles
|
|
*/
|
|
async function getRoles() {
|
|
console.log('[getRoles][Entry]');
|
|
try {
|
|
const roles = await api.requestApi('/admin/roles', 'GET');
|
|
console.log('[getRoles][Coherence:OK]');
|
|
return roles;
|
|
} catch (e) {
|
|
console.error('[getRoles][Coherence:Failed]', e);
|
|
throw e;
|
|
}
|
|
}
|
|
// [/DEF:getRoles:Function]
|
|
|
|
// [DEF:getADGroupMappings:Function]
|
|
/**
|
|
* @purpose Fetches mappings between AD groups and local roles.
|
|
* @pre User must be authenticated with Admin privileges.
|
|
* @post Returns an array of AD group mapping objects.
|
|
* @returns {Promise<Array>}
|
|
*/
|
|
async function getADGroupMappings() {
|
|
console.log('[getADGroupMappings][Entry]');
|
|
try {
|
|
const mappings = await api.requestApi('/admin/ad-mappings', 'GET');
|
|
console.log('[getADGroupMappings][Coherence:OK]');
|
|
return mappings;
|
|
} catch (e) {
|
|
console.error('[getADGroupMappings][Coherence:Failed]', e);
|
|
throw e;
|
|
}
|
|
}
|
|
// [/DEF:getADGroupMappings:Function]
|
|
|
|
// [DEF:createADGroupMapping:Function]
|
|
/**
|
|
* @purpose Creates or updates an AD group to Role mapping.
|
|
* @pre User must be authenticated with Admin privileges.
|
|
* @post New or updated mapping created in auth.db.
|
|
* @param {Object} mappingData - Mapping details (ad_group, role_id).
|
|
* @returns {Promise<Object>}
|
|
*/
|
|
async function createADGroupMapping(mappingData) {
|
|
console.log('[createADGroupMapping][Entry]');
|
|
try {
|
|
const mapping = await api.postApi('/admin/ad-mappings', mappingData);
|
|
console.log('[createADGroupMapping][Coherence:OK]');
|
|
return mapping;
|
|
} catch (e) {
|
|
console.error('[createADGroupMapping][Coherence:Failed]', e);
|
|
throw e;
|
|
}
|
|
}
|
|
// [/DEF:createADGroupMapping:Function]
|
|
|
|
// [DEF:updateUser:Function]
|
|
/**
|
|
* @purpose Updates an existing user.
|
|
* @pre User must be authenticated with Admin privileges.
|
|
* @post User record updated in auth.db.
|
|
* @param {string} userId - Target user ID.
|
|
* @param {Object} userData - Updated user data.
|
|
* @returns {Promise<Object>}
|
|
*/
|
|
async function updateUser(userId, userData) {
|
|
console.log('[updateUser][Entry]');
|
|
try {
|
|
const user = await api.requestApi(`/admin/users/${userId}`, 'PUT', userData);
|
|
console.log('[updateUser][Coherence:OK]');
|
|
return user;
|
|
} catch (e) {
|
|
console.error('[updateUser][Coherence:Failed]', e);
|
|
throw e;
|
|
}
|
|
}
|
|
// [/DEF:updateUser:Function]
|
|
|
|
// [DEF:deleteUser:Function]
|
|
/**
|
|
* @purpose Deletes a user.
|
|
* @pre User must be authenticated with Admin privileges.
|
|
* @post User record removed from auth.db.
|
|
* @param {string} userId - Target user ID.
|
|
* @returns {Promise<void>}
|
|
*/
|
|
async function deleteUser(userId) {
|
|
console.log('[deleteUser][Entry]');
|
|
try {
|
|
await api.requestApi(`/admin/users/${userId}`, 'DELETE');
|
|
console.log('[deleteUser][Coherence:OK]');
|
|
} catch (e) {
|
|
console.error('[deleteUser][Coherence:Failed]', e);
|
|
throw e;
|
|
}
|
|
}
|
|
// [/DEF:deleteUser:Function]
|
|
|
|
// [DEF:createRole:Function]
|
|
/**
|
|
* @purpose Creates a new role.
|
|
* @pre User must be authenticated with Admin privileges.
|
|
* @post New role created in auth.db.
|
|
* @param {Object} roleData - Role details (name, description, permissions).
|
|
* @returns {Promise<Object>}
|
|
*/
|
|
async function createRole(roleData) {
|
|
console.log('[createRole][Entry]');
|
|
try {
|
|
const role = await api.postApi('/admin/roles', roleData);
|
|
console.log('[createRole][Coherence:OK]');
|
|
return role;
|
|
} catch (e) {
|
|
console.error('[createRole][Coherence:Failed]', e);
|
|
throw e;
|
|
}
|
|
}
|
|
// [/DEF:createRole:Function]
|
|
|
|
// [DEF:updateRole:Function]
|
|
/**
|
|
* @purpose Updates an existing role.
|
|
* @param {string} roleId - Target role ID.
|
|
* @param {Object} roleData - Updated role data.
|
|
* @returns {Promise<Object>}
|
|
*/
|
|
async function updateRole(roleId, roleData) {
|
|
console.log('[updateRole][Entry]');
|
|
try {
|
|
const role = await api.requestApi(`/admin/roles/${roleId}`, 'PUT', roleData);
|
|
console.log('[updateRole][Coherence:OK]');
|
|
return role;
|
|
} catch (e) {
|
|
console.error('[updateRole][Coherence:Failed]', e);
|
|
throw e;
|
|
}
|
|
}
|
|
// [/DEF:updateRole:Function]
|
|
|
|
// [DEF:deleteRole:Function]
|
|
/**
|
|
* @purpose Deletes a role.
|
|
* @param {string} roleId - Target role ID.
|
|
* @returns {Promise<void>}
|
|
*/
|
|
async function deleteRole(roleId) {
|
|
console.log('[deleteRole][Entry]');
|
|
try {
|
|
await api.requestApi(`/admin/roles/${roleId}`, 'DELETE');
|
|
console.log('[deleteRole][Coherence:OK]');
|
|
} catch (e) {
|
|
console.error('[deleteRole][Coherence:Failed]', e);
|
|
throw e;
|
|
}
|
|
}
|
|
// [/DEF:deleteRole:Function]
|
|
|
|
// [DEF:getPermissions:Function]
|
|
/**
|
|
* @purpose Fetches all available permissions.
|
|
* @returns {Promise<Array>}
|
|
*/
|
|
async function getPermissions() {
|
|
console.log('[getPermissions][Entry]');
|
|
try {
|
|
const permissions = await api.requestApi('/admin/permissions', 'GET');
|
|
console.log('[getPermissions][Coherence:OK]');
|
|
return permissions;
|
|
} catch (e) {
|
|
console.error('[getPermissions][Coherence:Failed]', e);
|
|
throw e;
|
|
}
|
|
}
|
|
// [/DEF:getPermissions:Function]
|
|
|
|
// [DEF:getLoggingConfig:Function]
|
|
/**
|
|
* @purpose Fetches current logging configuration.
|
|
* @returns {Promise<Object>} - Logging config with level, task_log_level, enable_belief_state.
|
|
* @relation CALLS -> backend.src.api.routes.settings.get_logging_config
|
|
*/
|
|
async function getLoggingConfig() {
|
|
console.log('[getLoggingConfig][Entry]');
|
|
try {
|
|
const config = await api.requestApi('/settings/logging', 'GET');
|
|
console.log('[getLoggingConfig][Coherence:OK]');
|
|
return config;
|
|
} catch (e) {
|
|
console.error('[getLoggingConfig][Coherence:Failed]', e);
|
|
throw e;
|
|
}
|
|
}
|
|
// [/DEF:getLoggingConfig:Function]
|
|
|
|
// [DEF:updateLoggingConfig:Function]
|
|
/**
|
|
* @purpose Updates logging configuration.
|
|
* @param {Object} configData - Logging config (level, task_log_level, enable_belief_state).
|
|
* @returns {Promise<Object>}
|
|
* @relation CALLS -> backend.src.api.routes.settings.update_logging_config
|
|
*/
|
|
async function updateLoggingConfig(configData) {
|
|
console.log('[updateLoggingConfig][Entry]');
|
|
try {
|
|
const config = await api.requestApi('/settings/logging', 'PATCH', configData);
|
|
console.log('[updateLoggingConfig][Coherence:OK]');
|
|
return config;
|
|
} catch (e) {
|
|
console.error('[updateLoggingConfig][Coherence:Failed]', e);
|
|
throw e;
|
|
}
|
|
}
|
|
// [/DEF:updateLoggingConfig:Function]
|
|
|
|
export const adminService = {
|
|
getUsers,
|
|
createUser,
|
|
updateUser,
|
|
deleteUser,
|
|
getRoles,
|
|
createRole,
|
|
updateRole,
|
|
deleteRole,
|
|
getPermissions,
|
|
getADGroupMappings,
|
|
createADGroupMapping,
|
|
getLoggingConfig,
|
|
updateLoggingConfig
|
|
};
|
|
|
|
// [/DEF:adminService:Module]
|