user_settings_controller.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. var r = 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(r[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.isAuthorized('CLUSTER.UPGRADE_DOWNGRADE_STACK')) {
  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 key;
  195. var privileges = {
  196. clusters: {},
  197. views: {}
  198. };
  199. data.items.forEach(function(privilege) {
  200. privilege = privilege.PrivilegeInfo;
  201. if(privilege.type === 'CLUSTER'){
  202. // This is cluster
  203. privileges.clusters[privilege.cluster_name] = privileges.clusters[privilege.cluster_name] || [];
  204. privileges.clusters[privilege.cluster_name].push(privilege.permission_label);
  205. } else if ( privilege.type === 'VIEW'){
  206. privileges.views[privilege.instance_name] = privileges.views[privilege.instance_name] || { privileges:[]};
  207. privileges.views[privilege.instance_name].version = privilege.version;
  208. privileges.views[privilege.instance_name].view_name = privilege.view_name;
  209. privileges.views[privilege.instance_name].privileges.push(privilege.permission_label);
  210. }
  211. });
  212. // restructure data for view
  213. var clusters = [];
  214. var views = [];
  215. for (key in privileges.clusters) {
  216. clusters.push({
  217. name: key,
  218. privileges: privileges.clusters[key]
  219. });
  220. }
  221. for (key in privileges.views) {
  222. views.push({
  223. instance_name: key,
  224. privileges: privileges.views[key].privileges,
  225. version: privileges.views[key].version,
  226. view_name: privileges.views[key].view_name
  227. });
  228. }
  229. privileges.clusters = clusters;
  230. privileges.views = views;
  231. this.set('privileges', data.items.length ? privileges : null);
  232. this.set('noClusterPriv', $.isEmptyObject(privileges.clusters));
  233. this.set('noViewPriv', $.isEmptyObject(privileges.views));
  234. this.set('hidePrivileges', this.get('noClusterPriv') && this.get('noViewPriv'));
  235. },
  236. /**
  237. * Show popup with settings for user
  238. * Don't call this method directly! Use <code>showSettingsPopup</code>
  239. *
  240. * @param {object} response
  241. * @returns {App.ModalPopup}
  242. * @method _showSettingsPopup
  243. * @private
  244. */
  245. _showSettingsPopup: function (response) {
  246. var keys = this.get('userSettingsKeys');
  247. var curValue, self, timezonesFormatted, initValue, initTimezone;
  248. if (response[keys.show_bg.name]) {
  249. curValue = null;
  250. self = this;
  251. timezonesFormatted = timezoneUtils.get('timezones');
  252. initValue = JSON.parse(response[keys.show_bg.name]);
  253. initTimezone = timezonesFormatted.findProperty('value', JSON.parse(response[keys.timezone.name]));
  254. return App.ModalPopup.show({
  255. header: Em.I18n.t('common.userSettings'),
  256. bodyClass: Em.View.extend({
  257. templateName: require('templates/common/settings'),
  258. isNotShowBgChecked: !initValue,
  259. updateValue: function () {
  260. curValue = !this.get('isNotShowBgChecked');
  261. }.observes('isNotShowBgChecked'),
  262. timezonesList: timezonesFormatted,
  263. privileges: self.get('privileges'),
  264. isAdmin: App.get('isAdmin'),
  265. noClusterPriv: self.get('noClusterPriv'),
  266. noViewPriv: self.get('noViewPriv'),
  267. hidePrivileges: self.get('hidePrivileges') || App.get('isAdmin')
  268. }),
  269. /**
  270. * @type {string}
  271. */
  272. selectedTimezone: initTimezone,
  273. primary: Em.I18n.t('common.save'),
  274. onPrimary: function () {
  275. if (Em.isNone(curValue)) {
  276. curValue = initValue;
  277. }
  278. var tz = this.get('selectedTimezone.value');
  279. var popup = this;
  280. if (!App.get('testMode')) {
  281. self.postUserPref('show_bg', curValue).always(function () {
  282. self.postUserPref('timezone', tz).always(function () {
  283. if (popup.needsPageRefresh()) {
  284. location.reload();
  285. }
  286. });
  287. });
  288. }
  289. this._super();
  290. },
  291. /**
  292. * Determines if page should be refreshed after user click "Save"
  293. *
  294. * @returns {boolean}
  295. */
  296. needsPageRefresh: function () {
  297. return initTimezone !== this.get('selectedTimezone');
  298. }
  299. });
  300. } else {
  301. App.showAlertPopup(Em.I18n.t('common.error'), Em.I18n.t('app.settings.noData'));
  302. }
  303. }
  304. });