router.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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 App = require('app');
  19. App.WizardRoute = Em.Route.extend({
  20. gotoStep0: Em.Router.transitionTo('step0'),
  21. gotoStep1: Em.Router.transitionTo('step1'),
  22. gotoStep2: Em.Router.transitionTo('step2'),
  23. gotoStep3: Em.Router.transitionTo('step3'),
  24. gotoStep4: Em.Router.transitionTo('step4'),
  25. gotoStep5: Em.Router.transitionTo('step5'),
  26. gotoStep6: Em.Router.transitionTo('step6'),
  27. gotoStep7: Em.Router.transitionTo('step7'),
  28. gotoStep8: Em.Router.transitionTo('step8'),
  29. gotoStep9: Em.Router.transitionTo('step9'),
  30. gotoStep10: Em.Router.transitionTo('step10'),
  31. isRoutable: function() {
  32. return (typeof this.get('route') === 'string' && App.router.get('loggedIn'));
  33. }.property('App.router.loggedIn')
  34. });
  35. App.Router = Em.Router.extend({
  36. enableLogging: true,
  37. isFwdNavigation: true,
  38. backBtnForHigherStep: false,
  39. /**
  40. * Is true, if cluster.provisioning_state is equal to 'INSTALLED'
  41. * @type {Boolean}
  42. */
  43. clusterInstallCompleted: false,
  44. /**
  45. * user prefered path to route
  46. */
  47. preferedPath: null,
  48. setNavigationFlow: function (step) {
  49. var matches = step.match(/\d+$/);
  50. var newStep;
  51. if (matches) {
  52. newStep = parseInt(matches[0]);
  53. }
  54. var previousStep = parseInt(this.getInstallerCurrentStep());
  55. this.set('isFwdNavigation', newStep >= previousStep);
  56. },
  57. clearAllSteps: function () {
  58. this.get('installerController').clear();
  59. this.get('addHostController').clear();
  60. this.get('addServiceController').clear();
  61. this.get('backgroundOperationsController').clear();
  62. for (var i = 1; i < 11; i++) {
  63. this.set('wizardStep' + i + 'Controller.hasSubmitted', false);
  64. this.set('wizardStep' + i + 'Controller.isDisabled', true);
  65. }
  66. },
  67. /**
  68. * Temporary fix for getting cluster name
  69. * @return {*}
  70. */
  71. getClusterName: function () {
  72. return App.router.get('clusterController').get('clusterName');
  73. },
  74. /**
  75. * Get current step of Installer wizard
  76. * @return {*}
  77. */
  78. getInstallerCurrentStep: function () {
  79. return this.getWizardCurrentStep('installer');
  80. },
  81. /**
  82. * Get current step for <code>wizardType</code> wizard
  83. * @param wizardType one of <code>installer</code>, <code>addHost</code>, <code>addServices</code>
  84. */
  85. getWizardCurrentStep: function (wizardType) {
  86. var loginName = this.getLoginName();
  87. var currentStep = App.db.getWizardCurrentStep(wizardType);
  88. console.log('getWizardCurrentStep: loginName=' + loginName + ", currentStep=" + currentStep);
  89. if (!currentStep) {
  90. currentStep = wizardType === 'installer' ? '0' : '1';
  91. }
  92. console.log('returning currentStep=' + currentStep);
  93. return currentStep;
  94. },
  95. loggedIn: App.db.getAuthenticated(),
  96. loginName: function() {
  97. return this.getLoginName();
  98. }.property('loggedIn'),
  99. getAuthenticated: function () {
  100. var dfd = $.Deferred();
  101. var self = this;
  102. var auth = App.db.getAuthenticated();
  103. var authResp = (auth && auth === true);
  104. if (authResp) {
  105. App.ajax.send({
  106. name: 'router.login.clusters',
  107. sender: this,
  108. success: 'onAuthenticationSuccess',
  109. error: 'onAuthenticationError'
  110. }).complete(function () {
  111. dfd.resolve(self.get('loggedIn'));
  112. });
  113. } else {
  114. this.set('loggedIn', false);
  115. dfd.resolve(false);
  116. }
  117. return dfd.promise();
  118. },
  119. onAuthenticationSuccess: function (data) {
  120. this.setAuthenticated(true);
  121. if (data.items.length) {
  122. this.setClusterInstalled(data);
  123. }
  124. },
  125. onAuthenticationError: function (data) {
  126. if (data.status === 403) {
  127. this.setAuthenticated(false);
  128. } else {
  129. console.log('error in getAuthenticated');
  130. }
  131. },
  132. setAuthenticated: function (authenticated) {
  133. console.log("TRACE: Entering router:setAuthenticated function");
  134. App.db.setAuthenticated(authenticated);
  135. this.set('loggedIn', authenticated);
  136. },
  137. getLoginName: function () {
  138. return App.db.getLoginName();
  139. },
  140. setLoginName: function (loginName) {
  141. App.db.setLoginName(loginName);
  142. },
  143. /**
  144. * Set user model to local storage
  145. * @param user
  146. */
  147. setUser: function (user) {
  148. App.db.setUser(user);
  149. },
  150. /**
  151. * Get user model from local storage
  152. * @return {*}
  153. */
  154. getUser: function () {
  155. return App.db.getUser();
  156. },
  157. setUserLoggedIn: function(userName) {
  158. this.setAuthenticated(true);
  159. this.setLoginName(userName);
  160. this.setUser(App.User.find().findProperty('id', userName));
  161. },
  162. /**
  163. * Set `clusterInstallCompleted` property based on cluster info response.
  164. *
  165. * @param {Object} clusterObject
  166. **/
  167. setClusterInstalled: function(clusterObject) {
  168. this.set('clusterInstallCompleted', clusterObject.items[0].Clusters.provisioning_state === 'INSTALLED')
  169. },
  170. login: function () {
  171. var controller = this.get('loginController');
  172. var loginName = controller.get('loginName').toLowerCase();
  173. controller.set('loginName', loginName);
  174. var hash = window.btoa(loginName + ":" + controller.get('password'));
  175. var usr = '';
  176. if (App.get('testMode')) {
  177. if (loginName === "admin" && controller.get('password') === 'admin') {
  178. usr = 'admin';
  179. } else if (loginName === 'user' && controller.get('password') === 'user') {
  180. usr = 'user';
  181. }
  182. }
  183. App.ajax.send({
  184. name: 'router.login',
  185. sender: this,
  186. data: {
  187. auth: "Basic " + hash,
  188. usr: usr,
  189. loginName: encodeURIComponent(loginName)
  190. },
  191. beforeSend: 'authBeforeSend',
  192. success: 'loginSuccessCallback',
  193. error: 'loginErrorCallback'
  194. });
  195. },
  196. authBeforeSend: function(opt, xhr, data) {
  197. xhr.setRequestHeader("Authorization", data.auth);
  198. },
  199. loginSuccessCallback: function(data, opt, params) {
  200. console.log('login success');
  201. App.usersMapper.map({"items": [data]});
  202. this.setUserLoggedIn(params.loginName);
  203. App.router.get('mainViewsController').loadAmbariViews();
  204. App.ajax.send({
  205. name: 'router.login.clusters',
  206. sender: this,
  207. data: {
  208. loginName: params.loginName,
  209. loginData: data
  210. },
  211. success: 'loginGetClustersSuccessCallback',
  212. error: 'loginGetClustersErrorCallback'
  213. });
  214. },
  215. loginErrorCallback: function(request, ajaxOptions, error, opt) {
  216. var controller = this.get('loginController');
  217. console.log("login error: " + error);
  218. this.setAuthenticated(false);
  219. if (request.status == 403) {
  220. var responseMessage = request.responseText;
  221. try{
  222. responseMessage = JSON.parse(request.responseText).message;
  223. }catch(e){}
  224. controller.postLogin(true, false, responseMessage);
  225. } else {
  226. controller.postLogin(false, false, null);
  227. }
  228. },
  229. loginGetClustersSuccessCallback: function (clustersData, opt, params) {
  230. var adminViewUrl = '/views/ADMIN_VIEW/2.0.0/INSTANCE/#/';
  231. //TODO: Replace hard coded value with query. Same in templates/application.hbs
  232. var loginController = this.get('loginController');
  233. var loginData = params.loginData;
  234. var privileges = loginData.privileges || [];
  235. var router = this;
  236. var permissionList = privileges.mapProperty('PrivilegeInfo.permission_name');
  237. var isAdmin = permissionList.contains('AMBARI.ADMIN');
  238. var transitionToApp = false;
  239. if (isAdmin) {
  240. App.set('isAdmin', true);
  241. if (clustersData.items.length) {
  242. router.setClusterInstalled(clustersData);
  243. transitionToApp = true;
  244. } else {
  245. window.location = adminViewUrl;
  246. return;
  247. }
  248. } else {
  249. if (clustersData.items.length) {
  250. router.setClusterInstalled(clustersData);
  251. //TODO: Iterate over clusters
  252. var clusterName = clustersData.items[0].Clusters.cluster_name;
  253. var clusterPermissions = privileges.filterProperty('PrivilegeInfo.cluster_name', clusterName).mapProperty('PrivilegeInfo.permission_name');
  254. if (clusterPermissions.contains('CLUSTER.OPERATE')) {
  255. App.set('isAdmin', true);
  256. App.set('isOperator', true);
  257. transitionToApp = true;
  258. } else if (clusterPermissions.contains('CLUSTER.READ')) {
  259. transitionToApp = true;
  260. }
  261. }
  262. }
  263. if (transitionToApp) {
  264. if (!Em.isNone(router.get('preferedPath'))) {
  265. window.location = router.get('preferedPath');
  266. router.set('preferedPath', null);
  267. } else {
  268. router.getSection(function (route) {
  269. router.transitionTo(route);
  270. loginController.postLogin(true, true);
  271. });
  272. }
  273. } else {
  274. router.transitionTo('main.views.index');
  275. loginController.postLogin(true,true);
  276. }
  277. },
  278. loginGetClustersErrorCallback: function (req) {
  279. console.log("Get clusters error: " + req.statusCode);
  280. },
  281. getSection: function (callback) {
  282. if (App.get('testMode')) {
  283. if (App.alwaysGoToInstaller) {
  284. callback('installer');
  285. } else {
  286. callback('main.dashboard.index');
  287. }
  288. } else {
  289. if (this.get('clusterInstallCompleted')) {
  290. App.clusterStatus.updateFromServer(false).complete(function () {
  291. var clusterStatusOnServer = App.clusterStatus.get('value');
  292. var route = 'main.dashboard.index';
  293. if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('addHostController.name')) {
  294. // if wizardControllerName == "addHostController", then it means someone closed the browser or the browser was crashed when we were last in Add Hosts wizard
  295. route = 'main.hostAdd';
  296. } else if (clusterStatusOnServer && (clusterStatusOnServer.wizardControllerName === App.router.get('addSecurityController.name') || clusterStatusOnServer.wizardControllerName === App.router.get('mainAdminSecurityDisableController.name'))) {
  297. // if wizardControllerName == "addSecurityController", then it means someone closed the browser or the browser was crashed when we were last in Add Security wizard
  298. route = 'main.admin.adminSecurity';
  299. } else if (clusterStatusOnServer && (clusterStatusOnServer.wizardControllerName === App.router.get('kerberosWizardController.name'))) {
  300. // if wizardControllerName == "adminKerberosController", then it means someone closed the browser or the browser was crashed when we were last in Add Kerberos wizard
  301. route = 'main.admin.adminKerberos.adminAddKerberos';
  302. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('addServiceController.name')) {
  303. // if wizardControllerName == "addHostController", then it means someone closed the browser or the browser was crashed when we were last in Add Hosts wizard
  304. route = 'main.serviceAdd';
  305. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('reassignMasterController.name')) {
  306. // if wizardControllerName == "reassignMasterController", then it means someone closed the browser or the browser was crashed when we were last in Reassign Master wizard
  307. route = 'main.reassign';
  308. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('highAvailabilityWizardController.name')) {
  309. // if wizardControllerName == "highAvailabilityWizardController", then it means someone closed the browser or the browser was crashed when we were last in NameNode High Availability wizard
  310. route = 'main.services.enableHighAvailability';
  311. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('rMHighAvailabilityWizardController.name')) {
  312. // if wizardControllerName == "highAvailabilityWizardController", then it means someone closed the browser or the browser was crashed when we were last in NameNode High Availability wizard
  313. route = 'main.services.enableRMHighAvailability';
  314. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('rollbackHighAvailabilityWizardController.name')) {
  315. // if wizardControllerName == "highAvailabilityRollbackController", then it means someone closed the browser or the browser was crashed when we were last in NameNode High Availability Rollback wizard
  316. route = 'main.services.rollbackHighAvailability';
  317. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('mainAdminStackAndUpgradeController.name')) {
  318. // if wizardControllerName == "mainAdminStackAndUpgradeController", then it means someone closed the browser or the browser was crashed when we were last in Rolling Upgrade wizard
  319. route = 'main.admin.stackAndUpgrade';
  320. }
  321. callback(route);
  322. });
  323. } else {
  324. callback('installer');
  325. }
  326. }
  327. },
  328. logOff: function (context) {
  329. $('title').text(Em.I18n.t('app.name'));
  330. var hash = window.btoa(this.get('loginController.loginName') + ":" + this.get('loginController.password'));
  331. App.router.get('mainController').stopPolling();
  332. // App.db.cleanUp() must be called before router.clearAllSteps().
  333. // otherwise, this.set('installerController.currentStep, 0) would have no effect
  334. // since it's a computed property but we are not setting it as a dependent of App.db.
  335. App.db.cleanUp();
  336. App.set('isAdmin', false);
  337. App.set('isOperator', false);
  338. this.set('loggedIn', false);
  339. this.clearAllSteps();
  340. console.log("Log off: " + App.router.getClusterName());
  341. this.set('loginController.loginName', '');
  342. this.set('loginController.password', '');
  343. // 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.
  344. if (!App.get('testMode') && context) {
  345. App.ajax.send({
  346. name: 'router.logoff',
  347. sender: this,
  348. data: {
  349. auth: "Basic " + hash
  350. },
  351. beforeSend: 'authBeforeSend',
  352. success: 'logOffSuccessCallback',
  353. error:'logOffErrorCallback'
  354. });
  355. }
  356. if (App.router.get('clusterController.isLoaded')) {
  357. window.location.reload();
  358. } else {
  359. this.transitionTo('login', context);
  360. }
  361. },
  362. logOffSuccessCallback: function (data) {
  363. console.log("invoked logout on the server successfully");
  364. var applicationController = App.router.get('applicationController');
  365. applicationController.set('isPollerRunning',false);
  366. },
  367. logOffErrorCallback: function (req) {
  368. console.log("failed to invoke logout on the server");
  369. },
  370. /**
  371. * initialize isAdmin if user is administrator
  372. */
  373. initAdmin: function(){
  374. if (App.db) {
  375. var user = App.db.getUser();
  376. if (user) {
  377. if (user.admin) {
  378. App.set('isAdmin', true);
  379. console.log('Administrator logged in');
  380. }
  381. if (user.operator) {
  382. App.set('isOperator', true);
  383. }
  384. }
  385. }
  386. },
  387. root: Em.Route.extend({
  388. index: Em.Route.extend({
  389. route: '/',
  390. redirectsTo: 'login'
  391. }),
  392. enter: function(router){
  393. router.initAdmin();
  394. },
  395. login: Em.Route.extend({
  396. route: '/login',
  397. /**
  398. * If the user is already logged in, redirect to where the user was previously
  399. */
  400. enter: function (router, context) {
  401. router.getAuthenticated().done(function (loggedIn) {
  402. if (loggedIn) {
  403. Ember.run.next(function () {
  404. console.log(router.getLoginName() + ' already authenticated. Redirecting...');
  405. router.getSection(function (route) {
  406. router.transitionTo(route, context);
  407. });
  408. });
  409. }
  410. });
  411. },
  412. connectOutlets: function (router, context) {
  413. $('title').text(Em.I18n.t('app.name'));
  414. console.log('/login:connectOutlet');
  415. console.log('currentStep is: ' + router.getInstallerCurrentStep());
  416. console.log('authenticated is: ' + router.getAuthenticated());
  417. router.get('applicationController').connectOutlet('login');
  418. }
  419. }),
  420. installer: require('routes/installer'),
  421. main: require('routes/main'),
  422. adminView: Em.Route.extend({
  423. route: '/adminView',
  424. enter: function (router) {
  425. if (!router.get('loggedIn') || !App.isAccessible('upgrade_ADMIN') || App.isAccessible('upgrade_OPERATOR')) {
  426. Em.run.next(function () {
  427. router.transitionTo('login');
  428. });
  429. } else {
  430. window.location.replace('/views/ADMIN_VIEW/2.0.0/INSTANCE/#/');
  431. }
  432. }
  433. }),
  434. experimental: Em.Route.extend({
  435. route: '/experimental',
  436. enter: function (router, context) {
  437. if (App.isAccessible('upgrade_OPERATOR')) {
  438. Em.run.next(function () {
  439. if (router.get('clusterInstallCompleted')) {
  440. router.transitionTo("main.dashboard.widgets");
  441. } else {
  442. router.route("installer");
  443. }
  444. });
  445. } else if (!App.isAccessible('upgrade_ADMIN')) {
  446. Em.run.next(function () {
  447. router.transitionTo("main.views.index");
  448. });
  449. }
  450. },
  451. connectOutlets: function (router, context) {
  452. if (App.isAccessible('upgrade_ONLY_ADMIN')) {
  453. $('title').text(Em.I18n.t('app.name.subtitle.experimental'));
  454. console.log('/experimental:connectOutlet');
  455. router.get('applicationController').connectOutlet('experimental');
  456. }
  457. }
  458. }),
  459. logoff: function (router, context) {
  460. router.logOff(context);
  461. }
  462. })
  463. });