Sửa lỗi quản lí users, chỉnh sửa users

This commit is contained in:
2026-06-08 21:30:03 +07:00
parent 6dfc811292
commit fadfb6ba09
7 changed files with 376 additions and 35 deletions
+60 -6
View File
@@ -569,7 +569,7 @@ router.delete('/scenes/:id', protect, async (req, res) => {
}
// Kiểm tra quyền: Người tạo hoặc Admin
const isAdmin = req.user.role === 'Chủ sở hữu' || req.user.role === 'admin';
const isAdmin = req.user.role === 'admin';
const isOwner = rootScene.createdBy.toString() === req.user._id.toString();
if (!isAdmin && !isOwner) {
@@ -689,7 +689,7 @@ router.get('/me/scenes', protect, async (req, res) => {
router.get('/me/assets', protect, async (req, res) => {
try {
// Sử dụng Aggregation để lấy Asset kèm thông tin Scene và Parent Scene
const query = req.user.role === 'Chủ sở hữu' ? {} : { uploadedBy: req.user._id };
const query = (req.user.role === 'admin' || req.user.role === 'Chủ sở hữu') ? {} : { uploadedBy: req.user._id };
const assets = await Asset.aggregate([
{ $match: query },
@@ -744,7 +744,7 @@ router.delete('/assets/:id', protect, async (req, res) => {
// Kiểm tra quyền: Người upload hoặc Admin (Chủ sở hữu)
const isOwner = asset.uploadedBy && asset.uploadedBy.toString() === req.user._id.toString();
const isAdmin = req.user.role === 'Chủ sở hữu' || req.user.role === 'admin';
const isAdmin = req.user.role === 'admin' || req.user.role === 'Chủ sở hữu';
if (!isOwner && !isAdmin) {
return res.status(403).json({ message: 'Bạn không có quyền xóa tập tin này' });
@@ -783,17 +783,67 @@ router.delete('/assets/:id', protect, async (req, res) => {
* @access Private (Admin)
*/
router.get('/admin/users', protect, async (req, res) => {
if (req.user.role !== 'Chủ sở hữu') {
if (req.user.role !== 'admin' && req.user.role !== 'Chủ sở hữu') {
return res.status(403).json({ message: 'Bạn không có quyền truy cập quản trị' });
}
try {
const users = await User.find({}).select('-password').sort({ createdAt: -1 });
const users = await User.find({}).sort({ createdAt: -1 });
res.json(users);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
/**
* @route PUT /api/admin/users/:id
* @desc Admin cập nhật thông tin người dùng (Quyền, Mật khẩu, Email, Họ tên)
*/
router.put('/admin/users/:id', protect, async (req, res) => {
if (req.user.role !== 'admin' && req.user.role !== 'Chủ sở hữu') return res.status(403).json({ message: 'Forbidden' });
try {
const { fullName, email, role, password } = req.body;
const user = await User.findById(req.params.id);
if (!user) return res.status(404).json({ message: 'Người dùng không tồn tại' });
// Cập nhật thông tin cơ bản
if (fullName) user.fullName = fullName;
if (email) user.email = email;
if (role) user.role = role;
// Nếu có nhập mật khẩu mới thì cập nhật (Middleware pre-save sẽ tự hash)
if (password && password.trim() !== '') {
user.password = password;
}
await user.save();
res.json({ message: 'Cập nhật người dùng thành công' });
} catch (error) {
res.status(500).json({ message: error.message });
}
});
/**
* @route DELETE /api/admin/users/:id
* @desc Admin xóa vĩnh viễn người dùng
*/
router.delete('/admin/users/:id', protect, async (req, res) => {
if (req.user.role !== 'admin' && req.user.role !== 'Chủ sở hữu') return res.status(403).json({ message: 'Forbidden' });
try {
const user = await User.findById(req.params.id);
if (!user) return res.status(404).json({ message: 'Người dùng không tồn tại' });
if (user.role === 'admin' && user._id.toString() === req.user._id.toString()) {
return res.status(400).json({ message: 'Bạn không thể tự xóa chính mình' });
}
// Lưu ý: Trong thực tế bạn có thể muốn xóa cả các Scene của user này
await User.findByIdAndDelete(req.params.id);
res.json({ message: 'Đã xóa người dùng vĩnh viễn' });
} catch (error) {
res.status(500).json({ message: error.message });
}
});
/**
* @route GET/PUT /api/system/settings
* @desc Quản lý thiết lập hệ thống
@@ -810,7 +860,7 @@ router.get('/system/settings', optionalAuth, async (req, res) => {
});
router.put('/system/settings', protect, async (req, res) => {
if (req.user.role !== 'Chủ sở hữu') return res.status(403).json({ message: 'Forbidden' });
if (req.user.role !== 'admin' && req.user.role !== 'Chủ sở hữu') return res.status(403).json({ message: 'Forbidden' });
try {
const settings = await Setting.findOneAndUpdate({}, req.body, { new: true, upsert: true });
res.json(settings);
@@ -826,6 +876,10 @@ router.put('/system/settings', protect, async (req, res) => {
*/
router.post('/maintenance/reset-all', protect, async (req, res) => {
try {
if (req.user.role !== 'admin' && req.user.role !== 'Chủ sở hữu') {
return res.status(403).json({ message: 'Chỉ Admin tối cao mới có quyền thực hiện thao tác này' });
}
// 1. Xóa toàn bộ dữ liệu trong Database
await Scene.deleteMany({});
await Asset.deleteMany({});
+15 -6
View File
@@ -11,26 +11,35 @@ const router = express.Router();
*/
router.post('/register', async (req, res) => {
try {
const { username, password, role } = req.body;
const { fullName, email, username, password, agreedToRules } = req.body;
// Check if user already exists
const userExists = await User.findOne({ username });
// Kiểm tra thông tin bắt buộc
if (!fullName || !email || !username || !password || agreedToRules === undefined) {
return res.status(400).json({ message: 'Vui lòng cung cấp đầy đủ thông tin đăng ký' });
}
// Kiểm tra xem username hoặc email đã tồn tại chưa
const userExists = await User.findOne({ $or: [{ username }, { email }] });
if (userExists) {
return res.status(400).json({ message: 'User already exists' });
const field = userExists.username === username ? 'Tên đăng nhập' : 'Email';
return res.status(400).json({ message: `${field} đã được sử dụng` });
}
// Check if this is the very first user registering
const userCount = await User.countDocuments();
let finalRole = 'Thành viên';
let finalRole = 'user';
if (userCount === 0) {
// First user to register in the system gets the supreme admin role
finalRole = 'Chủ sở hữu';
finalRole = 'admin';
}
const user = new User({
fullName,
email,
username,
password,
agreedToRules,
role: finalRole
});