router.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. /*var totalSteps = 10
  34. for (var step = 1; step <= totalSteps; step++){
  35. this.get('installerStep' + step + 'Controller').clearStep();
  36. }*/
  37. },
  38. /*
  39. loadAllPriorSteps: function(step) {
  40. var stepVal = parseInt(step);
  41. switch(step){
  42. case '10':
  43. this.get('installerStep9Controller').loadStep();
  44. case '9':
  45. this.get('installerStep8Controller').loadStep();
  46. case '8':
  47. this.get('installerStep7Controller').loadStep();
  48. case '7':
  49. this.get('installerStep6Controller').loadStep();
  50. case '6':
  51. this.get('installerStep5Controller').loadStep();
  52. case '5':
  53. this.get('installerStep4Controller').loadStep();
  54. case '4':
  55. this.get('installerStep3Controller').loadStep();
  56. case '3':
  57. this.get('installerStep2Controller').loadStep();
  58. case '2':
  59. this.get('installerStep1Controller').loadStep();
  60. case '1':
  61. }
  62. },
  63. */
  64. setInstallerCurrentStep: function (currentStep, completed) {
  65. App.db.setInstallerCurrentStep(currentStep, completed);
  66. this.set('installerController.currentStep', currentStep);
  67. },
  68. getInstallerCurrentStep: function () {
  69. var loginName = this.getLoginName();
  70. var currentStep = App.db.getInstallerCurrentStep();
  71. console.log('getInstallerCurrentStep: loginName=' + loginName + ", currentStep=" + currentStep);
  72. if (!currentStep) {
  73. currentStep = '1';
  74. }
  75. console.log('returning currentStep=' + currentStep);
  76. return currentStep;
  77. },
  78. /**
  79. * Get current step for <code>wizardType</code> wizard
  80. * @param wizardType one of <code>installer</code>, <code>addHost</code>, <code>addServices</code>
  81. */
  82. getWizardCurrentStep: function (wizardType) {
  83. var loginName = this.getLoginName();
  84. var currentStep = App.db.getWizardCurrentStep(wizardType);
  85. console.log('getWizardCurrentStep: loginName=' + loginName + ", currentStep=" + currentStep);
  86. if (!currentStep) {
  87. currentStep = '1';
  88. }
  89. console.log('returning currentStep=' + currentStep);
  90. return currentStep;
  91. },
  92. loggedIn: false,
  93. getAuthenticated: function () {
  94. // TODO: this needs to be hooked up with server authentication
  95. // this.authenticated();
  96. var auth = App.db.getAuthenticated();
  97. var authResp = (auth && auth === true);
  98. this.set('loggedIn', authResp);
  99. return authResp;
  100. },
  101. setAuthenticated: function (authenticated) {
  102. // TODO: this needs to be hooked up with server authentication
  103. console.log("TRACE: Entering router:setAuthenticated function");
  104. App.db.setAuthenticated(authenticated);
  105. this.set('loggedIn', authenticated);
  106. },
  107. getLoginName: function () {
  108. // TODO: this needs to be hooked up with server authentication
  109. return App.db.getLoginName();
  110. },
  111. setLoginName: function (loginName) {
  112. // TODO: this needs to be hooked up with server authentication
  113. App.db.setLoginName(loginName);
  114. },
  115. // that works incorrectly
  116. setUser: function (user) {
  117. App.db.setUser(user);
  118. },
  119. // that works incorrectly
  120. getUser: function () {
  121. return App.db.getUser();
  122. },
  123. resetAuth: function (authenticated) {
  124. if (!authenticated){
  125. App.db.cleanUp();
  126. this.set('loggedIn', false);
  127. this.set('loginController.loginName', '');
  128. this.set('loginController.password', '');
  129. this.transitionTo('login');
  130. }
  131. return authenticated;
  132. },
  133. login: function (postLogin) {
  134. var controller = this.get('loginController');
  135. var hash = window.btoa(controller.get('loginName') + ":" + controller.get('password'));
  136. var router = this;
  137. $.ajax({
  138. url : '/api/check',
  139. dataType : 'json',
  140. type: 'GET',
  141. beforeSend: function(xhr) {
  142. xhr.setRequestHeader("Authorization", "Basic " + hash);
  143. },
  144. statusCode: {
  145. 200: function() {
  146. console.log('Authorization status: 200');
  147. },
  148. 401: function() {
  149. console.log('Authorization status: 401');
  150. },
  151. 403: function(){
  152. console.log('Authorization status: 403');
  153. }
  154. },
  155. success: function (data) {
  156. console.log('login success');
  157. router.setAuthenticated(true);
  158. router.setLoginName(loginName);
  159. // TODO: set real usr
  160. router.setUser(App.User.find(1));
  161. router.transitionTo(this.getSection());
  162. postLogin(true);
  163. },
  164. error: function (req) {
  165. console.log("login error: " + req.statusText);
  166. router.setAuthenticated(false);
  167. postLogin(false);
  168. }
  169. });
  170. },
  171. mockLogin: function (postLogin) {
  172. var controller = this.get('loginController');
  173. if (controller.get('loginName') === 'admin' && controller.get('password') === 'admin') {
  174. this.setAuthenticated(true);
  175. this.setLoginName('admin');
  176. this.setUser(App.User.find(1));
  177. this.transitionTo(this.getSection());
  178. postLogin(true);
  179. } else {
  180. this.setAuthenticated(false);
  181. postLogin(false);
  182. }
  183. },
  184. defaultSection: 'installer',
  185. getSection: function () {
  186. var section = App.db.getSection();
  187. console.log("The section is: " + section);
  188. var section = localStorage.getItem(this.getLoginName() + 'section');
  189. return section || this.defaultSection;
  190. },
  191. setSection: function (section) {
  192. App.db.setSection(section);
  193. },
  194. root: Em.Route.extend({
  195. index: Em.Route.extend({
  196. route: '/',
  197. redirectsTo: 'login'
  198. }),
  199. login: Em.Route.extend({
  200. route: '/login',
  201. /**
  202. * If the user is already logged in, redirect to where the user was previously
  203. */
  204. enter: function (router, context) {
  205. if (router.getAuthenticated()) {
  206. Ember.run.next(function () {
  207. console.log(router.getLoginName() + ' already authenticated. Redirecting...');
  208. router.transitionTo(router.getSection(), context);
  209. });
  210. }
  211. },
  212. connectOutlets: function (router, context) {
  213. console.log('/login:connectOutlet');
  214. console.log('currentStep is: ' + router.getInstallerCurrentStep());
  215. console.log('authenticated is: ' + router.getAuthenticated());
  216. router.get('applicationController').connectOutlet('login', App.LoginView);
  217. }
  218. }),
  219. installer: require('routes/installer'),
  220. main: require('routes/main'),
  221. logoff: function (router, context) {
  222. console.log('logging off');
  223. router.clearAllSteps();
  224. App.db.cleanUp();
  225. router.set('loginController.loginName', '');
  226. router.set('loginController.password', '');
  227. router.transitionTo('login', context);
  228. }
  229. })
  230. })