router.js 14 KB

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