router.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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. /**
  37. * This route executes "back" and "next" handler on the next run-loop
  38. * Reason: It's done like this, because in general transitions have highest priority in the Ember run-loop
  39. * So, CP's and observers for <code>App.router.backBtnClickInProgress</code> and <code>App.router.nextBtnClickInProgress</code>
  40. * will be triggered after "back" or "next" are complete and not before them.
  41. * It's more important for "back", because usually it doesn't do any requests that may cause a little delay for run loops
  42. * <code>Em.run.next</code> is used to avoid this
  43. *
  44. * Example:
  45. * <pre>
  46. * App.Step2Route = App.StepRoute.extend({
  47. *
  48. * route: '/step2',
  49. *
  50. * connectOutlets: function (router, context) {
  51. * // some code
  52. * },
  53. *
  54. * nextTransition: function (router) {
  55. * router.transitionTo('step3');
  56. * },
  57. *
  58. * backTransition: function (router) {
  59. * router.transitionTo('step1');
  60. * }
  61. *
  62. * });
  63. * </pre>
  64. * In this case both <code>transitionTo</code> will be executed in the next run loop after loop where "next" or "back" were called
  65. * <b>IMPORTANT!</b> Flags <code>App.router.backBtnClickInProgress</code> and <code>App.router.nextBtnClickInProgress</code> are set to <code>true</code>
  66. * in the "back" and "next". Be sure to set them <code>false</code> when needed
  67. *
  68. * @type {Em.Route}
  69. */
  70. App.StepRoute = Em.Route.extend({
  71. /**
  72. * @type {Function}
  73. */
  74. backTransition: Em.K,
  75. /**
  76. * @type {Function}
  77. */
  78. nextTransition: Em.K,
  79. /**
  80. * Default "Back"-action
  81. * Execute <code>backTransition</code> once
  82. *
  83. * @param {Em.Router} router
  84. */
  85. back: function (router) {
  86. if (App.get('router.btnClickInProgress')) {
  87. return;
  88. }
  89. App.set('router.backBtnClickInProgress', true);
  90. var self = this;
  91. Em.run.next(function () {
  92. Em.tryInvoke(self, 'backTransition', [router]);
  93. })
  94. },
  95. /**
  96. * Default "Next"-action
  97. * Execute <code>nextTransition</code> once
  98. *
  99. * @param {Em.Router} router
  100. */
  101. next: function (router) {
  102. if (App.get('router.btnClickInProgress')) {
  103. return;
  104. }
  105. App.set('router.nextBtnClickInProgress', true);
  106. var self = this;
  107. Em.run.next(function () {
  108. Em.tryInvoke(self, 'nextTransition', [router]);
  109. })
  110. }
  111. });
  112. App.Router = Em.Router.extend({
  113. enableLogging: true,
  114. isFwdNavigation: true,
  115. backBtnForHigherStep: false,
  116. /**
  117. * Checks if Back button is clicked
  118. * Set to default value on the <code>App.WizardController.connectOutlet</code>
  119. *
  120. * @type {boolean}
  121. * @default false
  122. */
  123. backBtnClickInProgress: false,
  124. /**
  125. * Checks if Next button is clicked
  126. * Set to default value on the <code>App.WizardController.connectOutlet</code>
  127. *
  128. * @type {boolean}
  129. * @default false
  130. */
  131. nextBtnClickInProgress: false,
  132. /**
  133. * Checks if Next or Back button is clicked
  134. * Used in the <code>App.StepRoute</code>-instances to avoid "next"/"back" double-clicks
  135. *
  136. * @type {boolean}
  137. * @default false
  138. */
  139. btnClickInProgress: Em.computed.or('backBtnClickInProgress', 'nextBtnClickInProgress'),
  140. /**
  141. * Path for local login page. This page will be always accessible without
  142. * redirect to auth server different from ambari-server. Used in some types of
  143. * authorizations like knox sso.
  144. *
  145. * @type {string}
  146. */
  147. localUserAuthUrl: '/login/local',
  148. /**
  149. * LocalStorage property <code>redirectsCount</code> from <code>tmp</code> namespace
  150. * will be incremented by each redirect action performed by UI and reset on success login.
  151. * <code>redirectsLimitCount</code> determines maximum redirect tries. When redirects count overflow
  152. * then something goes wrong and we have to inform user about the problem.
  153. *
  154. * @type {number}
  155. */
  156. redirectsLimitCount: 0,
  157. /**
  158. * Is true, if cluster.provisioning_state is equal to 'INSTALLED'
  159. * @type {Boolean}
  160. */
  161. clusterInstallCompleted: false,
  162. /**
  163. * user prefered path to route
  164. */
  165. preferedPath: null,
  166. setNavigationFlow: function (step) {
  167. var matches = step.match(/\d+$/);
  168. var newStep;
  169. if (matches) {
  170. newStep = parseInt(matches[0], 10);
  171. }
  172. var previousStep = parseInt(this.getInstallerCurrentStep(), 10);
  173. this.set('isFwdNavigation', newStep >= previousStep);
  174. },
  175. clearAllSteps: function () {
  176. this.get('installerController').clear();
  177. this.get('addHostController').clear();
  178. this.get('addServiceController').clear();
  179. this.get('backgroundOperationsController').clear();
  180. for (var i = 1; i < 11; i++) {
  181. this.set('wizardStep' + i + 'Controller.hasSubmitted', false);
  182. this.set('wizardStep' + i + 'Controller.isDisabled', true);
  183. }
  184. },
  185. /**
  186. * Temporary fix for getting cluster name
  187. * @return {*}
  188. */
  189. getClusterName: function () {
  190. return App.router.get('clusterController').get('clusterName');
  191. },
  192. /**
  193. * Get current step of Installer wizard
  194. * @return {*}
  195. */
  196. getInstallerCurrentStep: function () {
  197. return this.getWizardCurrentStep('installer');
  198. },
  199. /**
  200. * Get current step for <code>wizardType</code> wizard
  201. * @param wizardType one of <code>installer</code>, <code>addHost</code>, <code>addServices</code>
  202. */
  203. getWizardCurrentStep: function (wizardType) {
  204. var currentStep = App.db.getWizardCurrentStep(wizardType);
  205. if (!currentStep) {
  206. currentStep = wizardType === 'installer' ? '0' : '1';
  207. }
  208. return currentStep;
  209. },
  210. /**
  211. * @type {boolean}
  212. */
  213. loggedIn: App.db.getAuthenticated(),
  214. loginName: function() {
  215. return this.getLoginName();
  216. }.property('loggedIn'),
  217. displayLoginName: Em.computed.truncate('loginName', 10, 10),
  218. /**
  219. * @type {$.ajax|null}
  220. */
  221. clusterDataRequest: null,
  222. /**
  223. * If request was already sent on login then use saved clusterDataRequest and don't make second call
  224. * @returns {$.ajax}
  225. */
  226. getClusterDataRequest: function() {
  227. var clusterDataRequest = this.get('clusterDataRequest');
  228. if (clusterDataRequest) {
  229. this.set('clusterDataRequest', null);
  230. return clusterDataRequest;
  231. } else {
  232. return App.ajax.send({
  233. name: 'router.login.clusters',
  234. sender: this,
  235. success: 'onAuthenticationSuccess',
  236. error: 'onAuthenticationError'
  237. });
  238. }
  239. },
  240. getAuthenticated: function () {
  241. var dfd = $.Deferred();
  242. var self = this;
  243. var auth = App.db.getAuthenticated();
  244. this.getClusterDataRequest().complete(function (xhr) {
  245. if (xhr.isResolved()) {
  246. // if server knows the user and user authenticated by UI
  247. if (auth) {
  248. dfd.resolve(self.get('loggedIn'));
  249. // if server knows the user but UI don't, check the response header
  250. // and try to authorize
  251. } else if (xhr.getResponseHeader('User')) {
  252. var user = xhr.getResponseHeader('User');
  253. App.ajax.send({
  254. name: 'router.login',
  255. sender: self,
  256. data: {
  257. usr: user,
  258. loginName: encodeURIComponent(user)
  259. },
  260. success: 'loginSuccessCallback',
  261. error: 'loginErrorCallback'
  262. }).then(function() {
  263. dfd.resolve(true);
  264. });
  265. } else {
  266. self.setAuthenticated(false);
  267. dfd.resolve(false);
  268. }
  269. } else {
  270. //if provisioning state unreachable then consider user as unauthenticated
  271. self.setAuthenticated(false);
  272. dfd.resolve(false);
  273. }
  274. });
  275. return dfd.promise();
  276. },
  277. /**
  278. * Response for <code>/clusters?fields=Clusters/provisioning_state</code>
  279. * @type {null|object}
  280. */
  281. clusterData: null,
  282. onAuthenticationSuccess: function (data) {
  283. if (App.db.getAuthenticated() === true) {
  284. this.set('clusterData', data);
  285. this.setAuthenticated(true);
  286. if (data.items.length) {
  287. this.setClusterInstalled(data);
  288. }
  289. }
  290. },
  291. /**
  292. * If authentication failed, need to check for jwt auth url
  293. * and redirect user if current location is not <code>localUserAuthUrl</code>
  294. *
  295. * @param {?object} data
  296. */
  297. onAuthenticationError: function (data) {
  298. if (data.status === 403) {
  299. try {
  300. var responseJson = JSON.parse(data.responseText);
  301. if (responseJson.jwtProviderUrl && this.get('location.lastSetURL') !== this.get('localUserAuthUrl')) {
  302. this.redirectByURL(responseJson.jwtProviderUrl + encodeURIComponent(this.getCurrentLocationUrl()));
  303. }
  304. } catch (e) {
  305. } finally {
  306. this.setAuthenticated(false);
  307. }
  308. } else if (data.status >= 500) {
  309. this.setAuthenticated(false);
  310. this.loginErrorCallback(data);
  311. }
  312. },
  313. setAuthenticated: function (authenticated) {
  314. App.db.setAuthenticated(authenticated);
  315. this.set('loggedIn', authenticated);
  316. },
  317. getLoginName: function () {
  318. return App.db.getLoginName();
  319. },
  320. setLoginName: function (loginName) {
  321. App.db.setLoginName(loginName);
  322. },
  323. /**
  324. * Set user model to local storage
  325. * @param user
  326. */
  327. setUser: function (user) {
  328. App.db.setUser(user);
  329. },
  330. /**
  331. * Get user model from local storage
  332. * @return {*}
  333. */
  334. getUser: function () {
  335. return App.db.getUser();
  336. },
  337. setUserLoggedIn: function(userName) {
  338. this.setAuthenticated(true);
  339. this.setLoginName(userName);
  340. this.setUser(App.User.find().findProperty('id', userName));
  341. App.db.set('tmp', 'redirectsCount', 0);
  342. },
  343. /**
  344. * Set `clusterInstallCompleted` property based on cluster info response.
  345. *
  346. * @param {Object} clusterObject
  347. **/
  348. setClusterInstalled: function(clusterObject) {
  349. this.set('clusterInstallCompleted', clusterObject.items[0].Clusters.provisioning_state === 'INSTALLED')
  350. },
  351. login: function () {
  352. var controller = this.get('loginController');
  353. var loginName = controller.get('loginName');
  354. controller.set('loginName', loginName);
  355. var hash = misc.utf8ToB64(loginName + ":" + controller.get('password'));
  356. var usr = '';
  357. if (App.get('testMode')) {
  358. if (loginName === "admin" && controller.get('password') === 'admin') {
  359. usr = 'admin';
  360. } else if (loginName === 'user' && controller.get('password') === 'user') {
  361. usr = 'user';
  362. }
  363. }
  364. App.ajax.send({
  365. name: 'router.login',
  366. sender: this,
  367. data: {
  368. auth: "Basic " + hash,
  369. usr: usr,
  370. loginName: encodeURIComponent(loginName)
  371. },
  372. beforeSend: 'authBeforeSend',
  373. success: 'loginSuccessCallback',
  374. error: 'loginErrorCallback'
  375. });
  376. },
  377. authBeforeSend: function(opt, xhr, data) {
  378. xhr.setRequestHeader("Authorization", data.auth);
  379. },
  380. loginSuccessCallback: function(data, opt, params) {
  381. var self = this;
  382. App.router.set('loginController.isSubmitDisabled', false);
  383. App.usersMapper.map({"items": [data]});
  384. this.setUserLoggedIn(data.Users.user_name);
  385. var requestData = {
  386. loginName: data.Users.user_name,
  387. loginData: data
  388. };
  389. App.router.get('clusterController').loadAuthorizations().complete(function() {
  390. App.ajax.send({
  391. name: 'router.login.message',
  392. sender: self,
  393. data: requestData,
  394. success: 'showLoginMessageSuccessCallback',
  395. error: 'showLoginMessageErrorCallback'
  396. });
  397. });
  398. },
  399. loginErrorCallback: function(request) {
  400. var controller = this.get('loginController');
  401. this.setAuthenticated(false);
  402. if (request.status > 400) {
  403. var responseMessage = request.responseText;
  404. try{
  405. responseMessage = JSON.parse(request.responseText).message;
  406. }catch(e){}
  407. }
  408. if (request.status == 403) {
  409. controller.postLogin(true, false, responseMessage);
  410. } else if (request.status == 500) {
  411. controller.postLogin(false, false, responseMessage);
  412. } else {
  413. controller.postLogin(false, false, null);
  414. }
  415. },
  416. /**
  417. * success callback of router.login.message
  418. * @param {object} data
  419. * @param {object} opt
  420. * @param {object} params
  421. */
  422. showLoginMessageSuccessCallback: function (data, opt, params) {
  423. try {
  424. var response = JSON.parse(data.Settings.content.replace(/\n/g, "\\n"))
  425. } catch (e) {
  426. this.setClusterData(data, opt, params);
  427. return false;
  428. }
  429. var
  430. text = response.text ? response.text.replace(/(\r\n|\n|\r)/gm, '<br>') : "",
  431. buttonText = response.button ? response.button : Em.I18n.t('ok'),
  432. status = response.status && response.status == "true" ? true : false,
  433. self = this;
  434. if(text && status){
  435. return App.ModalPopup.show({
  436. classNames: ['sixty-percent-width-modal'],
  437. header: Em.I18n.t('login.message.title'),
  438. bodyClass: Ember.View.extend({
  439. template: Ember.Handlebars.compile(text)
  440. }),
  441. primary:null,
  442. secondary: null,
  443. footerClass: Ember.View.extend({
  444. template: Ember.Handlebars.compile(
  445. '<div class="modal-footer">' +
  446. '<button class="btn btn-success" {{action onPrimary target="view"}}>' + buttonText + '</button>'+
  447. '</div>'
  448. ),
  449. onPrimary: function() {
  450. this.get('parentView').onPrimary();
  451. }
  452. }),
  453. onPrimary: function () {
  454. self.setClusterData(data, opt, params);
  455. this.hide();
  456. },
  457. onClose: function () {
  458. self.setClusterData(data, opt, params);
  459. this.hide();
  460. }
  461. });
  462. }
  463. this.setClusterData(data, opt, params);
  464. return false;
  465. },
  466. /**
  467. * error callback of router.login.message
  468. * @param {object} request
  469. * @param {string} ajaxOptions
  470. * @param {string} error
  471. * @param {object} opt
  472. * @param {object} params
  473. */
  474. showLoginMessageErrorCallback: function (request, ajaxOptions, error, opt, params) {
  475. this.showLoginMessageSuccessCallback(null, opt, params);
  476. },
  477. setClusterData: function (data, opt, params) {
  478. var
  479. self = this,
  480. requestData = {
  481. loginName: params.loginName,
  482. loginData: params.loginData
  483. };
  484. // no need to load cluster data if it's already loaded
  485. if (this.get('clusterData')) {
  486. this.loginGetClustersSuccessCallback(self.get('clusterData'), {}, requestData);
  487. }
  488. else {
  489. this.set('clusterDataRequest', App.ajax.send({
  490. name: 'router.login.clusters',
  491. sender: self,
  492. data: requestData,
  493. success: 'loginGetClustersSuccessCallback'
  494. }));
  495. }
  496. },
  497. /**
  498. * success callback of login request
  499. * @param {object} clustersData
  500. * @param {object} opt
  501. * @param {object} params
  502. */
  503. loginGetClustersSuccessCallback: function (clustersData, opt, params) {
  504. var privileges = params.loginData.privileges || [];
  505. var router = this;
  506. var isAdmin = privileges.mapProperty('PrivilegeInfo.permission_name').contains('AMBARI.ADMINISTRATOR');
  507. App.set('isAdmin', isAdmin);
  508. if (clustersData.items.length) {
  509. var clusterPermissions = privileges.
  510. filterProperty('PrivilegeInfo.cluster_name', clustersData.items[0].Clusters.cluster_name).
  511. mapProperty('PrivilegeInfo.permission_name');
  512. //cluster installed
  513. router.setClusterInstalled(clustersData);
  514. if (clusterPermissions.contains('CLUSTER.ADMINISTRATOR')) {
  515. App.setProperties({
  516. isAdmin: true,
  517. isOperator: true,
  518. isClusterUser: false
  519. });
  520. }
  521. if (App.get('isOnlyViewUser')) {
  522. router.transitionToViews();
  523. } else {
  524. router.transitionToApp();
  525. }
  526. } else {
  527. if (App.get('isOnlyViewUser')) {
  528. router.transitionToViews();
  529. } else {
  530. router.transitionToAdminView();
  531. }
  532. }
  533. // set cluster name and security type
  534. App.router.get('clusterController').reloadSuccessCallback(clustersData);
  535. App.set('isPermissionDataLoaded', true);
  536. App.router.get('userSettingsController').dataLoading();
  537. },
  538. /**
  539. * redirect user to Admin View
  540. * @returns {$.ajax}
  541. */
  542. transitionToAdminView: function() {
  543. return App.ajax.send({
  544. name: 'ambari.service.load_server_version',
  545. sender: this,
  546. success: 'adminViewInfoSuccessCallback',
  547. error: 'adminViewInfoErrorCallback'
  548. });
  549. },
  550. /**
  551. * redirect user to application Dashboard
  552. */
  553. transitionToApp: function () {
  554. var router = this;
  555. if (!router.restorePreferedPath()) {
  556. router.getSection(function (route) {
  557. router.transitionTo(route);
  558. });
  559. }
  560. },
  561. /**
  562. * redirect user to application Views
  563. */
  564. transitionToViews: function() {
  565. App.router.get('mainViewsController').loadAmbariViews();
  566. this.transitionTo('main.views.index');
  567. },
  568. adminViewInfoSuccessCallback: function(data) {
  569. var components = Em.get(data,'components');
  570. if (Em.isArray(components)) {
  571. var mappedVersions = components.map(function(component) {
  572. if (Em.get(component, 'RootServiceComponents.component_version')) {
  573. return Em.get(component, 'RootServiceComponents.component_version');
  574. }
  575. }),
  576. sortedMappedVersions = mappedVersions.sort(),
  577. latestVersion = sortedMappedVersions[sortedMappedVersions.length-1];
  578. window.location.replace(App.appURLRoot + 'views/ADMIN_VIEW/' + latestVersion + '/INSTANCE/#/');
  579. }
  580. },
  581. adminViewInfoErrorCallback: function() {
  582. this.transitionToViews();
  583. },
  584. getSection: function (callback) {
  585. if (App.get('testMode')) {
  586. if (App.alwaysGoToInstaller) {
  587. callback('installer');
  588. } else {
  589. callback('main.dashboard.index');
  590. }
  591. } else {
  592. if (this.get('clusterInstallCompleted')) {
  593. App.router.get('wizardWatcherController').getUser().complete(function() {
  594. App.clusterStatus.updateFromServer(false).complete(function () {
  595. var route = 'main.dashboard.index';
  596. var clusterStatusOnServer = App.clusterStatus.get('value');
  597. if (clusterStatusOnServer) {
  598. var wizardControllerRoutes = require('data/controller_route');
  599. var wizardControllerRoute = wizardControllerRoutes.findProperty('wizardControllerName', clusterStatusOnServer.wizardControllerName);
  600. if (wizardControllerRoute && !App.router.get('wizardWatcherController').get('isNonWizardUser')) {
  601. route = wizardControllerRoute.route;
  602. }
  603. }
  604. if (wizardControllerRoute && wizardControllerRoute.wizardControllerName === 'mainAdminStackAndUpgradeController') {
  605. var clusterController = App.router.get('clusterController');
  606. clusterController.loadClusterName().done(function(){
  607. clusterController.restoreUpgradeState().done(function(){
  608. callback(route);
  609. });
  610. });
  611. } else {
  612. callback(route);
  613. }
  614. });
  615. });
  616. } else {
  617. callback('installer');
  618. }
  619. }
  620. },
  621. logOff: function (context) {
  622. var self = this;
  623. $('title').text(Em.I18n.t('app.name'));
  624. App.router.get('mainController').stopPolling();
  625. // App.db.cleanUp() must be called before router.clearAllSteps().
  626. // otherwise, this.set('installerController.currentStep, 0) would have no effect
  627. // since it's a computed property but we are not setting it as a dependent of App.db.
  628. App.db.cleanUp();
  629. App.setProperties({
  630. isAdmin: false,
  631. auth: null,
  632. isOperator: false,
  633. isClusterUser: false,
  634. isPermissionDataLoaded: false
  635. });
  636. this.set('loggedIn', false);
  637. this.clearAllSteps();
  638. this.set('loginController.loginName', '');
  639. this.set('loginController.password', '');
  640. // 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.
  641. if (!App.get('testMode') && context) {
  642. App.ajax.send({
  643. name: 'router.logoff',
  644. sender: this,
  645. success: 'logOffSuccessCallback',
  646. error: 'logOffErrorCallback',
  647. beforeSend: 'logOffBeforeSend'
  648. }).complete(function() {
  649. self.logoffRedirect(context);
  650. });
  651. } else {
  652. this.logoffRedirect();
  653. }
  654. },
  655. logOffSuccessCallback: function () {
  656. var applicationController = App.router.get('applicationController');
  657. applicationController.set('isPollerRunning', false);
  658. },
  659. logOffErrorCallback: function () {
  660. },
  661. logOffBeforeSend: function(opt, xhr) {
  662. xhr.setRequestHeader('Authorization', '');
  663. },
  664. /**
  665. * Redirect function on sign off request.
  666. *
  667. * @param {$.Event} [context=undefined] - triggered event context
  668. */
  669. logoffRedirect: function(context) {
  670. this.transitionTo('login', context);
  671. if (App.router.get('clusterController.isLoaded')) {
  672. Em.run.next(function() {
  673. window.location.reload();
  674. });
  675. }
  676. },
  677. /**
  678. * save prefered path
  679. * @param {string} path
  680. * @param {string} key
  681. */
  682. savePreferedPath: function(path, key) {
  683. if (key) {
  684. if (path.contains(key)) {
  685. this.set('preferedPath', path.slice(path.indexOf(key) + key.length));
  686. }
  687. } else {
  688. this.set('preferedPath', path);
  689. }
  690. },
  691. /**
  692. * If path exist route to it, otherwise return false
  693. * @returns {boolean}
  694. */
  695. restorePreferedPath: function() {
  696. var preferredPath = this.get('preferedPath');
  697. var isRestored = false;
  698. if (preferredPath) {
  699. // If the preferred path is relative, allow a redirect to it.
  700. // If the path is not relative, silently ignore it - if the path is an absolute URL, the user
  701. // may be routed to a different server where the possibility exists for a phishing attack.
  702. if ((preferredPath.startsWith('/') || preferredPath.startsWith('#')) && !preferredPath.contains('#/login')) {
  703. window.location = preferredPath;
  704. isRestored = true;
  705. }
  706. // Unset preferedPath
  707. this.set('preferedPath', null);
  708. }
  709. return isRestored;
  710. },
  711. /**
  712. * initialize isAdmin if user is administrator
  713. */
  714. initAdmin: function(){
  715. if (App.db) {
  716. var user = App.db.getUser();
  717. if (user) {
  718. if (user.admin) {
  719. App.set('isAdmin', true);
  720. }
  721. if (user.operator) {
  722. App.set('isOperator', true);
  723. }
  724. if (user.cluster_user) {
  725. App.set('isClusterUser', true);
  726. }
  727. App.set('isPermissionDataLoaded', true);
  728. }
  729. }
  730. },
  731. /**
  732. * initialize Auth for user
  733. */
  734. initAuth: function(){
  735. if (App.db) {
  736. var auth = App.db.getAuth();
  737. if(auth) {
  738. App.set('auth', auth);
  739. }
  740. }
  741. },
  742. /**
  743. * Increment redirect count if <code>redirected</code> parameter passed.
  744. */
  745. handleUIRedirect: function() {
  746. if (/(\?|&)redirected=/.test(location.hash)) {
  747. var redirectsCount = App.db.get('tmp', 'redirectsCount') || 0;
  748. App.db.set('tmp', 'redirectsCount', ++redirectsCount);
  749. }
  750. },
  751. /**
  752. * <code>window.location</code> setter. Will add query param which determines that we redirect user
  753. * @param {string} url - url to navigate
  754. */
  755. redirectByURL: function(url) {
  756. var suffix = "?redirected=true";
  757. var redirectsCount = App.db.get('tmp', 'redirectsCount') || 0;
  758. if (redirectsCount > this.get('redirectsLimitCount')) {
  759. this.showRedirectIssue();
  760. return;
  761. }
  762. // skip adding redirected parameter if added
  763. if (/(\?|&)redirected=/.test(location.hash)) {
  764. this.setLocationUrl(url);
  765. return;
  766. }
  767. // detect if query params were assigned and replace "?" with "&" for suffix param
  768. if (/\?\w+=/.test(location.hash)) {
  769. suffix = suffix.replace('?', '&');
  770. }
  771. this.setLocationUrl(url + suffix);
  772. },
  773. /**
  774. * Convenient method to set <code>window.location</code>.
  775. * Useful for faking url manipulation in tests.
  776. *
  777. * @param {string} url
  778. */
  779. setLocationUrl: function(url) {
  780. window.location = url;
  781. },
  782. /**
  783. * Convenient method to get current <code>window.location</code>.
  784. * Useful for faking url manipulation in tests.
  785. */
  786. getCurrentLocationUrl: function() {
  787. return window.location.href;
  788. },
  789. /**
  790. * Inform user about redirect issue in modal popup.
  791. *
  792. * @returns {App.ModalPopup}
  793. */
  794. showRedirectIssue: function() {
  795. var bodyMessage = Em.I18n.t('app.redirectIssuePopup.body').format(location.origin + '/#' + this.get('localUserAuthUrl'));
  796. var popupHeader = Em.I18n.t('app.redirectIssuePopup.header');
  797. var popup = App.showAlertPopup(popupHeader, bodyMessage);
  798. popup.set('encodeBody', false);
  799. return popup;
  800. },
  801. root: Em.Route.extend({
  802. index: Em.Route.extend({
  803. route: '/',
  804. redirectsTo: 'login'
  805. }),
  806. enter: function(router){
  807. router.initAdmin();
  808. router.initAuth();
  809. router.handleUIRedirect();
  810. },
  811. login: Em.Route.extend({
  812. route: '/login:suffix',
  813. /**
  814. * If the user is already logged in, redirect to where the user was previously
  815. */
  816. enter: function (router, context) {
  817. if ($.mocho) {
  818. return;
  819. }
  820. var location = router.location.location.hash;
  821. router.getAuthenticated().done(function (loggedIn) {
  822. if (loggedIn) {
  823. Ember.run.next(function () {
  824. router.getSection(function (route) {
  825. router.transitionTo(route, context);
  826. });
  827. });
  828. } else {
  829. //key to parse URI for prefered path to route
  830. router.savePreferedPath(location, '?targetURI=');
  831. }
  832. });
  833. },
  834. connectOutlets: function (router, context) {
  835. $('title').text(Em.I18n.t('app.name'));
  836. router.get('applicationController').connectOutlet('login');
  837. },
  838. serialize: function(router, context) {
  839. // check for login/local hash
  840. var location = router.get('location.location.hash');
  841. return {
  842. suffix: location === '#' + router.get('localUserAuthUrl') ? '/local' : ''
  843. };
  844. }
  845. }),
  846. installer: require('routes/installer'),
  847. main: require('routes/main'),
  848. adminView: Em.Route.extend({
  849. route: '/adminView',
  850. enter: function (router) {
  851. if (!router.get('loggedIn') || !App.isAuthorized('CLUSTER.UPGRADE_DOWNGRADE_STACK')) {
  852. Em.run.next(function () {
  853. router.transitionTo('login');
  854. });
  855. } else {
  856. App.ajax.send({
  857. name: 'ambari.service.load_server_version',
  858. sender: router,
  859. success: 'adminViewInfoSuccessCallback'
  860. });
  861. }
  862. }
  863. }),
  864. experimental: Em.Route.extend({
  865. route: '/experimental',
  866. enter: function (router, context) {
  867. if (!App.isAuthorized('AMBARI.MANAGE_SETTINGS')) {
  868. if (App.isAuthorized('CLUSTER.UPGRADE_DOWNGRADE_STACK')) {
  869. Em.run.next(function () {
  870. if (router.get('clusterInstallCompleted')) {
  871. router.transitionTo("main.dashboard.widgets");
  872. } else {
  873. router.transitionTo("main.views.index");
  874. }
  875. });
  876. } else {
  877. Em.run.next(function () {
  878. router.transitionTo("main.views.index");
  879. });
  880. }
  881. }
  882. },
  883. connectOutlets: function (router, context) {
  884. if (App.isAuthorized('AMBARI.MANAGE_SETTINGS')) {
  885. App.router.get('experimentalController').loadSupports().complete(function () {
  886. $('title').text(Em.I18n.t('app.name.subtitle.experimental'));
  887. router.get('applicationController').connectOutlet('experimental');
  888. });
  889. }
  890. }
  891. }),
  892. logoff: function (router, context) {
  893. router.logOff(context);
  894. }
  895. })
  896. });