router.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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').toLowerCase();
  115. controller.set('loginName', loginName);
  116. var hash = window.btoa(loginName + ":" + controller.get('password'));
  117. var router = this;
  118. var url = '';
  119. if(loginName === "admin" && controller.get('password') === 'admin')
  120. {
  121. url = '/data/users/user_admin.json';
  122. }else if(loginName === 'user' && controller.get('password') === 'user'){
  123. url = '/data/users/user_user.json';
  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. var clusterController = App.router.get('clusterController');
  223. clusterController.loadClusterName(false);
  224. if (clusterController.get('clusterName')) {
  225. return 'main.index';
  226. } else {
  227. return 'installer';
  228. }
  229. },
  230. logOff: function(context){
  231. var hash = window.btoa(this.get('loginController.loginName') + ":" + this.get('loginController.password'));
  232. // App.db.cleanUp() must be called before router.clearAllSteps().
  233. // otherwise, this.set('installerController.currentStep, 0) would have no effect
  234. // since it's a computed property but we are not setting it as a dependent of App.db.
  235. App.db.cleanUp();
  236. this.clearAllSteps();
  237. console.log("Log off: " + App.router.getClusterName());
  238. this.set('loginController.loginName', '');
  239. this.set('loginController.password', '');
  240. if (!App.testMode) {
  241. $.ajax({
  242. url: App.apiPrefix + '/logout',
  243. dataType: 'json',
  244. type: 'GET',
  245. beforeSend: function (xhr) {
  246. xhr.setRequestHeader("Authorization", "Basic " + hash);
  247. },
  248. statusCode: {
  249. 200: function () {
  250. console.log("Status code 200: Success.");
  251. },
  252. 401: function () {
  253. console.log("Error code 401: Unauthorized.");
  254. },
  255. 403: function () {
  256. console.log("Error code 403: Forbidden.");
  257. }
  258. },
  259. success: function (data) {
  260. console.log("invoked logout on the server successfully");
  261. },
  262. error: function (data) {
  263. console.log("failed to invoke logout on the server");
  264. },
  265. complete: function () {
  266. console.log('done');
  267. }
  268. });
  269. }
  270. this.transitionTo('login', context);
  271. },
  272. root: Em.Route.extend({
  273. index: Em.Route.extend({
  274. route: '/',
  275. redirectsTo: 'login'
  276. }),
  277. login: Em.Route.extend({
  278. route: '/login',
  279. /**
  280. * If the user is already logged in, redirect to where the user was previously
  281. */
  282. enter: function (router, context) {
  283. if (router.getAuthenticated()) {
  284. Ember.run.next(function () {
  285. console.log(router.getLoginName() + ' already authenticated. Redirecting...');
  286. router.transitionTo(router.getSection(), context);
  287. });
  288. }
  289. },
  290. connectOutlets: function (router, context) {
  291. console.log('/login:connectOutlet');
  292. console.log('currentStep is: ' + router.getInstallerCurrentStep());
  293. console.log('authenticated is: ' + router.getAuthenticated());
  294. router.get('applicationController').connectOutlet('login', App.LoginView);
  295. }
  296. }),
  297. installer: require('routes/installer'),
  298. main: require('routes/main'),
  299. logoff: function (router, context) {
  300. router.logOff(context);
  301. }
  302. })
  303. })