router.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. this.get('backgroundOperationsController').clear();
  43. for (var i = 1; i < 11; i++) {
  44. this.set('wizardStep' + i + 'Controller.hasSubmitted', false);
  45. this.set('wizardStep' + i + 'Controller.isDisabled', true);
  46. }
  47. },
  48. /**
  49. * Temporary fix for getting cluster name
  50. * @return {*}
  51. */
  52. getClusterName: function () {
  53. return App.router.get('clusterController').get('clusterName');
  54. },
  55. /**
  56. * Get current step of Installer wizard
  57. * @return {*}
  58. */
  59. getInstallerCurrentStep: function () {
  60. return this.getWizardCurrentStep('installer');
  61. },
  62. /**
  63. * Get current step for <code>wizardType</code> wizard
  64. * @param wizardType one of <code>installer</code>, <code>addHost</code>, <code>addServices</code>
  65. */
  66. getWizardCurrentStep: function (wizardType) {
  67. var loginName = this.getLoginName();
  68. var currentStep = App.db.getWizardCurrentStep(wizardType);
  69. console.log('getWizardCurrentStep: loginName=' + loginName + ", currentStep=" + currentStep);
  70. if (!currentStep) {
  71. currentStep = wizardType === 'installer' ? '0' : '1';
  72. }
  73. console.log('returning currentStep=' + currentStep);
  74. return currentStep;
  75. },
  76. loggedIn: App.db.getAuthenticated(),
  77. loginName: function() {
  78. return this.getLoginName();
  79. }.property('loggedIn'),
  80. getAuthenticated: function () {
  81. var dfd = $.Deferred();
  82. var self = this;
  83. var auth = App.db.getAuthenticated();
  84. var authResp = (auth && auth === true);
  85. if (authResp) {
  86. App.ajax.send({
  87. name: 'router.authentication',
  88. sender: this,
  89. success: 'onAuthenticationSuccess',
  90. error: 'onAuthenticationError'
  91. }).complete(function () {
  92. dfd.resolve(self.get('loggedIn'));
  93. });
  94. } else {
  95. this.set('loggedIn', false);
  96. dfd.resolve(false);
  97. }
  98. return dfd.promise();
  99. },
  100. onAuthenticationSuccess: function (data) {
  101. this.set('loggedIn', true);
  102. },
  103. onAuthenticationError: function (data) {
  104. if (data.status === 403) {
  105. this.set('loggedIn', false);
  106. } else {
  107. console.log('error in getAuthenticated');
  108. }
  109. },
  110. setAuthenticated: function (authenticated) {
  111. console.log("TRACE: Entering router:setAuthenticated function");
  112. App.db.setAuthenticated(authenticated);
  113. this.set('loggedIn', authenticated);
  114. },
  115. getLoginName: function () {
  116. return App.db.getLoginName();
  117. },
  118. setLoginName: function (loginName) {
  119. App.db.setLoginName(loginName);
  120. },
  121. /**
  122. * Set user model to local storage
  123. * @param user
  124. */
  125. setUser: function (user) {
  126. App.db.setUser(user);
  127. },
  128. /**
  129. * Get user model from local storage
  130. * @return {*}
  131. */
  132. getUser: function () {
  133. return App.db.getUser();
  134. },
  135. login: function () {
  136. var controller = this.get('loginController');
  137. var loginName = controller.get('loginName').toLowerCase();
  138. controller.set('loginName', loginName);
  139. var hash = window.btoa(loginName + ":" + controller.get('password'));
  140. var usr = '';
  141. if (App.get('testMode')) {
  142. if (loginName === "admin" && controller.get('password') === 'admin') {
  143. usr = 'admin';
  144. } else if (loginName === 'user' && controller.get('password') === 'user') {
  145. usr = 'user';
  146. }
  147. }
  148. App.ajax.send({
  149. name: 'router.login',
  150. sender: this,
  151. data: {
  152. auth: "Basic " + hash,
  153. usr: usr,
  154. loginName: loginName
  155. },
  156. beforeSend: 'authBeforeSend',
  157. success: 'loginSuccessCallback',
  158. error: 'loginErrorCallback'
  159. });
  160. },
  161. authBeforeSend: function(opt, xhr, data) {
  162. xhr.setRequestHeader("Authorization", data.auth);
  163. },
  164. loginSuccessCallback: function(data, opt, params) {
  165. console.log('login success');
  166. var d = data;
  167. var isAdmin = data.Users.roles.indexOf('admin') >= 0;
  168. var self = this;
  169. if (isAdmin) {
  170. App.set('isAdmin', true);
  171. var controller = this.get('loginController');
  172. this.setAuthenticated(true);
  173. this.setLoginName(params.loginName);
  174. App.usersMapper.map({"items": [data]});
  175. this.setUser(App.User.find().findProperty('id', params.loginName));
  176. this.getSection(function(route){
  177. self.transitionTo(route);
  178. controller.postLogin(true,true);
  179. });
  180. }
  181. else {
  182. App.ajax.send({
  183. name: 'router.login2',
  184. sender: this,
  185. data: {
  186. loginName: params.loginName,
  187. loginData: data
  188. },
  189. success: 'login2SuccessCallback',
  190. error: 'login2ErrorCallback'
  191. });
  192. }
  193. },
  194. loginErrorCallback: function(request, ajaxOptions, error, opt) {
  195. var controller = this.get('loginController');
  196. console.log("login error: " + error);
  197. this.setAuthenticated(false);
  198. if (request.status == 403) {
  199. controller.postLogin(true, false);
  200. } else {
  201. controller.postLogin(false, false);
  202. }
  203. },
  204. login2SuccessCallback: function (clusterResp, opt, params) {
  205. var controller = this.get('loginController');
  206. var self = this;
  207. if (clusterResp.items.length) {
  208. this.setAuthenticated(true);
  209. this.setLoginName(params.loginName);
  210. App.usersMapper.map({"items": [params.loginData]});
  211. this.setUser(App.User.find().findProperty('id', params.loginName));
  212. this.getSection(function(route){
  213. self.transitionTo(route);
  214. controller.postLogin(true,true);
  215. });
  216. }
  217. else {
  218. controller.set('errorMessage', Em.I18n.t('router.hadoopClusterNotSetUp'));
  219. }
  220. },
  221. login2ErrorCallback: function (req) {
  222. console.log("Server not responding: " + req.statusCode);
  223. },
  224. getSection: function (callback) {
  225. if (App.get('testMode')) {
  226. if (App.alwaysGoToInstaller) {
  227. callback('installer');
  228. } else {
  229. callback('main.dashboard.index');
  230. }
  231. }
  232. App.clusterStatus.updateFromServer(false).complete(function () {
  233. var clusterStatusOnServer = App.clusterStatus.get('value');
  234. // if wizardControllerName == "installerController", then it means someone closed the browser or the browser was crashed when we were last in Installer wizard
  235. var route = 'installer';
  236. if (!App.get('isAdmin') || clusterStatusOnServer && (clusterStatusOnServer.clusterState === 'DEFAULT' || clusterStatusOnServer.clusterState === 'CLUSTER_STARTED_5')) {
  237. route = 'main.dashboard.index';
  238. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('addHostController.name')) {
  239. // if wizardControllerName == "addHostController", then it means someone closed the browser or the browser was crashed when we were last in Add Hosts wizard
  240. route = 'main.hostAdd';
  241. } else if (clusterStatusOnServer && (clusterStatusOnServer.wizardControllerName === App.router.get('addSecurityController.name') || clusterStatusOnServer.wizardControllerName === App.router.get('mainAdminSecurityDisableController.name'))) {
  242. // if wizardControllerName == "addSecurityController", then it means someone closed the browser or the browser was crashed when we were last in Add Security wizard
  243. route = 'main.admin.adminSecurity';
  244. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('addServiceController.name')) {
  245. // if wizardControllerName == "addHostController", then it means someone closed the browser or the browser was crashed when we were last in Add Hosts wizard
  246. route = 'main.serviceAdd';
  247. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('stackUpgradeController.name')) {
  248. // if wizardControllerName == "stackUpgradeController", then it means someone closed the browser or the browser was crashed when we were last in Stack Upgrade wizard
  249. route = 'main.stackUpgrade';
  250. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('reassignMasterController.name')) {
  251. // if wizardControllerName == "reassignMasterController", then it means someone closed the browser or the browser was crashed when we were last in Reassign Master wizard
  252. route = 'main.reassign';
  253. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('highAvailabilityWizardController.name')) {
  254. // if wizardControllerName == "highAvailabilityWizardController", then it means someone closed the browser or the browser was crashed when we were last in NameNode High Availability wizard
  255. route = 'main.admin.enableHighAvailability';
  256. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('rollbackHighAvailabilityWizardController.name')) {
  257. // 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
  258. route = 'main.admin.rollbackHighAvailability';
  259. }
  260. callback(route);
  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.get('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. /**
  299. * initialize isAdmin if user is administrator
  300. */
  301. initAdmin: function(){
  302. if(App.db && App.db.getUser() && App.db.getUser().admin) {
  303. App.set('isAdmin', true);
  304. console.log('Administrator logged in');
  305. }
  306. },
  307. root: Em.Route.extend({
  308. index: Em.Route.extend({
  309. route: '/',
  310. redirectsTo: 'login'
  311. }),
  312. enter: function(router){
  313. router.initAdmin();
  314. },
  315. login: Em.Route.extend({
  316. route: '/login',
  317. /**
  318. * If the user is already logged in, redirect to where the user was previously
  319. */
  320. enter: function (router, context) {
  321. router.getAuthenticated().done(function (loggedIn) {
  322. if (loggedIn) {
  323. Ember.run.next(function () {
  324. console.log(router.getLoginName() + ' already authenticated. Redirecting...');
  325. router.getSection(function (route) {
  326. router.transitionTo(route, context);
  327. });
  328. });
  329. }
  330. });
  331. },
  332. connectOutlets: function (router, context) {
  333. $('title').text(Em.I18n.t('app.name'));
  334. console.log('/login:connectOutlet');
  335. console.log('currentStep is: ' + router.getInstallerCurrentStep());
  336. console.log('authenticated is: ' + router.getAuthenticated());
  337. router.get('applicationController').connectOutlet('login');
  338. }
  339. }),
  340. installer: require('routes/installer'),
  341. main: require('routes/main'),
  342. experimental: Em.Route.extend({
  343. route: '/experimental',
  344. enter: function (router, context) {
  345. },
  346. connectOutlets: function (router, context) {
  347. $('title').text("Ambari Experimental");
  348. console.log('/experimental:connectOutlet');
  349. router.get('applicationController').connectOutlet('experimental');
  350. }
  351. }),
  352. logoff: function (router, context) {
  353. router.logOff(context);
  354. }
  355. })
  356. });