router.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. /**
  29. * Is true, if cluster.provisioning_state is equal to 'INSTALLED'
  30. * @type {Boolean}
  31. */
  32. clusterInstallCompleted: false,
  33. /**
  34. * user prefered path to route
  35. */
  36. preferedPath: null,
  37. setNavigationFlow: function (step) {
  38. var matches = step.match(/\d+$/);
  39. var newStep;
  40. if (matches) {
  41. newStep = parseInt(matches[0]);
  42. }
  43. var previousStep = parseInt(this.getInstallerCurrentStep());
  44. this.set('isFwdNavigation', newStep >= previousStep);
  45. },
  46. clearAllSteps: function () {
  47. this.get('installerController').clear();
  48. this.get('addHostController').clear();
  49. this.get('addServiceController').clear();
  50. this.get('stackUpgradeController').clear();
  51. this.get('backgroundOperationsController').clear();
  52. for (var i = 1; i < 11; i++) {
  53. this.set('wizardStep' + i + 'Controller.hasSubmitted', false);
  54. this.set('wizardStep' + i + 'Controller.isDisabled', true);
  55. }
  56. },
  57. /**
  58. * Temporary fix for getting cluster name
  59. * @return {*}
  60. */
  61. getClusterName: function () {
  62. return App.router.get('clusterController').get('clusterName');
  63. },
  64. /**
  65. * Get current step of Installer wizard
  66. * @return {*}
  67. */
  68. getInstallerCurrentStep: function () {
  69. return this.getWizardCurrentStep('installer');
  70. },
  71. /**
  72. * Get current step for <code>wizardType</code> wizard
  73. * @param wizardType one of <code>installer</code>, <code>addHost</code>, <code>addServices</code>
  74. */
  75. getWizardCurrentStep: function (wizardType) {
  76. var loginName = this.getLoginName();
  77. var currentStep = App.db.getWizardCurrentStep(wizardType);
  78. console.log('getWizardCurrentStep: loginName=' + loginName + ", currentStep=" + currentStep);
  79. if (!currentStep) {
  80. currentStep = wizardType === 'installer' ? '0' : '1';
  81. }
  82. console.log('returning currentStep=' + currentStep);
  83. return currentStep;
  84. },
  85. loggedIn: App.db.getAuthenticated(),
  86. loginName: function() {
  87. return this.getLoginName();
  88. }.property('loggedIn'),
  89. getAuthenticated: function () {
  90. var dfd = $.Deferred();
  91. var self = this;
  92. var auth = App.db.getAuthenticated();
  93. var authResp = (auth && auth === true);
  94. if (authResp) {
  95. App.ajax.send({
  96. name: 'router.authentication',
  97. sender: this,
  98. success: 'onAuthenticationSuccess',
  99. error: 'onAuthenticationError'
  100. }).complete(function () {
  101. dfd.resolve(self.get('loggedIn'));
  102. });
  103. } else {
  104. this.set('loggedIn', false);
  105. dfd.resolve(false);
  106. }
  107. return dfd.promise();
  108. },
  109. onAuthenticationSuccess: function (data) {
  110. this.set('loggedIn', true);
  111. if (data.items.length) {
  112. this.set('clusterInstallCompleted', data.items[0].Clusters.provisioning_state === 'INSTALLED');
  113. }
  114. },
  115. onAuthenticationError: function (data) {
  116. if (data.status === 403) {
  117. this.set('loggedIn', false);
  118. } else {
  119. console.log('error in getAuthenticated');
  120. }
  121. },
  122. setAuthenticated: function (authenticated) {
  123. console.log("TRACE: Entering router:setAuthenticated function");
  124. App.db.setAuthenticated(authenticated);
  125. this.set('loggedIn', authenticated);
  126. },
  127. getLoginName: function () {
  128. return App.db.getLoginName();
  129. },
  130. setLoginName: function (loginName) {
  131. App.db.setLoginName(loginName);
  132. },
  133. /**
  134. * Set user model to local storage
  135. * @param user
  136. */
  137. setUser: function (user) {
  138. App.db.setUser(user);
  139. },
  140. /**
  141. * Get user model from local storage
  142. * @return {*}
  143. */
  144. getUser: function () {
  145. return App.db.getUser();
  146. },
  147. /**
  148. * Get user privileges.
  149. *
  150. * @param {String} userName
  151. * @returns {$.Deferred}
  152. **/
  153. getUserPrivileges: function(userName) {
  154. return App.ajax.send({
  155. name: 'router.user.privileges',
  156. sender: this,
  157. data: {
  158. userName: userName
  159. },
  160. success: 'getUserPrivilegesSuccess',
  161. error: 'getUserPrivilegesError'
  162. });
  163. },
  164. getUserPrivilegesSuccess: function() {},
  165. getUserPrivilegesError: function(req) {
  166. console.log("Get user privileges error: " + req.statusCode);
  167. },
  168. setUserLoggedIn: function(userName) {
  169. this.setAuthenticated(true);
  170. this.setLoginName(userName);
  171. this.setUser(App.User.find().findProperty('id', userName));
  172. },
  173. login: function () {
  174. var controller = this.get('loginController');
  175. var loginName = controller.get('loginName').toLowerCase();
  176. controller.set('loginName', loginName);
  177. var hash = window.btoa(loginName + ":" + controller.get('password'));
  178. var usr = '';
  179. if (App.get('testMode')) {
  180. if (loginName === "admin" && controller.get('password') === 'admin') {
  181. usr = 'admin';
  182. } else if (loginName === 'user' && controller.get('password') === 'user') {
  183. usr = 'user';
  184. }
  185. }
  186. App.ajax.send({
  187. name: 'router.login',
  188. sender: this,
  189. data: {
  190. auth: "Basic " + hash,
  191. usr: usr,
  192. loginName: loginName
  193. },
  194. beforeSend: 'authBeforeSend',
  195. success: 'loginSuccessCallback',
  196. error: 'loginErrorCallback'
  197. });
  198. },
  199. authBeforeSend: function(opt, xhr, data) {
  200. xhr.setRequestHeader("Authorization", data.auth);
  201. },
  202. loginSuccessCallback: function(data, opt, params) {
  203. console.log('login success');
  204. App.ajax.send({
  205. name: 'router.login.clusters',
  206. sender: this,
  207. data: {
  208. loginName: params.loginName,
  209. loginData: data
  210. },
  211. success: 'loginGetClustersSuccessCallback',
  212. error: 'loginGetClustersErrorCallback'
  213. });
  214. },
  215. loginErrorCallback: function(request, ajaxOptions, error, opt) {
  216. var controller = this.get('loginController');
  217. console.log("login error: " + error);
  218. this.setAuthenticated(false);
  219. if (request.status == 403) {
  220. var responseMessage = request.responseText;
  221. try{
  222. responseMessage = JSON.parse(request.responseText).message;
  223. }catch(e){}
  224. controller.postLogin(true, false, responseMessage);
  225. } else {
  226. controller.postLogin(false, false, null);
  227. }
  228. },
  229. loginGetClustersSuccessCallback: function (clustersData, opt, params) {
  230. var adminViewUrl = '/views/ADMIN_VIEW/1.0.0/INSTANCE/#/';
  231. //TODO: Replace hard coded value with query. Same in templates/application.hbs
  232. var loginController = this.get('loginController');
  233. var loginData = params.loginData;
  234. var router = this;
  235. this.getUserPrivileges(params.loginName).done(function(privileges) {
  236. loginData.privileges = privileges;
  237. App.usersMapper.map({"items": [loginData]});
  238. router.setUserLoggedIn(params.loginName);
  239. var permissionList = privileges.items.mapProperty('PrivilegeInfo.permission_name');
  240. var isAdmin = permissionList.contains('AMBARI.ADMIN');
  241. var transitionToApp = false;
  242. if (isAdmin) {
  243. App.set('isAdmin', true);
  244. if (clustersData.items.length) {
  245. transitionToApp = true;
  246. } else {
  247. window.location = adminViewUrl;
  248. return;
  249. }
  250. } else {
  251. if (clustersData.items.length) {
  252. this.set('clusterInstallCompleted', clustersData.items[0].Clusters.provisioning_state === 'INSTALLED');
  253. //TODO: Iterate over clusters
  254. var clusterName = clustersData.items[0].Clusters.cluster_name;
  255. var clusterPermissions = privileges.items.filterProperty('PrivilegeInfo.cluster_name', clusterName).mapProperty('PrivilegeInfo.permission_name');
  256. if (clusterPermissions.contains('CLUSTER.OPERATE')) {
  257. App.set('isAdmin', true);
  258. App.set('isOperator', true);
  259. transitionToApp = true;
  260. } else if (clusterPermissions.contains('CLUSTER.READ')) {
  261. transitionToApp = true;
  262. }
  263. }
  264. }
  265. if (transitionToApp) {
  266. if (!Em.isNone(router.get('preferedPath'))) {
  267. window.location = router.get('preferedPath');
  268. router.set('preferedPath', null);
  269. } else {
  270. router.getSection(function (route) {
  271. router.transitionTo(route);
  272. loginController.postLogin(true, true);
  273. });
  274. }
  275. } else {
  276. router.transitionTo('main.views.index');
  277. loginController.postLogin(true,true);
  278. }
  279. });
  280. },
  281. loginGetClustersErrorCallback: function (req) {
  282. console.log("Get clusters error: " + req.statusCode);
  283. },
  284. getSection: function (callback) {
  285. if (App.get('testMode')) {
  286. if (App.alwaysGoToInstaller) {
  287. callback('installer');
  288. } else {
  289. callback('main.dashboard.index');
  290. }
  291. } else {
  292. if (this.get('clusterInstallCompleted')) {
  293. App.clusterStatus.updateFromServer(false).complete(function () {
  294. var clusterStatusOnServer = App.clusterStatus.get('value');
  295. var route = 'main.dashboard.index';
  296. if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('addHostController.name')) {
  297. // if wizardControllerName == "addHostController", then it means someone closed the browser or the browser was crashed when we were last in Add Hosts wizard
  298. route = 'main.hostAdd';
  299. } else if (clusterStatusOnServer && (clusterStatusOnServer.wizardControllerName === App.router.get('addSecurityController.name') || clusterStatusOnServer.wizardControllerName === App.router.get('mainAdminSecurityDisableController.name'))) {
  300. // if wizardControllerName == "addSecurityController", then it means someone closed the browser or the browser was crashed when we were last in Add Security wizard
  301. route = 'main.admin.adminSecurity';
  302. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('addServiceController.name')) {
  303. // if wizardControllerName == "addHostController", then it means someone closed the browser or the browser was crashed when we were last in Add Hosts wizard
  304. route = 'main.serviceAdd';
  305. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('stackUpgradeController.name')) {
  306. // if wizardControllerName == "stackUpgradeController", then it means someone closed the browser or the browser was crashed when we were last in Stack Upgrade wizard
  307. route = 'main.stackUpgrade';
  308. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('reassignMasterController.name')) {
  309. // if wizardControllerName == "reassignMasterController", then it means someone closed the browser or the browser was crashed when we were last in Reassign Master wizard
  310. route = 'main.reassign';
  311. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('highAvailabilityWizardController.name')) {
  312. // if wizardControllerName == "highAvailabilityWizardController", then it means someone closed the browser or the browser was crashed when we were last in NameNode High Availability wizard
  313. route = 'main.services.enableHighAvailability';
  314. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('rMHighAvailabilityWizardController.name')) {
  315. // if wizardControllerName == "highAvailabilityWizardController", then it means someone closed the browser or the browser was crashed when we were last in NameNode High Availability wizard
  316. route = 'main.services.enableRMHighAvailability';
  317. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('rollbackHighAvailabilityWizardController.name')) {
  318. // 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
  319. route = 'main.services.rollbackHighAvailability';
  320. }
  321. callback(route);
  322. });
  323. } else {
  324. callback('installer');
  325. }
  326. }
  327. },
  328. logOff: function (context) {
  329. $('title').text('Ambari');
  330. var hash = window.btoa(this.get('loginController.loginName') + ":" + this.get('loginController.password'));
  331. App.router.get('mainController').stopPolling();
  332. // App.db.cleanUp() must be called before router.clearAllSteps().
  333. // otherwise, this.set('installerController.currentStep, 0) would have no effect
  334. // since it's a computed property but we are not setting it as a dependent of App.db.
  335. App.db.cleanUp();
  336. App.set('isAdmin', false);
  337. App.set('isOperator', false);
  338. this.set('loggedIn', false);
  339. this.clearAllSteps();
  340. console.log("Log off: " + App.router.getClusterName());
  341. this.set('loginController.loginName', '');
  342. this.set('loginController.password', '');
  343. // 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.
  344. if (!App.get('testMode') && context) {
  345. App.ajax.send({
  346. name: 'router.logoff',
  347. sender: this,
  348. data: {
  349. auth: "Basic " + hash
  350. },
  351. beforeSend: 'authBeforeSend',
  352. success: 'logOffSuccessCallback',
  353. error:'logOffErrorCallback'
  354. });
  355. }
  356. if (App.router.get('clusterController.isLoaded')) {
  357. window.location.reload();
  358. } else {
  359. this.transitionTo('login', context);
  360. }
  361. },
  362. logOffSuccessCallback: function (data) {
  363. console.log("invoked logout on the server successfully");
  364. },
  365. logOffErrorCallback: function (req) {
  366. console.log("failed to invoke logout on the server");
  367. },
  368. /**
  369. * initialize isAdmin if user is administrator
  370. */
  371. initAdmin: function(){
  372. if(App.db && App.db.getUser() && App.db.getUser().admin) {
  373. App.set('isAdmin', true);
  374. console.log('Administrator logged in');
  375. }
  376. },
  377. root: Em.Route.extend({
  378. index: Em.Route.extend({
  379. route: '/',
  380. redirectsTo: 'login'
  381. }),
  382. enter: function(router){
  383. router.initAdmin();
  384. },
  385. login: Em.Route.extend({
  386. route: '/login',
  387. /**
  388. * If the user is already logged in, redirect to where the user was previously
  389. */
  390. enter: function (router, context) {
  391. router.getAuthenticated().done(function (loggedIn) {
  392. if (loggedIn) {
  393. Ember.run.next(function () {
  394. console.log(router.getLoginName() + ' already authenticated. Redirecting...');
  395. router.getSection(function (route) {
  396. router.transitionTo(route, context);
  397. });
  398. });
  399. }
  400. });
  401. },
  402. connectOutlets: function (router, context) {
  403. $('title').text(Em.I18n.t('app.name'));
  404. console.log('/login:connectOutlet');
  405. console.log('currentStep is: ' + router.getInstallerCurrentStep());
  406. console.log('authenticated is: ' + router.getAuthenticated());
  407. router.get('applicationController').connectOutlet('login');
  408. }
  409. }),
  410. installer: require('routes/installer'),
  411. main: require('routes/main'),
  412. experimental: Em.Route.extend({
  413. route: '/experimental',
  414. enter: function (router, context) {
  415. if (!App.get('isAdmin')) {
  416. router.transitionTo("main");
  417. }
  418. },
  419. connectOutlets: function (router, context) {
  420. if (App.get('isAdmin')) {
  421. $('title').text("Ambari Experimental");
  422. console.log('/experimental:connectOutlet');
  423. router.get('applicationController').connectOutlet('experimental');
  424. }
  425. }
  426. }),
  427. logoff: function (router, context) {
  428. router.logOff(context);
  429. }
  430. })
  431. });