router.js 14 KB

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