From fe3c054825e1ae36b57efed7255ef52131ebcbc7 Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Thu, 6 Sep 2018 21:30:05 +0200 Subject: [PATCH] changetype delete --- lam/lib/import.inc | 39 ++++++++++++++++++++++++++++++++++++ lam/tests/lib/importTest.php | 37 ++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/lam/lib/import.inc b/lam/lib/import.inc index 27674fbb..9110d089 100644 --- a/lam/lib/import.inc +++ b/lam/lib/import.inc @@ -278,6 +278,12 @@ class Importer { elseif ($type === 'modrdn') { return $this->createModRdnTask($dn, $entry); } + elseif ($type === 'delete') { + if (!empty($entry)) { + throw new LAMException(_('Invalid data'), htmlspecialchars($dn)); + } + return new DeleteEntryTask($dn); + } $changes = array(); $subtasks = array(); $currentLines = array(); @@ -477,6 +483,39 @@ class RenameEntryTask implements ImporterTask { } +/** + * Deletes an LDAP entry. + * + * @author Roland Gruber + */ +class DeleteEntryTask implements ImporterTask { + + private $dn = ''; + + /** + * Constructor + * + * @param string $dn DN + */ + public function __construct($dn) { + $this->dn = $dn; + } + + /** + * {@inheritDoc} + * @see \LAM\TOOLS\IMPORT_EXPORT\ImporterTask::run() + */ + public function run() { + $ldap = $_SESSION['ldap']->server(); + $success = @ldap_delete($ldap, $this->dn); + if ($success) { + return Importer::formatMessage('INFO', sprintf(_('Successfully deleted DN %s'), $this->dn), ''); + } + throw new LAMException(_('Could not delete the entry.') . '
' . $this->dn, getExtendedLDAPErrorMessage($ldap)); + } + +} + /** * Combines multiple import tasks. * diff --git a/lam/tests/lib/importTest.php b/lam/tests/lib/importTest.php index a8cfdc74..9cfbcff9 100644 --- a/lam/tests/lib/importTest.php +++ b/lam/tests/lib/importTest.php @@ -4,6 +4,7 @@ use LAM\TOOLS\IMPORT_EXPORT\MultiTask; use LAM\TOOLS\IMPORT_EXPORT\AddAttributesTask; use LAM\TOOLS\IMPORT_EXPORT\AddEntryTask; use LAM\TOOLS\IMPORT_EXPORT\RenameEntryTask; +use LAM\TOOLS\IMPORT_EXPORT\DeleteEntryTask; /* This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) @@ -220,4 +221,40 @@ class ImporterTest extends PHPUnit_Framework_TestCase { $this->assertEquals(RenameEntryTask::class, get_class($task)); } + /** + * Change entry with delete changetype with extra line. + */ + public function testChangeDeleteInvalid() { + $lines = array( + "version: 1", + "", + "dn: uid=test,dc=example,dc=com", + "changeType: delete", + "uid: test", + ); + + $this->setExpectedException(LAMException::class, 'uid=test,dc=example,dc=com'); + + $importer = new Importer(); + $tasks = $importer->getTasks($lines); + } + + /** + * Change entry with delete changetype. + */ + public function testChangeDelete() { + $lines = array( + "version: 1", + "", + "dn: uid=test,dc=example,dc=com", + "changeType: delete", + ); + + $importer = new Importer(); + $tasks = $importer->getTasks($lines); + $this->assertEquals(1, sizeof($tasks)); + $task = $tasks[0]; + $this->assertEquals(DeleteEntryTask::class, get_class($task)); + } + }