router.js 23 KB

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