router.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. transitionToApp = true;
  242. } else if (clusterPermissions.indexOf('CLUSTER.READ') > -1) {
  243. transitionToApp = true;
  244. }
  245. }
  246. }
  247. if (transitionToApp) {
  248. router.getSection(function (route) {
  249. router.transitionTo(route);
  250. loginController.postLogin(true, true);
  251. });
  252. } else {
  253. router.transitionTo('views.index');
  254. loginController.postLogin(true,true);
  255. }
  256. });
  257. },
  258. loginGetClustersErrorCallback: function (req) {
  259. console.log("Get clusters error: " + req.statusCode);
  260. },
  261. getSection: function (callback) {
  262. if (App.get('testMode')) {
  263. if (App.alwaysGoToInstaller) {
  264. callback('installer');
  265. } else {
  266. callback('main.dashboard.index');
  267. }
  268. }
  269. App.clusterStatus.updateFromServer(false).complete(function () {
  270. var clusterStatusOnServer = App.clusterStatus.get('value');
  271. // if wizardControllerName == "installerController", then it means someone closed the browser or the browser was crashed when we were last in Installer wizard
  272. var route = 'installer';
  273. if (!App.get('isAdmin') || clusterStatusOnServer && (clusterStatusOnServer.clusterState === 'DEFAULT' || clusterStatusOnServer.clusterState === 'CLUSTER_STARTED_5')) {
  274. route = 'main.dashboard.index';
  275. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('addHostController.name')) {
  276. // if wizardControllerName == "addHostController", then it means someone closed the browser or the browser was crashed when we were last in Add Hosts wizard
  277. route = 'main.hostAdd';
  278. } else if (clusterStatusOnServer && (clusterStatusOnServer.wizardControllerName === App.router.get('addSecurityController.name') || clusterStatusOnServer.wizardControllerName === App.router.get('mainAdminSecurityDisableController.name'))) {
  279. // if wizardControllerName == "addSecurityController", then it means someone closed the browser or the browser was crashed when we were last in Add Security wizard
  280. route = 'main.admin.adminSecurity';
  281. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('addServiceController.name')) {
  282. // if wizardControllerName == "addHostController", then it means someone closed the browser or the browser was crashed when we were last in Add Hosts wizard
  283. route = 'main.serviceAdd';
  284. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('stackUpgradeController.name')) {
  285. // if wizardControllerName == "stackUpgradeController", then it means someone closed the browser or the browser was crashed when we were last in Stack Upgrade wizard
  286. route = 'main.stackUpgrade';
  287. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('reassignMasterController.name')) {
  288. // if wizardControllerName == "reassignMasterController", then it means someone closed the browser or the browser was crashed when we were last in Reassign Master wizard
  289. route = 'main.reassign';
  290. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('highAvailabilityWizardController.name')) {
  291. // if wizardControllerName == "highAvailabilityWizardController", then it means someone closed the browser or the browser was crashed when we were last in NameNode High Availability wizard
  292. route = 'main.services.enableHighAvailability';
  293. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('rMHighAvailabilityWizardController.name')) {
  294. // if wizardControllerName == "highAvailabilityWizardController", then it means someone closed the browser or the browser was crashed when we were last in NameNode High Availability wizard
  295. route = 'main.services.enableRMHighAvailability';
  296. } else if (clusterStatusOnServer && clusterStatusOnServer.wizardControllerName === App.router.get('rollbackHighAvailabilityWizardController.name')) {
  297. // 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
  298. route = 'main.services.rollbackHighAvailability';
  299. }
  300. callback(route);
  301. });
  302. },
  303. logOff: function (context) {
  304. $('title').text('Ambari');
  305. var hash = window.btoa(this.get('loginController.loginName') + ":" + this.get('loginController.password'));
  306. App.router.get('mainController').stopPolling();
  307. // App.db.cleanUp() must be called before router.clearAllSteps().
  308. // otherwise, this.set('installerController.currentStep, 0) would have no effect
  309. // since it's a computed property but we are not setting it as a dependent of App.db.
  310. App.db.cleanUp();
  311. App.set('isAdmin', false);
  312. this.set('loggedIn', false);
  313. this.clearAllSteps();
  314. console.log("Log off: " + App.router.getClusterName());
  315. this.set('loginController.loginName', '');
  316. this.set('loginController.password', '');
  317. // 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.
  318. if (!App.get('testMode') && context) {
  319. App.ajax.send({
  320. name: 'router.logoff',
  321. sender: this,
  322. data: {
  323. auth: "Basic " + hash
  324. },
  325. beforeSend: 'authBeforeSend',
  326. success: 'logOffSuccessCallback',
  327. error:'logOffErrorCallback'
  328. });
  329. }
  330. this.transitionTo('login', context);
  331. window.location.reload();
  332. },
  333. logOffSuccessCallback: function (data) {
  334. console.log("invoked logout on the server successfully");
  335. },
  336. logOffErrorCallback: function (req) {
  337. console.log("failed to invoke logout on the server");
  338. },
  339. /**
  340. * initialize isAdmin if user is administrator
  341. */
  342. initAdmin: function(){
  343. if(App.db && App.db.getUser() && App.db.getUser().admin) {
  344. App.set('isAdmin', true);
  345. console.log('Administrator logged in');
  346. }
  347. },
  348. root: Em.Route.extend({
  349. index: Em.Route.extend({
  350. route: '/',
  351. redirectsTo: 'login'
  352. }),
  353. enter: function(router){
  354. router.initAdmin();
  355. },
  356. login: Em.Route.extend({
  357. route: '/login',
  358. /**
  359. * If the user is already logged in, redirect to where the user was previously
  360. */
  361. enter: function (router, context) {
  362. router.getAuthenticated().done(function (loggedIn) {
  363. if (loggedIn) {
  364. Ember.run.next(function () {
  365. console.log(router.getLoginName() + ' already authenticated. Redirecting...');
  366. router.getSection(function (route) {
  367. router.transitionTo(route, context);
  368. });
  369. });
  370. }
  371. });
  372. },
  373. connectOutlets: function (router, context) {
  374. $('title').text(Em.I18n.t('app.name'));
  375. console.log('/login:connectOutlet');
  376. console.log('currentStep is: ' + router.getInstallerCurrentStep());
  377. console.log('authenticated is: ' + router.getAuthenticated());
  378. router.get('applicationController').connectOutlet('login');
  379. }
  380. }),
  381. installer: require('routes/installer'),
  382. main: require('routes/main'),
  383. views: require('routes/views'),
  384. experimental: Em.Route.extend({
  385. route: '/experimental',
  386. enter: function (router, context) {
  387. },
  388. connectOutlets: function (router, context) {
  389. $('title').text("Ambari Experimental");
  390. console.log('/experimental:connectOutlet');
  391. router.get('applicationController').connectOutlet('experimental');
  392. }
  393. }),
  394. logoff: function (router, context) {
  395. router.logOff(context);
  396. }
  397. })
  398. });