router.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. App.Router = Em.Router.extend({
  19. enableLogging: true,
  20. setInstallerCurrentStep: function (currentStep, completed) {
  21. var loginName = this.getLoginName();
  22. localStorage.setItem(loginName + 'Installer' + 'currentStep', currentStep);
  23. localStorage.setItem(loginName + 'Installer' + 'completed', completed);
  24. this.set('installerController.currentStep', currentStep);
  25. },
  26. getInstallerCurrentStep: function () {
  27. var loginName = this.getLoginName();
  28. var currentStep = localStorage.getItem(loginName + 'Installer' + 'currentStep');
  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 = localStorage.getItem('Ambari' + 'authenticated');
  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. localStorage.setItem('Ambari' + 'authenticated', authenticated);
  47. this.set('loggedIn', authenticated);
  48. },
  49. getLoginName: function () {
  50. // TODO: this needs to be hooked up with server authentication
  51. return localStorage.getItem('Ambari' + 'loginName');
  52. },
  53. setLoginName: function (loginName) {
  54. // TODO: this needs to be hooked up with server authentication
  55. localStorage.setItem('Ambari' + 'loginName', loginName);
  56. },
  57. login: function (loginName) {
  58. // TODO: this needs to be hooked up with server authentication
  59. this.setAuthenticated(true);
  60. this.setLoginName(loginName);
  61. this.transitionTo(this.getSection());
  62. },
  63. defaultSection: 'installer',
  64. getSection: function () {
  65. var section = localStorage.getItem(this.getLoginName() + 'section');
  66. return section || this.defaultSection;
  67. },
  68. setSection: function(section) {
  69. localStorage.setItem(this.getLoginName() + 'section', section);
  70. },
  71. root: Em.Route.extend({
  72. index: Em.Route.extend({
  73. route: '/',
  74. redirectsTo: 'login'
  75. }),
  76. login: Em.Route.extend({
  77. route: '/login',
  78. /**
  79. * If the user is already logged in, redirect to where the user was previously
  80. */
  81. enter: function (router, context) {
  82. if (router.getAuthenticated()) {
  83. Ember.run.next(function () {
  84. console.log(router.getLoginName() + ' already authenticated. Redirecting...');
  85. router.transitionTo(router.getSection(), context);
  86. });
  87. }
  88. },
  89. connectOutlets: function (router, context) {
  90. console.log('/login:connectOutlet');
  91. console.log('currentStep is: ' + router.getInstallerCurrentStep());
  92. console.log('authenticated is: ' + router.getAuthenticated());
  93. router.get('applicationController').connectOutlet('login', App.LoginView);
  94. }
  95. }),
  96. installer: require('routes/installer'),
  97. main: require('routes/main'),
  98. logoff: function (router, context) {
  99. console.log('logging off');
  100. router.setAuthenticated(false);
  101. router.setLoginName('');
  102. router.set('loginController.loginName', '');
  103. router.set('loginController.password', '');
  104. router.transitionTo('login', context);
  105. }
  106. })
  107. })