router.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var misc = require('utils/misc');
  19. var App = require('app');
  20. App.WizardRoute = Em.Route.extend({
  21. gotoStep0: Em.Router.transitionTo('step0'),
  22. gotoStep1: Em.Router.transitionTo('step1'),
  23. gotoStep2: Em.Router.transitionTo('step2'),
  24. gotoStep3: Em.Router.transitionTo('step3'),
  25. gotoStep4: Em.Router.transitionTo('step4'),
  26. gotoStep5: Em.Router.transitionTo('step5'),
  27. gotoStep6: Em.Router.transitionTo('step6'),
  28. gotoStep7: Em.Router.transitionTo('step7'),
  29. gotoStep8: Em.Router.transitionTo('step8'),
  30. gotoStep9: Em.Router.transitionTo('step9'),
  31. gotoStep10: Em.Router.transitionTo('step10'),
  32. isRoutable: function() {
  33. return (typeof this.get('route') === 'string' && App.router.get('loggedIn'));
  34. }.property('App.router.loggedIn')
  35. });
  36. App.Router = Em.Router.extend({
  37. enableLogging: true,
  38. isFwdNavigation: true,
  39. backBtnForHigherStep: false,
  40. transitionInProgress: false,
  41. nextBtnClickInProgress: false,
  42. /**
  43. * Path for local login page. This page will be always accessible without
  44. * redirect to auth server different from ambari-server. Used in some types of
  45. * authorizations like knox sso.
  46. *
  47. * @type {string}
  48. */
  49. localUserAuthUrl: '/login/local',
  50. /**
  51. * LocalStorage property <code>redirectsCount</code> from <code>tmp</code> namespace
  52. * will be incremented by each redirect action performed by UI and reset on success login.
  53. * <code>redirectsLimitCount</code> determines maximum redirect tries. When redirects count overflow
  54. * then something goes wrong and we have to inform user about the problem.
  55. *
  56. * @type {number}
  57. */
  58. redirectsLimitCount: 0,
  59. /**
  60. * Is true, if cluster.provisioning_state is equal to 'INSTALLED'
  61. * @type {Boolean}
  62. */
  63. clusterInstallCompleted: false,
  64. /**
  65. * user prefered path to route
  66. */
  67. preferedPath: null,
  68. setNavigationFlow: function (step) {
  69. var matches = step.match(/\d+$/);
  70. var newStep;
  71. if (matches) {
  72. newStep = parseInt(matches[0]);
  73. }
  74. var previousStep = parseInt(this.getInstallerCurrentStep());
  75. this.set('isFwdNavigation', newStep >= previousStep);
  76. },
  77. clearAllSteps: function () {
  78. this.get('installerController').clear();
  79. this.get('addHostController').clear();
  80. this.get('addServiceController').clear();
  81. this.get('backgroundOperationsController').clear();
  82. for (var i = 1; i < 11; i++) {
  83. this.set('wizardStep' + i + 'Controller.hasSubmitted', false);
  84. this.set('wizardStep' + i + 'Controller.isDisabled', true);
  85. }
  86. },
  87. /**
  88. * Temporary fix for getting cluster name
  89. * @return {*}
  90. */
  91. getClusterName: function () {
  92. return App.router.get('clusterController').get('clusterName');
  93. },
  94. /**
  95. * Get current step of Installer wizard
  96. * @return {*}
  97. */
  98. getInstallerCurrentStep: function () {
  99. return this.getWizardCurrentStep('installer');
  100. },
  101. /**
  102. * Get current step for <code>wizardType</code> wizard
  103. * @param wizardType one of <code>installer</code>, <code>addHost</code>, <code>addServices</code>
  104. */
  105. getWizardCurrentStep: function (wizardType) {
  106. var currentStep = App.db.getWizardCurrentStep(wizardType);
  107. if (!currentStep) {
  108. currentStep = wizardType === 'installer' ? '0' : '1';
  109. }
  110. return currentStep;
  111. },
  112. /**
  113. * @type {boolean}
  114. */
  115. loggedIn: App.db.getAuthenticated(),
  116. loginName: function() {
  117. return this.getLoginName();
  118. }.property('loggedIn'),
  119. getAuthenticated: function () {
  120. var dfd = $.Deferred();
  121. var self = this;
  122. var auth = App.db.getAuthenticated();
  123. App.ajax.send({
  124. name: 'router.login.clusters',
  125. sender: this,
  126. success: 'onAuthenticationSuccess',
  127. error: 'onAuthenticationError'
  128. }).complete(function (xhr) {
  129. if (xhr.isResolved()) {
  130. // if server knows the user and user authenticated by UI
  131. if (auth) {
  132. dfd.resolve(self.get('loggedIn'));
  133. // if server knows the user but UI don't, check the response header
  134. // and try to authorize
  135. } else if (xhr.getResponseHeader('User')) {
  136. var user = xhr.getResponseHeader('User');
  137. App.ajax.send({
  138. name: 'router.login',
  139. sender: self,
  140. data: {
  141. usr: user,
  142. loginName: encodeURIComponent(user)
  143. },
  144. success: 'loginSuccessCallback',
  145. error: 'loginErrorCallback'
  146. });
  147. } else {
  148. self.setAuthenticated(false);
  149. dfd.resolve(false);
  150. }
  151. } else {
  152. //if provisioning state unreachable then consider user as unauthenticated
  153. self.setAuthenticated(false);
  154. dfd.resolve(false);
  155. }
  156. });
  157. return dfd.promise();
  158. },
  159. /**
  160. * Response for <code>/clusters?fields=Clusters/provisioning_state</code>
  161. * @type {null|object}
  162. */
  163. clusterData: null,
  164. onAuthenticationSuccess: function (data) {
  165. if (App.db.getAuthenticated() === true) {
  166. this.set('clusterData', data);
  167. this.setAuthenticated(true);
  168. if (data.items.length) {
  169. this.setClusterInstalled(data);
  170. }
  171. }
  172. },
  173. /**
  174. * If authentication failed, need to check for jwt auth url
  175. * and redirect user if current location is not <code>localUserAuthUrl</code>
  176. *
  177. * @param {?object} data
  178. */
  179. onAuthenticationError: function (data) {
  180. if (data.status === 403) {
  181. try {
  182. var responseJson = JSON.parse(data.responseText);
  183. if (responseJson.jwtProviderUrl && this.get('location.lastSetURL') !== this.get('localUserAuthUrl')) {
  184. this.redirectByURL(responseJson.jwtProviderUrl + encodeURIComponent(this.getCurrentLocationUrl()));
  185. }
  186. } catch (e) {
  187. } finally {
  188. this.setAuthenticated(false);
  189. }
  190. } else if (data.status >= 500) {
  191. this.setAuthenticated(false);
  192. this.loginErrorCallback(data);
  193. }
  194. },
  195. setAuthenticated: function (authenticated) {
  196. App.db.setAuthenticated(authenticated);
  197. this.set('loggedIn', authenticated);
  198. },
  199. getLoginName: function () {
  200. return App.db.getLoginName();
  201. },
  202. setLoginName: function (loginName) {
  203. App.db.setLoginName(loginName);
  204. },
  205. /**
  206. * Set user model to local storage
  207. * @param user
  208. */
  209. setUser: function (user) {
  210. App.db.setUser(user);
  211. },
  212. /**
  213. * Get user model from local storage
  214. * @return {*}
  215. */
  216. getUser: function () {
  217. return App.db.getUser();
  218. },
  219. setUserLoggedIn: function(userName) {
  220. this.setAuthenticated(true);
  221. this.setLoginName(userName);
  222. this.setUser(App.User.find().findProperty('id', userName));
  223. App.db.set('tmp', 'redirectsCount', 0);
  224. },
  225. /**
  226. * Set `clusterInstallCompleted` property based on cluster info response.
  227. *
  228. * @param {Object} clusterObject
  229. **/
  230. setClusterInstalled: function(clusterObject) {
  231. this.set('clusterInstallCompleted', clusterObject.items[0].Clusters.provisioning_state === 'INSTALLED')
  232. },
  233. login: function () {
  234. var controller = this.get('loginController');
  235. var loginName = controller.get('loginName');
  236. controller.set('loginName', loginName);
  237. var hash = misc.utf8ToB64(loginName + ":" + controller.get('password'));
  238. var usr = '';
  239. if (App.get('testMode')) {
  240. if (loginName === "admin" && controller.get('password') === 'admin') {
  241. usr = 'admin';
  242. } else if (loginName === 'user' && controller.get('password') === 'user') {
  243. usr = 'user';
  244. }
  245. }
  246. App.ajax.send({
  247. name: 'router.login',
  248. sender: this,
  249. data: {
  250. auth: "Basic " + hash,
  251. usr: usr,
  252. loginName: encodeURIComponent(loginName)
  253. },
  254. beforeSend: 'authBeforeSend',
  255. success: 'loginSuccessCallback',
  256. error: 'loginErrorCallback'
  257. });
  258. },
  259. authBeforeSend: function(opt, xhr, data) {
  260. xhr.setRequestHeader("Authorization", data.auth);
  261. },
  262. loginSuccessCallback: function(data, opt, params) {
  263. var self = this;
  264. App.router.set('loginController.isSubmitDisabled', false);
  265. App.usersMapper.map({"items": [data]});
  266. this.setUserLoggedIn(decodeURIComponent(params.loginName));
  267. var requestData = {
  268. loginName: params.loginName,
  269. loginData: data
  270. };
  271. App.router.get('clusterController').loadAuthorizations().complete(function() {
  272. App.ajax.send({
  273. name: 'router.login.message',
  274. sender: self,
  275. data: requestData,
  276. success: 'showLoginMessage',
  277. error: 'showLoginMessage'
  278. });
  279. });
  280. },
  281. loginErrorCallback: function(request) {
  282. var controller = this.get('loginController');
  283. this.setAuthenticated(false);
  284. if (request.status > 400) {
  285. var responseMessage = request.responseText;
  286. try{
  287. responseMessage = JSON.parse(request.responseText).message;
  288. }catch(e){}
  289. }
  290. if (request.status == 403) {
  291. controller.postLogin(true, false, responseMessage);
  292. } else if (request.status == 500) {
  293. controller.postLogin(false, false, responseMessage);
  294. } else {
  295. controller.postLogin(false, false, null);
  296. }
  297. },
  298. /**
  299. * success callback of router.login.message
  300. * @param {object} data
  301. * @param {object} opt
  302. * @param {object} params
  303. */
  304. showLoginMessage: function (data, opt, params){
  305. try {
  306. var response = JSON.parse(data.Settings.content.replace(/\n/g, "\\n"))
  307. } catch (e) {
  308. this.setClusterData(data, opt, params);
  309. return false;
  310. }
  311. var
  312. text = response.text ? response.text.replace(/(\r\n|\n|\r)/gm, '<br>') : "",
  313. buttonText = response.button ? response.button : Em.I18n.t('ok'),
  314. status = response.status && response.status == "true" ? true : false,
  315. self = this;
  316. if(text && status){
  317. return App.ModalPopup.show({
  318. classNames: ['sixty-percent-width-modal'],
  319. header: Em.I18n.t('login.message.title'),
  320. bodyClass: Ember.View.extend({
  321. template: Ember.Handlebars.compile(text)
  322. }),
  323. primary:null,
  324. secondary: null,
  325. footerClass: Ember.View.extend({
  326. template: Ember.Handlebars.compile(
  327. '<div class="modal-footer">' +
  328. '<button class="btn btn-success" {{action onPrimary target="view"}}>' + buttonText + '</button>'+
  329. '</div>'
  330. )
  331. }),
  332. onPrimary: function () {
  333. self.setClusterData(data, opt, params);
  334. this.hide();
  335. },
  336. onClose: function () {
  337. self.setClusterData(data, opt, params);
  338. this.hide();
  339. },
  340. didInsertElement: function () {
  341. this.fitHeight();
  342. }
  343. });
  344. }
  345. },
  346. setClusterData: function (data, opt, params) {
  347. var
  348. self = this,
  349. requestData = {
  350. loginName: params.loginName,
  351. loginData: data
  352. };
  353. // no need to load cluster data if it's already loaded
  354. if (this.get('clusterData')) {
  355. this.loginGetClustersSuccessCallback(self.get('clusterData'), {}, requestData);
  356. }
  357. else {
  358. App.ajax.send({
  359. name: 'router.login.clusters',
  360. sender: self,
  361. data: requestData,
  362. success: 'loginGetClustersSuccessCallback'
  363. });
  364. }
  365. },
  366. /**
  367. * success callback of login request
  368. * @param {object} clustersData
  369. * @param {object} opt
  370. * @param {object} params
  371. */
  372. loginGetClustersSuccessCallback: function (clustersData, opt, params) {
  373. var privileges = params.loginData.privileges || [];
  374. var router = this;
  375. var isAdmin = privileges.mapProperty('PrivilegeInfo.permission_name').contains('AMBARI.ADMINISTRATOR');
  376. App.set('isAdmin', isAdmin);
  377. if (clustersData.items.length) {
  378. var clusterPermissions = privileges.
  379. filterProperty('PrivilegeInfo.cluster_name', clustersData.items[0].Clusters.cluster_name).
  380. mapProperty('PrivilegeInfo.permission_name');
  381. //cluster installed
  382. router.setClusterInstalled(clustersData);
  383. if (clusterPermissions.contains('CLUSTER.ADMINISTRATOR')) {
  384. App.setProperties({
  385. isAdmin: true,
  386. isOperator: true
  387. });
  388. }
  389. if (App.get('isOnlyViewUser')) {
  390. router.transitionToViews();
  391. } else {
  392. router.transitionToApp();
  393. }
  394. } else {
  395. if (App.get('isOnlyViewUser')) {
  396. router.transitionToViews();
  397. } else {
  398. router.transitionToAdminView();
  399. }
  400. }
  401. App.set('isPermissionDataLoaded', true);
  402. App.router.get('userSettingsController').dataLoading();
  403. },
  404. /**
  405. * redirect user to Admin View
  406. * @returns {$.ajax}
  407. */
  408. transitionToAdminView: function() {
  409. return App.ajax.send({
  410. name: 'ambari.service.load_server_version',
  411. sender: this,
  412. success: 'adminViewInfoSuccessCallback',
  413. error: 'adminViewInfoErrorCallback'
  414. });
  415. },
  416. /**
  417. * redirect user to application Dashboard
  418. */
  419. transitionToApp: function () {
  420. var router = this;
  421. if (!router.restorePreferedPath()) {
  422. router.getSection(function (route) {
  423. router.transitionTo(route);
  424. });
  425. }
  426. },
  427. /**
  428. * redirect user to application Views
  429. */
  430. transitionToViews: function() {
  431. App.router.get('mainViewsController').loadAmbariViews();
  432. this.transitionTo('main.views.index');
  433. },
  434. adminViewInfoSuccessCallback: function(data) {
  435. var components = Em.get(data,'components');
  436. if (Em.isArray(components)) {
  437. var mappedVersions = components.map(function(component) {
  438. if (Em.get(component, 'RootServiceComponents.component_version')) {
  439. return Em.get(component, 'RootServiceComponents.component_version');
  440. }
  441. }),
  442. sortedMappedVersions = mappedVersions.sort(),
  443. latestVersion = sortedMappedVersions[sortedMappedVersions.length-1];
  444. window.location.replace('/views/ADMIN_VIEW/' + latestVersion + '/INSTANCE/#/');
  445. }
  446. },
  447. adminViewInfoErrorCallback: function() {
  448. this.transitionToViews();
  449. },
  450. getSection: function (callback) {
  451. if (App.get('testMode')) {
  452. if (App.alwaysGoToInstaller) {
  453. callback('installer');
  454. } else {
  455. callback('main.dashboard.index');
  456. }
  457. } else {
  458. if (this.get('clusterInstallCompleted')) {
  459. App.router.get('wizardWatcherController').getUser().complete(function() {
  460. App.clusterStatus.updateFromServer(false).complete(function () {
  461. var route = 'main.dashboard.index';
  462. var clusterStatusOnServer = App.clusterStatus.get('value');
  463. if (clusterStatusOnServer) {
  464. var wizardControllerRoutes = require('data/controller_route');
  465. var wizardControllerRoute = wizardControllerRoutes.findProperty('wizardControllerName', clusterStatusOnServer.wizardControllerName);
  466. if (wizardControllerRoute && !App.router.get('wizardWatcherController').get('isNonWizardUser')) {
  467. route = wizardControllerRoute.route;
  468. }
  469. }
  470. if (wizardControllerRoute && wizardControllerRoute.wizardControllerName === 'mainAdminStackAndUpgradeController') {
  471. var clusterController = App.router.get('clusterController');
  472. clusterController.loadClusterName().done(function(){
  473. clusterController.restoreUpgradeState().done(function(){
  474. callback(route);
  475. });
  476. });
  477. } else {
  478. callback(route);
  479. }
  480. });
  481. });
  482. } else {
  483. callback('installer');
  484. }
  485. }
  486. },
  487. logOff: function (context) {
  488. var self = this;
  489. $('title').text(Em.I18n.t('app.name'));
  490. App.router.get('mainController').stopPolling();
  491. // App.db.cleanUp() must be called before router.clearAllSteps().
  492. // otherwise, this.set('installerController.currentStep, 0) would have no effect
  493. // since it's a computed property but we are not setting it as a dependent of App.db.
  494. App.db.cleanUp();
  495. App.setProperties({
  496. isAdmin: false,
  497. auth: null,
  498. isOperator: false,
  499. isPermissionDataLoaded: false
  500. });
  501. this.set('loggedIn', false);
  502. this.clearAllSteps();
  503. this.set('loginController.loginName', '');
  504. this.set('loginController.password', '');
  505. // When logOff is called by Sign Out button, context contains event object. As it is only case we should send logoff request, we are checking context below.
  506. if (!App.get('testMode') && context) {
  507. App.ajax.send({
  508. name: 'router.logoff',
  509. sender: this,
  510. success: 'logOffSuccessCallback',
  511. error: 'logOffErrorCallback'
  512. }).complete(function() {
  513. self.logoffRedirect(context);
  514. });
  515. } else {
  516. this.logoffRedirect();
  517. }
  518. },
  519. logOffSuccessCallback: function () {
  520. var applicationController = App.router.get('applicationController');
  521. applicationController.set('isPollerRunning', false);
  522. },
  523. logOffErrorCallback: function () {
  524. },
  525. /**
  526. * Redirect function on sign off request.
  527. *
  528. * @param {$.Event} [context=undefined] - triggered event context
  529. */
  530. logoffRedirect: function(context) {
  531. if (App.router.get('clusterController.isLoaded')) {
  532. window.location.reload();
  533. } else {
  534. this.transitionTo('login', context);
  535. }
  536. },
  537. /**
  538. * save prefered path
  539. * @param {string} path
  540. * @param {string} key
  541. */
  542. savePreferedPath: function(path, key) {
  543. if (key) {
  544. if (path.contains(key)) {
  545. this.set('preferedPath', path.slice(path.indexOf(key) + key.length));
  546. }
  547. } else {
  548. this.set('preferedPath', path);
  549. }
  550. },
  551. /**
  552. * If path exist route to it, otherwise return false
  553. * @returns {boolean}
  554. */
  555. restorePreferedPath: function() {
  556. var preferredPath = this.get('preferedPath');
  557. var isRestored = false;
  558. if (preferredPath) {
  559. // If the preferred path is relative, allow a redirect to it.
  560. // If the path is not relative, silently ignore it - if the path is an absolute URL, the user
  561. // may be routed to a different server where the possibility exists for a phishing attack.
  562. if ((preferredPath.startsWith('/') || preferredPath.startsWith('#')) && !preferredPath.contains('#/login')) {
  563. window.location = preferredPath;
  564. isRestored = true;
  565. }
  566. // Unset preferedPath
  567. this.set('preferedPath', null);
  568. }
  569. return isRestored;
  570. },
  571. /**
  572. * initialize isAdmin if user is administrator
  573. */
  574. initAdmin: function(){
  575. if (App.db) {
  576. var user = App.db.getUser();
  577. if (user) {
  578. if (user.admin) {
  579. App.set('isAdmin', true);
  580. }
  581. if (user.operator) {
  582. App.set('isOperator', true);
  583. }
  584. App.set('isPermissionDataLoaded', true);
  585. }
  586. }
  587. },
  588. /**
  589. * initialize Auth for user
  590. */
  591. initAuth: function(){
  592. if (App.db) {
  593. var auth = App.db.getAuth();
  594. if(auth)
  595. App.set('auth', auth);
  596. }
  597. },
  598. /**
  599. * Increment redirect count if <code>redirected</code> parameter passed.
  600. */
  601. handleUIRedirect: function() {
  602. if (/(\?|&)redirected=/.test(location.hash)) {
  603. var redirectsCount = App.db.get('tmp', 'redirectsCount') || 0;
  604. App.db.set('tmp', 'redirectsCount', ++redirectsCount);
  605. }
  606. },
  607. /**
  608. * <code>window.location</code> setter. Will add query param which determines that we redirect user
  609. * @param {string} url - url to navigate
  610. */
  611. redirectByURL: function(url) {
  612. var suffix = "?redirected=true";
  613. var redirectsCount = App.db.get('tmp', 'redirectsCount') || 0;
  614. if (redirectsCount > this.get('redirectsLimitCount')) {
  615. this.showRedirectIssue();
  616. return;
  617. }
  618. // skip adding redirected parameter if added
  619. if (/(\?|&)redirected=/.test(location.hash)) {
  620. this.setLocationUrl(url);
  621. return;
  622. }
  623. // detect if query params were assigned and replace "?" with "&" for suffix param
  624. if (/\?\w+=/.test(location.hash)) {
  625. suffix = suffix.replace('?', '&');
  626. }
  627. this.setLocationUrl(url + suffix);
  628. },
  629. /**
  630. * Convenient method to set <code>window.location</code>.
  631. * Useful for faking url manipulation in tests.
  632. *
  633. * @param {string} url
  634. */
  635. setLocationUrl: function(url) {
  636. window.location = url;
  637. },
  638. /**
  639. * Convenient method to get current <code>window.location</code>.
  640. * Useful for faking url manipulation in tests.
  641. */
  642. getCurrentLocationUrl: function() {
  643. return window.location.href;
  644. },
  645. /**
  646. * Inform user about redirect issue in modal popup.
  647. *
  648. * @returns {App.ModalPopup}
  649. */
  650. showRedirectIssue: function() {
  651. var bodyMessage = Em.I18n.t('app.redirectIssuePopup.body').format(location.origin + '/#' + this.get('localUserAuthUrl'));
  652. var popupHeader = Em.I18n.t('app.redirectIssuePopup.header');
  653. var popup = App.showAlertPopup(popupHeader, bodyMessage);
  654. popup.set('encodeBody', false);
  655. return popup;
  656. },
  657. root: Em.Route.extend({
  658. index: Em.Route.extend({
  659. route: '/',
  660. redirectsTo: 'login'
  661. }),
  662. enter: function(router){
  663. router.initAdmin();
  664. router.initAuth();
  665. router.handleUIRedirect();
  666. },
  667. login: Em.Route.extend({
  668. route: '/login:suffix',
  669. /**
  670. * If the user is already logged in, redirect to where the user was previously
  671. */
  672. enter: function (router, context) {
  673. if ($.mocho) {
  674. return;
  675. }
  676. var location = router.location.location.hash;
  677. router.getAuthenticated().done(function (loggedIn) {
  678. if (loggedIn) {
  679. Ember.run.next(function () {
  680. router.getSection(function (route) {
  681. router.transitionTo(route, context);
  682. });
  683. });
  684. } else {
  685. //key to parse URI for prefered path to route
  686. router.savePreferedPath(location, '?targetURI=');
  687. }
  688. });
  689. },
  690. connectOutlets: function (router, context) {
  691. $('title').text(Em.I18n.t('app.name'));
  692. router.get('applicationController').connectOutlet('login');
  693. },
  694. serialize: function(router, context) {
  695. // check for login/local hash
  696. var location = router.get('location.location.hash');
  697. return {
  698. suffix: location === '#' + router.get('localUserAuthUrl') ? '/local' : ''
  699. };
  700. }
  701. }),
  702. installer: require('routes/installer'),
  703. main: require('routes/main'),
  704. adminView: Em.Route.extend({
  705. route: '/adminView',
  706. enter: function (router) {
  707. if (!router.get('loggedIn') || !App.isAuthorized('CLUSTER.UPGRADE_DOWNGRADE_STACK')) {
  708. Em.run.next(function () {
  709. router.transitionTo('login');
  710. });
  711. } else {
  712. App.ajax.send({
  713. name: 'ambari.service.load_server_version',
  714. sender: router,
  715. success: 'adminViewInfoSuccessCallback'
  716. });
  717. }
  718. }
  719. }),
  720. experimental: Em.Route.extend({
  721. route: '/experimental',
  722. enter: function (router, context) {
  723. if (App.isAuthorized('CLUSTER.UPGRADE_DOWNGRADE_STACK')) {
  724. Em.run.next(function () {
  725. if (router.get('clusterInstallCompleted')) {
  726. router.transitionTo("main.dashboard.widgets");
  727. } else {
  728. router.route("installer");
  729. }
  730. });
  731. } else if (!App.isAuthorized('CLUSTER.UPGRADE_DOWNGRADE_STACK')) {
  732. Em.run.next(function () {
  733. router.transitionTo("main.views.index");
  734. });
  735. }
  736. },
  737. connectOutlets: function (router, context) {
  738. if (App.isAuthorized('CLUSTER.UPGRADE_DOWNGRADE_STACK')) {
  739. App.router.get('experimentalController').loadSupports().complete(function () {
  740. $('title').text(Em.I18n.t('app.name.subtitle.experimental'));
  741. router.get('applicationController').connectOutlet('experimental');
  742. });
  743. }
  744. }
  745. }),
  746. logoff: function (router, context) {
  747. router.logOff(context);
  748. }
  749. })
  750. });