From 0a45bdd3b71ec0f8744abb3830858a91a4bca146 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 10 Aug 2023 21:06:08 +0000 Subject: [PATCH] Preparations for a moderator role --- .../Session/Capability/IHandleUserSessions.php | 7 +++++++ src/Core/Session/Model/UserSession.php | 6 ++++++ src/Model/User.php | 14 ++++++++++++++ src/Module/BaseModeration.php | 6 +++--- src/Module/Notifications/Ping.php | 2 +- 5 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/Core/Session/Capability/IHandleUserSessions.php b/src/Core/Session/Capability/IHandleUserSessions.php index 5734eafdf..3b135b521 100644 --- a/src/Core/Session/Capability/IHandleUserSessions.php +++ b/src/Core/Session/Capability/IHandleUserSessions.php @@ -93,6 +93,13 @@ interface IHandleUserSessions extends IHandleSessions */ public function isSiteAdmin(): bool; + /** + * Check if current user is a moderator. + * + * @return bool true if user is a moderator + */ + public function isModerator(): bool; + /** * Returns User ID of the managed user in case it's a different identity * diff --git a/src/Core/Session/Model/UserSession.php b/src/Core/Session/Model/UserSession.php index 8dfc3d832..c1a38dffa 100644 --- a/src/Core/Session/Model/UserSession.php +++ b/src/Core/Session/Model/UserSession.php @@ -139,6 +139,12 @@ class UserSession implements IHandleUserSessions return User::isSiteAdmin($this->getLocalUserId()); } + /** {@inheritDoc} */ + public function isModerator(): bool + { + return User::isModerator($this->getLocalUserId()); + } + /** {@inheritDoc} */ public function setVisitorsContacts(string $my_url) { diff --git a/src/Model/User.php b/src/Model/User.php index 89d75849f..18da3e956 100644 --- a/src/Model/User.php +++ b/src/Model/User.php @@ -880,6 +880,20 @@ class User ]); } + /** + * Returns if the given uid is valid and a moderator + * + * @param int $uid + * + * @return bool + * @throws Exception + */ + public static function isModerator(int $uid): bool + { + // @todo Replace with a moderator check in the future + return self::isSiteAdmin($uid); + } + /** * Checks if a nickname is in the list of the forbidden nicknames * diff --git a/src/Module/BaseModeration.php b/src/Module/BaseModeration.php index 3de46b319..a575e51f7 100644 --- a/src/Module/BaseModeration.php +++ b/src/Module/BaseModeration.php @@ -82,12 +82,12 @@ abstract class BaseModeration extends BaseModule } } - if (!$this->app->isSiteAdmin()) { - throw new HTTPException\ForbiddenException($this->t('You don\'t have access to administration pages.')); + if (!$this->session->isModerator()) { + throw new HTTPException\ForbiddenException($this->t('You don\'t have access to moderation pages.')); } if ($this->session->getSubManagedUserId()) { - throw new HTTPException\ForbiddenException($this->t('Submanaged account can\'t access the administration pages. Please log back in as the main account.')); + throw new HTTPException\ForbiddenException($this->t('Submanaged account can\'t access the moderation pages. Please log back in as the main account.')); } } diff --git a/src/Module/Notifications/Ping.php b/src/Module/Notifications/Ping.php index e54bd577b..603d6408c 100644 --- a/src/Module/Notifications/Ping.php +++ b/src/Module/Notifications/Ping.php @@ -175,7 +175,7 @@ class Ping extends BaseModule $myurl = $this->session->getMyUrl(); $mail_count = $this->database->count('mail', ["`uid` = ? AND NOT `seen` AND `from-url` != ?", $this->session->getLocalUserId(), $myurl]); - if (intval($this->config->get('config', 'register_policy')) === Register::APPROVE && $this->app->isSiteAdmin()) { + if (intval($this->config->get('config', 'register_policy')) === Register::APPROVE && $this->session->isSiteAdmin()) { $registrations = \Friendica\Model\Register::getPending(); $register_count = count($registrations); }