router.js 14 KB

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