Änderung: Erweiterung der Benutzer- und Rechteverwaltung im Admin-Bereich
Änderungen: - Neue Funktionen zur Benutzerverwaltung hinzugefügt: Benutzer suchen, Benutzer abrufen und Benutzer aktualisieren. - Implementierung von Funktionen zur Verwaltung von Benutzerrechten: Rechtearten auflisten, Benutzerrechte auflisten, Recht hinzufügen und Recht entfernen. - Routen für die neuen Funktionen im Admin-Router definiert. - Übersetzungen für Benutzer- und Rechteverwaltung in den Sprachdateien aktualisiert. Diese Anpassungen verbessern die Verwaltung von Benutzern und deren Rechten im Admin-Bereich und erweitern die Funktionalität der Anwendung.
This commit is contained in:
@@ -409,6 +409,103 @@ class AdminService {
|
||||
await character.save();
|
||||
}
|
||||
|
||||
// --- User Administration ---
|
||||
async searchUsers(requestingHashedUserId, query) {
|
||||
if (!(await this.hasUserAccess(requestingHashedUserId, 'useradministration'))) {
|
||||
throw new Error('noaccess');
|
||||
}
|
||||
if (!query || query.trim().length === 0) return [];
|
||||
|
||||
const users = await User.findAll({
|
||||
where: {
|
||||
[Op.or]: [
|
||||
{ username: { [Op.iLike]: `%${query}%` } },
|
||||
// email is encrypted, can't search directly reliably; skip email search
|
||||
]
|
||||
},
|
||||
attributes: ['id', 'hashedId', 'username', 'active', 'registrationDate']
|
||||
});
|
||||
return users.map(u => ({ id: u.hashedId, username: u.username, active: u.active, registrationDate: u.registrationDate }));
|
||||
}
|
||||
|
||||
async getUserByHashedId(requestingHashedUserId, targetHashedId) {
|
||||
if (!(await this.hasUserAccess(requestingHashedUserId, 'useradministration'))) {
|
||||
throw new Error('noaccess');
|
||||
}
|
||||
const user = await User.findOne({
|
||||
where: { hashedId: targetHashedId },
|
||||
attributes: ['id', 'hashedId', 'username', 'active', 'registrationDate']
|
||||
});
|
||||
if (!user) throw new Error('notfound');
|
||||
return { id: user.hashedId, username: user.username, active: user.active, registrationDate: user.registrationDate };
|
||||
}
|
||||
|
||||
async updateUser(requestingHashedUserId, targetHashedId, data) {
|
||||
if (!(await this.hasUserAccess(requestingHashedUserId, 'useradministration'))) {
|
||||
throw new Error('noaccess');
|
||||
}
|
||||
const user = await User.findOne({ where: { hashedId: targetHashedId } });
|
||||
if (!user) throw new Error('notfound');
|
||||
|
||||
const updates = {};
|
||||
if (typeof data.username === 'string' && data.username.trim().length > 0) {
|
||||
updates.username = data.username.trim();
|
||||
}
|
||||
if (typeof data.active === 'boolean') {
|
||||
updates.active = data.active;
|
||||
}
|
||||
if (Object.keys(updates).length === 0) return { id: user.hashedId, username: user.username, active: user.active };
|
||||
await user.update(updates);
|
||||
return { id: user.hashedId, username: user.username, active: user.active };
|
||||
}
|
||||
|
||||
// --- User Rights Administration ---
|
||||
async listUserRightTypes(requestingHashedUserId) {
|
||||
if (!(await this.hasUserAccess(requestingHashedUserId, 'rights'))) {
|
||||
throw new Error('noaccess');
|
||||
}
|
||||
const types = await UserRightType.findAll({ attributes: ['id', 'title'] });
|
||||
// map to tr keys if needed; keep title as key used elsewhere
|
||||
return types.map(t => ({ id: t.id, title: t.title }));
|
||||
}
|
||||
|
||||
async listUserRightsForUser(requestingHashedUserId, targetHashedId) {
|
||||
if (!(await this.hasUserAccess(requestingHashedUserId, 'rights'))) {
|
||||
throw new Error('noaccess');
|
||||
}
|
||||
const user = await User.findOne({ where: { hashedId: targetHashedId }, attributes: ['id', 'hashedId', 'username'] });
|
||||
if (!user) throw new Error('notfound');
|
||||
const rights = await UserRight.findAll({
|
||||
where: { userId: user.id },
|
||||
include: [{ model: UserRightType, as: 'rightType' }]
|
||||
});
|
||||
return rights.map(r => ({ rightTypeId: r.rightTypeId, title: r.rightType?.title }));
|
||||
}
|
||||
|
||||
async addUserRight(requestingHashedUserId, targetHashedId, rightTypeId) {
|
||||
if (!(await this.hasUserAccess(requestingHashedUserId, 'rights'))) {
|
||||
throw new Error('noaccess');
|
||||
}
|
||||
const user = await User.findOne({ where: { hashedId: targetHashedId } });
|
||||
if (!user) throw new Error('notfound');
|
||||
const type = await UserRightType.findByPk(rightTypeId);
|
||||
if (!type) throw new Error('wrongtype');
|
||||
const existing = await UserRight.findOne({ where: { userId: user.id, rightTypeId } });
|
||||
if (existing) return existing; // idempotent
|
||||
const created = await UserRight.create({ userId: user.id, rightTypeId });
|
||||
return created;
|
||||
}
|
||||
|
||||
async removeUserRight(requestingHashedUserId, targetHashedId, rightTypeId) {
|
||||
if (!(await this.hasUserAccess(requestingHashedUserId, 'rights'))) {
|
||||
throw new Error('noaccess');
|
||||
}
|
||||
const user = await User.findOne({ where: { hashedId: targetHashedId } });
|
||||
if (!user) throw new Error('notfound');
|
||||
await UserRight.destroy({ where: { userId: user.id, rightTypeId } });
|
||||
return true;
|
||||
}
|
||||
|
||||
// --- Chat Room Admin ---
|
||||
async getRoomTypes(userId) {
|
||||
if (!(await this.hasUserAccess(userId, 'chatrooms'))) {
|
||||
|
||||
Reference in New Issue
Block a user