class UserManagement { constructor() { this.authManager = authManager; } getCurrentUser() { return this.authManager.getCurrentUser(); } getUserProfile() { const user = this.getCurrentUser(); return user ? { userId: user.userId, username: user.username, email: user.email, role: user.role, profile: user.profile, progress: user.progress } : null; } updateProfile(updates) { const users = this.authManager.getUsers(); const currentUser = this.getCurrentUser(); if (!currentUser) { return { success: false, message: 'User not authenticated' }; } const user = users[currentUser.userId]; if (!user) { return { success: false, message: 'User not found' }; } // به‌روزرسانی فیلدها if (updates.email) user.email = updates.email; if (updates.profile) { user.profile = { ...user.profile, ...updates.profile }; } this.authManager.saveUsers(users); this.authManager.setCurrentUser(user); return { success: true, user: user }; } changePassword(currentPassword, newPassword) { const users = this.authManager.getUsers(); const currentUser = this.getCurrentUser(); if (!currentUser) { return { success: false, message: 'User not authenticated' }; } const user = users[currentUser.userId]; const currentHashedPassword = this.authManager.hashPassword(currentPassword); if (user.password !== currentHashedPassword) { return { success: false, message: 'Current password is incorrect' }; } user.password = this.authManager.hashPassword(newPassword); this.authManager.saveUsers(users); return { success: true, message: 'Password changed successfully' }; } getAllUsers() { if (!authzManager.canManageUsers()) { return { success: false, message: 'Access denied' }; } const users = this.authManager.getUsers(); // حذف اطلاعات حساس قبل از بازگرداندن const sanitizedUsers = {}; for (const userId in users) { const user = users[userId]; sanitizedUsers[userId] = { userId: user.userId, username: user.username, email: user.email, role: user.role, profile: user.profile, progress: user.progress, isActive: user.isActive, createdAt: user.createdAt }; } return { success: true, users: sanitizedUsers }; } deactivateUser(userId) { if (!authzManager.canManageUsers()) { return { success: false, message: 'Access denied' }; } const users = this.authManager.getUsers(); if (users[userId]) { users[userId].isActive = false; this.authManager.saveUsers(users); return { success: true, message: 'User deactivated successfully' }; } return { success: false, message: 'User not found' }; } getUserProgress(userId = null) { const targetUserId = userId || (this.getCurrentUser()?.userId); if (!targetUserId) { return { success: false, message: 'User not specified' }; } const users = this.authManager.getUsers(); const user = users[targetUserId]; if (!user) { return { success: false, message: 'User not found' }; } // اگر کاربر درخواست‌دهنده خود کاربر باشد یا ادمین باشد const currentUser = this.getCurrentUser(); if (currentUser.userId !== targetUserId && !authzManager.canManageUsers()) { return { success: false, message: 'Access denied' }; } return { success: true, progress: user.progress }; } updateUserProgress(score = 0, completedLesson = null) { const currentUser = this.getCurrentUser(); if (!currentUser) { return { success: false, message: 'User not authenticated' }; } return this.authManager.updateUserProgress(currentUser.userId, score, completedLesson); } getLeaderboard(limit = 10) { const users = this.authManager.getUsers(); const usersArray = Object.values(users) .filter(user => user.isActive) .map(user => ({ username: user.username, profile: user.profile, progress: user.progress })) .sort((a, b) => (b.progress.totalScore || 0) - (a.progress.totalScore || 0)) .slice(0, limit); return { success: true, leaderboard: usersArray }; } } // ایجاد نمونه singleton const userManager = new UserManagement();