router.js 25 KB

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