router.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. // TODO: this needs to be hooked up with server authentication
  72. // this.authenticated();
  73. var auth = App.db.getAuthenticated();
  74. var authResp = (auth && auth === true);
  75. this.set('loggedIn', authResp);
  76. return authResp;
  77. },
  78. setAuthenticated: function (authenticated) {
  79. // TODO: this needs to be hooked up with server authentication
  80. console.log("TRACE: Entering router:setAuthenticated function");
  81. App.db.setAuthenticated(authenticated);
  82. this.set('loggedIn', authenticated);
  83. },
  84. getLoginName: function () {
  85. // TODO: this needs to be hooked up with server authentication
  86. return App.db.getLoginName();
  87. },
  88. setLoginName: function (loginName) {
  89. // TODO: this needs to be hooked up with server authentication
  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(loginName === "admin" && controller.get('password') === 'admin')
  124. {
  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. $.ajax({
  130. url : (App.testMode) ? url : App.apiPrefix + '/users/' + loginName ,
  131. dataType : 'json',
  132. type: 'GET',
  133. beforeSend: function (xhr) {
  134. xhr.setRequestHeader("Authorization", "Basic " + hash);
  135. },
  136. statusCode: {
  137. 200: function () {
  138. console.log("Status code 200: Success.");
  139. },
  140. 401: function () {
  141. console.log("Error code 401: Unauthorized.");
  142. },
  143. 403: function () {
  144. console.log("Error code 403: Forbidden.");
  145. }
  146. },
  147. success: function (data) {
  148. console.log('login success');
  149. var resp = data;
  150. var isAdmin = resp.Users.roles.indexOf('admin') >= 0;
  151. if(isAdmin){
  152. router.setAuthenticated(true);
  153. router.setLoginName(loginName);
  154. App.usersMapper.map({"items":[data]});
  155. router.setUser(App.User.find(loginName));
  156. router.transitionTo(router.getSection());
  157. postLogin(true);
  158. } else {
  159. $.ajax({
  160. url: (App.testMode) ? '/data/clusters/info.json' : App.apiPrefix + '/clusters',
  161. dataType: 'text',
  162. type: 'GET',
  163. success: function (data) {
  164. var clusterResp = $.parseJSON(data);
  165. if (clusterResp.items.length) {
  166. router.setAuthenticated(true);
  167. router.setLoginName(loginName);
  168. App.usersMapper.map({"items":[resp]});
  169. router.setUser(App.User.find(loginName));
  170. router.transitionTo(router.getSection());
  171. postLogin(true);
  172. } else {
  173. controller.set('errorMessage', "Your administrator has not set up a Hadoop cluster yet.");
  174. }
  175. },
  176. error: function (req) {
  177. console.log("Server not responding: " + req.statusCode);
  178. }
  179. });
  180. }
  181. },
  182. error: function (req) {
  183. console.log("login error: " + req.statusCode);
  184. router.setAuthenticated(false);
  185. postLogin(false);
  186. }
  187. });
  188. },
  189. setAmbariStacks: function () {
  190. var self = this;
  191. var method = 'GET';
  192. var url = (App.testMode) ? '/data/wizard/stack/stacks.json' : App.apiPrefix + '/stacks';
  193. $.ajax({
  194. type: method,
  195. url: url,
  196. async: false,
  197. dataType: 'text',
  198. timeout: App.timeout,
  199. success: function (data) {
  200. var jsonData = jQuery.parseJSON(data);
  201. console.log("TRACE: In success function for the setAmbariStacks call");
  202. console.log("TRACE: value of the url is: " + url);
  203. var stacks = [];
  204. jsonData.forEach(function (_stack) {
  205. stacks.pushObject({
  206. name:_stack.name,
  207. version: _stack.version
  208. });
  209. }, this);
  210. App.db.setAmbariStacks(stacks);
  211. console.log('TRACEIINNGG: ambaristacks: ' + JSON.stringify(App.db.getAmbariStacks()));
  212. },
  213. error: function (request, ajaxOptions, error) {
  214. console.log("TRACE: In error function for the setAmbariStacks call");
  215. console.log("TRACE: value of the url is: " + url);
  216. console.log("TRACE: error code status is: " + request.status);
  217. console.log('Error message is: ' + request.responseText);
  218. },
  219. statusCode: require('data/statusCodes')
  220. });
  221. },
  222. getSection: function () {
  223. if (App.alwaysGoToInstaller) {
  224. return 'installer';
  225. }
  226. var clusterStatusOnServer = App.clusterStatus.get('value');
  227. if (clusterStatusOnServer && (clusterStatusOnServer.clusterState === 'CLUSTER_STARTED_5' || clusterStatusOnServer.clusterState === 'ADD_HOSTS_COMPLETED_5' )) {
  228. return 'main.index';
  229. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('addHostController.name')) {
  230. // if wizardControllerName == "addHostController", then it means someone closed the browser or the browser was crashed when we were last in Add Hosts wizard
  231. return 'main.hostAdd';
  232. } else {
  233. // if wizardControllerName == "installerController", then it means someone closed the browser or the browser was crashed when we were last in Installer wizard
  234. return 'installer';
  235. }
  236. },
  237. logOff: function(context){
  238. $('title').text('Ambari');
  239. var hash = window.btoa(this.get('loginController.loginName') + ":" + this.get('loginController.password'));
  240. App.router.get('mainController').stopPolling();
  241. // App.db.cleanUp() must be called before router.clearAllSteps().
  242. // otherwise, this.set('installerController.currentStep, 0) would have no effect
  243. // since it's a computed property but we are not setting it as a dependent of App.db.
  244. App.db.cleanUp();
  245. this.clearAllSteps();
  246. console.log("Log off: " + App.router.getClusterName());
  247. this.set('loginController.loginName', '');
  248. this.set('loginController.password', '');
  249. if (!App.testMode) {
  250. $.ajax({
  251. url: App.apiPrefix + '/logout',
  252. dataType: 'json',
  253. type: 'GET',
  254. beforeSend: function (xhr) {
  255. xhr.setRequestHeader("Authorization", "Basic " + hash);
  256. },
  257. statusCode: {
  258. 200: function () {
  259. console.log("Status code 200: Success.");
  260. },
  261. 401: function () {
  262. console.log("Error code 401: Unauthorized.");
  263. },
  264. 403: function () {
  265. console.log("Error code 403: Forbidden.");
  266. }
  267. },
  268. success: function (data) {
  269. console.log("invoked logout on the server successfully");
  270. },
  271. error: function (data) {
  272. console.log("failed to invoke logout on the server");
  273. },
  274. complete: function () {
  275. console.log('done');
  276. }
  277. });
  278. }
  279. this.transitionTo('login', context);
  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('Ambari');
  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', App.LoginView);
  305. }
  306. }),
  307. installer: require('routes/installer'),
  308. main: require('routes/main'),
  309. logoff: function (router, context) {
  310. router.logOff(context);
  311. }
  312. })
  313. })