diff --git a/public/ui.js b/public/ui.js index 31cac93..e9ff1c2 100644 --- a/public/ui.js +++ b/public/ui.js @@ -660,25 +660,68 @@ function closeAddFileModal() { async function loadTaskFiles(taskId) { try { const response = await fetch(`/api/tasks/${taskId}/files`); - const files = await response.json(); + const allFiles = await response.json(); - // Обновляем файлы в задаче + // Получаем задачу для определения создателя const taskIndex = tasks.findIndex(t => t.id === taskId); - if (taskIndex !== -1) { - tasks[taskIndex].files = files; + if (taskIndex === -1) { + console.error('Задача не найдена:', taskId); + return; } + const task = tasks[taskIndex]; + const creatorId = parseInt(task.created_by); + + // Фильтруем файлы по тем же правилам, что и в HTML + const filteredFiles = allFiles.filter(file => { + // Определяем, может ли пользователь видеть этот файл + let canSeeFile = false; + + // 1. Администратор видит все файлы + if (currentUser.role === 'admin') { + canSeeFile = true; + } + // 2. Создатель задачи видит все файлы + else if (creatorId === currentUser.id) { + canSeeFile = true; + } + // 3. Исполнитель видит: + // - Файлы, загруженные создателем + // - Свои файлы + else { + const fileUserId = parseInt(file.user_id); + + // Файл загружен создателем + if (fileUserId === creatorId) { + canSeeFile = true; + } + // Файл загружен текущим пользователем (исполнителем) + else if (fileUserId === currentUser.id) { + canSeeFile = true; + } + // Если файл загружен другим исполнителем - не показываем + else { + canSeeFile = false; + } + } + + return canSeeFile; + }); + + // Обновляем отфильтрованные файлы в задаче + tasks[taskIndex].files = filteredFiles; + // Обновляем отображение файлов const fileContainer = document.getElementById(`files-${taskId}`); if (fileContainer) { const fileList = fileContainer.querySelector('.file-icons-container'); if (fileList) { - fileList.innerHTML = files.map(file => renderFileIcon(file)).join(''); + fileList.innerHTML = filteredFiles.map(file => renderFileIcon(file)).join(''); } else { fileContainer.innerHTML = ` Файлы: - ${files.length > 0 ? - `
` : + ${filteredFiles.length > 0 ? + `` : 'нет файлов' } `;