From 0158333c9039927b0247338ead5e0b2e0b3ce244 Mon Sep 17 00:00:00 2001 From: flashwave Date: Sat, 29 Jul 2023 18:15:30 +0000 Subject: [PATCH] Removed permissions stuff from the User object. --- public-legacy/comments.php | 2 +- public/index.php | 5 ++++ src/Comments/CommentsEx.php | 4 ++- src/Users/User.php | 22 -------------- src/perms.php | 11 +++++++ templates/_layout/comments.twig | 51 ++++++++++++++++----------------- templates/master.twig | 2 +- 7 files changed, 45 insertions(+), 52 deletions(-) diff --git a/public-legacy/comments.php b/public-legacy/comments.php index c2f7e92..68da310 100644 --- a/public-legacy/comments.php +++ b/public-legacy/comments.php @@ -31,7 +31,7 @@ if($msz->hasActiveBan()) { $comments = $msz->getComments(); -$commentPerms = $currentUserInfo->commentPerms(); +$commentPerms = perms_for_comments($currentUserInfo->getId()); $commentId = (string)filter_input(INPUT_GET, 'c', FILTER_SANITIZE_NUMBER_INT); $commentMode = (string)filter_input(INPUT_GET, 'm'); diff --git a/public/index.php b/public/index.php index 020e765..9d3fb76 100644 --- a/public/index.php +++ b/public/index.php @@ -168,6 +168,11 @@ $hasManageAccess = User::hasCurrent() && perms_check_user(MSZ_PERMS_GENERAL, User::getCurrent()->getId(), MSZ_PERM_GENERAL_CAN_MANAGE); Template::set('has_manage_access', $hasManageAccess); +$canViewForumLeaderboard = User::hasCurrent() + && !$msz->hasActiveBan() + && perms_check_user(MSZ_PERMS_GENERAL, User::getCurrent()->getId(), MSZ_PERM_FORUM_VIEW_LEADERBOARD); +Template::set('can_view_forum_leaderboard', $canViewForumLeaderboard); + if($inManageMode) { if(!$hasManageAccess) { echo render_error(403); diff --git a/src/Comments/CommentsEx.php b/src/Comments/CommentsEx.php index b89dddd..dcffad5 100644 --- a/src/Comments/CommentsEx.php +++ b/src/Comments/CommentsEx.php @@ -19,7 +19,9 @@ class CommentsEx { if(is_string($category)) $category = $this->comments->ensureCategory($category); - $info->user = User::getCurrent(); + $hasUser = User::hasCurrent(); + $info->user = $hasUser ? User::getCurrent() : null; + $info->perms = $hasUser ? perms_for_comments($info->user->getId()) : []; $info->category = $category; $info->posts = []; diff --git a/src/Users/User.php b/src/Users/User.php index 31e78b2..4dd3127 100644 --- a/src/Users/User.php +++ b/src/Users/User.php @@ -314,28 +314,6 @@ class User { ->execute(); } - // TODO: Is this the proper location/implementation for this? (no) - private $commentPermsArray = null; - public function commentPerms(): array { - if($this->commentPermsArray === null) - $this->commentPermsArray = perms_check_user_bulk(MSZ_PERMS_COMMENTS, $this->getId(), [ - 'can_comment' => MSZ_PERM_COMMENTS_CREATE, - 'can_delete' => MSZ_PERM_COMMENTS_DELETE_OWN | MSZ_PERM_COMMENTS_DELETE_ANY, - 'can_delete_any' => MSZ_PERM_COMMENTS_DELETE_ANY, - 'can_pin' => MSZ_PERM_COMMENTS_PIN, - 'can_lock' => MSZ_PERM_COMMENTS_LOCK, - 'can_vote' => MSZ_PERM_COMMENTS_VOTE, - ]); - return $this->commentPermsArray; - } - - private $legacyPerms = null; - public function getLegacyPerms(): array { - if($this->legacyPerms === null) - $this->legacyPerms = perms_get_user($this->getId()); - return $this->legacyPerms; - } - /************ * PASSWORD * ************/ diff --git a/src/perms.php b/src/perms.php index 5388131..635955f 100644 --- a/src/perms.php +++ b/src/perms.php @@ -261,3 +261,14 @@ function perms_check_user_bulk(string $prefix, ?int $userId, array $set, bool $s $perms = perms_get_user($userId)[$prefix] ?? 0; return perms_check_bulk($perms, $set, $strict); } + +function perms_for_comments(string|int $userId): array { + return perms_check_user_bulk(MSZ_PERMS_COMMENTS, (int)$userId, [ + 'can_comment' => MSZ_PERM_COMMENTS_CREATE, + 'can_delete' => MSZ_PERM_COMMENTS_DELETE_OWN | MSZ_PERM_COMMENTS_DELETE_ANY, + 'can_delete_any' => MSZ_PERM_COMMENTS_DELETE_ANY, + 'can_pin' => MSZ_PERM_COMMENTS_PIN, + 'can_lock' => MSZ_PERM_COMMENTS_LOCK, + 'can_vote' => MSZ_PERM_COMMENTS_VOTE, + ]); +} diff --git a/templates/_layout/comments.twig b/templates/_layout/comments.twig index 55e01a5..1b2d3c0 100644 --- a/templates/_layout/comments.twig +++ b/templates/_layout/comments.twig @@ -1,4 +1,4 @@ -{% macro comments_input(category, user, reply_to) %} +{% macro comments_input(category, user, perms, reply_to) %} {% set reply_mode = reply_to is not null %} {% from 'macros.twig' import avatar %} @@ -24,10 +24,10 @@ name="comment[text]" placeholder="Share your extensive insights...">
{% if not reply_mode %} - {% if user.commentPerms.can_pin|default(false) %} + {% if perms.can_pin|default(false) %} {{ input_checkbox('comment[pin]', 'Pin this comment', false, 'comment__action') }} {% endif %} - {% if user.commentPerms.can_lock|default(false) %} + {% if perms.can_lock|default(false) %} {{ input_checkbox('comment[lock]', 'Toggle locked status', false, 'comment__action') }} {% endif %} {% endif %} @@ -40,7 +40,7 @@ {% endmacro %} -{% macro comments_entry(comment, indent, category, user) %} +{% macro comments_entry(comment, indent, category, user, perms) %} {% from 'macros.twig' import avatar %} {% from '_layout/input.twig' import input_checkbox_raw %} @@ -61,9 +61,9 @@ {% set isReply = comment.hasParent %} {% endif %} - {% set hide_details = poster is null or comment.deleted and not user.commentPerms.can_delete_any|default(false) %} + {% set hide_details = poster is null or comment.deleted and not perms.can_delete_any|default(false) %} - {% if user.commentPerms.can_delete_any|default(false) or (not comment.deleted or replies|length > 0) %} + {% if perms.can_delete_any|default(false) or (not comment.deleted or replies|length > 0) %}
{% if hide_details %} @@ -106,7 +106,7 @@
{% if not comment.deleted and user is not null %} - {% if user.commentPerms.can_vote|default(false) %} + {% if perms.can_vote|default(false) %} {% set like_vote_state = userVote > 0 ? 0 : 1 %} {% set dislike_vote_state = userVote < 0 ? 0 : -1 %} @@ -125,19 +125,19 @@ {% endif %} {% endif %} - {% if user.commentPerms.can_comment|default(false) %} + {% if perms.can_comment|default(false) %} {% endif %} - {% if user.commentPerms.can_delete_any|default(false) or (poster.id|default(0) == user.id and user.commentPerms.can_delete|default(false)) %} + {% if perms.can_delete_any|default(false) or (poster.id|default(0) == user.id and perms.can_delete|default(false)) %} Delete {% endif %} {# if user is not null %} Report {% endif #} - {% if not isReply and user.commentPerms.can_pin|default(false) %} + {% if not isReply and perms.can_pin|default(false) %} {{ comment.pinned ? 'Unpin' : 'Pin' }} {% endif %} - {% elseif user.commentPerms.can_delete_any|default(false) %} + {% elseif perms.can_delete_any|default(false) %} Restore {% endif %}
@@ -146,13 +146,13 @@
{% from _self import comments_entry, comments_input %} - {% if user|default(null) is not null and category|default(null) is not null and user.commentPerms.can_comment|default(false) %} + {% if user|default(null) is not null and category|default(null) is not null and perms.can_comment|default(false) %} {{ input_checkbox_raw('', false, 'comment__reply-toggle', '', false, {'id':'comment-reply-toggle-' ~ comment.id}) }} - {{ comments_input(category, user, comment) }} + {{ comments_input(category, user, perms, comment) }} {% endif %} {% if replies|length > 0 %} {% for reply in replies %} - {{ comments_entry(reply, indent + 1, category, user) }} + {{ comments_entry(reply, indent + 1, category, user, perms) }} {% endfor %} {% endif %}
@@ -160,14 +160,11 @@ {% endif %} {% endmacro %} -{% macro comments_section(category, user) %} - {% if category.category is defined %} - {% set user = category.user %} - {% set posts = category.posts %} - {% set category = category.category %} - {% else %} - {% set posts = category.posts %} - {% endif %} +{% macro comments_section(category) %} + {% set user = category.user %} + {% set posts = category.posts %} + {% set perms = category.perms %} + {% set category = category.category %}
@@ -179,21 +176,21 @@
Posting new comments here is disabled.
- {% elseif not user.commentPerms.can_lock|default(false) and category.locked %} + {% elseif not perms.can_lock|default(false) and category.locked %}
This comment section was locked, .
- {% elseif not user.commentPerms.can_comment|default(false) %} + {% elseif not perms.can_comment|default(false) %}
You are not allowed to post comments.
{% else %} {% from _self import comments_input %} - {{ comments_input(category, user) }} + {{ comments_input(category, user, perms) }} {% endif %}
- {% if user.commentPerms.can_lock|default(false) and category.locked %} + {% if perms.can_lock|default(false) and category.locked %}
This comment section was locked, .
@@ -209,7 +206,7 @@ {% if posts|length > 0 %} {% from _self import comments_entry %} {% for comment in posts %} - {{ comments_entry(comment, 1, category, user) }} + {{ comments_entry(comment, 1, category, user, perms) }} {% endfor %} {% else %}
diff --git a/templates/master.twig b/templates/master.twig index d3b8a73..7609edc 100644 --- a/templates/master.twig +++ b/templates/master.twig @@ -61,7 +61,7 @@ { 'title': 'Leaderboard', 'url': url('forum-leaderboard'), - 'display': current_user.legacyPerms.forum|default(0)|perms_check(constant('MSZ_PERM_FORUM_VIEW_LEADERBOARD')), + 'display': can_view_forum_leaderboard, }, ], },