router.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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. displayLoginName: Em.computed.truncate('loginName', 10, 10),
  120. getAuthenticated: function () {
  121. var dfd = $.Deferred();
  122. var self = this;
  123. var auth = App.db.getAuthenticated();
  124. App.ajax.send({
  125. name: 'router.login.clusters',
  126. sender: this,
  127. success: 'onAuthenticationSuccess',
  128. error: 'onAuthenticationError'
  129. }).complete(function (xhr) {
  130. if (xhr.isResolved()) {
  131. // if server knows the user and user authenticated by UI
  132. if (auth) {
  133. dfd.resolve(self.get('loggedIn'));
  134. // if server knows the user but UI don't, check the response header
  135. // and try to authorize
  136. } else if (xhr.getResponseHeader('User')) {
  137. var user = xhr.getResponseHeader('User');
  138. App.ajax.send({
  139. name: 'router.login',
  140. sender: self,
  141. data: {
  142. usr: user,
  143. loginName: encodeURIComponent(user)
  144. },
  145. success: 'loginSuccessCallback',
  146. error: 'loginErrorCallback'
  147. }).then(function() {
  148. dfd.resolve(true);
  149. });
  150. } else {
  151. self.setAuthenticated(false);
  152. dfd.resolve(false);
  153. }
  154. } else {
  155. //if provisioning state unreachable then consider user as unauthenticated
  156. self.setAuthenticated(false);
  157. dfd.resolve(false);
  158. }
  159. });
  160. return dfd.promise();
  161. },
  162. /**
  163. * Response for <code>/clusters?fields=Clusters/provisioning_state</code>
  164. * @type {null|object}
  165. */
  166. clusterData: null,
  167. onAuthenticationSuccess: function (data) {
  168. if (App.db.getAuthenticated() === true) {
  169. this.set('clusterData', data);
  170. this.setAuthenticated(true);
  171. if (data.items.length) {
  172. this.setClusterInstalled(data);
  173. }
  174. }
  175. },
  176. /**
  177. * If authentication failed, need to check for jwt auth url
  178. * and redirect user if current location is not <code>localUserAuthUrl</code>
  179. *
  180. * @param {?object} data
  181. */
  182. onAuthenticationError: function (data) {
  183. if (data.status === 403) {
  184. try {
  185. var responseJson = JSON.parse(data.responseText);
  186. if (responseJson.jwtProviderUrl && this.get('location.lastSetURL') !== this.get('localUserAuthUrl')) {
  187. this.redirectByURL(responseJson.jwtProviderUrl + encodeURIComponent(this.getCurrentLocationUrl()));
  188. }
  189. } catch (e) {
  190. } finally {
  191. this.setAuthenticated(false);
  192. }
  193. } else if (data.status >= 500) {
  194. this.setAuthenticated(false);
  195. this.loginErrorCallback(data);
  196. }
  197. },
  198. setAuthenticated: function (authenticated) {
  199. App.db.setAuthenticated(authenticated);
  200. this.set('loggedIn', authenticated);
  201. },
  202. getLoginName: function () {
  203. return App.db.getLoginName();
  204. },
  205. setLoginName: function (loginName) {
  206. App.db.setLoginName(loginName);
  207. },
  208. /**
  209. * Set user model to local storage
  210. * @param user
  211. */
  212. setUser: function (user) {
  213. App.db.setUser(user);
  214. },
  215. /**
  216. * Get user model from local storage
  217. * @return {*}
  218. */
  219. getUser: function () {
  220. return App.db.getUser();
  221. },
  222. setUserLoggedIn: function(userName) {
  223. this.setAuthenticated(true);
  224. this.setLoginName(userName);
  225. this.setUser(App.User.find().findProperty('id', userName));
  226. App.db.set('tmp', 'redirectsCount', 0);
  227. },
  228. /**
  229. * Set `clusterInstallCompleted` property based on cluster info response.
  230. *
  231. * @param {Object} clusterObject
  232. **/
  233. setClusterInstalled: function(clusterObject) {
  234. this.set('clusterInstallCompleted', clusterObject.items[0].Clusters.provisioning_state === 'INSTALLED')
  235. },
  236. login: function () {
  237. var controller = this.get('loginController');
  238. var loginName = controller.get('loginName');
  239. controller.set('loginName', loginName);
  240. var hash = misc.utf8ToB64(loginName + ":" + controller.get('password'));
  241. var usr = '';
  242. if (App.get('testMode')) {
  243. if (loginName === "admin" && controller.get('password') === 'admin') {
  244. usr = 'admin';
  245. } else if (loginName === 'user' && controller.get('password') === 'user') {
  246. usr = 'user';
  247. }
  248. }
  249. App.ajax.send({
  250. name: 'router.login',
  251. sender: this,
  252. data: {
  253. auth: "Basic " + hash,
  254. usr: usr,
  255. loginName: encodeURIComponent(loginName)
  256. },
  257. beforeSend: 'authBeforeSend',
  258. success: 'loginSuccessCallback',
  259. error: 'loginErrorCallback'
  260. });
  261. },
  262. authBeforeSend: function(opt, xhr, data) {
  263. xhr.setRequestHeader("Authorization", data.auth);
  264. },
  265. loginSuccessCallback: function(data, opt, params) {
  266. var self = this;
  267. App.router.set('loginController.isSubmitDisabled', false);
  268. App.usersMapper.map({"items": [data]});
  269. this.setUserLoggedIn(data.Users.user_name);
  270. var requestData = {
  271. loginName: data.Users.user_name,
  272. loginData: data
  273. };
  274. App.router.get('clusterController').loadAuthorizations().complete(function() {
  275. App.ajax.send({
  276. name: 'router.login.message',
  277. sender: self,
  278. data: requestData,
  279. success: 'showLoginMessageSuccessCallback',
  280. error: 'showLoginMessageErrorCallback'
  281. });
  282. });
  283. },
  284. loginErrorCallback: function(request) {
  285. var controller = this.get('loginController');
  286. this.setAuthenticated(false);
  287. if (request.status > 400) {
  288. var responseMessage = request.responseText;
  289. try{
  290. responseMessage = JSON.parse(request.responseText).message;
  291. }catch(e){}
  292. }
  293. if (request.status == 403) {
  294. controller.postLogin(true, false, responseMessage);
  295. } else if (request.status == 500) {
  296. controller.postLogin(false, false, responseMessage);
  297. } else {
  298. controller.postLogin(false, false, null);
  299. }
  300. },
  301. /**
  302. * success callback of router.login.message
  303. * @param {object} data
  304. * @param {object} opt
  305. * @param {object} params
  306. */
  307. showLoginMessageSuccessCallback: function (data, opt, params) {
  308. try {
  309. var response = JSON.parse(data.Settings.content.replace(/\n/g, "\\n"))
  310. } catch (e) {
  311. this.setClusterData(data, opt, params);
  312. return false;
  313. }
  314. var
  315. text = response.text ? response.text.replace(/(\r\n|\n|\r)/gm, '<br>') : "",
  316. buttonText = response.button ? response.button : Em.I18n.t('ok'),
  317. status = response.status && response.status == "true" ? true : false,
  318. self = this;
  319. if(text && status){
  320. return App.ModalPopup.show({
  321. classNames: ['sixty-percent-width-modal'],
  322. header: Em.I18n.t('login.message.title'),
  323. bodyClass: Ember.View.extend({
  324. template: Ember.Handlebars.compile(text)
  325. }),
  326. primary:null,
  327. secondary: null,
  328. footerClass: Ember.View.extend({
  329. template: Ember.Handlebars.compile(
  330. '<div class="modal-footer">' +
  331. '<button class="btn btn-success" {{action onPrimary target="view"}}>' + buttonText + '</button>'+
  332. '</div>'
  333. ),
  334. onPrimary: function() {
  335. this.get('parentView').onPrimary();
  336. }
  337. }),
  338. onPrimary: function () {
  339. self.setClusterData(data, opt, params);
  340. this.hide();
  341. },
  342. onClose: function () {
  343. self.setClusterData(data, opt, params);
  344. this.hide();
  345. }
  346. });
  347. }else{
  348. this.setClusterData(data, opt, params);
  349. return false;
  350. }
  351. },
  352. /**
  353. * error callback of router.login.message
  354. * @param {object} request
  355. * @param {string} ajaxOptions
  356. * @param {string} error
  357. * @param {object} opt
  358. * @param {object} params
  359. */
  360. showLoginMessageErrorCallback: function (request, ajaxOptions, error, opt, params) {
  361. this.showLoginMessageSuccessCallback(null, opt, params);
  362. },
  363. setClusterData: function (data, opt, params) {
  364. var
  365. self = this,
  366. requestData = {
  367. loginName: params.loginName,
  368. loginData: params.loginData
  369. };
  370. // no need to load cluster data if it's already loaded
  371. if (this.get('clusterData')) {
  372. this.loginGetClustersSuccessCallback(self.get('clusterData'), {}, requestData);
  373. }
  374. else {
  375. App.ajax.send({
  376. name: 'router.login.clusters',
  377. sender: self,
  378. data: requestData,
  379. success: 'loginGetClustersSuccessCallback'
  380. });
  381. }
  382. },
  383. /**
  384. * success callback of login request
  385. * @param {object} clustersData
  386. * @param {object} opt
  387. * @param {object} params
  388. */
  389. loginGetClustersSuccessCallback: function (clustersData, opt, params) {
  390. var privileges = params.loginData.privileges || [];
  391. var router = this;
  392. var isAdmin = privileges.mapProperty('PrivilegeInfo.permission_name').contains('AMBARI.ADMINISTRATOR');
  393. App.set('isAdmin', isAdmin);
  394. if (clustersData.items.length) {
  395. var clusterPermissions = privileges.
  396. filterProperty('PrivilegeInfo.cluster_name', clustersData.items[0].Clusters.cluster_name).
  397. mapProperty('PrivilegeInfo.permission_name');
  398. //cluster installed
  399. router.setClusterInstalled(clustersData);
  400. if (clusterPermissions.contains('CLUSTER.ADMINISTRATOR')) {
  401. App.setProperties({
  402. isAdmin: true,
  403. isOperator: true,
  404. isClusterUser: false
  405. });
  406. }
  407. if (App.get('isOnlyViewUser')) {
  408. router.transitionToViews();
  409. } else {
  410. router.transitionToApp();
  411. }
  412. } else {
  413. if (App.get('isOnlyViewUser')) {
  414. router.transitionToViews();
  415. } else {
  416. router.transitionToAdminView();
  417. }
  418. }
  419. App.set('isPermissionDataLoaded', true);
  420. App.router.get('userSettingsController').dataLoading();
  421. },
  422. /**
  423. * redirect user to Admin View
  424. * @returns {$.ajax}
  425. */
  426. transitionToAdminView: function() {
  427. return App.ajax.send({
  428. name: 'ambari.service.load_server_version',
  429. sender: this,
  430. success: 'adminViewInfoSuccessCallback',
  431. error: 'adminViewInfoErrorCallback'
  432. });
  433. },
  434. /**
  435. * redirect user to application Dashboard
  436. */
  437. transitionToApp: function () {
  438. var router = this;
  439. if (!router.restorePreferedPath()) {
  440. router.getSection(function (route) {
  441. router.transitionTo(route);
  442. });
  443. }
  444. },
  445. /**
  446. * redirect user to application Views
  447. */
  448. transitionToViews: function() {
  449. App.router.get('mainViewsController').loadAmbariViews();
  450. this.transitionTo('main.views.index');
  451. },
  452. adminViewInfoSuccessCallback: function(data) {
  453. var components = Em.get(data,'components');
  454. if (Em.isArray(components)) {
  455. var mappedVersions = components.map(function(component) {
  456. if (Em.get(component, 'RootServiceComponents.component_version')) {
  457. return Em.get(component, 'RootServiceComponents.component_version');
  458. }
  459. }),
  460. sortedMappedVersions = mappedVersions.sort(),
  461. latestVersion = sortedMappedVersions[sortedMappedVersions.length-1];
  462. window.location.replace('/views/ADMIN_VIEW/' + latestVersion + '/INSTANCE/#/');
  463. }
  464. },
  465. adminViewInfoErrorCallback: function() {
  466. this.transitionToViews();
  467. },
  468. getSection: function (callback) {
  469. if (App.get('testMode')) {
  470. if (App.alwaysGoToInstaller) {
  471. callback('installer');
  472. } else {
  473. callback('main.dashboard.index');
  474. }
  475. } else {
  476. if (this.get('clusterInstallCompleted')) {
  477. App.router.get('wizardWatcherController').getUser().complete(function() {
  478. App.clusterStatus.updateFromServer(false).complete(function () {
  479. var route = 'main.dashboard.index';
  480. var clusterStatusOnServer = App.clusterStatus.get('value');
  481. if (clusterStatusOnServer) {
  482. var wizardControllerRoutes = require('data/controller_route');
  483. var wizardControllerRoute = wizardControllerRoutes.findProperty('wizardControllerName', clusterStatusOnServer.wizardControllerName);
  484. if (wizardControllerRoute && !App.router.get('wizardWatcherController').get('isNonWizardUser')) {
  485. route = wizardControllerRoute.route;
  486. }
  487. }
  488. if (wizardControllerRoute && wizardControllerRoute.wizardControllerName === 'mainAdminStackAndUpgradeController') {
  489. var clusterController = App.router.get('clusterController');
  490. clusterController.loadClusterName().done(function(){
  491. clusterController.restoreUpgradeState().done(function(){
  492. callback(route);
  493. });
  494. });
  495. } else {
  496. callback(route);
  497. }
  498. });
  499. });
  500. } else {
  501. callback('installer');
  502. }
  503. }
  504. },
  505. logOff: function (context) {
  506. var self = this;
  507. $('title').text(Em.I18n.t('app.name'));
  508. App.router.get('mainController').stopPolling();
  509. // App.db.cleanUp() must be called before router.clearAllSteps().
  510. // otherwise, this.set('installerController.currentStep, 0) would have no effect
  511. // since it's a computed property but we are not setting it as a dependent of App.db.
  512. App.db.cleanUp();
  513. App.setProperties({
  514. isAdmin: false,
  515. auth: null,
  516. isOperator: false,
  517. isClusterUser: false,
  518. isPermissionDataLoaded: false
  519. });
  520. this.set('loggedIn', false);
  521. this.clearAllSteps();
  522. this.set('loginController.loginName', '');
  523. this.set('loginController.password', '');
  524. // 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.
  525. if (!App.get('testMode') && context) {
  526. App.ajax.send({
  527. name: 'router.logoff',
  528. sender: this,
  529. success: 'logOffSuccessCallback',
  530. error: 'logOffErrorCallback'
  531. }).complete(function() {
  532. self.logoffRedirect(context);
  533. });
  534. } else {
  535. this.logoffRedirect();
  536. }
  537. },
  538. logOffSuccessCallback: function () {
  539. var applicationController = App.router.get('applicationController');
  540. applicationController.set('isPollerRunning', false);
  541. },
  542. logOffErrorCallback: function () {
  543. },
  544. /**
  545. * Redirect function on sign off request.
  546. *
  547. * @param {$.Event} [context=undefined] - triggered event context
  548. */
  549. logoffRedirect: function(context) {
  550. if (App.router.get('clusterController.isLoaded')) {
  551. window.location.reload();
  552. } else {
  553. this.transitionTo('login', context);
  554. }
  555. },
  556. /**
  557. * save prefered path
  558. * @param {string} path
  559. * @param {string} key
  560. */
  561. savePreferedPath: function(path, key) {
  562. if (key) {
  563. if (path.contains(key)) {
  564. this.set('preferedPath', path.slice(path.indexOf(key) + key.length));
  565. }
  566. } else {
  567. this.set('preferedPath', path);
  568. }
  569. },
  570. /**
  571. * If path exist route to it, otherwise return false
  572. * @returns {boolean}
  573. */
  574. restorePreferedPath: function() {
  575. var preferredPath = this.get('preferedPath');
  576. var isRestored = false;
  577. if (preferredPath) {
  578. // If the preferred path is relative, allow a redirect to it.
  579. // If the path is not relative, silently ignore it - if the path is an absolute URL, the user
  580. // may be routed to a different server where the possibility exists for a phishing attack.
  581. if ((preferredPath.startsWith('/') || preferredPath.startsWith('#')) && !preferredPath.contains('#/login')) {
  582. window.location = preferredPath;
  583. isRestored = true;
  584. }
  585. // Unset preferedPath
  586. this.set('preferedPath', null);
  587. }
  588. return isRestored;
  589. },
  590. /**
  591. * initialize isAdmin if user is administrator
  592. */
  593. initAdmin: function(){
  594. if (App.db) {
  595. var user = App.db.getUser();
  596. if (user) {
  597. if (user.admin) {
  598. App.set('isAdmin', true);
  599. }
  600. if (user.operator) {
  601. App.set('isOperator', true);
  602. }
  603. if (user.cluster_user) {
  604. App.set('isClusterUser', true);
  605. }
  606. App.set('isPermissionDataLoaded', true);
  607. }
  608. }
  609. },
  610. /**
  611. * initialize Auth for user
  612. */
  613. initAuth: function(){
  614. if (App.db) {
  615. var auth = App.db.getAuth();
  616. if(auth)
  617. App.set('auth', auth);
  618. }
  619. },
  620. /**
  621. * Increment redirect count if <code>redirected</code> parameter passed.
  622. */
  623. handleUIRedirect: function() {
  624. if (/(\?|&)redirected=/.test(location.hash)) {
  625. var redirectsCount = App.db.get('tmp', 'redirectsCount') || 0;
  626. App.db.set('tmp', 'redirectsCount', ++redirectsCount);
  627. }
  628. },
  629. /**
  630. * <code>window.location</code> setter. Will add query param which determines that we redirect user
  631. * @param {string} url - url to navigate
  632. */
  633. redirectByURL: function(url) {
  634. var suffix = "?redirected=true";
  635. var redirectsCount = App.db.get('tmp', 'redirectsCount') || 0;
  636. if (redirectsCount > this.get('redirectsLimitCount')) {
  637. this.showRedirectIssue();
  638. return;
  639. }
  640. // skip adding redirected parameter if added
  641. if (/(\?|&)redirected=/.test(location.hash)) {
  642. this.setLocationUrl(url);
  643. return;
  644. }
  645. // detect if query params were assigned and replace "?" with "&" for suffix param
  646. if (/\?\w+=/.test(location.hash)) {
  647. suffix = suffix.replace('?', '&');
  648. }
  649. this.setLocationUrl(url + suffix);
  650. },
  651. /**
  652. * Convenient method to set <code>window.location</code>.
  653. * Useful for faking url manipulation in tests.
  654. *
  655. * @param {string} url
  656. */
  657. setLocationUrl: function(url) {
  658. window.location = url;
  659. },
  660. /**
  661. * Convenient method to get current <code>window.location</code>.
  662. * Useful for faking url manipulation in tests.
  663. */
  664. getCurrentLocationUrl: function() {
  665. return window.location.href;
  666. },
  667. /**
  668. * Inform user about redirect issue in modal popup.
  669. *
  670. * @returns {App.ModalPopup}
  671. */
  672. showRedirectIssue: function() {
  673. var bodyMessage = Em.I18n.t('app.redirectIssuePopup.body').format(location.origin + '/#' + this.get('localUserAuthUrl'));
  674. var popupHeader = Em.I18n.t('app.redirectIssuePopup.header');
  675. var popup = App.showAlertPopup(popupHeader, bodyMessage);
  676. popup.set('encodeBody', false);
  677. return popup;
  678. },
  679. root: Em.Route.extend({
  680. index: Em.Route.extend({
  681. route: '/',
  682. redirectsTo: 'login'
  683. }),
  684. enter: function(router){
  685. router.initAdmin();
  686. router.initAuth();
  687. router.handleUIRedirect();
  688. },
  689. login: Em.Route.extend({
  690. route: '/login:suffix',
  691. /**
  692. * If the user is already logged in, redirect to where the user was previously
  693. */
  694. enter: function (router, context) {
  695. if ($.mocho) {
  696. return;
  697. }
  698. var location = router.location.location.hash;
  699. router.getAuthenticated().done(function (loggedIn) {
  700. if (loggedIn) {
  701. Ember.run.next(function () {
  702. router.getSection(function (route) {
  703. router.transitionTo(route, context);
  704. });
  705. });
  706. } else {
  707. //key to parse URI for prefered path to route
  708. router.savePreferedPath(location, '?targetURI=');
  709. }
  710. });
  711. },
  712. connectOutlets: function (router, context) {
  713. $('title').text(Em.I18n.t('app.name'));
  714. router.get('applicationController').connectOutlet('login');
  715. },
  716. serialize: function(router, context) {
  717. // check for login/local hash
  718. var location = router.get('location.location.hash');
  719. return {
  720. suffix: location === '#' + router.get('localUserAuthUrl') ? '/local' : ''
  721. };
  722. }
  723. }),
  724. installer: require('routes/installer'),
  725. main: require('routes/main'),
  726. adminView: Em.Route.extend({
  727. route: '/adminView',
  728. enter: function (router) {
  729. if (!router.get('loggedIn') || !App.isAuthorized('CLUSTER.UPGRADE_DOWNGRADE_STACK')) {
  730. Em.run.next(function () {
  731. router.transitionTo('login');
  732. });
  733. } else {
  734. App.ajax.send({
  735. name: 'ambari.service.load_server_version',
  736. sender: router,
  737. success: 'adminViewInfoSuccessCallback'
  738. });
  739. }
  740. }
  741. }),
  742. experimental: Em.Route.extend({
  743. route: '/experimental',
  744. enter: function (router, context) {
  745. if (App.isAuthorized('CLUSTER.UPGRADE_DOWNGRADE_STACK')) {
  746. Em.run.next(function () {
  747. if (router.get('clusterInstallCompleted')) {
  748. router.transitionTo("main.dashboard.widgets");
  749. } else {
  750. router.route("installer");
  751. }
  752. });
  753. } else if (!App.isAuthorized('CLUSTER.UPGRADE_DOWNGRADE_STACK')) {
  754. Em.run.next(function () {
  755. router.transitionTo("main.views.index");
  756. });
  757. }
  758. },
  759. connectOutlets: function (router, context) {
  760. if (App.isAuthorized('CLUSTER.UPGRADE_DOWNGRADE_STACK')) {
  761. App.router.get('experimentalController').loadSupports().complete(function () {
  762. $('title').text(Em.I18n.t('app.name.subtitle.experimental'));
  763. router.get('applicationController').connectOutlet('experimental');
  764. });
  765. }
  766. }
  767. }),
  768. logoff: function (router, context) {
  769. router.logOff(context);
  770. }
  771. })
  772. });