HM-136. Закрытие доступа к страницам для простых пользователей (#68)
This commit is contained in:
@ -37,7 +37,7 @@ const initAppComponents = () => {
|
|||||||
routerPagesContainer.addRoutes([
|
routerPagesContainer.addRoutes([
|
||||||
{url: ROUTES.MAIN, pageComponent: MainPage},
|
{url: ROUTES.MAIN, pageComponent: MainPage},
|
||||||
{url: ROUTES.STORE, pageComponent: ApiPage},
|
{url: ROUTES.STORE, pageComponent: ApiPage},
|
||||||
{url: ROUTES.LOGS, pageComponent: LogsPage},
|
{url: ROUTES.LOGS, pageComponent: LogsPage, onlyAdmin: true},
|
||||||
{url: ROUTES.USERS, pageComponent: UsersPage},
|
{url: ROUTES.USERS, pageComponent: UsersPage},
|
||||||
{url: ROUTES.LOGIN, pageComponent: LoginPage},
|
{url: ROUTES.LOGIN, pageComponent: LoginPage},
|
||||||
{url: ROUTES.PROFILE, pageComponent: ProfilePage},
|
{url: ROUTES.PROFILE, pageComponent: ProfilePage},
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import routeService from '../../../services/RouteService';
|
|||||||
import './MainMenu.css';
|
import './MainMenu.css';
|
||||||
import {EVENTS, ROUTES, TAG_NAME} from '../../consts';
|
import {EVENTS, ROUTES, TAG_NAME} from '../../consts';
|
||||||
import tokenApi from '../../../api/TokenAPI';
|
import tokenApi from '../../../api/TokenAPI';
|
||||||
|
import userInfoService from '../../../services/UserInfoService';
|
||||||
|
|
||||||
const NAV_MENU = [
|
const NAV_MENU = [
|
||||||
{
|
{
|
||||||
@ -17,6 +18,7 @@ const NAV_MENU = [
|
|||||||
{
|
{
|
||||||
title: 'Журнал',
|
title: 'Журнал',
|
||||||
url: ROUTES.LOGS,
|
url: ROUTES.LOGS,
|
||||||
|
onlyAdmin: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Пользователи',
|
title: 'Пользователи',
|
||||||
@ -76,7 +78,7 @@ class MainMenu extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
render = () => {
|
render = () => {
|
||||||
this.menuItems = NAV_MENU.map(({url, title, className = ''}) => {
|
this.menuItems = NAV_MENU.map(({url, title, className = '', onlyAdmin}) => {
|
||||||
const li = this.createElement({
|
const li = this.createElement({
|
||||||
tagName: TAG_NAME.LI,
|
tagName: TAG_NAME.LI,
|
||||||
parentNode: this.buttonsContainer,
|
parentNode: this.buttonsContainer,
|
||||||
@ -99,7 +101,7 @@ class MainMenu extends Component {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return {url, link};
|
return {url, li, link, onlyAdmin};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,7 +113,12 @@ class MainMenu extends Component {
|
|||||||
this.mainNode.remove();
|
this.mainNode.remove();
|
||||||
}
|
}
|
||||||
|
|
||||||
showMenu = () => {
|
showMenu = async () => {
|
||||||
|
const {is_admin} = await userInfoService.getUserInfo();
|
||||||
|
this.menuItems.forEach(({li, onlyAdmin}) => {
|
||||||
|
const isShow = !onlyAdmin || is_admin;
|
||||||
|
li.style.display = isShow ? 'list-item' : 'none';
|
||||||
|
});
|
||||||
document.body.prepend(this.mainNode);
|
document.body.prepend(this.mainNode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,7 @@ import routeService from '../../../services/RouteService';
|
|||||||
import NotFoundPage from '../../../pages/not-found/components/page/Page';
|
import NotFoundPage from '../../../pages/not-found/components/page/Page';
|
||||||
|
|
||||||
import {EVENTS, ROUTES} from '../../consts';
|
import {EVENTS, ROUTES} from '../../consts';
|
||||||
|
import userInfoService from '../../../services/UserInfoService';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @interface Route
|
* @interface Route
|
||||||
@ -59,6 +60,14 @@ class RouterPagesContainer extends Component {
|
|||||||
this.currentPage = new PageComponent('#page', this.mainNode);
|
this.currentPage = new PageComponent('#page', this.mainNode);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.addSubscribe(userInfoService, EVENTS.CHANGE_USER_INFO, ({is_admin}) => {
|
||||||
|
const {url} = routeService.getUrlData();
|
||||||
|
const currentRoute = this.routes.find((route) => route.url === url);
|
||||||
|
if (currentRoute.onlyAdmin && !is_admin) {
|
||||||
|
routeService.goTo(ROUTES.MAIN);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -27,7 +27,7 @@ class UsersPage extends Component {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.addSubscribe(userInfoService, EVENTS.CHANGE_USER_INFO, ({is_admin}) => {
|
this.addSubscribe(userInfoService, EVENTS.CHANGE_USER_INFO, ({is_admin}) => {
|
||||||
this.createUserButton.disabled = !is_admin;
|
this.setCreateUserButton(is_admin);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.addEventListener(this.createUserButton, EVENTS.CLICK, () => {
|
this.addEventListener(this.createUserButton, EVENTS.CLICK, () => {
|
||||||
@ -61,9 +61,13 @@ class UsersPage extends Component {
|
|||||||
this.initPage();
|
this.initPage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setCreateUserButton = (isAdmin) => {
|
||||||
|
this.createUserButton.style.display = isAdmin ? 'inline-block' : 'none';
|
||||||
|
}
|
||||||
|
|
||||||
initPage = async () => {
|
initPage = async () => {
|
||||||
const user = await userInfoService.getUserInfo();
|
const user = await userInfoService.getUserInfo();
|
||||||
this.createUserButton.disabled = !user.is_admin;
|
this.setCreateUserButton(user.is_admin);
|
||||||
this.userList = await usersServiceApi.request();
|
this.userList = await usersServiceApi.request();
|
||||||
this.renderTable();
|
this.renderTable();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user