From d42dff116543fbee274101d6b0eb80aba1ca0362 Mon Sep 17 00:00:00 2001 From: NOrtmann1 Date: Fri, 5 Jun 2026 11:27:43 +0200 Subject: [PATCH] =?UTF-8?q?deleteArticle=20->=20Autorisierungspr=C3=BCfung?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- php/model/ArticleManagerDAO.php | 6 +++--- php/model/DatabaseArticleManager.php | 13 +++++++++++-- php/model/LocalArticleManager.php | 13 +++++++++++-- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/php/model/ArticleManagerDAO.php b/php/model/ArticleManagerDAO.php index 01826e1..fe7af03 100644 --- a/php/model/ArticleManagerDAO.php +++ b/php/model/ArticleManagerDAO.php @@ -40,15 +40,15 @@ interface ArticleManagerDAO public function updateArticle($id, $article, $author); /** - * Löscht einen Beitrag aus übergebener ID. - * TODO: sollte auch die Autorisierung prüfen... + * Löscht einen Beitrag aus übergebener ID und dem Nutzer, der die Löschung ausführt. * @param $id + * @param $author * @return void * @throws InternalServerErrorException * @throws NotFoundException * @throws UnauthorizedAccessException */ - public function deleteArticle($id); + public function deleteArticle($id, $author); /** * Beitrag aufrufen. diff --git a/php/model/DatabaseArticleManager.php b/php/model/DatabaseArticleManager.php index 72de769..20c7484 100644 --- a/php/model/DatabaseArticleManager.php +++ b/php/model/DatabaseArticleManager.php @@ -147,9 +147,18 @@ class DatabaseArticleManager implements ArticleManagerDAO { } } - public function deleteArticle($id) + public function deleteArticle($id, $author) { - // TODO: Sollte auch die Autorisierung prüfen... + $article = getArticle($id); + if (empty($article)) { + throw new NotFoundException("not_found_article"); + } + + // Berechtigungsprüfung: + if ($article->getAuthor() !== $author) { + throw new UnauthorizedAccessException("unauthorized_access"); + } + try { $db = $this->getConnection(); $sql = "DELETE FROM articles WHERE id = :id;"; diff --git a/php/model/LocalArticleManager.php b/php/model/LocalArticleManager.php index 62879e9..aff23c1 100644 --- a/php/model/LocalArticleManager.php +++ b/php/model/LocalArticleManager.php @@ -102,9 +102,18 @@ class LocalArticleManager implements ArticleManagerDAO { } } - public function deleteArticle($id) + public function deleteArticle($id, $author) { - // TODO: Sollte auch die Autorisierung prüfen... + $article = getArticle($id); + if (empty($article)) { + throw new NotFoundException("not_found_article"); + } + + // Berechtigungsprüfung: + if ($article->getAuthor() !== $author) { + throw new UnauthorizedAccessException("unauthorized_access"); + } + $articles = $this->getAllArticles(); $articleFound = false;