router.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. error: 'getUserPrivilegesError'
  150. });
  151. },
  152. getUserPrivilegesSuccess: function() {},
  153. getUserPrivilegesError: function(req) {
  154. console.log("Get user privileges error: " + req.statusCode);
  155. },
  156. setUserLoggedIn: function(userName) {
  157. this.setAuthenticated(true);
  158. this.setLoginName(userName);
  159. this.setUser(App.User.find().findProperty('id', userName));
  160. },
  161. login: function () {
  162. var controller = this.get('loginController');
  163. var loginName = controller.get('loginName').toLowerCase();
  164. controller.set('loginName', loginName);
  165. var hash = window.btoa(loginName + ":" + controller.get('password'));
  166. var usr = '';
  167. if (App.get('testMode')) {
  168. if (loginName === "admin" && controller.get('password') === 'admin') {
  169. usr = 'admin';
  170. } else if (loginName === 'user' && controller.get('password') === 'user') {
  171. usr = 'user';
  172. }
  173. }
  174. App.ajax.send({
  175. name: 'router.login',
  176. sender: this,
  177. data: {
  178. auth: "Basic " + hash,
  179. usr: usr,
  180. loginName: loginName
  181. },
  182. beforeSend: 'authBeforeSend',
  183. success: 'loginSuccessCallback',
  184. error: 'loginErrorCallback'
  185. });
  186. },
  187. authBeforeSend: function(opt, xhr, data) {
  188. xhr.setRequestHeader("Authorization", data.auth);
  189. },
  190. loginSuccessCallback: function(data, opt, params) {
  191. console.log('login success');
  192. App.ajax.send({
  193. name: 'router.login.clusters',
  194. sender: this,
  195. data: {
  196. loginName: params.loginName,
  197. loginData: data
  198. },
  199. success: 'loginGetClustersSuccessCallback',
  200. error: 'loginGetClustersErrorCallback'
  201. });
  202. },
  203. loginErrorCallback: function(request, ajaxOptions, error, opt) {
  204. var controller = this.get('loginController');
  205. console.log("login error: " + error);
  206. this.setAuthenticated(false);
  207. if (request.status == 403) {
  208. controller.postLogin(true, false);
  209. } else {
  210. controller.postLogin(false, false);
  211. }
  212. },
  213. loginGetClustersSuccessCallback: function (clustersData, opt, params) {
  214. var adminViewUrl = '/views/ADMIN_VIEW/1.0.0/INSTANCE/#/';
  215. //TODO: Replace hard coded value with query. Same in templates/application.hbs
  216. var loginController = this.get('loginController');
  217. var loginData = params.loginData;
  218. var router = this;
  219. this.getUserPrivileges(params.loginName).done(function(privileges) {
  220. loginData.privileges = privileges;
  221. App.usersMapper.map({"items": [loginData]});
  222. router.setUserLoggedIn(params.loginName);
  223. var permissionList = privileges.items.mapProperty('PrivilegeInfo.permission_name');
  224. var isAdmin = permissionList.indexOf('AMBARI.ADMIN') > -1;
  225. var transitionToApp = false;
  226. if (isAdmin) {
  227. App.set('isAdmin', true);
  228. if (clustersData.items.length) {
  229. transitionToApp = true;
  230. } else {
  231. window.location = adminViewUrl;
  232. return;
  233. }
  234. } else {
  235. if (clustersData.items.length) {
  236. //TODO: Iterate over clusters
  237. var clusterName = clustersData.items[0].Clusters.cluster_name;
  238. var clusterPermissions = privileges.items.filterProperty('PrivilegeInfo.cluster_name', clusterName).mapProperty('PrivilegeInfo.permission_name');
  239. if (clusterPermissions.indexOf('CLUSTER.OPERATE') > -1) {
  240. App.set('isAdmin', true);
  241. App.set('isOperator', true);
  242. transitionToApp = true;
  243. } else if (clusterPermissions.indexOf('CLUSTER.READ') > -1) {
  244. transitionToApp = true;
  245. }
  246. }
  247. }
  248. if (transitionToApp) {
  249. router.getSection(function (route) {
  250. router.transitionTo(route);
  251. loginController.postLogin(true, true);
  252. });
  253. } else {
  254. router.transitionTo('views.index');
  255. loginController.postLogin(true,true);
  256. }
  257. });
  258. },
  259. loginGetClustersErrorCallback: function (req) {
  260. console.log("Get clusters error: " + req.statusCode);
  261. },
  262. getSection: function (callback) {
  263. if (App.get('testMode')) {
  264. if (App.alwaysGoToInstaller) {
  265. callback('installer');
  266. } else {
  267. callback('main.dashboard.index');
  268. }
  269. }
  270. App.clusterStatus.updateFromServer(false).complete(function () {
  271. var clusterStatusOnServer = App.clusterStatus.get('value');
  272. // if wizardControllerName == "installerController", then it means someone closed the browser or the browser was crashed when we were last in Installer wizard
  273. var route = 'installer';
  274. if (!App.get('isAdmin') || clusterStatusOnServer && (clusterStatusOnServer.clusterState === 'DEFAULT' || clusterStatusOnServer.clusterState === 'CLUSTER_STARTED_5')) {
  275. route = 'main.dashboard.index';
  276. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('addHostController.name')) {
  277. // if wizardControllerName == "addHostController", then it means someone closed the browser or the browser was crashed when we were last in Add Hosts wizard
  278. route = 'main.hostAdd';
  279. } else if (clusterStatusOnServer && (clusterStatusOnServer.wizardControllerName === App.router.get('addSecurityController.name') || clusterStatusOnServer.wizardControllerName === App.router.get('mainAdminSecurityDisableController.name'))) {
  280. // if wizardControllerName == "addSecurityController", then it means someone closed the browser or the browser was crashed when we were last in Add Security wizard
  281. route = 'main.admin.adminSecurity';
  282. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('addServiceController.name')) {
  283. // if wizardControllerName == "addHostController", then it means someone closed the browser or the browser was crashed when we were last in Add Hosts wizard
  284. route = 'main.serviceAdd';
  285. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('stackUpgradeController.name')) {
  286. // if wizardControllerName == "stackUpgradeController", then it means someone closed the browser or the browser was crashed when we were last in Stack Upgrade wizard
  287. route = 'main.stackUpgrade';
  288. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('reassignMasterController.name')) {
  289. // if wizardControllerName == "reassignMasterController", then it means someone closed the browser or the browser was crashed when we were last in Reassign Master wizard
  290. route = 'main.reassign';
  291. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('highAvailabilityWizardController.name')) {
  292. // if wizardControllerName == "highAvailabilityWizardController", then it means someone closed the browser or the browser was crashed when we were last in NameNode High Availability wizard
  293. route = 'main.services.enableHighAvailability';
  294. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('rMHighAvailabilityWizardController.name')) {
  295. // if wizardControllerName == "highAvailabilityWizardController", then it means someone closed the browser or the browser was crashed when we were last in NameNode High Availability wizard
  296. route = 'main.services.enableRMHighAvailability';
  297. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('rollbackHighAvailabilityWizardController.name')) {
  298. // 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
  299. route = 'main.services.rollbackHighAvailability';
  300. }
  301. callback(route);
  302. });
  303. },
  304. logOff: function (context) {
  305. $('title').text('Ambari');
  306. var hash = window.btoa(this.get('loginController.loginName') + ":" + this.get('loginController.password'));
  307. App.router.get('mainController').stopPolling();
  308. // App.db.cleanUp() must be called before router.clearAllSteps().
  309. // otherwise, this.set('installerController.currentStep, 0) would have no effect
  310. // since it's a computed property but we are not setting it as a dependent of App.db.
  311. App.db.cleanUp();
  312. App.set('isAdmin', false);
  313. App.set('isOperator', false);
  314. this.set('loggedIn', false);
  315. this.clearAllSteps();
  316. console.log("Log off: " + App.router.getClusterName());
  317. this.set('loginController.loginName', '');
  318. this.set('loginController.password', '');
  319. // 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.
  320. if (!App.get('testMode') && context) {
  321. App.ajax.send({
  322. name: 'router.logoff',
  323. sender: this,
  324. data: {
  325. auth: "Basic " + hash
  326. },
  327. beforeSend: 'authBeforeSend',
  328. success: 'logOffSuccessCallback',
  329. error:'logOffErrorCallback'
  330. });
  331. }
  332. if (App.router.get('clusterController.isLoaded')) {
  333. window.location.reload();
  334. } else {
  335. this.transitionTo('login', context);
  336. }
  337. },
  338. logOffSuccessCallback: function (data) {
  339. console.log("invoked logout on the server successfully");
  340. },
  341. logOffErrorCallback: function (req) {
  342. console.log("failed to invoke logout on the server");
  343. },
  344. /**
  345. * initialize isAdmin if user is administrator
  346. */
  347. initAdmin: function(){
  348. if(App.db && App.db.getUser() && App.db.getUser().admin) {
  349. App.set('isAdmin', true);
  350. console.log('Administrator logged in');
  351. }
  352. },
  353. root: Em.Route.extend({
  354. index: Em.Route.extend({
  355. route: '/',
  356. redirectsTo: 'login'
  357. }),
  358. enter: function(router){
  359. router.initAdmin();
  360. },
  361. login: Em.Route.extend({
  362. route: '/login',
  363. /**
  364. * If the user is already logged in, redirect to where the user was previously
  365. */
  366. enter: function (router, context) {
  367. router.getAuthenticated().done(function (loggedIn) {
  368. if (loggedIn) {
  369. Ember.run.next(function () {
  370. console.log(router.getLoginName() + ' already authenticated. Redirecting...');
  371. router.getSection(function (route) {
  372. router.transitionTo(route, context);
  373. });
  374. });
  375. }
  376. });
  377. },
  378. connectOutlets: function (router, context) {
  379. $('title').text(Em.I18n.t('app.name'));
  380. console.log('/login:connectOutlet');
  381. console.log('currentStep is: ' + router.getInstallerCurrentStep());
  382. console.log('authenticated is: ' + router.getAuthenticated());
  383. router.get('applicationController').connectOutlet('login');
  384. }
  385. }),
  386. installer: require('routes/installer'),
  387. main: require('routes/main'),
  388. experimental: Em.Route.extend({
  389. route: '/experimental',
  390. enter: function (router, context) {
  391. if (!App.get('isAdmin')) {
  392. router.transitionTo("main");
  393. }
  394. },
  395. connectOutlets: function (router, context) {
  396. if (App.get('isAdmin')) {
  397. $('title').text("Ambari Experimental");
  398. console.log('/experimental:connectOutlet');
  399. router.get('applicationController').connectOutlet('experimental');
  400. }
  401. }
  402. }),
  403. logoff: function (router, context) {
  404. router.logOff(context);
  405. }
  406. })
  407. });