router.js 11 KB

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