router.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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.Router = Em.Router.extend({
  20. enableLogging: true,
  21. isFwdNavigation: true,
  22. backBtnForHigherStep: false,
  23. setNavigationFlow: function (step) {
  24. var matches = step.match(/\d+$/);
  25. var newStep;
  26. if (matches) {
  27. newStep = parseInt(matches[0]);
  28. }
  29. var previousStep = parseInt(this.getInstallerCurrentStep());
  30. this.set('isFwdNavigation', newStep >= previousStep);
  31. },
  32. clearAllSteps: function () {
  33. this.get('installerController').clear();
  34. this.get('addHostController').clear();
  35. this.get('addServiceController').clear();
  36. this.get('stackUpgradeController').clear();
  37. for (var i = 1; i < 11; i++) {
  38. this.set('wizardStep' + i + 'Controller.hasSubmitted', false);
  39. this.set('wizardStep' + i + 'Controller.isDisabled', true);
  40. }
  41. },
  42. /**
  43. * Temporary fix for getting cluster name
  44. * @return {*}
  45. */
  46. getClusterName: function () {
  47. return App.router.get('clusterController').get('clusterName');
  48. },
  49. /**
  50. * Get current step of Installer wizard
  51. * @return {*}
  52. */
  53. getInstallerCurrentStep: function () {
  54. return this.getWizardCurrentStep('installer');
  55. },
  56. /**
  57. * Get current step for <code>wizardType</code> wizard
  58. * @param wizardType one of <code>installer</code>, <code>addHost</code>, <code>addServices</code>
  59. */
  60. getWizardCurrentStep: function (wizardType) {
  61. var loginName = this.getLoginName();
  62. var currentStep = App.db.getWizardCurrentStep(wizardType);
  63. console.log('getWizardCurrentStep: loginName=' + loginName + ", currentStep=" + currentStep);
  64. if (!currentStep) {
  65. currentStep = wizardType === 'installer' ? '0' : '1';
  66. }
  67. console.log('returning currentStep=' + currentStep);
  68. return currentStep;
  69. },
  70. loggedIn: false,
  71. loginName: function() {
  72. return this.getLoginName();
  73. }.property('loggedIn'),
  74. getAuthenticated: function () {
  75. var auth = App.db.getAuthenticated();
  76. var authResp = (auth && auth === true);
  77. if (authResp) {
  78. App.ajax.send({
  79. name: 'router.authentication',
  80. sender: this,
  81. success: 'onAuthenticationSuccess',
  82. error: 'onAuthenticationError'
  83. });
  84. } else {
  85. this.set('loggedIn', false);
  86. }
  87. return this.get('loggedIn');
  88. },
  89. onAuthenticationSuccess: function (data) {
  90. this.set('loggedIn', true);
  91. },
  92. onAuthenticationError: function (data) {
  93. if (data.status === 403) {
  94. this.set('loggedIn', false);
  95. } else {
  96. console.log('error in getAuthenticated');
  97. }
  98. },
  99. setAuthenticated: function (authenticated) {
  100. console.log("TRACE: Entering router:setAuthenticated function");
  101. App.db.setAuthenticated(authenticated);
  102. this.set('loggedIn', authenticated);
  103. },
  104. getLoginName: function () {
  105. return App.db.getLoginName();
  106. },
  107. setLoginName: function (loginName) {
  108. App.db.setLoginName(loginName);
  109. },
  110. /**
  111. * Set user model to local storage
  112. * @param user
  113. */
  114. setUser: function (user) {
  115. App.db.setUser(user);
  116. },
  117. /**
  118. * Get user model from local storage
  119. * @return {*}
  120. */
  121. getUser: function () {
  122. return App.db.getUser();
  123. },
  124. login: function () {
  125. var controller = this.get('loginController');
  126. var loginName = controller.get('loginName').toLowerCase();
  127. controller.set('loginName', loginName);
  128. var hash = window.btoa(loginName + ":" + controller.get('password'));
  129. var usr = '';
  130. if (App.testMode) {
  131. if (loginName === "admin" && controller.get('password') === 'admin') {
  132. usr = 'admin';
  133. } else if (loginName === 'user' && controller.get('password') === 'user') {
  134. usr = 'user';
  135. }
  136. }
  137. App.ajax.send({
  138. name: 'router.login',
  139. sender: this,
  140. data: {
  141. auth: "Basic " + hash,
  142. usr: usr,
  143. loginName: loginName
  144. },
  145. beforeSend: 'authBeforeSend',
  146. success: 'loginSuccessCallback',
  147. error: 'loginErrorCallback'
  148. });
  149. },
  150. authBeforeSend: function(opt, xhr, data) {
  151. xhr.setRequestHeader("Authorization", data.auth);
  152. },
  153. loginSuccessCallback: function(data, opt, params) {
  154. console.log('login success');
  155. var d = data;
  156. var isAdmin = data.Users.roles.indexOf('admin') >= 0;
  157. if (isAdmin) {
  158. App.set('isAdmin', true);
  159. var controller = this.get('loginController');
  160. this.setAuthenticated(true);
  161. this.setLoginName(params.loginName);
  162. App.usersMapper.map({"items": [data]});
  163. this.setUser(App.User.find(params.loginName));
  164. this.transitionTo(this.getSection());
  165. controller.postLogin(true);
  166. }
  167. else {
  168. App.ajax.send({
  169. name: 'router.login2',
  170. sender: this,
  171. data: {
  172. loginName: params.loginName,
  173. loginData: data
  174. },
  175. success: 'login2SuccessCallback',
  176. error: 'login2ErrorCallback'
  177. });
  178. }
  179. },
  180. loginErrorCallback: function(request, ajaxOptions, error, opt) {
  181. var controller = this.get('loginController');
  182. console.log("login error: " + error);
  183. this.setAuthenticated(false);
  184. controller.postLogin(false);
  185. },
  186. login2SuccessCallback: function (clusterResp, opt, params) {
  187. var controller = this.get('loginController');
  188. if (clusterResp.items.length) {
  189. this.setAuthenticated(true);
  190. this.setLoginName(params.loginName);
  191. App.usersMapper.map({"items": [params.loginData]});
  192. this.setUser(App.User.find(params.loginName));
  193. this.transitionTo(this.getSection());
  194. controller.postLogin(true);
  195. }
  196. else {
  197. controller.set('errorMessage', Em.I18n.t('router.hadoopClusterNotSetUp'));
  198. }
  199. },
  200. login2ErrorCallback: function (req) {
  201. console.log("Server not responding: " + req.statusCode);
  202. },
  203. setAmbariStacks: function () {
  204. App.ajax.send({
  205. name: 'router.set_ambari_stacks',
  206. sender: this,
  207. success: 'setAmbariStacksSuccessCallback',
  208. error: 'setAmbariStacksErrorCallback'
  209. });
  210. },
  211. setAmbariStacksSuccessCallback: function (jsonData) {
  212. console.log("TRACE: In success function for the setAmbariStacks call");
  213. var stacks = [];
  214. jsonData.forEach(function (_stack) {
  215. stacks.pushObject({
  216. name: _stack.name,
  217. version: _stack.version
  218. });
  219. }, this);
  220. App.db.setAmbariStacks(stacks);
  221. console.log('TRACEIINNGG: ambaristacks: ' + JSON.stringify(App.db.getAmbariStacks()));
  222. },
  223. setAmbariStacksErrorCallback: function (request, ajaxOptions, error) {
  224. console.log("TRACE: In error function for the setAmbariStacks call");
  225. console.log("TRACE: error code status is: " + request.status);
  226. console.log('Error message is: ' + request.responseText);
  227. },
  228. getSection: function () {
  229. if (App.testMode) {
  230. if (App.alwaysGoToInstaller) {
  231. return 'installer';
  232. } else {
  233. return 'main.index';
  234. }
  235. }
  236. App.clusterStatus.updateFromServer(false, false);
  237. var clusterStatusOnServer = App.clusterStatus.get('value');
  238. if (!App.get('isAdmin') || clusterStatusOnServer && clusterStatusOnServer.clusterState === 'DEFAULT') {
  239. return 'main.index';
  240. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('addHostController.name')) {
  241. // if wizardControllerName == "addHostController", then it means someone closed the browser or the browser was crashed when we were last in Add Hosts wizard
  242. return 'main.hostAdd';
  243. } else if (clusterStatusOnServer && (clusterStatusOnServer.wizardControllerName === App.router.get('addSecurityController.name') || clusterStatusOnServer.wizardControllerName === App.router.get('mainAdminSecurityDisableController.name'))) {
  244. // if wizardControllerName == "addSecurityController", then it means someone closed the browser or the browser was crashed when we were last in Add Security wizard
  245. return 'main.admin.adminSecurity';
  246. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('addServiceController.name')) {
  247. // if wizardControllerName == "addHostController", then it means someone closed the browser or the browser was crashed when we were last in Add Hosts wizard
  248. return 'main.serviceAdd';
  249. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('stackUpgradeController.name')) {
  250. // if wizardControllerName == "stackUpgradeController", then it means someone closed the browser or the browser was crashed when we were last in Stack Upgrade wizard
  251. return 'main.stackUpgrade';
  252. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('reassignMasterController.name')) {
  253. // if wizardControllerName == "reassignMasterController", then it means someone closed the browser or the browser was crashed when we were last in Reassign Master wizard
  254. return 'main.services.reassign';
  255. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('highAvailabilityWizardController.name')) {
  256. // if wizardControllerName == "highAvailabilityWizardController", then it means someone closed the browser or the browser was crashed when we were last in NameNode High Availability wizard
  257. return 'main.admin.enableHighAvailability';
  258. }else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('rollbackHighAvailabilityWizardController.name')) {
  259. // 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
  260. return 'main.admin.rollbackHighAvailability';
  261. } else {
  262. // if wizardControllerName == "installerController", then it means someone closed the browser or the browser was crashed when we were last in Installer wizard
  263. return 'installer';
  264. }
  265. },
  266. logOff: function (context) {
  267. $('title').text('Ambari');
  268. var hash = window.btoa(this.get('loginController.loginName') + ":" + this.get('loginController.password'));
  269. App.router.get('mainController').stopPolling();
  270. // App.db.cleanUp() must be called before router.clearAllSteps().
  271. // otherwise, this.set('installerController.currentStep, 0) would have no effect
  272. // since it's a computed property but we are not setting it as a dependent of App.db.
  273. App.db.cleanUp();
  274. App.set('isAdmin', false);
  275. this.set('loggedIn', false);
  276. this.clearAllSteps();
  277. console.log("Log off: " + App.router.getClusterName());
  278. this.set('loginController.loginName', '');
  279. this.set('loginController.password', '');
  280. // 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.
  281. if (!App.testMode && context) {
  282. App.ajax.send({
  283. name: 'router.logoff',
  284. sender: this,
  285. data: {
  286. auth: "Basic " + hash
  287. },
  288. beforeSend: 'authBeforeSend',
  289. success: 'logOffSuccessCallback',
  290. error:'logOffErrorCallback'
  291. });
  292. }
  293. this.transitionTo('login', context);
  294. },
  295. logOffSuccessCallback: function (data) {
  296. console.log("invoked logout on the server successfully");
  297. },
  298. logOffErrorCallback: function (req) {
  299. console.log("failed to invoke logout on the server");
  300. },
  301. /**
  302. * initialize isAdmin if user is administrator
  303. */
  304. initAdmin: function(){
  305. if(App.db && App.db.getUser() && App.db.getUser().admin) {
  306. App.set('isAdmin', true);
  307. console.log('Administrator logged in');
  308. }
  309. },
  310. root: Em.Route.extend({
  311. index: Em.Route.extend({
  312. route: '/',
  313. redirectsTo: 'login'
  314. }),
  315. enter: function(router){
  316. router.initAdmin();
  317. },
  318. login: Em.Route.extend({
  319. route: '/login',
  320. /**
  321. * If the user is already logged in, redirect to where the user was previously
  322. */
  323. enter: function (router, context) {
  324. if (router.getAuthenticated()) {
  325. Ember.run.next(function () {
  326. console.log(router.getLoginName() + ' already authenticated. Redirecting...');
  327. router.transitionTo(router.getSection(), context);
  328. });
  329. }
  330. },
  331. connectOutlets: function (router, context) {
  332. $('title').text(Em.I18n.t('app.name'));
  333. console.log('/login:connectOutlet');
  334. console.log('currentStep is: ' + router.getInstallerCurrentStep());
  335. console.log('authenticated is: ' + router.getAuthenticated());
  336. router.get('applicationController').connectOutlet('login');
  337. }
  338. }),
  339. installer: require('routes/installer'),
  340. main: require('routes/main'),
  341. experimental: Em.Route.extend({
  342. route: '/experimental',
  343. enter: function (router, context) {
  344. },
  345. connectOutlets: function (router, context) {
  346. $('title').text("Ambari Experimental");
  347. console.log('/experimental:connectOutlet');
  348. router.get('applicationController').connectOutlet('experimental');
  349. }
  350. }),
  351. logoff: function (router, context) {
  352. router.logOff(context);
  353. }
  354. })
  355. });