diff --git a/app/BBCode/Tags/NamedQuote.php b/app/BBCode/Tags/NamedQuote.php index 343ba2d..1302e0a 100644 --- a/app/BBCode/Tags/NamedQuote.php +++ b/app/BBCode/Tags/NamedQuote.php @@ -9,7 +9,6 @@ namespace Sakura\BBCode\Tags; use Sakura\BBCode\TagBase; use Sakura\Forum\Forum; use Sakura\Forum\Post; -use Sakura\Perms\Forum as ForumPerms; use Sakura\User; /** @@ -36,7 +35,7 @@ class NamedQuote extends TagBase $post = new Post(intval($matches[2])); $forum = new Forum($post->forum); - if ($post->id !== 0 && $forum->permission(ForumPerms::VIEW, $poster->id)) { + if ($post->id !== 0 && $forum->perms->view) { $link = route('forums.post', $post->id); $quoting = "{$post->poster->username}"; diff --git a/app/Console/Command/SetupCommand.php b/app/Console/Command/SetupCommand.php index 867610f..7a1befe 100644 --- a/app/Console/Command/SetupCommand.php +++ b/app/Console/Command/SetupCommand.php @@ -238,7 +238,6 @@ class SetupCommand extends Command [ 'forum_id' => 1, 'rank_id' => config('rank.regular'), - 'forum_perms' => '00000011111', 'perm_view' => true, 'perm_reply' => true, 'perm_topic_create' => true, diff --git a/app/Controllers/Forum/ForumController.php b/app/Controllers/Forum/ForumController.php index d17ebd8..75437f3 100644 --- a/app/Controllers/Forum/ForumController.php +++ b/app/Controllers/Forum/ForumController.php @@ -14,7 +14,6 @@ use Sakura\DB; use Sakura\Forum\Forum; use Sakura\Forum\Post; use Sakura\Forum\Topic; -use Sakura\Perms\Forum as ForumPerms; use Sakura\User; /** @@ -45,7 +44,7 @@ class ForumController extends Controller $forum = new Forum($topic->forum); // Check if we have permission to view it - if (!$forum->permission(ForumPerms::VIEW, CurrentSession::$user->id)) { + if (!$forum->perms->view) { $fetch = DB::table('posts') ->groupBy('topic_id') ->orderByRaw('COUNT(*) DESC') @@ -75,7 +74,7 @@ class ForumController extends Controller $forum = new Forum($post->forum); // Check if we have permission to view it - if (!$forum->permission(ForumPerms::VIEW, CurrentSession::$user->id)) { + if (!$forum->perms->view) { $fetch = DB::table('posts') ->orderBy('post_id', 'desc') ->skip(11 + $_n) @@ -124,7 +123,7 @@ class ForumController extends Controller // Check if the forum exists if ($forum->id < 0 - || !$forum->permission(ForumPerms::VIEW, CurrentSession::$user->id)) { + || !$forum->perms->view) { throw new HttpRouteNotFoundException(); } @@ -152,7 +151,7 @@ class ForumController extends Controller // Check if the forum exists if ($forum->id < 1 - || !$forum->permission(ForumPerms::VIEW, CurrentSession::$user->id)) { + || !$forum->perms->view) { throw new HttpRouteNotFoundException(); } diff --git a/app/Controllers/Forum/PostController.php b/app/Controllers/Forum/PostController.php index 221d166..e4cda1d 100644 --- a/app/Controllers/Forum/PostController.php +++ b/app/Controllers/Forum/PostController.php @@ -13,8 +13,6 @@ use Sakura\DB; use Sakura\Forum\Forum; use Sakura\Forum\Post; use Sakura\Forum\Topic; -use Sakura\Perms; -use Sakura\Perms\Forum as ForumPerms; /** * Topic controller. @@ -37,7 +35,7 @@ class PostController extends Controller // Check if the forum exists if ($post->id === 0 || $topic->id === 0 - || !$forum->permission(ForumPerms::VIEW, CurrentSession::$user->id)) { + || !$forum->perms->view) { throw new HttpRouteNotFoundException(); } @@ -74,7 +72,7 @@ class PostController extends Controller // Check if the forum exists if ($post->id === 0 || $topic->id === 0 - || !$forum->permission(ForumPerms::VIEW, CurrentSession::$user->id)) { + || !$forum->perms->view) { return ""; } @@ -98,15 +96,15 @@ class PostController extends Controller // Check permissions $noAccess = $post->id === 0 || $topic->id === 0 - || !$forum->permission(ForumPerms::VIEW, CurrentSession::$user->id); + || !$forum->perms->view; $noEdit = ( $post->poster->id === CurrentSession::$user->id - ? !CurrentSession::$user->permission(ForumPerms::EDIT_OWN, Perms::FORUM) - : !$forum->permission(ForumPerms::EDIT_ANY, CurrentSession::$user->id) + ? !$forum->perms->edit + : !$forum->perms->editAny ) || ( $topic->status === 1 - && !$forum->permission(ForumPerms::LOCK, CurrentSession::$user->id) + && !$forum->perms->changeStatus ); // Check if the forum exists @@ -195,15 +193,20 @@ class PostController extends Controller // Check permissions $noAccess = $post->id === 0 || $topic->id === 0 - || !$forum->permission(ForumPerms::VIEW, CurrentSession::$user->id); + || !$forum->perms->view; + + $replies = $topic->replyCount(); $noDelete = ( $post->poster->id === CurrentSession::$user->id - ? !CurrentSession::$user->permission(ForumPerms::DELETE_OWN, Perms::FORUM) - : !$forum->permission(ForumPerms::DELETE_ANY, CurrentSession::$user->id) + ? !$forum->perms->delete + : !$forum->perms->deleteAny ) || ( $topic->status === 1 - && !$forum->permission(ForumPerms::LOCK, CurrentSession::$user->id) + && !$forum->perms->changeStatus + ) || ( + $replies === 1 && + !$forum->perms->topicDelete ); // Check if the forum exists @@ -212,7 +215,7 @@ class PostController extends Controller } // Check if the topic only has 1 post - if ($topic->replyCount() === 1) { + if ($replies === 1) { // Delete the entire topic $topic->delete(); } else { diff --git a/app/Controllers/Forum/TopicController.php b/app/Controllers/Forum/TopicController.php index c8f60bf..e1078f4 100644 --- a/app/Controllers/Forum/TopicController.php +++ b/app/Controllers/Forum/TopicController.php @@ -12,7 +12,6 @@ use Sakura\CurrentSession; use Sakura\Forum\Forum; use Sakura\Forum\Post; use Sakura\Forum\Topic; -use Sakura\Perms\Forum as ForumPerms; /** * Topic controller. @@ -34,7 +33,7 @@ class TopicController extends Controller // Check if the forum exists if ($topic->id === 0 - || !$forum->permission(ForumPerms::VIEW, CurrentSession::$user->id)) { + || !$forum->perms->view) { throw new HttpRouteNotFoundException; } @@ -56,7 +55,7 @@ class TopicController extends Controller $forum = new Forum($topic->forum); if ($topic->id !== 0 - || $forum->permission(ForumPerms::VIEW, CurrentSession::$user->id) + || $forum->perms->view || session_check()) { return compact('topic', 'forum'); } @@ -74,7 +73,7 @@ class TopicController extends Controller { extract($this->modBase($id)); - if (!$forum->permission(ForumPerms::STICKY, CurrentSession::$user->id)) { + if (!$forum->perms->changeType) { throw new HttpMethodNotAllowedException; } @@ -94,7 +93,7 @@ class TopicController extends Controller { extract($this->modBase($id)); - if (!$forum->permission(ForumPerms::ANNOUNCEMENT, CurrentSession::$user->id)) { + if (!$forum->perms->changeType) { throw new HttpMethodNotAllowedException; } @@ -114,7 +113,7 @@ class TopicController extends Controller { extract($this->modBase($id)); - if (!$forum->permission(ForumPerms::LOCK, CurrentSession::$user->id)) { + if (!$forum->perms->changeStatus) { throw new HttpMethodNotAllowedException; } @@ -137,10 +136,10 @@ class TopicController extends Controller $trash = intval(config('forum.trash')); if ($topic->forum === $trash - && $forum->permission(ForumPerms::DELETE_ANY, CurrentSession::$user->id)) { + && $forum->perms->deleteAny) { $redirect = route('forums.forum', $trash); $topic->delete(); - } elseif ($forum->permission(ForumPerms::MOVE, CurrentSession::$user->id)) { + } elseif ($forum->perms->topicMove) { $redirect = route('forums.topic', $topic->id); $topic->move($trash); } else { @@ -160,7 +159,7 @@ class TopicController extends Controller { extract($this->modBase($id)); - if (!$forum->permission(ForumPerms::MOVE, CurrentSession::$user->id)) { + if (!$forum->perms->topicMove) { throw new HttpMethodNotAllowedException; } @@ -182,9 +181,9 @@ class TopicController extends Controller extract($this->modBase($id)); $dest_forum = new Forum($_REQUEST['forum_id'] ?? 0); - if (!$forum->permission(ForumPerms::MOVE, CurrentSession::$user->id) + if (!$forum->perms->topicMove || $dest_forum->id === 0 - || $dest_forum->permission(ForumPerms::VIEW, CurrentSession::$user->id)) { + || $dest_forum->perms->view) { throw new HttpMethodNotAllowedException; } @@ -211,7 +210,7 @@ class TopicController extends Controller // Check if the topic exists if ($topic->id === 0 || $forum->type !== 0 - || !$forum->permission(ForumPerms::VIEW, CurrentSession::$user->id)) { + || !$forum->perms->view) { $message = "This post doesn't exist or you don't have access to it!"; $redirect = route('forums.index'); @@ -219,10 +218,10 @@ class TopicController extends Controller } // Check if the topic exists - if (!$forum->permission(ForumPerms::REPLY, CurrentSession::$user->id) + if (!$forum->perms->reply || ( $topic->status === 1 - && !$forum->permission(ForumPerms::LOCK, CurrentSession::$user->id) + && !$forum->perms->changeStatus )) { $message = "You are not allowed to post in this topic!"; $redirect = route('forums.topic', $topic->id); @@ -292,9 +291,9 @@ class TopicController extends Controller // Check if the forum exists if ($forum->id === 0 || $forum->type !== 0 - || !$forum->permission(ForumPerms::VIEW, CurrentSession::$user->id) - || !$forum->permission(ForumPerms::REPLY, CurrentSession::$user->id) - || !$forum->permission(ForumPerms::CREATE_THREADS, CurrentSession::$user->id)) { + || !$forum->perms->view + || !$forum->perms->reply + || !$forum->perms->topicCreate) { $message = "This forum doesn't exist or you don't have access to it!"; $redirect = route('forums.index'); diff --git a/app/Forum/Forum.php b/app/Forum/Forum.php index 3fb3f79..627f5cf 100644 --- a/app/Forum/Forum.php +++ b/app/Forum/Forum.php @@ -122,18 +122,6 @@ class Forum $this->perms = new ForumPerms($this, CurrentSession::$user); } - /** - * Checking a permission flag. - * @param int $flag - * @param int $user - * @param bool $raw - * @return bool|int - */ - public function permission($flag, $user, $raw = false) - { - return $raw ? 1024 : true; - } - /** * Gets all subforums of this forum. * @return array @@ -157,12 +145,9 @@ class Forum } $this->forumsCache = $forums; - } else { - $forums = $this->forumsCache; } - // Return the forum objects - return $forums; + return $this->forumsCache; } /** diff --git a/app/Forum/ForumPerms.php b/app/Forum/ForumPerms.php index b2a0cb2..45ce8d9 100644 --- a/app/Forum/ForumPerms.php +++ b/app/Forum/ForumPerms.php @@ -6,6 +6,7 @@ namespace Sakura\Forum; +use Sakura\DB; use Sakura\User; /** @@ -15,14 +16,16 @@ use Sakura\User; */ class ForumPerms { - private $forum = []; + private $forums = []; private $user = 0; private $ranks = []; private $cache = []; public function __construct(Forum $forum, User $user) { - // + $this->forums = [0, $forum->id, $forum->category]; + $this->user = $user->id; + $this->ranks = array_keys($user->ranks); } public function __get($name) @@ -31,7 +34,7 @@ class ForumPerms $column = 'perm_' . camel_to_snake($name); $result = array_column(DB::table('forum_perms') - ->whereIn('forum_id', $this->forum) + ->whereIn('forum_id', $this->forums) ->where(function ($query) { $query->whereIn('rank_id', $this->ranks) ->orWhere('user_id', $this->user); diff --git a/app/Perms/Forum.php b/app/Perms/Forum.php deleted file mode 100644 index 23bad66..0000000 --- a/app/Perms/Forum.php +++ /dev/null @@ -1,70 +0,0 @@ - - */ -class Forum -{ - /** - * Can this user view/read this forum? - */ - const VIEW = 1; - - /** - * Can this user post/reply in this forum? - */ - const REPLY = 2; - - /** - * Can this user create topics in this forum? - */ - const CREATE_THREADS = 4; - - /** - * Can this user edit their own posts? - */ - const EDIT_OWN = 8; - - /** - * Can this user delete their own posts? - */ - const DELETE_OWN = 16; - - /** - * Can this user change topics to the sticky type? - */ - const STICKY = 32; - - /** - * Can this user change topics to the announcement type? - */ - const ANNOUNCEMENT = 64; - - /** - * Can this user edit any post in this forum? - */ - const EDIT_ANY = 128; - - /** - * Can this user delete any post in this forum? - */ - const DELETE_ANY = 256; - - /** - * Can this user toggle the locked status on topics in this forum? - */ - const LOCK = 512; - - /** - * Can this user move topics to other forums from/to this forum? - */ - const MOVE = 1024; -} diff --git a/app/Rank.php b/app/Rank.php index 1386f44..a6f0f6e 100644 --- a/app/Rank.php +++ b/app/Rank.php @@ -6,8 +6,6 @@ namespace Sakura; -use Sakura\Perms; - /** * Serves Rank data. * @package Sakura @@ -63,12 +61,6 @@ class Rank */ private $hidden = true; - /** - * Permission container. - * @var Perms - */ - private $permissions; - /** * Instance cache container. * @var array @@ -136,22 +128,6 @@ class Rank return $this->hidden; } - /** - * Check permissions. - * @param int $flag - * @return bool - */ - public function permission($flag) - { - // Set default permission value - $perm = 0; - - // Bitwise OR it with the permissions for this forum - $perm = $perm | $this->permissions->rank($this->id); - - return $this->permissions->check($flag, $perm); - } - /** * Returns all users that are part of this rank. * @param bool $justIds diff --git a/app/User.php b/app/User.php index e096fdc..c511ead 100644 --- a/app/User.php +++ b/app/User.php @@ -11,7 +11,6 @@ use LastFmApi\Api\AuthApi; use LastFmApi\Api\UserApi; use LastFmApi\Exception\LastFmApiExeption; use Sakura\Exceptions\NetAddressTypeException; -use Sakura\Perms; use stdClass; /** @@ -818,26 +817,6 @@ class User return $objects; } - /** - * Check if the user has a certaing permission flag. - * @param int $flag - * @param string $mode - * @return bool - */ - public function permission($flag, $mode = null) - { - // Set mode - $this->permissions->mode($mode ? $mode : Perms::SITE); - - // Set default permission value - $perm = 0; - - // Bitwise OR it with the permissions for this forum - $perm = $this->permissions->user($this->id); - - return $this->permissions->check($flag, $perm); - } - /** * Get the comments from the user's profile. * @return array diff --git a/database/2016_11_23_152450_restructure_permissions.php b/database/2016_11_23_152450_restructure_permissions.php index dfbbcfe..a3f168c 100644 --- a/database/2016_11_23_152450_restructure_permissions.php +++ b/database/2016_11_23_152450_restructure_permissions.php @@ -24,6 +24,9 @@ class RestructurePermissions extends Migration ->default(0); }); + $schema->drop('forum_permissions'); + $schema->drop('permissions'); + $schema->create('perms', function (Blueprint $table) { $table->integer('user_id')->default(0); $table->integer('rank_id')->default(0); @@ -88,8 +91,41 @@ class RestructurePermissions extends Migration public function down() { $schema = DB::getSchemaBuilder(); + $schema->drop('forum_perms'); $schema->drop('perms'); + + $schema->create('permissions', function (Blueprint $table) { + $table->integer('rank_id') + ->unsigned() + ->default(0); + + $table->integer('user_id') + ->unsigned() + ->default(0); + + $table->string('permissions_site', 255) + ->default(0); + + $table->string('permissions_manage', 255) + ->default(0); + }); + + $schema->create('forum_permissions', function (Blueprint $table) { + $table->integer('forum_id') + ->unsigned(); + + $table->integer('rank_id') + ->unsigned() + ->default(0); + + $table->integer('user_id') + ->unsigned() + ->default(0); + + $table->string('forum_perms', 255); + }); + $schema->table('users', function (Blueprint $table) { $table->dropColumn([ 'user_activated', diff --git a/resources/assets/typescript/Sakura/DOM.ts b/resources/assets/typescript/Sakura/DOM.ts index bab1980..7936469 100644 --- a/resources/assets/typescript/Sakura/DOM.ts +++ b/resources/assets/typescript/Sakura/DOM.ts @@ -55,8 +55,8 @@ namespace Sakura element.parentNode.removeChild(element); } - public static Class(className: string): NodeListOf { - return >document.getElementsByClassName(className); + public static Class(className: string): HTMLCollectionOf { + return >document.getElementsByClassName(className); } public static Prepend(target: HTMLElement, element: HTMLElement | Text, before: HTMLElement | Node = null): void { diff --git a/resources/assets/typescript/Sakura/TimeAgo.ts b/resources/assets/typescript/Sakura/TimeAgo.ts index 6e0e685..10b98a9 100644 --- a/resources/assets/typescript/Sakura/TimeAgo.ts +++ b/resources/assets/typescript/Sakura/TimeAgo.ts @@ -28,7 +28,7 @@ namespace Sakura return none; } - var times: Object = { + var times: any = { 31536000: ['year', 'a'], 2592000: ['month', 'a'], 604800: ['week', 'a'], diff --git a/resources/views/yuuno/forum/elements/forumBase.twig b/resources/views/yuuno/forum/elements/forumBase.twig index 059ee86..b6244a6 100644 --- a/resources/views/yuuno/forum/elements/forumBase.twig +++ b/resources/views/yuuno/forum/elements/forumBase.twig @@ -1,8 +1,9 @@
{{ title }}
{% for forum in forum.forums %} + {{ forum.perms.view ? 'yay' : 'no'}} {% if forum.type == 1 %} - {% if forum.forums|length and forum.permission(constant('Sakura\\Perms\\Forum::VIEW'), user.id) %} + {% if forum.forums|length and forum.perms.view %}
{% if forum.type != 1 %}Subforums{% else %}{{ forum.name }}{% endif %}
diff --git a/resources/views/yuuno/forum/elements/forumEntry.twig b/resources/views/yuuno/forum/elements/forumEntry.twig index b406bac..b9b6796 100644 --- a/resources/views/yuuno/forum/elements/forumEntry.twig +++ b/resources/views/yuuno/forum/elements/forumEntry.twig @@ -1,4 +1,4 @@ -{% if forum.permission(constant('Sakura\\Perms\\Forum::VIEW'), user.id) %} +{% if forum.perms.view %}
diff --git a/resources/views/yuuno/forum/topic.twig b/resources/views/yuuno/forum/topic.twig index 6987963..e51a801 100644 --- a/resources/views/yuuno/forum/topic.twig +++ b/resources/views/yuuno/forum/topic.twig @@ -5,35 +5,31 @@ {% set title %}{% if topic is defined %}{{ topic.title }}{% else %}Creating topic in {{ forum.name }}{% endif %}{% endset %} {% if topic is defined %} - {% if forum.permission(constant('Sakura\\Perms\\Forum::REPLY'), user.id) + {% if forum.perms.reply and ( topic.status != 1 - or forum.permission(constant('Sakura\\Perms\\Forum::LOCK'), user.id) + or forum.perms.changeStatus ) %} {% set forumReplyLink %}#reply{% endset %} {% endif %} - {% if forum.permission(constant('Sakura\\Perms\\Forum::STICKY'), user.id) - or forum.permission(constant('Sakura\\Perms\\Forum::ANNOUNCEMENT'), user.id) - or forum.permission(constant('Sakura\\Perms\\Forum::LOCK'), user.id) - or forum.permission(constant('Sakura\\Perms\\Forum::MOVE'), user.id) - or forum.permission(constant('Sakura\\Perms\\Forum::DELETE_ANY'), user.id) %} + {% if forum.perms.changeType + or forum.perms.changeStatus + or forum.perms.topicMove + or forum.perms.deleteAny %} {% set showMod = true %} {% endif %} - {% if forum.permission(constant('Sakura\\Perms\\Forum::STICKY'), user.id) %} + {% if forum.perms.changeType %} {% set forumSticky = topic.type == 1 ? true : false %} - {% endif %} - - {% if forum.permission(constant('Sakura\\Perms\\Forum::ANNOUNCEMENT'), user.id) %} {% set forumAnnounce = topic.type == 2 ? true : false %} {% endif %} - {% if forum.permission(constant('Sakura\\Perms\\Forum::LOCK'), user.id) %} + {% if forum.perms.changeStatus %} {% set forumLock = topic.status == 1 ? true : false %} {% endif %} - {% if forum.permission(constant('Sakura\\Perms\\Forum::MOVE'), user.id) %} + {% if forum.perms.topicMove %} {% if topic.oldForum %} {% set forumRestore = true %} {% endif %} @@ -43,7 +39,7 @@ {% endif %} {% endif %} - {% if forum.permission(constant('Sakura\\Perms\\Forum::DELETE_ANY'), user.id) %} + {% if forum.perms.deleteAny %} {% if topic.forum == config('forum.trash') %} {% set forumPrune = true %} {% endif %} @@ -116,10 +112,10 @@ Tenshi {{ post.poster.country(true) }}{% if post.poster.id == (topic.posts|first).poster.id %} OP{% endif %} {% if user.isActive %}
- {% if (user.id == post.poster.id and forum.permission(constant('Sakura\\Perms\\Forum::EDIT_OWN'), user.id)) or forum.permission(constant('Sakura\\Perms\\Forum::EDIT_ANY'), user.id) %} + {% if (user.id == post.poster.id and forum.perms.edit) or forum.perms.editAny %} {% endif %} - {% if (user.id == post.poster.id and forum.permission(constant('Sakura\\Perms\\Forum::DELETE_OWN'), user.id)) or forum.permission(constant('Sakura\\Perms\\Forum::DELETE_ANY'), user.id) %} + {% if (user.id == post.poster.id and forum.perms.delete) or forum.perms.deleteAny %} {% endif %} {% if not (post.poster.activated or post.poster.restricted or user.id == post.poster.id) %}