view.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /**
  2. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-FileCopyrightText: 2015-2016 ownCloud, Inc.
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. OCA = OCA || {};
  7. (function() {
  8. /**
  9. * @classdesc main view class. It takes care of tab-unrelated control
  10. * elements (status bar, control buttons) and does or requests configuration
  11. * checks. It also manages the separate tab views.
  12. *
  13. * @constructor
  14. */
  15. var WizardView = function() {};
  16. WizardView.prototype = {
  17. /** @constant {number} */
  18. STATUS_ERROR: 0,
  19. /** @constant {number} */
  20. STATUS_INCOMPLETE: 1,
  21. /** @constant {number} */
  22. STATUS_SUCCESS: 2,
  23. /** @constant {number} */
  24. STATUS_UNTESTED: 3,
  25. /**
  26. * initializes the instance. Always call it after creating the instance.
  27. */
  28. init: function () {
  29. this.tabs = {};
  30. this.tabs.server = new OCA.LDAP.Wizard.WizardTabElementary();
  31. this.$settings = $('#ldapSettings');
  32. this.$saveSpinners = $('.ldap_saving');
  33. this.saveProcesses = 0;
  34. _.bindAll(this, 'onTabChange', 'onTestButtonClick');
  35. },
  36. /**
  37. * applies click events to the forward and backward buttons
  38. */
  39. initControls: function() {
  40. var view = this;
  41. $('.ldap_action_continue').click(function(event) {
  42. event.preventDefault();
  43. view._controlContinue(view);
  44. });
  45. $('.ldap_action_back').click(function(event) {
  46. event.preventDefault();
  47. view._controlBack(view);
  48. });
  49. $('.ldap_action_test_connection').click(this.onTestButtonClick);
  50. },
  51. /**
  52. * registers a tab
  53. *
  54. * @param {OCA.LDAP.Wizard.WizardTabGeneric} tabView
  55. * @param {string} index
  56. * @returns {boolean}
  57. */
  58. registerTab: function(tabView, index) {
  59. if( _.isUndefined(this.tabs[index])
  60. && tabView instanceof OCA.LDAP.Wizard.WizardTabGeneric
  61. ) {
  62. this.tabs[index] = tabView;
  63. this.tabs[index].setModel(this.configModel);
  64. return true;
  65. }
  66. return false;
  67. },
  68. /**
  69. * checks certain config values for completeness and depending on them
  70. * enables or disables non-elementary tabs.
  71. */
  72. basicStatusCheck: function(view) {
  73. var host = view.configModel.configuration.ldap_host;
  74. var port = view.configModel.configuration.ldap_port;
  75. var base = view.configModel.configuration.ldap_base;
  76. var agent = view.configModel.configuration.ldap_dn;
  77. var pwd = view.configModel.configuration.ldap_agent_password;
  78. if(((host && port && base) || (host && base && host.indexOf('ldapi://') > -1 ))
  79. && ((!agent && !pwd) || (agent && pwd))) {
  80. view.enableTabs();
  81. } else {
  82. view.disableTabs();
  83. }
  84. },
  85. /**
  86. * if the configuration is sufficient the model is being request to
  87. * perform a configuration test. Otherwise, the status indicator is
  88. * being updated with the status "incomplete"
  89. */
  90. functionalityCheck: function() {
  91. // this method should be called only if necessary, because it may
  92. // cause an LDAP request!
  93. var host = this.configModel.configuration.ldap_host;
  94. var port = this.configModel.configuration.ldap_port;
  95. var base = this.configModel.configuration.ldap_base;
  96. var userFilter = this.configModel.configuration.ldap_userlist_filter;
  97. var loginFilter = this.configModel.configuration.ldap_login_filter;
  98. if((host && port && base && userFilter && loginFilter) ||
  99. (host && base && host.indexOf('ldapi://') > -1 && userFilter && loginFilter)) {
  100. this.configModel.requestConfigurationTest();
  101. } else {
  102. this._updateStatusIndicator(this.STATUS_INCOMPLETE);
  103. }
  104. },
  105. /**
  106. * will request a functionality check if one of the related configuration
  107. * settings was changed.
  108. *
  109. * @param {ConfigSetPayload|Object} [changeSet]
  110. */
  111. considerFunctionalityCheck: function(changeSet) {
  112. var testTriggers = [
  113. 'ldap_host', 'ldap_port', 'ldap_dn', 'ldap_agent_password',
  114. 'ldap_base', 'ldap_userlist_filter', 'ldap_login_filter'
  115. ];
  116. for(var key in changeSet) {
  117. if($.inArray(key, testTriggers) >= 0) {
  118. this.functionalityCheck();
  119. return;
  120. }
  121. }
  122. },
  123. /**
  124. * keeps number of running save processes and shows a spinner if
  125. * necessary
  126. *
  127. * @param {WizardView} [view]
  128. * @listens ConfigModel#setRequested
  129. */
  130. onSetRequested: function(view) {
  131. view.saveProcesses += 1;
  132. if(view.saveProcesses === 1) {
  133. view.showSaveSpinner();
  134. }
  135. },
  136. /**
  137. * keeps number of running save processes and hides the spinner if
  138. * necessary. Also triggers checks, to adjust tabs state and status bar.
  139. *
  140. * @param {WizardView} [view]
  141. * @param {ConfigSetPayload} [result]
  142. * @listens ConfigModel#setCompleted
  143. */
  144. onSetRequestDone: function(view, result) {
  145. if(view.saveProcesses > 0) {
  146. view.saveProcesses -= 1;
  147. if(view.saveProcesses === 0) {
  148. view.hideSaveSpinner();
  149. }
  150. }
  151. view.basicStatusCheck(view);
  152. var param = {};
  153. param[result.key] = 1;
  154. view.considerFunctionalityCheck(param);
  155. },
  156. /**
  157. * Base DN test results will arrive here
  158. *
  159. * @param {WizardTabElementary} view
  160. * @param {FeaturePayload} payload
  161. */
  162. onDetectionTestCompleted: function(view, payload) {
  163. if(payload.feature === 'TestBaseDN') {
  164. if(payload.data.status === 'success') {
  165. var objectsFound = parseInt(payload.data.changes.ldap_test_base, 10);
  166. if(objectsFound > 0) {
  167. view._updateStatusIndicator(view.STATUS_SUCCESS);
  168. return;
  169. }
  170. }
  171. view._updateStatusIndicator(view.STATUS_ERROR);
  172. OC.Notification.showTemporary(t('user_ldap', 'The Base DN appears to be wrong'));
  173. }
  174. },
  175. /**
  176. * updates the status indicator based on the configuration test result
  177. *
  178. * @param {WizardView} [view]
  179. * @param {ConfigTestPayload} [result]
  180. * @listens ConfigModel#configurationTested
  181. */
  182. onTestCompleted: function(view, result) {
  183. if(result.isSuccess) {
  184. view.configModel.requestWizard('ldap_test_base');
  185. } else {
  186. view._updateStatusIndicator(view.STATUS_ERROR);
  187. }
  188. },
  189. /**
  190. * triggers initial checks upon configuration loading to update status
  191. * controls
  192. *
  193. * @param {WizardView} [view]
  194. * @listens ConfigModel#configLoaded
  195. */
  196. onConfigLoaded: function(view) {
  197. view._updateStatusIndicator(view.STATUS_UNTESTED);
  198. view.basicStatusCheck(view);
  199. view.functionalityCheck();
  200. },
  201. /**
  202. * reacts on attempts to switch to a different tab
  203. *
  204. * @param {object} event
  205. * @param {object} ui
  206. * @returns {boolean}
  207. */
  208. onTabChange: function(event, ui) {
  209. if(this.saveProcesses > 0) {
  210. return false;
  211. }
  212. var newTabID = ui.newTab[0].id;
  213. if(newTabID === '#ldapWizard1') {
  214. newTabID = 'server';
  215. }
  216. var oldTabID = ui.oldTab[0].id;
  217. if(oldTabID === '#ldapWizard1') {
  218. oldTabID = 'server';
  219. }
  220. if(!_.isUndefined(this.tabs[newTabID])) {
  221. this.tabs[newTabID].isActive = true;
  222. this.tabs[newTabID].onActivate();
  223. } else {
  224. console.warn('Unreferenced activated tab ' + newTabID);
  225. }
  226. if(!_.isUndefined(this.tabs[oldTabID])) {
  227. this.tabs[oldTabID].isActive = false;
  228. } else {
  229. console.warn('Unreferenced left tab ' + oldTabID);
  230. }
  231. if(!_.isUndefined(this.tabs[newTabID])) {
  232. this._controlUpdate(this.tabs[newTabID].tabIndex);
  233. }
  234. },
  235. /**
  236. * triggers checks upon configuration updates to keep status controls
  237. * up to date
  238. *
  239. * @param {WizardView} [view]
  240. * @param {object} [changeSet]
  241. * @listens ConfigModel#configUpdated
  242. */
  243. onConfigUpdated: function(view, changeSet) {
  244. view.basicStatusCheck(view);
  245. view.considerFunctionalityCheck(changeSet);
  246. },
  247. /**
  248. * requests a configuration test
  249. */
  250. onTestButtonClick: function() {
  251. this.configModel.requestWizard('ldap_action_test_connection', {ldap_serverconfig_chooser: this.configModel.configID});
  252. },
  253. /**
  254. * sets the model instance and registers event listeners
  255. *
  256. * @param {OCA.LDAP.Wizard.ConfigModel} [configModel]
  257. */
  258. setModel: function(configModel) {
  259. /** @type {OCA.LDAP.Wizard.ConfigModel} */
  260. this.configModel = configModel;
  261. for(var i in this.tabs) {
  262. this.tabs[i].setModel(configModel);
  263. }
  264. // make sure this is definitely run after tabs did their work, order is important here
  265. // for now this works, because tabs are supposed to register their listeners in their
  266. // setModel() method.
  267. // alternative: make Elementary Tab a Publisher as well.
  268. this.configModel.on('configLoaded', this.onConfigLoaded, this);
  269. this.configModel.on('configUpdated', this.onConfigUpdated, this);
  270. this.configModel.on('setRequested', this.onSetRequested, this);
  271. this.configModel.on('setCompleted', this.onSetRequestDone, this);
  272. this.configModel.on('configurationTested', this.onTestCompleted, this);
  273. this.configModel.on('receivedLdapFeature', this.onDetectionTestCompleted, this);
  274. },
  275. /**
  276. * enables tab and navigation buttons
  277. */
  278. enableTabs: function() {
  279. //do not use this function directly, use basicStatusCheck instead.
  280. if(this.saveProcesses === 0) {
  281. $('.ldap_action_continue').removeAttr('disabled');
  282. $('.ldap_action_back').removeAttr('disabled');
  283. this.$settings.tabs('option', 'disabled', []);
  284. }
  285. },
  286. /**
  287. * disables tab and navigation buttons
  288. */
  289. disableTabs: function() {
  290. $('.ldap_action_continue').attr('disabled', 'disabled');
  291. $('.ldap_action_back').attr('disabled', 'disabled');
  292. this.$settings.tabs('option', 'disabled', [1, 2, 3, 4, 5]);
  293. },
  294. /**
  295. * shows a save spinner
  296. */
  297. showSaveSpinner: function() {
  298. this.$saveSpinners.removeClass('hidden');
  299. $('#ldap *').addClass('save-cursor');
  300. },
  301. /**
  302. * hides the save spinner
  303. */
  304. hideSaveSpinner: function() {
  305. this.$saveSpinners.addClass('hidden');
  306. $('#ldap *').removeClass('save-cursor');
  307. },
  308. /**
  309. * performs a config load request to the model
  310. *
  311. * @param {string} [configID]
  312. * @private
  313. */
  314. _requestConfig: function(configID) {
  315. this.configModel.load(configID);
  316. },
  317. /**
  318. * bootstraps the visual appearance and event listeners, as well as the
  319. * first config
  320. */
  321. render: function () {
  322. $('#ldapAdvancedAccordion').accordion({ heightStyle: 'content', animate: 'easeInOutCirc'});
  323. this.$settings.tabs({});
  324. $('#ldapSettings button:not(.icon-default-style):not(.ui-multiselect)').button();
  325. $('#ldapSettings').tabs({ beforeActivate: this.onTabChange });
  326. this.initControls();
  327. this.disableTabs();
  328. this._requestConfig(this.tabs.server.getConfigID());
  329. },
  330. /**
  331. * updates the status indicator / bar
  332. *
  333. * @param {number} [state]
  334. * @private
  335. */
  336. _updateStatusIndicator: function(state) {
  337. var $indicator = $('.ldap_config_state_indicator');
  338. var $indicatorLight = $('.ldap_config_state_indicator_sign');
  339. switch(state) {
  340. case this.STATUS_UNTESTED:
  341. $indicator.text(t('user_ldap',
  342. 'Testing configuration…'
  343. ));
  344. $indicator.addClass('ldap_grey');
  345. $indicatorLight.removeClass('error');
  346. $indicatorLight.removeClass('success');
  347. break;
  348. case this.STATUS_ERROR:
  349. $indicator.text(t('user_ldap',
  350. 'Configuration incorrect'
  351. ));
  352. $indicator.removeClass('ldap_grey');
  353. $indicatorLight.addClass('error');
  354. $indicatorLight.removeClass('success');
  355. break;
  356. case this.STATUS_INCOMPLETE:
  357. $indicator.text(t('user_ldap',
  358. 'Configuration incomplete'
  359. ));
  360. $indicator.removeClass('ldap_grey');
  361. $indicatorLight.removeClass('error');
  362. $indicatorLight.removeClass('success');
  363. break;
  364. case this.STATUS_SUCCESS:
  365. $indicator.text(t('user_ldap', 'Configuration OK'));
  366. $indicator.addClass('ldap_grey');
  367. $indicatorLight.removeClass('error');
  368. $indicatorLight.addClass('success');
  369. if(!this.tabs.server.isActive) {
  370. this.configModel.set('ldap_configuration_active', 1);
  371. }
  372. break;
  373. }
  374. },
  375. /**
  376. * handles a click on the Back button
  377. *
  378. * @param {WizardView} [view]
  379. * @private
  380. */
  381. _controlBack: function(view) {
  382. var curTabIndex = view.$settings.tabs('option', 'active');
  383. if(curTabIndex == 0) {
  384. return;
  385. }
  386. view.$settings.tabs('option', 'active', curTabIndex - 1);
  387. view._controlUpdate(curTabIndex - 1);
  388. },
  389. /**
  390. * handles a click on the Continue button
  391. *
  392. * @param {WizardView} [view]
  393. * @private
  394. */
  395. _controlContinue: function(view) {
  396. var curTabIndex = view.$settings.tabs('option', 'active');
  397. if(curTabIndex == 3) {
  398. return;
  399. }
  400. view.$settings.tabs('option', 'active', 1 + curTabIndex);
  401. view._controlUpdate(curTabIndex + 1);
  402. },
  403. /**
  404. * updates the controls (navigation buttons)
  405. *
  406. * @param {number} [nextTabIndex] - index of the tab being switched to
  407. * @private
  408. */
  409. _controlUpdate: function(nextTabIndex) {
  410. if(nextTabIndex == 0) {
  411. $('.ldap_action_back').addClass('invisible');
  412. $('.ldap_action_continue').removeClass('invisible');
  413. } else
  414. if(nextTabIndex == 1) {
  415. $('.ldap_action_back').removeClass('invisible');
  416. $('.ldap_action_continue').removeClass('invisible');
  417. } else
  418. if(nextTabIndex == 2) {
  419. $('.ldap_action_continue').removeClass('invisible');
  420. $('.ldap_action_back').removeClass('invisible');
  421. } else
  422. if(nextTabIndex == 3) {
  423. $('.ldap_action_back').removeClass('invisible');
  424. $('.ldap_action_continue').addClass('invisible');
  425. }
  426. }
  427. };
  428. OCA.LDAP.Wizard.WizardView = WizardView;
  429. })();