Гибкие права доступа
Видео поясненение
Таблица с правами
https://docs.google.com/spreadsheets/d/1Cp0ymngh0kfGEgZ9UMkN5gxa4bSTxdw2I3bvBtJbZ3w/edit?gid=0#gid=0
Скрипт
<script>
const sheetBaseUrl = 'https://docs.google.com/spreadsheets/d/1Cp0ymngh0kfGEgZ9UMkN5gxa4bSTxdw2I3bvBtJbZ3w/gviz/tq?tqx=out:csv';
const accessSheetUrl = sheetBaseUrl + '&sheet=Доступ';
const superUserSheetUrl = sheetBaseUrl + '&sheet=SUPERUSERS';
const currentPath = window.location.pathname.toLowerCase();
const userId = String(window.accountUserId).trim();
const waitForUserInfo = setInterval(() => {
const user = window.userInfo;
if (!user) return;
clearInterval(waitForUserInfo);
const hasAccessRole = user.isAdmin || user.isManager || user.isTeacher;
if (!hasAccessRole) return;
Promise.all([
fetch(accessSheetUrl).then(r => r.text()),
fetch(superUserSheetUrl).then(r => r.text())
])
.then(([accessCsv, superCsv]) => {
const superUsers = superCsv
.trim()
.split('\n')
.flatMap(row => row.split(','))
.map(id => id.replace(/["\r\n]/g, '').trim())
.filter(Boolean);
if (superUsers.includes(userId)) return;
const rows = accessCsv.trim().split('\n'); // 👈 теперь БЕЗ slice(1)
const cleanRows = [];
for (const row of rows) {
const parts = row.split(',').map(p => p.replace(/["\r\n]/g, '').trim());
const basePath = parts[1]?.toLowerCase() || "";
const ids = parts.slice(2).filter(Boolean);
if (basePath.length >= 3) {
cleanRows.push({ basePath, ids });
}
}
const matchedRow = cleanRows.find(r => currentPath.includes(r.basePath));
if (matchedRow) {
if (matchedRow.ids.includes(userId)) {
console.log(`✅ Доступ разрешён к "${matchedRow.basePath}"`);
} else {
console.warn(`❌ Доступ запрещён — ID не найден в строке "${matchedRow.basePath}"`);
window.location.replace('/redirect-acces');
}
} else {
console.log("🟢 Путь не ограничен — доступ открыт");
}
})
.catch(err => console.error('❌ Ошибка загрузки:', err));
}, 100);
</script>
No Comments