Add admin user management in settings panel (username and password change)

This commit is contained in:
salvacybersec
2025-11-11 05:39:13 +03:00
parent 3e012be78e
commit fba0a8469f
3 changed files with 310 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
const { Settings } = require('../models');
const { Settings, AdminUser } = require('../models');
const mailService = require('../services/mail.service');
const telegramService = require('../services/telegram.service');
const bcrypt = require('bcrypt');
// Get all settings
exports.getAllSettings = async (req, res, next) => {
@@ -208,5 +209,147 @@ exports.testTelegram = async (req, res, next) => {
}
};
// Get admin user info (current logged in user)
exports.getAdminInfo = async (req, res, next) => {
try {
const userId = req.session.userId;
if (!userId) {
return res.status(401).json({
success: false,
error: 'Authentication required',
});
}
const admin = await AdminUser.findByPk(userId, {
attributes: ['id', 'username', 'email', 'full_name', 'created_at', 'updated_at'],
});
if (!admin) {
return res.status(404).json({
success: false,
error: 'Admin user not found',
});
}
res.json({
success: true,
data: admin,
});
} catch (error) {
next(error);
}
};
// Update admin user info (username and/or password)
exports.updateAdminInfo = async (req, res, next) => {
try {
const userId = req.session.userId;
if (!userId) {
return res.status(401).json({
success: false,
error: 'Authentication required',
});
}
const { username, current_password, new_password, confirm_password } = req.body;
const admin = await AdminUser.findByPk(userId);
if (!admin) {
return res.status(404).json({
success: false,
error: 'Admin user not found',
});
}
// Update username if provided
if (username && username.trim() !== '') {
const newUsername = username.trim();
// Validate username
if (newUsername.length < 3) {
return res.status(400).json({
success: false,
error: 'Kullanıcı adı en az 3 karakter olmalıdır',
});
}
// Check if username is already taken by another user
const existingUser = await AdminUser.findOne({
where: {
username: newUsername,
id: { [require('sequelize').Op.ne]: userId },
},
});
if (existingUser) {
return res.status(400).json({
success: false,
error: 'Bu kullanıcı adı zaten kullanılıyor',
});
}
admin.username = newUsername;
}
// Update password if provided
if (new_password) {
// Validate password
if (new_password.length < 8) {
return res.status(400).json({
success: false,
error: 'Şifre en az 8 karakter olmalıdır',
});
}
// Check password confirmation
if (new_password !== confirm_password) {
return res.status(400).json({
success: false,
error: 'Yeni şifreler eşleşmiyor',
});
}
// Verify current password
if (!current_password) {
return res.status(400).json({
success: false,
error: 'Mevcut şifre gereklidir',
});
}
const isPasswordValid = await bcrypt.compare(current_password, admin.password);
if (!isPasswordValid) {
return res.status(400).json({
success: false,
error: 'Mevcut şifre yanlış',
});
}
// Hash new password
const hashedPassword = await bcrypt.hash(new_password, 10);
admin.password = hashedPassword;
}
await admin.save();
res.json({
success: true,
message: 'Admin bilgileri başarıyla güncellendi',
data: {
id: admin.id,
username: admin.username,
email: admin.email,
full_name: admin.full_name,
},
});
} catch (error) {
next(error);
}
};
module.exports = exports;

View File

@@ -13,5 +13,9 @@ router.put('/system', settingsController.updateSystemSettings);
router.post('/test-gmail', settingsController.testGmail);
router.post('/test-telegram', settingsController.testTelegram);
// Admin user management
router.get('/admin', settingsController.getAdminInfo);
router.put('/admin', settingsController.updateAdminInfo);
module.exports = router;