router.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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,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. if (request.status == 403) {
  190. controller.postLogin(true, false);
  191. } else {
  192. controller.postLogin(false, false);
  193. }
  194. },
  195. login2SuccessCallback: function (clusterResp, opt, params) {
  196. var controller = this.get('loginController');
  197. if (clusterResp.items.length) {
  198. this.setAuthenticated(true);
  199. this.setLoginName(params.loginName);
  200. App.usersMapper.map({"items": [params.loginData]});
  201. this.setUser(App.User.find(params.loginName));
  202. this.transitionTo(this.getSection());
  203. controller.postLogin(true,true);
  204. }
  205. else {
  206. controller.set('errorMessage', Em.I18n.t('router.hadoopClusterNotSetUp'));
  207. }
  208. },
  209. login2ErrorCallback: function (req) {
  210. console.log("Server not responding: " + req.statusCode);
  211. },
  212. setAmbariStacks: function () {
  213. App.ajax.send({
  214. name: 'router.set_ambari_stacks',
  215. sender: this,
  216. success: 'setAmbariStacksSuccessCallback',
  217. error: 'setAmbariStacksErrorCallback'
  218. });
  219. },
  220. setAmbariStacksSuccessCallback: function (jsonData) {
  221. console.log("TRACE: In success function for the setAmbariStacks call");
  222. var stacks = [];
  223. jsonData.forEach(function (_stack) {
  224. stacks.pushObject({
  225. name: _stack.name,
  226. version: _stack.version
  227. });
  228. }, this);
  229. App.db.setAmbariStacks(stacks);
  230. console.log('TRACEIINNGG: ambaristacks: ' + JSON.stringify(App.db.getAmbariStacks()));
  231. },
  232. setAmbariStacksErrorCallback: function (request, ajaxOptions, error) {
  233. console.log("TRACE: In error function for the setAmbariStacks call");
  234. console.log("TRACE: error code status is: " + request.status);
  235. console.log('Error message is: ' + request.responseText);
  236. },
  237. getSection: function () {
  238. if (App.testMode) {
  239. if (App.alwaysGoToInstaller) {
  240. return 'installer';
  241. } else {
  242. return 'main.dashboard.index';
  243. }
  244. }
  245. App.clusterStatus.updateFromServer(false, false);
  246. var clusterStatusOnServer = App.clusterStatus.get('value');
  247. if (!App.get('isAdmin') || clusterStatusOnServer && clusterStatusOnServer.clusterState === 'DEFAULT') {
  248. return 'main.dashboard.index';
  249. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('addHostController.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.hostAdd';
  252. } else if (clusterStatusOnServer && (clusterStatusOnServer.wizardControllerName === App.router.get('addSecurityController.name') || clusterStatusOnServer.wizardControllerName === App.router.get('mainAdminSecurityDisableController.name'))) {
  253. // if wizardControllerName == "addSecurityController", then it means someone closed the browser or the browser was crashed when we were last in Add Security wizard
  254. return 'main.admin.adminSecurity';
  255. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('addServiceController.name')) {
  256. // if wizardControllerName == "addHostController", then it means someone closed the browser or the browser was crashed when we were last in Add Hosts wizard
  257. return 'main.serviceAdd';
  258. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('stackUpgradeController.name')) {
  259. // if wizardControllerName == "stackUpgradeController", then it means someone closed the browser or the browser was crashed when we were last in Stack Upgrade wizard
  260. return 'main.stackUpgrade';
  261. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('reassignMasterController.name')) {
  262. // if wizardControllerName == "reassignMasterController", then it means someone closed the browser or the browser was crashed when we were last in Reassign Master wizard
  263. return 'main.reassign';
  264. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('highAvailabilityWizardController.name')) {
  265. // if wizardControllerName == "highAvailabilityWizardController", then it means someone closed the browser or the browser was crashed when we were last in NameNode High Availability wizard
  266. return 'main.admin.enableHighAvailability';
  267. }else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('rollbackHighAvailabilityWizardController.name')) {
  268. // 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
  269. return 'main.admin.rollbackHighAvailability';
  270. } else {
  271. // if wizardControllerName == "installerController", then it means someone closed the browser or the browser was crashed when we were last in Installer wizard
  272. return 'installer';
  273. }
  274. },
  275. logOff: function (context) {
  276. $('title').text('Ambari');
  277. var hash = window.btoa(this.get('loginController.loginName') + ":" + this.get('loginController.password'));
  278. App.router.get('mainController').stopPolling();
  279. // App.db.cleanUp() must be called before router.clearAllSteps().
  280. // otherwise, this.set('installerController.currentStep, 0) would have no effect
  281. // since it's a computed property but we are not setting it as a dependent of App.db.
  282. App.db.cleanUp();
  283. App.set('isAdmin', false);
  284. this.set('loggedIn', false);
  285. this.clearAllSteps();
  286. console.log("Log off: " + App.router.getClusterName());
  287. this.set('loginController.loginName', '');
  288. this.set('loginController.password', '');
  289. // 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.
  290. if (!App.testMode && context) {
  291. App.ajax.send({
  292. name: 'router.logoff',
  293. sender: this,
  294. data: {
  295. auth: "Basic " + hash
  296. },
  297. beforeSend: 'authBeforeSend',
  298. success: 'logOffSuccessCallback',
  299. error:'logOffErrorCallback'
  300. });
  301. }
  302. this.transitionTo('login', context);
  303. },
  304. logOffSuccessCallback: function (data) {
  305. console.log("invoked logout on the server successfully");
  306. },
  307. logOffErrorCallback: function (req) {
  308. console.log("failed to invoke logout on the server");
  309. },
  310. /**
  311. * initialize isAdmin if user is administrator
  312. */
  313. initAdmin: function(){
  314. if(App.db && App.db.getUser() && App.db.getUser().admin) {
  315. App.set('isAdmin', true);
  316. console.log('Administrator logged in');
  317. }
  318. },
  319. root: Em.Route.extend({
  320. index: Em.Route.extend({
  321. route: '/',
  322. redirectsTo: 'login'
  323. }),
  324. enter: function(router){
  325. router.initAdmin();
  326. },
  327. login: Em.Route.extend({
  328. route: '/login',
  329. /**
  330. * If the user is already logged in, redirect to where the user was previously
  331. */
  332. enter: function (router, context) {
  333. if (router.getAuthenticated()) {
  334. Ember.run.next(function () {
  335. console.log(router.getLoginName() + ' already authenticated. Redirecting...');
  336. router.transitionTo(router.getSection(), context);
  337. });
  338. }
  339. },
  340. connectOutlets: function (router, context) {
  341. $('title').text(Em.I18n.t('app.name'));
  342. console.log('/login:connectOutlet');
  343. console.log('currentStep is: ' + router.getInstallerCurrentStep());
  344. console.log('authenticated is: ' + router.getAuthenticated());
  345. router.get('applicationController').connectOutlet('login');
  346. }
  347. }),
  348. installer: require('routes/installer'),
  349. main: require('routes/main'),
  350. experimental: Em.Route.extend({
  351. route: '/experimental',
  352. enter: function (router, context) {
  353. },
  354. connectOutlets: function (router, context) {
  355. $('title').text("Ambari Experimental");
  356. console.log('/experimental:connectOutlet');
  357. router.get('applicationController').connectOutlet('experimental');
  358. }
  359. }),
  360. logoff: function (router, context) {
  361. router.logOff(context);
  362. }
  363. })
  364. });