Implement batch user retrieval in AdminController and update routes
- Added a new method `getUsers` in AdminController to handle batch retrieval of user information based on hashed IDs. - Updated adminRouter to include a new route for batch user retrieval. - Enhanced AdminService with a method to fetch user details by hashed IDs, ensuring proper access control. - Updated localization files to include the new "username" field for user connections in both German and English. - Modified ServicesStatusView to utilize the new batch user retrieval for displaying usernames alongside connection counts.
This commit is contained in:
@@ -27,6 +27,7 @@ class AdminController {
|
||||
// User administration
|
||||
this.searchUsers = this.searchUsers.bind(this);
|
||||
this.getUser = this.getUser.bind(this);
|
||||
this.getUsers = this.getUsers.bind(this);
|
||||
this.updateUser = this.updateUser.bind(this);
|
||||
|
||||
// Rights
|
||||
@@ -74,6 +75,30 @@ class AdminController {
|
||||
}
|
||||
}
|
||||
|
||||
async getUsers(req, res) {
|
||||
try {
|
||||
const { userid: requester } = req.headers;
|
||||
let { ids } = req.query;
|
||||
if (!ids) {
|
||||
return res.status(400).json({ error: 'ids query parameter is required' });
|
||||
}
|
||||
// Unterstütze sowohl Array-Format (ids[]=...) als auch komma-separierten String (ids=...)
|
||||
let hashedIds;
|
||||
if (Array.isArray(ids)) {
|
||||
hashedIds = ids;
|
||||
} else if (typeof ids === 'string') {
|
||||
hashedIds = ids.split(',').map(id => id.trim()).filter(id => id.length > 0);
|
||||
} else {
|
||||
return res.status(400).json({ error: 'ids must be an array or comma-separated string' });
|
||||
}
|
||||
const result = await AdminService.getUsersByHashedIds(requester, hashedIds);
|
||||
res.status(200).json(result);
|
||||
} catch (error) {
|
||||
const status = error.message === 'noaccess' ? 403 : 500;
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
async updateUser(req, res) {
|
||||
try {
|
||||
const { userid: requester } = req.headers;
|
||||
|
||||
@@ -18,6 +18,7 @@ router.delete('/chat/rooms/:id', authenticate, adminController.deleteRoom);
|
||||
// --- Users Admin ---
|
||||
router.get('/users/search', authenticate, adminController.searchUsers);
|
||||
router.get('/users/statistics', authenticate, adminController.getUserStatistics);
|
||||
router.get('/users/batch', authenticate, adminController.getUsers);
|
||||
router.get('/users/:id', authenticate, adminController.getUser);
|
||||
router.put('/users/:id', authenticate, adminController.updateUser);
|
||||
|
||||
|
||||
@@ -441,6 +441,30 @@ class AdminService {
|
||||
return { id: user.hashedId, username: user.username, active: user.active, registrationDate: user.registrationDate };
|
||||
}
|
||||
|
||||
async getUsersByHashedIds(requestingHashedUserId, targetHashedIds) {
|
||||
if (!(await this.hasUserAccess(requestingHashedUserId, 'useradministration'))) {
|
||||
throw new Error('noaccess');
|
||||
}
|
||||
if (!Array.isArray(targetHashedIds) || targetHashedIds.length === 0) {
|
||||
return [];
|
||||
}
|
||||
const users = await User.findAll({
|
||||
where: { hashedId: { [Op.in]: targetHashedIds } },
|
||||
attributes: ['id', 'hashedId', 'username', 'active', 'registrationDate']
|
||||
});
|
||||
// Erstelle ein Map für schnellen Zugriff
|
||||
const userMap = {};
|
||||
users.forEach(user => {
|
||||
userMap[user.hashedId] = {
|
||||
id: user.hashedId,
|
||||
username: user.username,
|
||||
active: user.active,
|
||||
registrationDate: user.registrationDate
|
||||
};
|
||||
});
|
||||
return userMap;
|
||||
}
|
||||
|
||||
async updateUser(requestingHashedUserId, targetHashedId, data) {
|
||||
if (!(await this.hasUserAccess(requestingHashedUserId, 'useradministration'))) {
|
||||
throw new Error('noaccess');
|
||||
|
||||
Reference in New Issue
Block a user