router.js 11 KB

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