2016-12-01 11:40:28 +02:00
|
|
|
/*
|
2017-01-09 23:11:09 +02:00
|
|
|
* Copyright © 2016-2017 The Thingsboard Authors
|
2016-12-01 11:40:28 +02:00
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
2016-12-20 17:32:29 +02:00
|
|
|
import Flow from '@flowjs/ng-flow/dist/ng-flow-standalone.min';
|
2017-03-13 16:00:49 +02:00
|
|
|
import UrlHandler from './url.handler';
|
2016-12-20 17:32:29 +02:00
|
|
|
|
2016-12-01 11:40:28 +02:00
|
|
|
/*@ngInject*/
|
2017-03-13 16:00:49 +02:00
|
|
|
export default function AppRun($rootScope, $window, $injector, $location, $log, $state, $mdDialog, $filter, loginService, userService, $translate) {
|
2016-12-01 11:40:28 +02:00
|
|
|
|
2016-12-20 17:32:29 +02:00
|
|
|
$window.Flow = Flow;
|
2016-12-01 11:40:28 +02:00
|
|
|
var frame = $window.frameElement;
|
|
|
|
|
var unauthorizedDialog = null;
|
|
|
|
|
var forbiddenDialog = null;
|
|
|
|
|
|
2017-02-06 18:11:39 +02:00
|
|
|
$rootScope.iframeMode = false;
|
|
|
|
|
|
2016-12-01 11:40:28 +02:00
|
|
|
if (frame) {
|
2017-02-06 18:11:39 +02:00
|
|
|
$rootScope.iframeMode = true;
|
2016-12-01 11:40:28 +02:00
|
|
|
var dataWidgetAttr = angular.element(frame).attr('data-widget');
|
|
|
|
|
if (dataWidgetAttr) {
|
|
|
|
|
$rootScope.editWidgetInfo = angular.fromJson(dataWidgetAttr);
|
|
|
|
|
$rootScope.widgetEditMode = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
initWatchers();
|
|
|
|
|
|
|
|
|
|
function initWatchers() {
|
|
|
|
|
$rootScope.unauthenticatedHandle = $rootScope.$on('unauthenticated', function (event, doLogout) {
|
|
|
|
|
if (doLogout) {
|
|
|
|
|
$state.go('login');
|
|
|
|
|
} else {
|
2017-03-13 16:00:49 +02:00
|
|
|
UrlHandler($injector, $location);
|
2016-12-01 11:40:28 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$rootScope.authenticatedHandle = $rootScope.$on('authenticated', function () {
|
2017-03-13 16:00:49 +02:00
|
|
|
UrlHandler($injector, $location);
|
2016-12-01 11:40:28 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$rootScope.forbiddenHandle = $rootScope.$on('forbidden', function () {
|
|
|
|
|
showForbiddenDialog();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$rootScope.stateChangeStartHandle = $rootScope.$on('$stateChangeStart', function (evt, to, params) {
|
|
|
|
|
if (userService.isUserLoaded() === true) {
|
|
|
|
|
if (userService.isAuthenticated()) {
|
2017-03-07 17:34:46 +02:00
|
|
|
if (userService.forceDefaultPlace(to, params)) {
|
2016-12-01 11:40:28 +02:00
|
|
|
evt.preventDefault();
|
2017-03-07 17:34:46 +02:00
|
|
|
gotoDefaultPlace(params);
|
|
|
|
|
} else {
|
|
|
|
|
var authority = userService.getAuthority();
|
|
|
|
|
if (to.module === 'public') {
|
|
|
|
|
evt.preventDefault();
|
|
|
|
|
gotoDefaultPlace(params);
|
|
|
|
|
} else if (angular.isDefined(to.auth) &&
|
|
|
|
|
to.auth.indexOf(authority) === -1) {
|
|
|
|
|
evt.preventDefault();
|
|
|
|
|
showForbiddenDialog();
|
|
|
|
|
} else if (to.redirectTo) {
|
|
|
|
|
evt.preventDefault();
|
|
|
|
|
$state.go(to.redirectTo, params)
|
|
|
|
|
}
|
2016-12-01 11:40:28 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (to.module === 'private') {
|
|
|
|
|
evt.preventDefault();
|
2017-03-07 17:34:46 +02:00
|
|
|
if (to.url === '/home' || to.url === '/') {
|
2016-12-01 11:40:28 +02:00
|
|
|
$state.go('login', params);
|
|
|
|
|
} else {
|
|
|
|
|
showUnauthorizedDialog();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
evt.preventDefault();
|
2017-03-07 17:34:46 +02:00
|
|
|
if ($rootScope.userLoadedHandle) {
|
|
|
|
|
$rootScope.userLoadedHandle();
|
|
|
|
|
}
|
2016-12-01 11:40:28 +02:00
|
|
|
$rootScope.userLoadedHandle = $rootScope.$on('userLoaded', function () {
|
|
|
|
|
$rootScope.userLoadedHandle();
|
|
|
|
|
$state.go(to.name, params);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
$rootScope.pageTitle = 'Thingsboard';
|
|
|
|
|
|
|
|
|
|
$rootScope.stateChangeSuccessHandle = $rootScope.$on('$stateChangeSuccess', function (evt, to) {
|
|
|
|
|
if (angular.isDefined(to.data.pageTitle)) {
|
|
|
|
|
$translate(to.data.pageTitle).then(function (translation) {
|
|
|
|
|
$rootScope.pageTitle = 'Thingsboard | ' + translation;
|
|
|
|
|
}, function (translationId) {
|
|
|
|
|
$rootScope.pageTitle = 'Thingsboard | ' + translationId;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-07 17:34:46 +02:00
|
|
|
function gotoDefaultPlace(params) {
|
|
|
|
|
userService.gotoDefaultPlace(params);
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-01 11:40:28 +02:00
|
|
|
function showUnauthorizedDialog() {
|
|
|
|
|
if (unauthorizedDialog === null) {
|
|
|
|
|
$translate(['access.unauthorized-access',
|
|
|
|
|
'access.unauthorized-access-text',
|
|
|
|
|
'access.unauthorized',
|
|
|
|
|
'action.cancel',
|
|
|
|
|
'action.sign-in']).then(function (translations) {
|
|
|
|
|
if (unauthorizedDialog === null) {
|
|
|
|
|
unauthorizedDialog = $mdDialog.confirm()
|
|
|
|
|
.title(translations['access.unauthorized-access'])
|
|
|
|
|
.textContent(translations['access.unauthorized-access-text'])
|
|
|
|
|
.ariaLabel(translations['access.unauthorized'])
|
|
|
|
|
.cancel(translations['action.cancel'])
|
|
|
|
|
.ok(translations['action.sign-in']);
|
|
|
|
|
$mdDialog.show(unauthorizedDialog).then(function () {
|
|
|
|
|
unauthorizedDialog = null;
|
|
|
|
|
$state.go('login');
|
|
|
|
|
}, function () {
|
|
|
|
|
unauthorizedDialog = null;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function showForbiddenDialog() {
|
|
|
|
|
if (forbiddenDialog === null) {
|
|
|
|
|
$translate(['access.access-forbidden',
|
|
|
|
|
'access.access-forbidden-text',
|
|
|
|
|
'access.access-forbidden',
|
|
|
|
|
'action.cancel',
|
|
|
|
|
'action.sign-in']).then(function (translations) {
|
|
|
|
|
if (forbiddenDialog === null) {
|
|
|
|
|
forbiddenDialog = $mdDialog.confirm()
|
|
|
|
|
.title(translations['access.access-forbidden'])
|
|
|
|
|
.htmlContent(translations['access.access-forbidden-text'])
|
|
|
|
|
.ariaLabel(translations['access.access-forbidden'])
|
|
|
|
|
.cancel(translations['action.cancel'])
|
|
|
|
|
.ok(translations['action.sign-in']);
|
|
|
|
|
$mdDialog.show(forbiddenDialog).then(function () {
|
|
|
|
|
forbiddenDialog = null;
|
|
|
|
|
userService.logout();
|
|
|
|
|
}, function () {
|
|
|
|
|
forbiddenDialog = null;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|