router.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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').clear();
  34. this.get('addHostController').clear();
  35. this.get('addServiceController').clear();
  36. this.get('stackUpgradeController').clear();
  37. this.get('reassignMasterController').clear();
  38. for (i = 1; i < 11; i++) {
  39. this.set('wizardStep' + i + 'Controller.hasSubmitted', false);
  40. this.set('wizardStep' + i + 'Controller.isDisabled', true);
  41. }
  42. },
  43. /**
  44. * Temporary fix for getting cluster name
  45. * @return {*}
  46. */
  47. getClusterName: function () {
  48. return App.router.get('clusterController').get('clusterName');
  49. },
  50. /**
  51. * Get current step of Installer wizard
  52. * @return {*}
  53. */
  54. getInstallerCurrentStep: function () {
  55. return this.getWizardCurrentStep('installer');
  56. },
  57. /**
  58. * Get current step for <code>wizardType</code> wizard
  59. * @param wizardType one of <code>installer</code>, <code>addHost</code>, <code>addServices</code>
  60. */
  61. getWizardCurrentStep: function (wizardType) {
  62. var loginName = this.getLoginName();
  63. var currentStep = App.db.getWizardCurrentStep(wizardType);
  64. console.log('getWizardCurrentStep: loginName=' + loginName + ", currentStep=" + currentStep);
  65. if (!currentStep) {
  66. currentStep = wizardType === 'installer' ? '0' : '1';
  67. }
  68. console.log('returning currentStep=' + currentStep);
  69. return currentStep;
  70. },
  71. loggedIn: false,
  72. loginName: function() {
  73. return this.getLoginName();
  74. }.property('loggedIn'),
  75. getAuthenticated: function () {
  76. var auth = App.db.getAuthenticated();
  77. var authResp = (auth && auth === true);
  78. if (authResp) {
  79. App.ajax.send({
  80. name: 'router.authentication',
  81. sender: this,
  82. success: 'onAuthenticationSuccess',
  83. error: 'onAuthenticationError'
  84. });
  85. } else {
  86. this.set('loggedIn', false);
  87. }
  88. return this.get('loggedIn');
  89. },
  90. onAuthenticationSuccess: function (data) {
  91. this.set('loggedIn', true);
  92. },
  93. onAuthenticationError: function (data) {
  94. if (data.status === 403) {
  95. this.set('loggedIn', false);
  96. } else {
  97. console.log('error in getAuthenticated');
  98. }
  99. },
  100. setAuthenticated: function (authenticated) {
  101. console.log("TRACE: Entering router:setAuthenticated function");
  102. App.db.setAuthenticated(authenticated);
  103. this.set('loggedIn', authenticated);
  104. },
  105. getLoginName: function () {
  106. return App.db.getLoginName();
  107. },
  108. setLoginName: function (loginName) {
  109. App.db.setLoginName(loginName);
  110. },
  111. /**
  112. * Set user model to local storage
  113. * @param user
  114. */
  115. setUser: function (user) {
  116. App.db.setUser(user);
  117. },
  118. /**
  119. * Get user model from local storage
  120. * @return {*}
  121. */
  122. getUser: function () {
  123. return App.db.getUser();
  124. },
  125. login: function () {
  126. var controller = this.get('loginController');
  127. var loginName = controller.get('loginName').toLowerCase();
  128. controller.set('loginName', loginName);
  129. var hash = window.btoa(loginName + ":" + controller.get('password'));
  130. var usr = '';
  131. if (App.testMode) {
  132. if (loginName === "admin" && controller.get('password') === 'admin') {
  133. usr = 'admin';
  134. } else if (loginName === 'user' && controller.get('password') === 'user') {
  135. usr = 'user';
  136. }
  137. }
  138. App.ajax.send({
  139. name: 'router.login',
  140. sender: this,
  141. data: {
  142. auth: "Basic " + hash,
  143. usr: usr,
  144. loginName: loginName
  145. },
  146. beforeSend: 'authBeforeSend',
  147. success: 'loginSuccessCallback',
  148. error: 'loginErrorCallback'
  149. });
  150. },
  151. authBeforeSend: function(opt, xhr, data) {
  152. xhr.setRequestHeader("Authorization", data.auth);
  153. },
  154. loginSuccessCallback: function(data, opt, params) {
  155. console.log('login success');
  156. var d = data;
  157. var isAdmin = data.Users.roles.indexOf('admin') >= 0;
  158. if (isAdmin) {
  159. App.set('isAdmin', true);
  160. var controller = this.get('loginController');
  161. this.setAuthenticated(true);
  162. this.setLoginName(params.loginName);
  163. App.usersMapper.map({"items": [data]});
  164. this.setUser(App.User.find(params.loginName));
  165. this.transitionTo(this.getSection());
  166. controller.postLogin(true);
  167. }
  168. else {
  169. App.ajax.send({
  170. name: 'router.login2',
  171. sender: this,
  172. data: {
  173. loginName: params.loginName,
  174. loginData: data
  175. },
  176. success: 'login2SuccessCallback',
  177. error: 'login2ErrorCallback'
  178. });
  179. }
  180. },
  181. loginErrorCallback: function(request, ajaxOptions, error, opt) {
  182. var controller = this.get('loginController');
  183. console.log("login error: " + error);
  184. this.setAuthenticated(false);
  185. controller.postLogin(false);
  186. },
  187. login2SuccessCallback: function (clusterResp, opt, params) {
  188. var controller = this.get('loginController');
  189. if (clusterResp.items.length) {
  190. this.setAuthenticated(true);
  191. this.setLoginName(params.loginName);
  192. App.usersMapper.map({"items": [params.loginData]});
  193. this.setUser(App.User.find(params.loginName));
  194. this.transitionTo(this.getSection());
  195. controller.postLogin(true);
  196. }
  197. else {
  198. controller.set('errorMessage', Em.I18n.t('router.hadoopClusterNotSetUp'));
  199. }
  200. },
  201. login2ErrorCallback: function (req) {
  202. console.log("Server not responding: " + req.statusCode);
  203. },
  204. setAmbariStacks: function () {
  205. App.ajax.send({
  206. name: 'router.set_ambari_stacks',
  207. sender: this,
  208. success: 'setAmbariStacksSuccessCallback',
  209. error: 'setAmbariStacksErrorCallback'
  210. });
  211. },
  212. setAmbariStacksSuccessCallback: function (jsonData) {
  213. console.log("TRACE: In success function for the setAmbariStacks call");
  214. var stacks = [];
  215. jsonData.forEach(function (_stack) {
  216. stacks.pushObject({
  217. name: _stack.name,
  218. version: _stack.version
  219. });
  220. }, this);
  221. App.db.setAmbariStacks(stacks);
  222. console.log('TRACEIINNGG: ambaristacks: ' + JSON.stringify(App.db.getAmbariStacks()));
  223. },
  224. setAmbariStacksErrorCallback: function (request, ajaxOptions, error) {
  225. console.log("TRACE: In error function for the setAmbariStacks call");
  226. console.log("TRACE: error code status is: " + request.status);
  227. console.log('Error message is: ' + request.responseText);
  228. },
  229. getSection: function () {
  230. if (App.testMode) {
  231. if (App.alwaysGoToInstaller) {
  232. return 'installer';
  233. } else {
  234. return 'main.index';
  235. }
  236. }
  237. App.clusterStatus.updateFromServer();
  238. var clusterStatusOnServer = App.clusterStatus.get('value');
  239. if (!localStorage.getObject('ambari').app.user.admin || clusterStatusOnServer && (clusterStatusOnServer.clusterState === 'CLUSTER_STARTED_5' ||
  240. clusterStatusOnServer.clusterState === 'ADD_HOSTS_COMPLETED_5' || clusterStatusOnServer.clusterState === 'STACK_UPGRADE_COMPLETED' ||
  241. clusterStatusOnServer.clusterState === 'REASSIGN_MASTER_COMPLETED') ||clusterStatusOnServer.clusterState === 'SECURITY_COMPLETED' ) {
  242. return 'main.index';
  243. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('addHostController.name')) {
  244. // if wizardControllerName == "addHostController", then it means someone closed the browser or the browser was crashed when we were last in Add Hosts wizard
  245. return 'main.hostAdd';
  246. } else if (clusterStatusOnServer && (clusterStatusOnServer.wizardControllerName === App.router.get('addSecurityController.name') || clusterStatusOnServer.wizardControllerName === App.router.get('mainAdminSecurityDisableController.name'))) {
  247. // if wizardControllerName == "addSecurityController", then it means someone closed the browser or the browser was crashed when we were last in Add Security wizard
  248. return 'main.admin.adminSecurity';
  249. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('addServiceController.name')) {
  250. // if wizardControllerName == "addHostController", then it means someone closed the browser or the browser was crashed when we were last in Add Hosts wizard
  251. return 'main.serviceAdd';
  252. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('stackUpgradeController.name')) {
  253. // if wizardControllerName == "stackUpgradeController", then it means someone closed the browser or the browser was crashed when we were last in Stack Upgrade wizard
  254. return 'main.stackUpgrade';
  255. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('reassignMasterController.name')) {
  256. // if wizardControllerName == "reassignMasterController", then it means someone closed the browser or the browser was crashed when we were last in Reassign Master wizard
  257. return 'main.reassignMaster';
  258. } else {
  259. // if wizardControllerName == "installerController", then it means someone closed the browser or the browser was crashed when we were last in Installer wizard
  260. return 'installer';
  261. }
  262. },
  263. logOff: function (context) {
  264. $('title').text('Ambari');
  265. var hash = window.btoa(this.get('loginController.loginName') + ":" + this.get('loginController.password'));
  266. App.router.get('mainController').stopPolling();
  267. // App.db.cleanUp() must be called before router.clearAllSteps().
  268. // otherwise, this.set('installerController.currentStep, 0) would have no effect
  269. // since it's a computed property but we are not setting it as a dependent of App.db.
  270. App.db.cleanUp();
  271. App.set('isAdmin', false);
  272. this.set('loggedIn', false);
  273. this.clearAllSteps();
  274. console.log("Log off: " + App.router.getClusterName());
  275. this.set('loginController.loginName', '');
  276. this.set('loginController.password', '');
  277. // When logOff is called by Sign Out button, context contains event object. As it is only case we should send logoff request, we are checking context below.
  278. if (!App.testMode && context) {
  279. App.ajax.send({
  280. name: 'router.logoff',
  281. sender: this,
  282. data: {
  283. auth: "Basic " + hash
  284. },
  285. beforeSend: 'authBeforeSend',
  286. success: 'logOffSuccessCallback',
  287. error:'logOffErrorCallback'
  288. });
  289. }
  290. this.transitionTo('login', context);
  291. },
  292. logOffSuccessCallback: function (data) {
  293. console.log("invoked logout on the server successfully");
  294. },
  295. logOffErrorCallback: function (req) {
  296. console.log("failed to invoke logout on the server");
  297. },
  298. root: Em.Route.extend({
  299. index: Em.Route.extend({
  300. route: '/',
  301. redirectsTo: 'login'
  302. }),
  303. login: Em.Route.extend({
  304. route: '/login',
  305. /**
  306. * If the user is already logged in, redirect to where the user was previously
  307. */
  308. enter: function (router, context) {
  309. if (router.getAuthenticated()) {
  310. Ember.run.next(function () {
  311. console.log(router.getLoginName() + ' already authenticated. Redirecting...');
  312. router.transitionTo(router.getSection(), context);
  313. });
  314. }
  315. },
  316. connectOutlets: function (router, context) {
  317. $('title').text(Em.I18n.t('app.name'));
  318. console.log('/login:connectOutlet');
  319. console.log('currentStep is: ' + router.getInstallerCurrentStep());
  320. console.log('authenticated is: ' + router.getAuthenticated());
  321. router.get('applicationController').connectOutlet('login');
  322. }
  323. }),
  324. installer: require('routes/installer'),
  325. main: require('routes/main'),
  326. logoff: function (router, context) {
  327. router.logOff(context);
  328. }
  329. })
  330. });