router.js 7.2 KB

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