From 5b9e181198a61f5c123f5e8d5acec4c30cc8908a Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Thu, 24 Jan 2013 14:11:53 +0100 Subject: [PATCH] LDAP: implement deleteConfiguration feature --- apps/user_ldap/js/settings.js | 33 +++++++++++++++++++ apps/user_ldap/lib/helper.php | 47 +++++++++++++++++++++------ apps/user_ldap/templates/settings.php | 4 ++- 3 files changed, 73 insertions(+), 11 deletions(-) diff --git a/apps/user_ldap/js/settings.js b/apps/user_ldap/js/settings.js index c3484da5ac..49bbc60d81 100644 --- a/apps/user_ldap/js/settings.js +++ b/apps/user_ldap/js/settings.js @@ -45,12 +45,32 @@ var LdapConfiguration = { $(this).removeAttr('checked'); } }); + }, + + deleteConfiguration: function() { + $.post( + OC.filePath('user_ldap','ajax','deleteConfiguration.php'), + $('#ldap_serverconfig_chooser').serialize(), + function (result) { + if(result.status == 'success') { + $('#ldap_serverconfig_chooser option:selected').remove(); + $('#ldap_serverconfig_chooser option:first').select(); + LdapConfiguration.refreshConfig(); + } else { + OC.dialogs.alert( + result.message, + 'Deletion failed' + ); + } + } + ); } } $(document).ready(function() { $('#ldapSettings').tabs(); $('#ldap_action_test_connection').button(); + $('#ldap_action_delete_configuration').button(); LdapConfiguration.refreshConfig(); $('#ldap_action_test_connection').click(function(event){ event.preventDefault(); @@ -73,6 +93,19 @@ $(document).ready(function() { ); }); + $('#ldap_action_delete_configuration').click(function(event) { + event.preventDefault(); + OC.dialogs.confirm( + 'Do you really want to delete the current Server Configuration?', + 'Confirm Deletion', + function(deleteConfiguration) { + if(deleteConfiguration) { + LdapConfiguration.deleteConfiguration(); + } + } + ); + }); + $('#ldap_submit').click(function(event) { event.preventDefault(); $.post( diff --git a/apps/user_ldap/lib/helper.php b/apps/user_ldap/lib/helper.php index 1751f57f50..5f6e2a1d03 100644 --- a/apps/user_ldap/lib/helper.php +++ b/apps/user_ldap/lib/helper.php @@ -24,13 +24,13 @@ namespace OCA\user_ldap\lib; class Helper { - + /** * @brief returns prefixes for each saved LDAP/AD server configuration. * @return array with a list of the available prefixes - * + * * Configuration prefixes are used to set up configurations for n LDAP or - * AD servers. Since configuration is stored in the database, table + * AD servers. Since configuration is stored in the database, table * appconfig under appid user_ldap, the common identifiers in column * 'configkey' have a prefix. The prefix for the very first server * configuration is empty. @@ -38,29 +38,56 @@ class Helper { * Server 1: ldap_login_filtter * Server 2: s1_ldap_login_filter * Server 3: s2_ldap_login_filter - * - * The prefix needs to be passed to the constructor of Connection class, + * + * The prefix needs to be passed to the constructor of Connection class, * except the default (first) server shall be connected to. - * + * */ static public function getServerConfigurationPrefixes() { $referenceConfigkey = 'ldap_login_filter'; - + $query = \OCP\DB::prepare(' SELECT DISTINCT `configkey` FROM `*PREFIX*appconfig` WHERE `configkey` LIKE ? '); - + $serverConfigs = $query->execute(array('%'.$referenceConfigkey))->fetchAll(); $prefixes = array(); - + foreach($serverConfigs as $serverConfig) { $len = strlen($serverConfig['configkey']) - strlen($referenceConfigkey); $prefixes[] = substr($serverConfig['configkey'], 0, $len); } - + return $prefixes; } + + static public function deleteServerConfiguration($prefix) { + //just to be on the safe side + \OCP\User::checkAdminUser(); + + if(!in_array($prefix, self::getServerConfigurationPrefixes())) { + return false; + } + + $query = \OCP\DB::prepare(' + DELETE + FROM `*PREFIX*appconfig` + WHERE `configkey` LIKE ? + AND appid = "user_ldap" + '); + $res = $query->execute(array($prefix.'%')); + + if(\OCP\DB::isError($res)) { + return false; + } + + if($res->numRows() == 0) { + return false; + } + + return true; + } } diff --git a/apps/user_ldap/templates/settings.php b/apps/user_ldap/templates/settings.php index 20297c02d3..513c59653e 100644 --- a/apps/user_ldap/templates/settings.php +++ b/apps/user_ldap/templates/settings.php @@ -15,7 +15,9 @@

+ + +