import { inject as service } from '@ember/service'; import Route from '@ember/routing/route'; import EmberError from '@ember/error'; import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin'; import MethodNotAllowedError from '../adapters/method-not-allowed-error'; import InternalServerError from '../adapters/internal-server-error'; import NotFoundError from '../adapters/not-found-error'; export default Route.extend(ApplicationRouteMixin, { sessionAccount: service('session-account'), homeNotification: service(), router: service(), splashScreenService: service('ember-cordova/splash'), session: service(), beforeModel(transition) { let result = this._loadCurrentUser(); this._redirectTo(transition); return result; }, afterModel() { this.get('splashScreenService').hide(); }, async sessionAuthenticated() { this._super(...arguments); // await this._loadCurrentUser(); this._loadCurrentUser(); }, _loadCurrentUser() { return this.sessionAccount.loadCurrentUser(); }, _redirectTo(transition) { let routeArr = transition.targetName.split("."); let routeName = routeArr[0]; console.log(routeName); console.log(routeName.underscore()); let routeAction = routeArr[1]; if (this.sessionAccount.currentPrivilegeInfo) { this.sessionAccount.currentPrivilegeInfo.then(result => { result.forEach(item => { let functionId = item.coreFunctionInfo.get("id"); if (functionId == routeName.underscore()) { let notAllowed = false; if (routeAction == "index") { if (!item.allowRead) { notAllowed = true; } } if (routeAction == "create") { if (!item.allowCreate) { notAllowed = true; } } if (routeAction == "edit") { if (!item.allowUpdate) { notAllowed = true; } } if (routeAction == "detail") { if (!item.allowRead) { notAllowed = true; } } if (notAllowed) { this.transitionTo('/error?type=405'); } } }); }); } }, actions: { willTransition(transition) { this._redirectTo(transition); }, error(error, transition) { if (error instanceof MethodNotAllowedError) { this.transitionTo('/error?type=405'); } if (error instanceof InternalServerError) { this.transitionTo('/error?type=500'); } if (error instanceof NotFoundError) { this.transitionTo('/error?type=404'); } let errorStatus = ""; if (error != null && error != undefined) { errorStatus = error.status; } if (errorStatus === '403') { this.replaceWith('login'); } else { this.transitionTo('/error'); } } } });