router.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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. onPrimary: function() {
  332. this.get('parentView').onPrimary();
  333. }
  334. }),
  335. onPrimary: function () {
  336. self.setClusterData(data, opt, params);
  337. this.hide();
  338. },
  339. onClose: function () {
  340. self.setClusterData(data, opt, params);
  341. this.hide();
  342. },
  343. didInsertElement: function () {
  344. this.fitHeight();
  345. }
  346. });
  347. }
  348. },
  349. setClusterData: function (data, opt, params) {
  350. var
  351. self = this,
  352. requestData = {
  353. loginName: params.loginName,
  354. loginData: data
  355. };
  356. // no need to load cluster data if it's already loaded
  357. if (this.get('clusterData')) {
  358. this.loginGetClustersSuccessCallback(self.get('clusterData'), {}, requestData);
  359. }
  360. else {
  361. App.ajax.send({
  362. name: 'router.login.clusters',
  363. sender: self,
  364. data: requestData,
  365. success: 'loginGetClustersSuccessCallback'
  366. });
  367. }
  368. },
  369. /**
  370. * success callback of login request
  371. * @param {object} clustersData
  372. * @param {object} opt
  373. * @param {object} params
  374. */
  375. loginGetClustersSuccessCallback: function (clustersData, opt, params) {
  376. var privileges = params.loginData.privileges || [];
  377. var router = this;
  378. var isAdmin = privileges.mapProperty('PrivilegeInfo.permission_name').contains('AMBARI.ADMINISTRATOR');
  379. App.set('isAdmin', isAdmin);
  380. if (clustersData.items.length) {
  381. var clusterPermissions = privileges.
  382. filterProperty('PrivilegeInfo.cluster_name', clustersData.items[0].Clusters.cluster_name).
  383. mapProperty('PrivilegeInfo.permission_name');
  384. //cluster installed
  385. router.setClusterInstalled(clustersData);
  386. if (clusterPermissions.contains('CLUSTER.ADMINISTRATOR')) {
  387. App.setProperties({
  388. isAdmin: true,
  389. isOperator: true
  390. });
  391. }
  392. if (App.get('isOnlyViewUser')) {
  393. router.transitionToViews();
  394. } else {
  395. router.transitionToApp();
  396. }
  397. } else {
  398. if (App.get('isOnlyViewUser')) {
  399. router.transitionToViews();
  400. } else {
  401. router.transitionToAdminView();
  402. }
  403. }
  404. App.set('isPermissionDataLoaded', true);
  405. App.router.get('userSettingsController').dataLoading();
  406. },
  407. /**
  408. * redirect user to Admin View
  409. * @returns {$.ajax}
  410. */
  411. transitionToAdminView: function() {
  412. return App.ajax.send({
  413. name: 'ambari.service.load_server_version',
  414. sender: this,
  415. success: 'adminViewInfoSuccessCallback',
  416. error: 'adminViewInfoErrorCallback'
  417. });
  418. },
  419. /**
  420. * redirect user to application Dashboard
  421. */
  422. transitionToApp: function () {
  423. var router = this;
  424. if (!router.restorePreferedPath()) {
  425. router.getSection(function (route) {
  426. router.transitionTo(route);
  427. });
  428. }
  429. },
  430. /**
  431. * redirect user to application Views
  432. */
  433. transitionToViews: function() {
  434. App.router.get('mainViewsController').loadAmbariViews();
  435. this.transitionTo('main.views.index');
  436. },
  437. adminViewInfoSuccessCallback: function(data) {
  438. var components = Em.get(data,'components');
  439. if (Em.isArray(components)) {
  440. var mappedVersions = components.map(function(component) {
  441. if (Em.get(component, 'RootServiceComponents.component_version')) {
  442. return Em.get(component, 'RootServiceComponents.component_version');
  443. }
  444. }),
  445. sortedMappedVersions = mappedVersions.sort(),
  446. latestVersion = sortedMappedVersions[sortedMappedVersions.length-1];
  447. window.location.replace('/views/ADMIN_VIEW/' + latestVersion + '/INSTANCE/#/');
  448. }
  449. },
  450. adminViewInfoErrorCallback: function() {
  451. this.transitionToViews();
  452. },
  453. getSection: function (callback) {
  454. if (App.get('testMode')) {
  455. if (App.alwaysGoToInstaller) {
  456. callback('installer');
  457. } else {
  458. callback('main.dashboard.index');
  459. }
  460. } else {
  461. if (this.get('clusterInstallCompleted')) {
  462. App.router.get('wizardWatcherController').getUser().complete(function() {
  463. App.clusterStatus.updateFromServer(false).complete(function () {
  464. var route = 'main.dashboard.index';
  465. var clusterStatusOnServer = App.clusterStatus.get('value');
  466. if (clusterStatusOnServer) {
  467. var wizardControllerRoutes = require('data/controller_route');
  468. var wizardControllerRoute = wizardControllerRoutes.findProperty('wizardControllerName', clusterStatusOnServer.wizardControllerName);
  469. if (wizardControllerRoute && !App.router.get('wizardWatcherController').get('isNonWizardUser')) {
  470. route = wizardControllerRoute.route;
  471. }
  472. }
  473. if (wizardControllerRoute && wizardControllerRoute.wizardControllerName === 'mainAdminStackAndUpgradeController') {
  474. var clusterController = App.router.get('clusterController');
  475. clusterController.loadClusterName().done(function(){
  476. clusterController.restoreUpgradeState().done(function(){
  477. callback(route);
  478. });
  479. });
  480. } else {
  481. callback(route);
  482. }
  483. });
  484. });
  485. } else {
  486. callback('installer');
  487. }
  488. }
  489. },
  490. logOff: function (context) {
  491. var self = this;
  492. $('title').text(Em.I18n.t('app.name'));
  493. App.router.get('mainController').stopPolling();
  494. // App.db.cleanUp() must be called before router.clearAllSteps().
  495. // otherwise, this.set('installerController.currentStep, 0) would have no effect
  496. // since it's a computed property but we are not setting it as a dependent of App.db.
  497. App.db.cleanUp();
  498. App.setProperties({
  499. isAdmin: false,
  500. auth: null,
  501. isOperator: false,
  502. isPermissionDataLoaded: false
  503. });
  504. this.set('loggedIn', false);
  505. this.clearAllSteps();
  506. this.set('loginController.loginName', '');
  507. this.set('loginController.password', '');
  508. // 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.
  509. if (!App.get('testMode') && context) {
  510. App.ajax.send({
  511. name: 'router.logoff',
  512. sender: this,
  513. success: 'logOffSuccessCallback',
  514. error: 'logOffErrorCallback'
  515. }).complete(function() {
  516. self.logoffRedirect(context);
  517. });
  518. } else {
  519. this.logoffRedirect();
  520. }
  521. },
  522. logOffSuccessCallback: function () {
  523. var applicationController = App.router.get('applicationController');
  524. applicationController.set('isPollerRunning', false);
  525. },
  526. logOffErrorCallback: function () {
  527. },
  528. /**
  529. * Redirect function on sign off request.
  530. *
  531. * @param {$.Event} [context=undefined] - triggered event context
  532. */
  533. logoffRedirect: function(context) {
  534. if (App.router.get('clusterController.isLoaded')) {
  535. window.location.reload();
  536. } else {
  537. this.transitionTo('login', context);
  538. }
  539. },
  540. /**
  541. * save prefered path
  542. * @param {string} path
  543. * @param {string} key
  544. */
  545. savePreferedPath: function(path, key) {
  546. if (key) {
  547. if (path.contains(key)) {
  548. this.set('preferedPath', path.slice(path.indexOf(key) + key.length));
  549. }
  550. } else {
  551. this.set('preferedPath', path);
  552. }
  553. },
  554. /**
  555. * If path exist route to it, otherwise return false
  556. * @returns {boolean}
  557. */
  558. restorePreferedPath: function() {
  559. var preferredPath = this.get('preferedPath');
  560. var isRestored = false;
  561. if (preferredPath) {
  562. // If the preferred path is relative, allow a redirect to it.
  563. // If the path is not relative, silently ignore it - if the path is an absolute URL, the user
  564. // may be routed to a different server where the possibility exists for a phishing attack.
  565. if ((preferredPath.startsWith('/') || preferredPath.startsWith('#')) && !preferredPath.contains('#/login')) {
  566. window.location = preferredPath;
  567. isRestored = true;
  568. }
  569. // Unset preferedPath
  570. this.set('preferedPath', null);
  571. }
  572. return isRestored;
  573. },
  574. /**
  575. * initialize isAdmin if user is administrator
  576. */
  577. initAdmin: function(){
  578. if (App.db) {
  579. var user = App.db.getUser();
  580. if (user) {
  581. if (user.admin) {
  582. App.set('isAdmin', true);
  583. }
  584. if (user.operator) {
  585. App.set('isOperator', true);
  586. }
  587. App.set('isPermissionDataLoaded', true);
  588. }
  589. }
  590. },
  591. /**
  592. * initialize Auth for user
  593. */
  594. initAuth: function(){
  595. if (App.db) {
  596. var auth = App.db.getAuth();
  597. if(auth)
  598. App.set('auth', auth);
  599. }
  600. },
  601. /**
  602. * Increment redirect count if <code>redirected</code> parameter passed.
  603. */
  604. handleUIRedirect: function() {
  605. if (/(\?|&)redirected=/.test(location.hash)) {
  606. var redirectsCount = App.db.get('tmp', 'redirectsCount') || 0;
  607. App.db.set('tmp', 'redirectsCount', ++redirectsCount);
  608. }
  609. },
  610. /**
  611. * <code>window.location</code> setter. Will add query param which determines that we redirect user
  612. * @param {string} url - url to navigate
  613. */
  614. redirectByURL: function(url) {
  615. var suffix = "?redirected=true";
  616. var redirectsCount = App.db.get('tmp', 'redirectsCount') || 0;
  617. if (redirectsCount > this.get('redirectsLimitCount')) {
  618. this.showRedirectIssue();
  619. return;
  620. }
  621. // skip adding redirected parameter if added
  622. if (/(\?|&)redirected=/.test(location.hash)) {
  623. this.setLocationUrl(url);
  624. return;
  625. }
  626. // detect if query params were assigned and replace "?" with "&" for suffix param
  627. if (/\?\w+=/.test(location.hash)) {
  628. suffix = suffix.replace('?', '&');
  629. }
  630. this.setLocationUrl(url + suffix);
  631. },
  632. /**
  633. * Convenient method to set <code>window.location</code>.
  634. * Useful for faking url manipulation in tests.
  635. *
  636. * @param {string} url
  637. */
  638. setLocationUrl: function(url) {
  639. window.location = url;
  640. },
  641. /**
  642. * Convenient method to get current <code>window.location</code>.
  643. * Useful for faking url manipulation in tests.
  644. */
  645. getCurrentLocationUrl: function() {
  646. return window.location.href;
  647. },
  648. /**
  649. * Inform user about redirect issue in modal popup.
  650. *
  651. * @returns {App.ModalPopup}
  652. */
  653. showRedirectIssue: function() {
  654. var bodyMessage = Em.I18n.t('app.redirectIssuePopup.body').format(location.origin + '/#' + this.get('localUserAuthUrl'));
  655. var popupHeader = Em.I18n.t('app.redirectIssuePopup.header');
  656. var popup = App.showAlertPopup(popupHeader, bodyMessage);
  657. popup.set('encodeBody', false);
  658. return popup;
  659. },
  660. root: Em.Route.extend({
  661. index: Em.Route.extend({
  662. route: '/',
  663. redirectsTo: 'login'
  664. }),
  665. enter: function(router){
  666. router.initAdmin();
  667. router.initAuth();
  668. router.handleUIRedirect();
  669. },
  670. login: Em.Route.extend({
  671. route: '/login:suffix',
  672. /**
  673. * If the user is already logged in, redirect to where the user was previously
  674. */
  675. enter: function (router, context) {
  676. if ($.mocho) {
  677. return;
  678. }
  679. var location = router.location.location.hash;
  680. router.getAuthenticated().done(function (loggedIn) {
  681. if (loggedIn) {
  682. Ember.run.next(function () {
  683. router.getSection(function (route) {
  684. router.transitionTo(route, context);
  685. });
  686. });
  687. } else {
  688. //key to parse URI for prefered path to route
  689. router.savePreferedPath(location, '?targetURI=');
  690. }
  691. });
  692. },
  693. connectOutlets: function (router, context) {
  694. $('title').text(Em.I18n.t('app.name'));
  695. router.get('applicationController').connectOutlet('login');
  696. },
  697. serialize: function(router, context) {
  698. // check for login/local hash
  699. var location = router.get('location.location.hash');
  700. return {
  701. suffix: location === '#' + router.get('localUserAuthUrl') ? '/local' : ''
  702. };
  703. }
  704. }),
  705. installer: require('routes/installer'),
  706. main: require('routes/main'),
  707. adminView: Em.Route.extend({
  708. route: '/adminView',
  709. enter: function (router) {
  710. if (!router.get('loggedIn') || !App.isAuthorized('CLUSTER.UPGRADE_DOWNGRADE_STACK')) {
  711. Em.run.next(function () {
  712. router.transitionTo('login');
  713. });
  714. } else {
  715. App.ajax.send({
  716. name: 'ambari.service.load_server_version',
  717. sender: router,
  718. success: 'adminViewInfoSuccessCallback'
  719. });
  720. }
  721. }
  722. }),
  723. experimental: Em.Route.extend({
  724. route: '/experimental',
  725. enter: function (router, context) {
  726. if (App.isAuthorized('CLUSTER.UPGRADE_DOWNGRADE_STACK')) {
  727. Em.run.next(function () {
  728. if (router.get('clusterInstallCompleted')) {
  729. router.transitionTo("main.dashboard.widgets");
  730. } else {
  731. router.route("installer");
  732. }
  733. });
  734. } else if (!App.isAuthorized('CLUSTER.UPGRADE_DOWNGRADE_STACK')) {
  735. Em.run.next(function () {
  736. router.transitionTo("main.views.index");
  737. });
  738. }
  739. },
  740. connectOutlets: function (router, context) {
  741. if (App.isAuthorized('CLUSTER.UPGRADE_DOWNGRADE_STACK')) {
  742. App.router.get('experimentalController').loadSupports().complete(function () {
  743. $('title').text(Em.I18n.t('app.name.subtitle.experimental'));
  744. router.get('applicationController').connectOutlet('experimental');
  745. });
  746. }
  747. }
  748. }),
  749. logoff: function (router, context) {
  750. router.logOff(context);
  751. }
  752. })
  753. });