router.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 : '/api/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. router.setAuthenticated(true);
  140. router.setLoginName(loginName);
  141. router.setUser(App.store.createRecord(App.User, { userName: loginName, admin: isAdmin }));
  142. router.transitionTo(router.getSection());
  143. postLogin(true);
  144. },
  145. error: function (req) {
  146. console.log("login error: " + req.statusCode);
  147. router.setAuthenticated(false);
  148. postLogin(false);
  149. }
  150. });
  151. },
  152. setAmbariStacks: function () {
  153. var self = this;
  154. var method = 'GET';
  155. var url = (App.testMode) ? '/data/wizard/stack/stacks.json' : '/api/stacks';
  156. $.ajax({
  157. type: method,
  158. url: url,
  159. async: false,
  160. dataType: 'text',
  161. timeout: 5000,
  162. success: function (data) {
  163. var jsonData = jQuery.parseJSON(data);
  164. console.log("TRACE: In success function for the setAmbariStacks call");
  165. console.log("TRACE: value of the url is: " + url);
  166. var stacks = [];
  167. jsonData.forEach(function (_stack) {
  168. stacks.pushObject({
  169. name:_stack.name,
  170. version: _stack.version
  171. });
  172. }, this);
  173. App.db.setAmbariStacks(stacks);
  174. console.log('TRACEIINNGG: ambaristacks: ' + JSON.stringify(App.db.getAmbariStacks()));
  175. },
  176. error: function (request, ajaxOptions, error) {
  177. console.log("TRACE: In error function for the setAmbariStacks call");
  178. console.log("TRACE: value of the url is: " + url);
  179. console.log("TRACE: error code status is: " + request.status);
  180. console.log('Error message is: ' + request.responseText);
  181. },
  182. statusCode: require('data/statusCodes')
  183. });
  184. },
  185. mockLogin: function (postLogin) {
  186. var controller = this.get('loginController');
  187. var loginName = controller.get('loginName');
  188. if ((loginName === 'admin' && controller.get('password') === 'admin') ||
  189. (loginName === 'user' && controller.get('password') === 'user')) {
  190. this.setAuthenticated(true);
  191. this.setLoginName(loginName);
  192. this.setUser(App.store.createRecord(App.User, { userName: loginName, admin: loginName === 'admin' }));
  193. this.setAmbariStacks();
  194. this.transitionTo(this.getSection());
  195. postLogin(true);
  196. } else {
  197. this.setAuthenticated(false);
  198. postLogin(false);
  199. }
  200. },
  201. getSection: function () {
  202. var clusterController = App.router.get('clusterController');
  203. clusterController.loadClusterName(false);
  204. if (clusterController.get('clusterName')) {
  205. return 'main.index';
  206. } else {
  207. return 'installer';
  208. }
  209. },
  210. root: Em.Route.extend({
  211. index: Em.Route.extend({
  212. route: '/',
  213. redirectsTo: 'login'
  214. }),
  215. login: Em.Route.extend({
  216. route: '/login',
  217. /**
  218. * If the user is already logged in, redirect to where the user was previously
  219. */
  220. enter: function (router, context) {
  221. if (router.getAuthenticated()) {
  222. Ember.run.next(function () {
  223. console.log(router.getLoginName() + ' already authenticated. Redirecting...');
  224. router.transitionTo(router.getSection(), context);
  225. });
  226. }
  227. },
  228. connectOutlets: function (router, context) {
  229. console.log('/login:connectOutlet');
  230. console.log('currentStep is: ' + router.getInstallerCurrentStep());
  231. console.log('authenticated is: ' + router.getAuthenticated());
  232. router.get('applicationController').connectOutlet('login', App.LoginView);
  233. }
  234. }),
  235. installer: require('routes/installer'),
  236. main: require('routes/main'),
  237. logoff: function (router, context) {
  238. console.log('logging off');
  239. // App.db.cleanUp() must be called before router.clearAllSteps().
  240. // otherwise, this.set('installerController.currentStep, 0) would have no effect
  241. // since it's a computed property but we are not setting it as a dependent of App.db.
  242. App.db.cleanUp();
  243. router.clearAllSteps();
  244. console.log("Log off: " + App.db.getClusterName());
  245. router.set('loginController.loginName', '');
  246. router.set('loginController.password', '');
  247. router.transitionTo('login', context);
  248. }
  249. })
  250. })