router.js 12 KB

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