1
0

wizardTabElementary.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /**
  2. * Copyright (c) 2015, Arthur Schiwon <blizzz@owncloud.com>
  3. * This file is licensed under the Affero General Public License version 3 or later.
  4. * See the COPYING-README file.
  5. */
  6. OCA = OCA || {};
  7. (function() {
  8. /**
  9. * @classdesc This class represents the view belonging to the server tab
  10. * in the LDAP wizard.
  11. */
  12. var WizardTabElementary = OCA.LDAP.Wizard.WizardTabGeneric.subClass({
  13. /** @property {number} */
  14. _configChooserNextServerNumber: 1,
  15. baseDNTestTriggered: false,
  16. /**
  17. * initializes the instance. Always call it after initialization.
  18. *
  19. * @param {any} tabIndex -
  20. * @param {any} tabID -
  21. */
  22. init: function (tabIndex, tabID) {
  23. tabIndex = 0;
  24. this._super(tabIndex, tabID);
  25. this.isActive = true;
  26. this.$configChooser = $('#ldap_serverconfig_chooser');
  27. var items = {
  28. ldap_host: {
  29. $element: $('#ldap_host'),
  30. setMethod: 'setHost'
  31. },
  32. ldap_port: {
  33. $element: $('#ldap_port'),
  34. setMethod: 'setPort',
  35. $relatedElements: $('.ldapDetectPort')
  36. },
  37. ldap_dn: {
  38. $element: $('#ldap_dn'),
  39. setMethod: 'setAgentDN',
  40. preventAutoSave: true,
  41. $saveButton: $('.ldapSaveAgentCredentials')
  42. },
  43. ldap_agent_password: {
  44. $element: $('#ldap_agent_password'),
  45. setMethod: 'setAgentPwd',
  46. preventAutoSave: true,
  47. $saveButton: $('.ldapSaveAgentCredentials')
  48. },
  49. ldap_base: {
  50. $element: $('#ldap_base'),
  51. setMethod: 'setBase',
  52. $relatedElements: $('.ldapDetectBase, .ldapTestBase'),
  53. $detectButton: $('.ldapDetectBase'),
  54. $testButton: $('.ldapTestBase')
  55. },
  56. ldap_base_test: {
  57. $element: $('#ldap_base')
  58. },
  59. ldap_experienced_admin: {
  60. $element: $('#ldap_experienced_admin'),
  61. setMethod: 'setExperiencedAdmin'
  62. }
  63. };
  64. this.setManagedItems(items);
  65. _.bindAll(this,
  66. 'onPortButtonClick',
  67. 'onBaseDNButtonClick',
  68. 'onBaseDNTestButtonClick'
  69. );
  70. this.managedItems.ldap_port.$relatedElements.click(this.onPortButtonClick);
  71. this.managedItems.ldap_base.$detectButton.click(this.onBaseDNButtonClick);
  72. this.managedItems.ldap_base.$testButton.click(this.onBaseDNTestButtonClick);
  73. },
  74. /**
  75. * Sets the config model for this view and subscribes to some events.
  76. * Also binds the config chooser to the model
  77. *
  78. * @param {OCA.LDAP.Wizard.ConfigModel} configModel
  79. */
  80. setModel: function(configModel) {
  81. this._super(configModel);
  82. this.configModel.on('configLoaded', this.onConfigSwitch, this);
  83. this.configModel.on('newConfiguration', this.onNewConfiguration, this);
  84. this.configModel.on('deleteConfiguration', this.onDeleteConfiguration, this);
  85. this.configModel.on('receivedLdapFeature', this.onTestResultReceived, this);
  86. this._enableConfigChooser();
  87. this._enableConfigButtons();
  88. },
  89. /**
  90. * returns the currently selected configuration ID
  91. *
  92. * @returns {string}
  93. */
  94. getConfigID: function() {
  95. return this.$configChooser.val();
  96. },
  97. /**
  98. * updates the host configuration text field
  99. *
  100. * @param {string} host
  101. */
  102. setHost: function(host) {
  103. this.setElementValue(this.managedItems.ldap_host.$element, host);
  104. if(host) {
  105. this.enableElement(this.managedItems.ldap_port.$relatedElements);
  106. } else {
  107. this.disableElement(this.managedItems.ldap_port.$relatedElements);
  108. }
  109. },
  110. /**
  111. * updates the port configuration text field
  112. *
  113. * @param {string} port
  114. */
  115. setPort: function(port) {
  116. this.setElementValue(this.managedItems.ldap_port.$element, port);
  117. },
  118. /**
  119. * updates the user (agent) DN text field
  120. *
  121. * @param {string} agentDN
  122. */
  123. setAgentDN: function(agentDN) {
  124. this.setElementValue(this.managedItems.ldap_dn.$element, agentDN);
  125. },
  126. /**
  127. * updates the user (agent) password field
  128. *
  129. * @param {string} agentPwd
  130. */
  131. setAgentPwd: function(agentPwd) {
  132. this.setElementValue(
  133. this.managedItems.ldap_agent_password.$element, agentPwd
  134. );
  135. if (agentPwd && $('html').hasClass('lte9')) {
  136. // make it a password field again (IE fix, placeholders bug)
  137. this.managedItems.ldap_agent_password.$element.attr('type', 'password');
  138. }
  139. },
  140. /**
  141. * updates the base DN text area
  142. *
  143. * @param {string} bases
  144. */
  145. setBase: function(bases) {
  146. this.setElementValue(this.managedItems.ldap_base.$element, bases);
  147. if(!bases) {
  148. this.disableElement(this.managedItems.ldap_base.$testButton);
  149. } else {
  150. this.enableElement(this.managedItems.ldap_base.$testButton);
  151. }
  152. },
  153. /**
  154. * updates the experienced admin check box
  155. *
  156. * @param {string} xpAdminMode contains an int
  157. */
  158. setExperiencedAdmin: function(xpAdminMode) {
  159. this.setElementValue(
  160. this.managedItems.ldap_experienced_admin.$element, xpAdminMode
  161. );
  162. },
  163. /**
  164. * @inheritdoc
  165. */
  166. overrideErrorMessage: function(message, key) {
  167. var original = message;
  168. message = this._super(message, key);
  169. if(original !== message) {
  170. // we pass the parents change
  171. return message;
  172. }
  173. switch(key) {
  174. case 'ldap_port':
  175. if (message === 'Invalid credentials') {
  176. return t('user_ldap', 'Please check the credentials, they seem to be wrong.');
  177. } else {
  178. return t('user_ldap', 'Please specify the port, it could not be auto-detected.');
  179. }
  180. break;
  181. case 'ldap_base':
  182. if( message === 'Server is unwilling to perform'
  183. || message === 'Could not connect to LDAP'
  184. ) {
  185. return t('user_ldap', 'Base DN could not be auto-detected, please revise credentials, host and port.');
  186. }
  187. return t('user_ldap', 'Could not detect Base DN, please enter it manually.');
  188. break;
  189. }
  190. return message;
  191. },
  192. /**
  193. * resets the view when a configuration switch happened.
  194. *
  195. * @param {WizardTabElementary} view
  196. * @param {Object} configuration
  197. */
  198. onConfigSwitch: function(view, configuration) {
  199. this.baseDNTestTriggered = false;
  200. view.disableElement(view.managedItems.ldap_port.$relatedElements);
  201. view.managedItems.ldap_dn.$saveButton.removeClass('primary');
  202. view.onConfigLoaded(view, configuration);
  203. },
  204. /**
  205. * updates the configuration chooser when a new configuration was added
  206. * which also means it is being switched to. The configuration fields
  207. * are updated on a different step.
  208. *
  209. * @param {WizardTabElementary} view
  210. * @param {Object} result
  211. */
  212. onNewConfiguration: function(view, result) {
  213. if(result.isSuccess === true) {
  214. var nthServer = view._configChooserNextServerNumber;
  215. view.$configChooser.find('option:selected').removeAttr('selected');
  216. var html = '<option value="'+result.configPrefix+'" selected="selected">'+t('user_ldap','{nthServer}. Server', {nthServer: nthServer})+'</option>';
  217. if(view.$configChooser.find('option:last').length > 0) {
  218. view.$configChooser.find('option:last').after(html);
  219. } else {
  220. view.$configChooser.html(html);
  221. }
  222. view._configChooserNextServerNumber++;
  223. }
  224. },
  225. /**
  226. * updates the configuration chooser upon the deletion of a
  227. * configuration and, if necessary, loads an existing one.
  228. *
  229. * @param {any} view -
  230. * @param {any} result -
  231. */
  232. onDeleteConfiguration: function(view, result) {
  233. if(result.isSuccess === true) {
  234. if(view.getConfigID() === result.configPrefix) {
  235. // if the deleted value is still the selected one (99% of
  236. // the cases), remove it from the list and load the topmost
  237. view.$configChooser.find('option:selected').remove();
  238. view.$configChooser.find('option:first').select();
  239. if(view.$configChooser.find(' option').length < 1) {
  240. view.configModel.newConfig(false);
  241. } else {
  242. view.configModel.load(view.getConfigID());
  243. }
  244. } else {
  245. // otherwise just remove the entry
  246. view.$configChooser.find('option[value=' + result.configPrefix + ']').remove();
  247. }
  248. } else {
  249. OC.Notification.showTemporary(result.errorMessage);
  250. }
  251. },
  252. /**
  253. * Base DN test results will arrive here
  254. *
  255. * @param {WizardTabElementary} view
  256. * @param {FeaturePayload} payload
  257. */
  258. onTestResultReceived: function(view, payload) {
  259. if(view.baseDNTestTriggered && payload.feature === 'TestBaseDN') {
  260. view.enableElement(view.managedItems.ldap_base.$testButton);
  261. var message;
  262. if(payload.data.status === 'success') {
  263. var objectsFound = parseInt(payload.data.changes.ldap_test_base, 10);
  264. if(objectsFound < 1) {
  265. message = t('user_ldap', 'No object found in the given Base DN. Please revise.');
  266. } else if(objectsFound > 1000) {
  267. message = t('user_ldap', 'More than 1,000 directory entries available.');
  268. } else {
  269. message = n(
  270. 'user_ldap',
  271. '{objectsFound} entry available within the provided Base DN',
  272. '{objectsFound} entries available within the provided Base DN',
  273. objectsFound,
  274. {
  275. objectsFound: objectsFound
  276. });
  277. }
  278. } else {
  279. message = view.overrideErrorMessage(payload.data.message);
  280. message = message || t('user_ldap', 'An error occurred. Please check the Base DN, as well as connection settings and credentials.');
  281. if(payload.data.message) {
  282. console.warn(payload.data.message);
  283. }
  284. }
  285. OC.Notification.showTemporary(message);
  286. }
  287. },
  288. /**
  289. * request to count the users with the current filter
  290. *
  291. * @param {Event} event
  292. */
  293. onPortButtonClick: function(event) {
  294. event.preventDefault();
  295. this.configModel.requestWizard('ldap_port');
  296. },
  297. /**
  298. * request to count the users with the current filter
  299. *
  300. * @param {Event} event
  301. */
  302. onBaseDNButtonClick: function(event) {
  303. event.preventDefault();
  304. this.configModel.requestWizard('ldap_base');
  305. },
  306. /**
  307. * request to count the users with the current filter
  308. *
  309. * @param {Event} event
  310. */
  311. onBaseDNTestButtonClick: function(event) {
  312. event.preventDefault();
  313. this.baseDNTestTriggered = true;
  314. this.configModel.requestWizard('ldap_test_base');
  315. this.disableElement(this.managedItems.ldap_base.$testButton);
  316. },
  317. /**
  318. * registers the change event on the configuration chooser and makes
  319. * the model load a newly selected configuration
  320. *
  321. * @private
  322. */
  323. _enableConfigChooser: function() {
  324. this._configChooserNextServerNumber = this.$configChooser.find(' option').length + 1;
  325. var view = this;
  326. this.$configChooser.change(function(){
  327. var value = view.$configChooser.find(' option:selected:first').attr('value');
  328. view.configModel.load(value);
  329. });
  330. },
  331. /**
  332. * adds actions to the action buttons for configuration management
  333. *
  334. * @private
  335. */
  336. _enableConfigButtons: function() {
  337. var view = this;
  338. $('#ldap_action_delete_configuration').click(function(event) {
  339. event.preventDefault();
  340. OC.dialogs.confirm(
  341. t('user_ldap', 'Do you really want to delete the current Server Configuration?'),
  342. t('user_ldap', 'Confirm Deletion'),
  343. function(doDelete) {
  344. if(doDelete) {
  345. view.configModel.deleteConfig(view.getConfigID());
  346. }
  347. },
  348. false
  349. );
  350. });
  351. $('#ldap_action_add_configuration').click(function(event) {
  352. event.preventDefault();
  353. view.configModel.newConfig(false);
  354. });
  355. $('#ldap_action_copy_configuration').click(function(event) {
  356. event.preventDefault();
  357. view.configModel.newConfig(true);
  358. });
  359. }
  360. });
  361. OCA.LDAP.Wizard.WizardTabElementary = WizardTabElementary;
  362. })();