router.js 11 KB

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