router.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. setNavigationFlow: function (step) {
  23. var newStep = parseInt(step.slice(-1));
  24. var previousStep = parseInt(this.getInstallerCurrentStep());
  25. if (newStep >= previousStep) {
  26. this.set('isFwdNavigation', true);
  27. } else {
  28. this.set('isFwdNavigation', false);
  29. }
  30. },
  31. setInstallerCurrentStep: function (currentStep, completed) {
  32. var loginName = this.getLoginName();
  33. App.db.setInstallerCurrentStep(currentStep, completed);
  34. this.set('installerController.currentStep', currentStep);
  35. },
  36. getInstallerCurrentStep: function () {
  37. var loginName = this.getLoginName();
  38. var currentStep = App.db.getInstallerCurrentStep();
  39. console.log('getInstallerCurrentStep: loginName=' + loginName + ", currentStep=" + currentStep);
  40. if (!currentStep) {
  41. currentStep = '1';
  42. }
  43. console.log('returning currentStep=' + currentStep);
  44. return currentStep;
  45. },
  46. loggedIn: false,
  47. getAuthenticated: function () {
  48. // TODO: this needs to be hooked up with server authentication
  49. var auth = App.db.getAuthenticated();
  50. var authResp = (auth && auth === true);
  51. this.set('loggedIn', authResp);
  52. return authResp;
  53. },
  54. setAuthenticated: function (authenticated) {
  55. // TODO: this needs to be hooked up with server authentication
  56. console.log("TRACE: Entering router:setAuthenticated function");
  57. App.db.setAuthenticated(authenticated);
  58. this.set('loggedIn', authenticated);
  59. },
  60. getLoginName: function () {
  61. // TODO: this needs to be hooked up with server authentication
  62. return App.db.getLoginName();
  63. //return localStorage.getItem('Ambari' + 'loginName');
  64. },
  65. setLoginName: function (loginName) {
  66. // TODO: this needs to be hooked up with server authentication
  67. App.db.setLoginName(loginName);
  68. //localStorage.setItem('Ambari' + 'loginName', loginName);
  69. },
  70. // that works incorrectly
  71. setUser: function (user) {
  72. App.db.setUser(user);
  73. },
  74. // that works incorrectly
  75. getUser: function () {
  76. return App.db.getUser();
  77. },
  78. login: function (loginName, user) {
  79. // TODO: this needs to be hooked up with server authentication
  80. console.log("In login function");
  81. this.setAuthenticated(true);
  82. this.setLoginName(loginName);
  83. // refactor to get user attributes
  84. // this.setUser(user);
  85. this.transitionTo(this.getSection());
  86. },
  87. defaultSection: 'installer',
  88. getSection: function () {
  89. var section = App.db.getSection();
  90. console.log("The section is: " + section);
  91. var section = localStorage.getItem(this.getLoginName() + 'section');
  92. return section || this.defaultSection;
  93. },
  94. setSection: function (section) {
  95. App.db.setSection(section);
  96. },
  97. root: Em.Route.extend({
  98. index: Em.Route.extend({
  99. route: '/',
  100. redirectsTo: 'login'
  101. }),
  102. login: Em.Route.extend({
  103. route: '/login',
  104. /**
  105. * If the user is already logged in, redirect to where the user was previously
  106. */
  107. enter: function (router, context) {
  108. if (router.getAuthenticated()) {
  109. Ember.run.next(function () {
  110. console.log(router.getLoginName() + ' already authenticated. Redirecting...');
  111. router.transitionTo(router.getSection(), context);
  112. });
  113. }
  114. },
  115. connectOutlets: function (router, context) {
  116. console.log('/login:connectOutlet');
  117. console.log('currentStep is: ' + router.getInstallerCurrentStep());
  118. console.log('authenticated is: ' + router.getAuthenticated());
  119. router.get('applicationController').connectOutlet('login', App.LoginView);
  120. }
  121. }),
  122. installer: require('routes/installer'),
  123. main: require('routes/main'),
  124. logoff: function (router, context) {
  125. console.log('logging off');
  126. App.db.cleanUp();
  127. router.set('loginController.loginName', '');
  128. router.set('loginController.password', '');
  129. router.transitionTo('login', context);
  130. }
  131. })
  132. })