user_settings_controller.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. var timezoneUtils = require('utils/date/timezone');
  20. /**
  21. * Controller for user settings
  22. * Allows to get them from persist and update them to the persist
  23. *
  24. * @class UserSettingsController
  25. */
  26. App.UserSettingsController = Em.Controller.extend(App.UserPref, {
  27. name: 'userSettingsController',
  28. /**
  29. * @type {object}
  30. */
  31. userSettings: {},
  32. /**
  33. * Each property's type is {name: string, defaultValue: *, formatter: function}
  34. *
  35. * @type {object}
  36. */
  37. userSettingsKeys: function () {
  38. var loginName = App.router.get('loginName');
  39. var prefix = 'admin-settings-';
  40. return {
  41. show_bg: {
  42. name: prefix +'show-bg-' + loginName,
  43. defaultValue: true
  44. },
  45. timezone: {
  46. name: prefix + 'timezone-' + loginName,
  47. defaultValue: timezoneUtils.detectUserTimezone(),
  48. formatter: function (v) {
  49. return timezoneUtils.get('timezonesMappedByValue')[v];
  50. }
  51. }
  52. };
  53. }.property('App.router.loginName'),
  54. /**
  55. * Load some user's setting from the persist
  56. * If <code>persistKey</code> is not provided, all settings are loaded
  57. *
  58. * @param {string} [persistKey]
  59. * @method dataLoading
  60. * @returns {$.Deferred.promise}
  61. */
  62. dataLoading: function (persistKey) {
  63. var key = persistKey ? this.get('userSettingsKeys.' + persistKey + '.name') : '';
  64. var dfd = $.Deferred();
  65. var self = this;
  66. this.getUserPref(key).complete(function () {
  67. var curPref = self.get('currentPrefObject');
  68. self.set('currentPrefObject', null);
  69. dfd.resolve(curPref);
  70. });
  71. return dfd.promise();
  72. },
  73. /**
  74. * Success-callback for user pref
  75. *
  76. * @param {*} response
  77. * @param {object} opt
  78. * @returns {*}
  79. * @method getUserPrefSuccessCallback
  80. */
  81. getUserPrefSuccessCallback: function (response, opt) {
  82. var getAllRequest = opt.url.contains('persist/?_');
  83. if (Em.isNone(response)) {
  84. this.updateUserPrefWithDefaultValues(response, getAllRequest);
  85. }
  86. this.set('currentPrefObject', response);
  87. return response;
  88. },
  89. /**
  90. * Error-callback for user-pref request
  91. * Update user pref with default values if user firstly login
  92. *
  93. * @param {object} request
  94. * @method getUserPrefErrorCallback
  95. */
  96. getUserPrefErrorCallback: function (request) {
  97. // this user is first time login
  98. if (404 == request.status) {
  99. this.updateUserPrefWithDefaultValues();
  100. }
  101. },
  102. /**
  103. * Load all current user's settings to the <code>userSettings</code>
  104. *
  105. * @method getAllUserSettings
  106. */
  107. getAllUserSettings: function () {
  108. var userSettingsKeys = this.get('userSettingsKeys');
  109. var userSettings = {};
  110. var self = this;
  111. this.dataLoading().done(function (json) {
  112. Object.keys(userSettingsKeys).forEach(function (k) {
  113. var value = userSettingsKeys[k].defaultValue;
  114. if (undefined === json[userSettingsKeys[k].name]) {
  115. self.postUserPref(k, userSettingsKeys[k].defaultValue);
  116. }
  117. else {
  118. value = JSON.parse(json[userSettingsKeys[k].name]);
  119. }
  120. if ('function' === Em.typeOf(userSettingsKeys[k].formatter)) {
  121. value = userSettingsKeys[k].formatter(value);
  122. }
  123. userSettings[k] = value;
  124. });
  125. });
  126. this.set('userSettings', userSettings);
  127. },
  128. /**
  129. * If user doesn't have any settings stored in the persist,
  130. * default values should be populated there
  131. *
  132. * @param {object} [response]
  133. * @param {boolean} [getAllRequest] determines, if user tried to get one field or all fields
  134. * @method updateUserPrefWithDefaultValues
  135. */
  136. updateUserPrefWithDefaultValues: function (response, getAllRequest) {
  137. response = response || {};
  138. var keys = this.get('userSettingsKeys');
  139. var self = this;
  140. if (getAllRequest) {
  141. Object.keys(keys).forEach(function (key) {
  142. if (Em.isNone(response[keys[key].name])) {
  143. self.postUserPref(key, keys[key].defaultValue);
  144. }
  145. });
  146. }
  147. },
  148. /**
  149. * "Short"-key method for post user settings to the persist
  150. * Example:
  151. * real key is something like 'userSettingsKeys.timezone.name'
  152. * but user should call this method with 'timezone'
  153. *
  154. * @method postUserPref
  155. * @param {string} key
  156. * @param {*} value
  157. * @returns {*}
  158. */
  159. postUserPref: function (key, value) {
  160. var k = key.startsWith('userSettingsKeys.') ? key : 'userSettingsKeys.' + key + '.name';
  161. var kk = k.replace('userSettingsKeys.', '').replace('.name', '');
  162. this.set('userSettings.' + kk, value);
  163. return this._super(this.get(k), value);
  164. },
  165. /**
  166. * Open popup with user settings after settings-request is complete
  167. *
  168. * @method showSettingsPopup
  169. */
  170. showSettingsPopup: function() {
  171. var self = this;
  172. // Settings only for admins
  173. if (!App.isAccessible('upgrade_ADMIN')) {
  174. return;
  175. }
  176. this.dataLoading()
  177. .done(function(response) {
  178. self.loadPrivileges().complete(function() {
  179. self._showSettingsPopup(response);
  180. });
  181. });
  182. },
  183. loadPrivileges: function() {
  184. return App.ajax.send({
  185. name: 'router.user.privileges',
  186. sender: this,
  187. data: {
  188. userName: App.db.getLoginName()
  189. },
  190. success: 'loadPrivilegesSuccessCallback'
  191. });
  192. },
  193. loadPrivilegesSuccessCallback: function(data) {
  194. var privileges = {
  195. clusters: {},
  196. views: {}
  197. };
  198. data.items.forEach(function(privilege) {
  199. privilege = privilege.PrivilegeInfo;
  200. if(privilege.type === 'CLUSTER'){
  201. // This is cluster
  202. privileges.clusters[privilege.cluster_name] = privileges.clusters[privilege.cluster_name] || [];
  203. privileges.clusters[privilege.cluster_name].push(privilege.permission_label);
  204. } else if ( privilege.type === 'VIEW'){
  205. privileges.views[privilege.instance_name] = privileges.views[privilege.instance_name] || { privileges:[]};
  206. privileges.views[privilege.instance_name].version = privilege.version;
  207. privileges.views[privilege.instance_name].view_name = privilege.view_name;
  208. privileges.views[privilege.instance_name].privileges.push(privilege.permission_label);
  209. }
  210. });
  211. // restructure data for view
  212. var clusters = [];
  213. var views = [];
  214. for (key in privileges.clusters){
  215. clusters.push({
  216. name: key,
  217. privileges: privileges.clusters[key]
  218. });
  219. }
  220. for (key in privileges.views) {
  221. views.push({
  222. instance_name: key,
  223. privileges: privileges.views[key].privileges,
  224. version: privileges.views[key].version,
  225. view_name: privileges.views[key].view_name
  226. });
  227. }
  228. privileges.clusters = clusters;
  229. privileges.views = views;
  230. this.set('privileges', data.items.length ? privileges : null);
  231. },
  232. /**
  233. * Show popup with settings for user
  234. * Don't call this method directly! Use <code>showSettingsPopup</code>
  235. *
  236. * @param {object} response
  237. * @returns {App.ModalPopup}
  238. * @method _showSettingsPopup
  239. * @private
  240. */
  241. _showSettingsPopup: function (response) {
  242. var curValue = null;
  243. var self = this;
  244. var keys = this.get('userSettingsKeys');
  245. var timezonesFormatted = timezoneUtils.get('timezones');
  246. var initValue = JSON.parse(response[keys.show_bg.name]);
  247. var initTimezone = timezonesFormatted.findProperty('value', JSON.parse(response[keys.timezone.name]));
  248. return App.ModalPopup.show({
  249. header: Em.I18n.t('common.userSettings'),
  250. bodyClass: Em.View.extend({
  251. templateName: require('templates/common/settings'),
  252. isNotShowBgChecked: !initValue,
  253. updateValue: function () {
  254. curValue = !this.get('isNotShowBgChecked');
  255. }.observes('isNotShowBgChecked'),
  256. timezonesList: timezonesFormatted,
  257. privileges: self.get('privileges'),
  258. isAdmin: App.get('isAmbariAdmin')
  259. }),
  260. /**
  261. * @type {string}
  262. */
  263. selectedTimezone: initTimezone,
  264. primary: Em.I18n.t('common.save'),
  265. onPrimary: function() {
  266. if (Em.isNone(curValue)) {
  267. curValue = initValue;
  268. }
  269. if (!App.get('testMode')) {
  270. self.postUserPref('show_bg', curValue);
  271. self.postUserPref('timezone', this.get('selectedTimezone.value'));
  272. }
  273. if (this.needsPageRefresh()) {
  274. location.reload();
  275. }
  276. this._super();
  277. },
  278. /**
  279. * Determines if page should be refreshed after user click "Save"
  280. *
  281. * @returns {boolean}
  282. */
  283. needsPageRefresh: function () {
  284. return initTimezone !== this.get('selectedTimezone');
  285. }
  286. })
  287. }
  288. });