From 93e96334a6fbe2c41a80664e662f94149c974b46 Mon Sep 17 00:00:00 2001 From: flashwave Date: Fri, 4 Nov 2016 17:45:53 +0100 Subject: [PATCH] fix perms + twig strict mode --- app/Forum/ForumPerms.php | 34 +++++++++++++++---- app/Template.php | 1 + app/UserPerms.php | 32 ++++++++++++++--- resources/views/aitemu/master.twig | 2 +- resources/views/aitemu/user/profile.twig | 4 +-- .../views/yuuno/forum/elements/forumBase.twig | 1 - .../views/yuuno/forum/elements/forumBtns.twig | 10 +++--- .../yuuno/forum/elements/forumEntry.twig | 2 +- .../views/yuuno/forum/elements/forumMod.twig | 2 +- .../views/yuuno/forum/elements/replyForm.twig | 2 +- resources/views/yuuno/forum/topic.twig | 2 +- resources/views/yuuno/global/information.twig | 4 +-- resources/views/yuuno/master.twig | 8 ++--- resources/views/yuuno/profile/friends.twig | 20 ++++++----- resources/views/yuuno/settings/master.twig | 2 +- resources/views/yuuno/user/members.twig | 6 ++-- resources/views/yuuno/user/profile.twig | 6 ++-- 17 files changed, 93 insertions(+), 45 deletions(-) diff --git a/app/Forum/ForumPerms.php b/app/Forum/ForumPerms.php index 45ce8d9..8ddda6f 100644 --- a/app/Forum/ForumPerms.php +++ b/app/Forum/ForumPerms.php @@ -16,24 +16,46 @@ use Sakura\User; */ class ForumPerms { + private static $table = 'forum_perms'; private $forums = []; private $user = 0; private $ranks = []; - private $cache = []; + private $permCache = []; + private $validCache = []; public function __construct(Forum $forum, User $user) { - $this->forums = [0, $forum->id, $forum->category]; + $this->forums = [0, $forum->id, $forum->category]; // make this inherit everything before release $this->user = $user->id; $this->ranks = array_keys($user->ranks); } public function __get($name) { - if (!array_key_exists($name, $this->cache)) { + return $this->check($name); + } + + public function __isset($name) + { + return $this->valid($name); + } + + public function valid($name) + { + if (!array_key_exists($name, $this->validCache)) { + $column = 'perm_' . camel_to_snake($name); + $this->validCache[$name] = DB::getSchemaBuilder()->hasColumn(static::$table, $column); + } + + return $this->validCache[$name]; + } + + public function check($name) + { + if (!array_key_exists($name, $this->permCache)) { $column = 'perm_' . camel_to_snake($name); - $result = array_column(DB::table('forum_perms') + $result = array_column(DB::table(static::$table) ->whereIn('forum_id', $this->forums) ->where(function ($query) { $query->whereIn('rank_id', $this->ranks) @@ -41,9 +63,9 @@ class ForumPerms }) ->get([$column]), $column); - $this->cache[$name] = !in_array('0', $result, true) && in_array('1', $result, true); + $this->permCache[$name] = !in_array('0', $result, true) && in_array('1', $result, true); } - return $this->cache[$name]; + return $this->permCache[$name]; } } diff --git a/app/Template.php b/app/Template.php index ef74dc5..38f25e9 100644 --- a/app/Template.php +++ b/app/Template.php @@ -106,6 +106,7 @@ class Template : false, 'auto_reload' => true, 'debug' => config("dev.twig_debug"), + 'strict_variables' => true, ]; // And now actually initialise the templating engine diff --git a/app/UserPerms.php b/app/UserPerms.php index 7b7a8d7..3c1f871 100644 --- a/app/UserPerms.php +++ b/app/UserPerms.php @@ -13,9 +13,11 @@ namespace Sakura; */ class UserPerms { + private static $table = 'perms'; private $user = 0; private $ranks = []; - private $cache = []; + private $permCache = []; + private $validCache = []; public function __construct(User $user) { @@ -25,19 +27,39 @@ class UserPerms public function __get($name) { - if (!array_key_exists($name, $this->cache)) { + return $this->check($name); + } + + public function __isset($name) + { + return $this->valid($name); + } + + public function valid($name) + { + if (!array_key_exists($name, $this->validCache)) { + $column = 'perm_' . camel_to_snake($name); + $this->validCache[$name] = DB::getSchemaBuilder()->hasColumn(static::$table, $column); + } + + return $this->validCache[$name]; + } + + public function check($name) + { + if (!array_key_exists($name, $this->permCache)) { $column = 'perm_' . camel_to_snake($name); - $result = array_column(DB::table('perms') + $result = array_column(DB::table(static::$table) ->where(function ($query) { $query->whereIn('rank_id', $this->ranks) ->orWhere('user_id', $this->user); }) ->get([$column]), $column); - $this->cache[$name] = !in_array('0', $result, true) && in_array('1', $result, true); + $this->permCache[$name] = !in_array('0', $result, true) && in_array('1', $result, true); } - return $this->cache[$name]; + return $this->permCache[$name]; } } diff --git a/resources/views/aitemu/master.twig b/resources/views/aitemu/master.twig index 9ca705e..f839023 100644 --- a/resources/views/aitemu/master.twig +++ b/resources/views/aitemu/master.twig @@ -25,7 +25,7 @@
-
+