ui5_easy_use
Version:
CLI tool for SAP ui5 and SAPUI5 projects to initialize apps, generate pages, insert form and table components, manage routing, and automate i18n bindings
73 lines (60 loc) • 2.46 kB
JavaScript
sap.ui.define([], function () {
"use strict";
return class PagesAccess {
constructor(component) {
this._componentJS = component;
this.accessControl = this._componentJS.accessControl;
this.authorizedPages = [];
this.setNavAndRulesNav();
}
setNavAndRulesNav() {
this.rulesNavList = this._getModelData("rulesNavList");
this.navList = this._getModelData("navList");
}
setAuthorizedPages() {
this.authorizedPages = [];
const roles = this.rulesNavList.roles || {};
Object.keys(roles).forEach(function (routeKey) {
if (this.accessControl.canAccess(roles[routeKey])) {
this.authorizedPages.push(routeKey);
}
}, this);
}
_onRouteMatched(oEvent) {
const routeName = oEvent.getParameter("name");
if (!this.authorizedPages.includes(routeName)) {
this.redirectToHome();
}
}
redirectToHome() {
sap.m.MessageToast.show("Access Denied! You don't have permission to access this view!!!");
this._componentJS.getRouter().navTo("RouteHome");
}
setAuthorizedNavigation() {
const filteredNavs = this.filterNavigation();
const navListModel = this._componentJS.getModel("navList");
if (navListModel) {
navListModel.setProperty("/navigation", filteredNavs.navigation);
}
return filteredNavs;
}
filterNavigation() {
return {
navigation: this._filterNavigationItems(this.navList.navigation || [])
};
}
_filterNavigationItems(items) {
return (items || []).reduce(function (authorizedItems, item) {
const filteredChildren = this._filterNavigationItems(item.items || []);
if (this.authorizedPages.includes(item.key) || filteredChildren.length > 0) {
authorizedItems.push(Object.assign({}, item, filteredChildren.length ? { items: filteredChildren } : {}));
}
return authorizedItems;
}.bind(this), []);
}
_getModelData(modelName) {
const model = this._componentJS.getModel(modelName);
return model ? model.getData() : {};
}
};
});