tehnicheskoe-zadanie-informa/auth.js

47 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Проверка авторизации
function getCurrentUser() {
const saved = localStorage.getItem("sdCurrentUser");
return saved ? JSON.parse(saved) : null;
}
function requireAuth() {
const user = getCurrentUser();
if (!user) {
window.location = "login.html";
return null;
}
return user;
}
function canEdit() {
const user = getCurrentUser();
return user && (user.role === "admin" || user.role === "pmo");
}
function canDelete() {
const user = getCurrentUser();
return user && user.role === "admin";
}
function logout() {
localStorage.removeItem("sdCurrentUser");
window.location = "login.html";
}
function renderUserMenu() {
const user = getCurrentUser();
if (!user) return;
const nav = document.querySelector(".nav");
if (nav && !document.getElementById("userMenu")) {
const menu = document.createElement("span");
menu.id = "userMenu";
menu.style.cssText = "margin-left:24px;display:flex;align-items:center;gap:12px";
menu.innerHTML = `
<span style="color:rgba(255,255,255,0.8);font-size:14px">${user.name} (${user.role === "admin" ? "Админ" : user.role === "pmo" ? "ПО" : user.role === "rm" ? "РМ" : "Исполнитель"})</span>
<button onclick="logout()" style="background:rgba(255,255,255,0.2);border:none;color:white;padding:6px 12px;border-radius:4px;cursor:pointer;font-size:13px">Выйти</button>
`;
nav.appendChild(menu);
}
}