HM-136. Закрытие доступа к страницам для простых пользователей (#68)

This commit is contained in:
Nikolay
2020-09-08 23:05:42 +03:00
committed by GitHub
parent 39a5e29781
commit 00a2c29a09
4 changed files with 26 additions and 6 deletions

View File

@ -37,7 +37,7 @@ const initAppComponents = () => {
routerPagesContainer.addRoutes([
{url: ROUTES.MAIN, pageComponent: MainPage},
{url: ROUTES.STORE, pageComponent: ApiPage},
{url: ROUTES.LOGS, pageComponent: LogsPage},
{url: ROUTES.LOGS, pageComponent: LogsPage, onlyAdmin: true},
{url: ROUTES.USERS, pageComponent: UsersPage},
{url: ROUTES.LOGIN, pageComponent: LoginPage},
{url: ROUTES.PROFILE, pageComponent: ProfilePage},

View File

@ -4,6 +4,7 @@ import routeService from '../../../services/RouteService';
import './MainMenu.css';
import {EVENTS, ROUTES, TAG_NAME} from '../../consts';
import tokenApi from '../../../api/TokenAPI';
import userInfoService from '../../../services/UserInfoService';
const NAV_MENU = [
{
@ -17,6 +18,7 @@ const NAV_MENU = [
{
title: 'Журнал',
url: ROUTES.LOGS,
onlyAdmin: true,
},
{
title: 'Пользователи',
@ -76,7 +78,7 @@ class MainMenu extends Component {
}
render = () => {
this.menuItems = NAV_MENU.map(({url, title, className = ''}) => {
this.menuItems = NAV_MENU.map(({url, title, className = '', onlyAdmin}) => {
const li = this.createElement({
tagName: TAG_NAME.LI,
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();
}
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);
}
}

View File

@ -3,6 +3,7 @@ import routeService from '../../../services/RouteService';
import NotFoundPage from '../../../pages/not-found/components/page/Page';
import {EVENTS, ROUTES} from '../../consts';
import userInfoService from '../../../services/UserInfoService';
/**
* @interface Route
@ -59,6 +60,14 @@ class RouterPagesContainer extends Component {
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);
}
});
}
/**

View File

@ -27,7 +27,7 @@ class UsersPage extends Component {
});
this.addSubscribe(userInfoService, EVENTS.CHANGE_USER_INFO, ({is_admin}) => {
this.createUserButton.disabled = !is_admin;
this.setCreateUserButton(is_admin);
});
this.addEventListener(this.createUserButton, EVENTS.CLICK, () => {
@ -61,9 +61,13 @@ class UsersPage extends Component {
this.initPage();
}
setCreateUserButton = (isAdmin) => {
this.createUserButton.style.display = isAdmin ? 'inline-block' : 'none';
}
initPage = async () => {
const user = await userInfoService.getUserInfo();
this.createUserButton.disabled = !user.is_admin;
this.setCreateUserButton(user.is_admin);
this.userList = await usersServiceApi.request();
this.renderTable();
}