Blame view
app/Core/ForumPost/ForumPostPolicy.php
4.23 KB
e77200db5 Initial commit |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
<?php namespace FootyRoom\Core\ForumPost; use FootyRoom\User\User; use FootyRoom\Core\AuthException; use FootyRoom\Core\CoreException; use FootyRoom\Core\Comment\CommentPolicy; use FootyRoom\Queries\Post\ForumPostQueryHandler; use FootyRoom\Queries\Post\ForumPostsInPeriodQuery; class ForumPostPolicy { /** * \FootyRoom\Core\Comment\CommentPolicy. */ protected $commentPolicy; /** * \FootyRoom\Queries\Post\ForumPostQueryHandler. */ protected $forumPostQueryHandler; /** * Constructor. * * @param \FootyRoom\Core\Comment\CommentPolicy $commentPolicy * @param \FootyRoom\Queries\Post\ForumPostQueryHandler $forumPostQueryHandler */ public function __construct( CommentPolicy $commentPolicy, ForumPostQueryHandler $forumPostQueryHandler ) { $this->commentPolicy = $commentPolicy; $this->forumPostQueryHandler = $forumPostQueryHandler; } /** * Determine if user can post in forum. * * @param int $userId * @param string $content * @param bool $throw * * @throws CoreException * @return bool */ public function canPost(User $user, string $content, $throw = false) { // Count comments in last 60 minutes. $count = $this->forumPostQueryHandler->postsInPeriod( new ForumPostsInPeriodQuery(60 * 60, $user->getUserId()) ); if ($count >= 3) { if ($throw) { throw new CoreException('Members can post in forum 3 times per hour.'); } else { return false; } } try { return $this->canComment($user, $content, $throw); } catch (Exception $e) { $message = str_replace('commenting', 'posting', $e->getMessage()); throw new CoreException($message); } } /** * Determine if user can comment in forum. * * @param \FootyRoom\User\User $user * @param string $content * @param bool $throw * * @return bool */ public function canComment(User $user, string $content, $throw = false) { if ($user->isNewborn()) { $hasLink = strstr($content, 'http://') ?: strstr($content, 'https://'); if ($hasLink && $throw) { throw new CoreException('New users are not allowed to post links.'); } if ($hasLink) { return false; } } return $this->commentPolicy->canComment($user, $throw); } /** * Checks whether user can edit specific comment. */ public function canEdit(User $user, int $commentUserId, string $discussionId, $throw = false): bool { return $this->commentPolicy->canEdit($user, $commentUserId, $discussionId, $throw); } /** * Decides whether user can moderate forum discussions. * * @param \FootyRoom\User\User $user * @param bool $throw * * @return bool */ public static function canModerate(User $user, $throw = false) { return CommentPolicy::canModerate($user, $throw); } /** * Decides whether user can administer forum discussions. * * @param \FootyRoom\User\User $user * @param bool $throw * * @return bool */ public static function canAdminister(User $user, $throw = false) { if ($user->getRole() >= 30) { return true; } if ($throw) { throw new AuthException(); } return false; } /** * Determine if user can create new discussions in specified forum category. * * @param int $userRole * @param string $categorySlug * * @return bool */ public static function canPostIn($userRole, $categorySlug) { if ($userRole >= 10 && $categorySlug !== 'footyroom-blog') { return true; } if ($userRole >= 30) { return true; } return false; } /** * Determine if specific user role can see unpublished comments. * * @param int $userRole * * @return bool */ public static function seeUnpublished($userRole) { return $userRole >= 25 ? true : false; } } |