router.js 4.4 KB

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