router.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 (postLogin) {
  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 router = this;
  122. var url = '';
  123. if (App.testMode) {
  124. if (loginName === "admin" && controller.get('password') === 'admin') {
  125. url = '/data/users/user_admin.json';
  126. } else if (loginName === 'user' && controller.get('password') === 'user') {
  127. url = '/data/users/user_user.json';
  128. }
  129. }
  130. $.ajax({
  131. url: (App.testMode) ? url : App.apiPrefix + '/users/' + loginName,
  132. dataType: 'json',
  133. type: 'GET',
  134. beforeSend: function (xhr) {
  135. xhr.setRequestHeader("Authorization", "Basic " + hash);
  136. },
  137. statusCode: {
  138. 200: function () {
  139. console.log("Status code 200: Success.");
  140. },
  141. 401: function () {
  142. console.log("Error code 401: Unauthorized.");
  143. },
  144. 403: function () {
  145. console.log("Error code 403: Forbidden.");
  146. }
  147. },
  148. success: function (data) {
  149. console.log('login success');
  150. var resp = data;
  151. var isAdmin = resp.Users.roles.indexOf('admin') >= 0;
  152. if (isAdmin) {
  153. router.setAuthenticated(true);
  154. router.setLoginName(loginName);
  155. App.usersMapper.map({"items": [data]});
  156. router.setUser(App.User.find(loginName));
  157. router.transitionTo(router.getSection());
  158. postLogin(true);
  159. } else {
  160. $.ajax({
  161. url: (App.testMode) ? '/data/clusters/info.json' : App.apiPrefix + '/clusters',
  162. dataType: 'text',
  163. type: 'GET',
  164. success: function (data) {
  165. var clusterResp = $.parseJSON(data);
  166. if (clusterResp.items.length) {
  167. router.setAuthenticated(true);
  168. router.setLoginName(loginName);
  169. App.usersMapper.map({"items": [resp]});
  170. router.setUser(App.User.find(loginName));
  171. router.transitionTo(router.getSection());
  172. postLogin(true);
  173. } else {
  174. controller.set('errorMessage', Em.I18n.t('router.hadoopClusterNotSetUp'));
  175. }
  176. },
  177. error: function (req) {
  178. console.log("Server not responding: " + req.statusCode);
  179. }
  180. });
  181. }
  182. },
  183. error: function (req) {
  184. console.log("login error: " + req.statusCode);
  185. router.setAuthenticated(false);
  186. postLogin(false);
  187. }
  188. });
  189. },
  190. setAmbariStacks: function () {
  191. var self = this;
  192. var method = 'GET';
  193. var url = (App.testMode) ? '/data/wizard/stack/stacks.json' : App.apiPrefix + '/stacks';
  194. $.ajax({
  195. type: method,
  196. url: url,
  197. async: false,
  198. dataType: 'text',
  199. timeout: App.timeout,
  200. success: function (data) {
  201. var jsonData = jQuery.parseJSON(data);
  202. console.log("TRACE: In success function for the setAmbariStacks call");
  203. console.log("TRACE: value of the url is: " + url);
  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. error: function (request, ajaxOptions, error) {
  215. console.log("TRACE: In error function for the setAmbariStacks call");
  216. console.log("TRACE: value of the url is: " + url);
  217. console.log("TRACE: error code status is: " + request.status);
  218. console.log('Error message is: ' + request.responseText);
  219. },
  220. statusCode: require('data/statusCodes')
  221. });
  222. },
  223. getSection: function () {
  224. if (App.testMode) {
  225. if (App.alwaysGoToInstaller) {
  226. return 'installer';
  227. } else {
  228. return 'main.index';
  229. }
  230. }
  231. App.clusterStatus.updateFromServer();
  232. var clusterStatusOnServer = App.clusterStatus.get('value');
  233. 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')) {
  234. return 'main.index';
  235. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('addHostController.name')) {
  236. // if wizardControllerName == "addHostController", then it means someone closed the browser or the browser was crashed when we were last in Add Hosts wizard
  237. return 'main.hostAdd';
  238. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('addServiceController.name')) {
  239. // if wizardControllerName == "addHostController", then it means someone closed the browser or the browser was crashed when we were last in Add Hosts wizard
  240. return 'main.serviceAdd';
  241. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('stackUpgradeController.name')) {
  242. // if wizardControllerName == "stackUpgradeController", then it means someone closed the browser or the browser was crashed when we were last in Stack Upgrade wizard
  243. return 'main.stackUpgrade';
  244. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('reassignMasterController.name')) {
  245. // if wizardControllerName == "reassignMasterController", then it means someone closed the browser or the browser was crashed when we were last in Reassign Master wizard
  246. return 'main.reassignMaster';
  247. } else {
  248. // if wizardControllerName == "installerController", then it means someone closed the browser or the browser was crashed when we were last in Installer wizard
  249. return 'installer';
  250. }
  251. },
  252. logOff: function (context) {
  253. $('title').text('Ambari');
  254. var hash = window.btoa(this.get('loginController.loginName') + ":" + this.get('loginController.password'));
  255. App.router.get('mainController').stopPolling();
  256. // App.db.cleanUp() must be called before router.clearAllSteps().
  257. // otherwise, this.set('installerController.currentStep, 0) would have no effect
  258. // since it's a computed property but we are not setting it as a dependent of App.db.
  259. App.db.cleanUp();
  260. App.set('isAdmin', false);
  261. this.clearAllSteps();
  262. console.log("Log off: " + App.router.getClusterName());
  263. this.set('loginController.loginName', '');
  264. this.set('loginController.password', '');
  265. if (!App.testMode) {
  266. $.ajax({
  267. url: App.apiPrefix + '/logout',
  268. dataType: 'json',
  269. type: 'GET',
  270. beforeSend: function (xhr) {
  271. xhr.setRequestHeader("Authorization", "Basic " + hash);
  272. },
  273. statusCode: {
  274. 200: function () {
  275. console.log("Status code 200: Success.");
  276. },
  277. 401: function () {
  278. console.log("Error code 401: Unauthorized.");
  279. },
  280. 403: function () {
  281. console.log("Error code 403: Forbidden.");
  282. }
  283. },
  284. success: function (data) {
  285. console.log("invoked logout on the server successfully");
  286. },
  287. error: function (data) {
  288. console.log("failed to invoke logout on the server");
  289. },
  290. complete: function () {
  291. console.log('done');
  292. }
  293. });
  294. }
  295. this.transitionTo('login', context);
  296. },
  297. root: Em.Route.extend({
  298. index: Em.Route.extend({
  299. route: '/',
  300. redirectsTo: 'login'
  301. }),
  302. login: Em.Route.extend({
  303. route: '/login',
  304. /**
  305. * If the user is already logged in, redirect to where the user was previously
  306. */
  307. enter: function (router, context) {
  308. if (router.getAuthenticated()) {
  309. Ember.run.next(function () {
  310. console.log(router.getLoginName() + ' already authenticated. Redirecting...');
  311. router.transitionTo(router.getSection(), context);
  312. });
  313. }
  314. },
  315. connectOutlets: function (router, context) {
  316. $('title').text(Em.I18n.t('app.name'));
  317. console.log('/login:connectOutlet');
  318. console.log('currentStep is: ' + router.getInstallerCurrentStep());
  319. console.log('authenticated is: ' + router.getAuthenticated());
  320. router.get('applicationController').connectOutlet('login');
  321. }
  322. }),
  323. installer: require('routes/installer'),
  324. main: require('routes/main'),
  325. logoff: function (router, context) {
  326. router.logOff(context);
  327. }
  328. })
  329. })