From 019e7eea7fdc3eeab39e67a449797d8367b7c983 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 18 Oct 2013 00:09:50 +0200 Subject: [PATCH] Closed #43 --- src/Controllers/CommentController.php | 7 ++--- src/Controllers/PostController.php | 44 ++++++++++++--------------- src/Controllers/TagController.php | 2 +- src/Controllers/UserController.php | 33 +++++++++----------- src/Helpers/PrivilegesHelper.php | 23 +++++++++----- src/Views/comment-small.phtml | 3 +- src/Views/layout-normal.phtml | 14 ++++----- src/Views/post-view.phtml | 11 +++---- src/Views/user-view.phtml | 19 ++++++------ 9 files changed, 75 insertions(+), 81 deletions(-) diff --git a/src/Controllers/CommentController.php b/src/Controllers/CommentController.php index 259bd54e..4cf688b9 100644 --- a/src/Controllers/CommentController.php +++ b/src/Controllers/CommentController.php @@ -17,7 +17,7 @@ class CommentController $page = intval($page); $commentsPerPage = intval($this->config->comments->commentsPerPage); - PrivilegesHelper::confirmWithException($this->context->user, Privilege::ListComments); + PrivilegesHelper::confirmWithException(Privilege::ListComments); $buildDbQuery = function($dbQuery) { @@ -59,7 +59,7 @@ class CommentController */ public function addAction($postId) { - PrivilegesHelper::confirmWithException($this->context->user, Privilege::AddComment); + PrivilegesHelper::confirmWithException(Privilege::AddComment); if ($this->config->registration->needEmailForCommenting) PrivilegesHelper::confirmEmail($this->context->user); @@ -90,8 +90,7 @@ class CommentController public function deleteAction($id) { $comment = Model_Comment::locate($id); - $secondary = $comment->commenter->id == $this->context->user->id ? 'own' : 'all'; - PrivilegesHelper::confirmWithException($this->context->user, Privilege::DeleteComment, $secondary); + PrivilegesHelper::confirmWithException(Privilege::DeleteComment, PrivilegesHelper::getIdentitySubPrivilege($comment->commenter)); R::trash($comment); $this->context->transport->success = true; } diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index 69b4423e..ee5e80b8 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -80,7 +80,7 @@ class PostController $postsPerPage = intval($this->config->browsing->postsPerPage); $this->context->subTitle = 'browsing posts'; $this->context->transport->searchQuery = $query; - PrivilegesHelper::confirmWithException($this->context->user, Privilege::ListPosts); + PrivilegesHelper::confirmWithException(Privilege::ListPosts); $buildDbQuery = function($dbQuery, $query) { @@ -90,7 +90,7 @@ class PostController /* safety */ $allowedSafety = array_filter(PostSafety::getAll(), function($safety) { - return PrivilegesHelper::confirm($this->context->user, Privilege::ListPosts, PostSafety::toString($safety)) and + return PrivilegesHelper::confirm(Privilege::ListPosts, PostSafety::toString($safety)) and $this->context->user->hasEnabledSafety($safety); }); $dbQuery->where('safety IN (' . R::genSlots($allowedSafety) . ')'); @@ -99,7 +99,7 @@ class PostController /* hidden */ - if (!PrivilegesHelper::confirm($this->context->user, Privilege::ListPosts, 'hidden')) + if (!PrivilegesHelper::confirm(Privilege::ListPosts, 'hidden')) $dbQuery->andNot('hidden'); @@ -158,7 +158,7 @@ class PostController $this->context->stylesheets []= 'upload.css'; $this->context->scripts []= 'upload.js'; $this->context->subTitle = 'upload'; - PrivilegesHelper::confirmWithException($this->context->user, Privilege::UploadPost); + PrivilegesHelper::confirmWithException(Privilege::UploadPost); if ($this->config->registration->needEmailForUploading) PrivilegesHelper::confirmEmail($this->context->user); @@ -246,7 +246,6 @@ class PostController $post = Model_Post::locate($id); R::preload($post, ['uploader' => 'user']); $edited = false; - $secondary = $post->uploader->id == $this->context->user->id ? 'own' : 'all'; $this->context->transport->post = $post; @@ -254,7 +253,7 @@ class PostController $suppliedSafety = InputHelper::get('safety'); if ($suppliedSafety !== null) { - PrivilegesHelper::confirmWithException($this->context->user, Privilege::EditPostSafety, $secondary); + PrivilegesHelper::confirmWithException(Privilege::EditPostSafety, PrivilegesHelper::getIdentitySubPrivilege($post->uploader)); $suppliedSafety = Model_Post::validateSafety($suppliedSafety); $post->safety = $suppliedSafety; $edited = true; @@ -265,7 +264,7 @@ class PostController $suppliedTags = InputHelper::get('tags'); if ($suppliedTags !== null) { - PrivilegesHelper::confirmWithException($this->context->user, Privilege::EditPostTags, $secondary); + PrivilegesHelper::confirmWithException(Privilege::EditPostTags, PrivilegesHelper::getIdentitySubPrivilege($post->uploader)); $currentToken = self::serializeTags($post); if (InputHelper::get('tags-token') != $currentToken) throw new SimpleException('Someone else has changed the tags in the meantime'); @@ -280,7 +279,7 @@ class PostController /* thumbnail */ if (!empty($_FILES['thumb']['name'])) { - PrivilegesHelper::confirmWithException($this->context->user, Privilege::EditPostThumb, $secondary); + PrivilegesHelper::confirmWithException(Privilege::EditPostThumb, PrivilegesHelper::getIdentitySubPrivilege($post->uploader)); $suppliedFile = $_FILES['thumb']; self::handleUploadErrors($suppliedFile); @@ -312,8 +311,7 @@ class PostController public function hideAction($id) { $post = Model_Post::locate($id); - $secondary = $post->uploader->id == $this->context->user->id ? 'own' : 'all'; - PrivilegesHelper::confirmWithException($this->context->user, Privilege::HidePost, $secondary); + PrivilegesHelper::confirmWithException(Privilege::HidePost, PrivilegesHelper::getIdentitySubPrivilege($post->uploader)); $post->hidden = true; R::store($post); $this->context->transport->success = true; @@ -325,8 +323,7 @@ class PostController public function unhideAction($id) { $post = Model_Post::locate($id); - $secondary = $post->uploader->id == $this->context->user->id ? 'own' : 'all'; - PrivilegesHelper::confirmWithException($this->context->user, Privilege::HidePost, $secondary); + PrivilegesHelper::confirmWithException(Privilege::HidePost, PrivilegesHelper::getIdentitySubPrivilege($post->uploader)); $post->hidden = false; R::store($post); $this->context->transport->success = true; @@ -338,8 +335,7 @@ class PostController public function deleteAction($id) { $post = Model_Post::locate($id); - $secondary = $post->uploader->id == $this->context->user->id ? 'own' : 'all'; - PrivilegesHelper::confirmWithException($this->context->user, Privilege::DeletePost, $secondary); + PrivilegesHelper::confirmWithException(Privilege::DeletePost, PrivilegesHelper::getIdentitySubPrivilege($post->uploader)); //remove stuff from auxiliary tables $post->ownFavoritee = []; $post->sharedTag = []; @@ -366,7 +362,7 @@ class PostController if ($fav->id == $this->context->user->id) throw new SimpleException('Already in favorites'); - PrivilegesHelper::confirmWithException($this->context->user, Privilege::FavoritePost); + PrivilegesHelper::confirmWithException(Privilege::FavoritePost); $post->link('favoritee')->user = $this->context->user; R::store($post); $this->context->transport->success = true; @@ -381,7 +377,7 @@ class PostController $post = Model_Post::locate($id); R::preload($post, ['favoritee' => 'user']); - PrivilegesHelper::confirmWithException($this->context->user, Privilege::FavoritePost); + PrivilegesHelper::confirmWithException(Privilege::FavoritePost); if (!$this->context->loggedIn) throw new SimpleException('Not logged in'); @@ -415,9 +411,9 @@ class PostController 'ownComment.commenter' => 'user']); if ($post->hidden) - PrivilegesHelper::confirmWithException($this->context->user, Privilege::ViewPost, 'hidden'); - PrivilegesHelper::confirmWithException($this->context->user, Privilege::ViewPost); - PrivilegesHelper::confirmWithException($this->context->user, Privilege::ViewPost, PostSafety::toString($post->safety)); + PrivilegesHelper::confirmWithException(Privilege::ViewPost, 'hidden'); + PrivilegesHelper::confirmWithException(Privilege::ViewPost); + PrivilegesHelper::confirmWithException(Privilege::ViewPost, PostSafety::toString($post->safety)); $buildNextPostQuery = function($dbQuery, $id, $next) { @@ -425,7 +421,7 @@ class PostController ->from('post') ->where($next ? 'id > ?' : 'id < ?') ->put($id); - if (!PrivilegesHelper::confirm($this->context->user, Privilege::ListPosts, 'hidden')) + if (!PrivilegesHelper::confirm(Privilege::ListPosts, 'hidden')) $dbQuery->andNot('hidden'); $dbQuery->orderBy($next ? 'id asc' : 'id desc') ->limit(1); @@ -481,8 +477,8 @@ class PostController $this->context->layoutName = 'layout-file'; $post = Model_Post::locate($id); - PrivilegesHelper::confirmWithException($this->context->user, Privilege::ViewPost); - PrivilegesHelper::confirmWithException($this->context->user, Privilege::ViewPost, PostSafety::toString($post->safety)); + PrivilegesHelper::confirmWithException(Privilege::ViewPost); + PrivilegesHelper::confirmWithException(Privilege::ViewPost, PostSafety::toString($post->safety)); $path = $this->config->main->thumbsPath . DS . $post->name; if (!file_exists($path)) @@ -554,8 +550,8 @@ class PostController $post = Model_Post::locate($name, true); R::preload($post, ['tag']); - PrivilegesHelper::confirmWithException($this->context->user, Privilege::RetrievePost); - PrivilegesHelper::confirmWithException($this->context->user, Privilege::RetrievePost, PostSafety::toString($post->safety)); + PrivilegesHelper::confirmWithException(Privilege::RetrievePost); + PrivilegesHelper::confirmWithException(Privilege::RetrievePost, PostSafety::toString($post->safety)); $path = $this->config->main->filesPath . DS . $post->name; if (!file_exists($path)) diff --git a/src/Controllers/TagController.php b/src/Controllers/TagController.php index 6c330d13..d78a1f94 100644 --- a/src/Controllers/TagController.php +++ b/src/Controllers/TagController.php @@ -8,7 +8,7 @@ class TagController { $this->context->subTitle = 'tags'; - PrivilegesHelper::confirmWithException($this->context->user, Privilege::ListTags); + PrivilegesHelper::confirmWithException(Privilege::ListTags); $dbQuery = R::$f->begin(); $dbQuery->select('tag.name, COUNT(1) AS count'); diff --git a/src/Controllers/UserController.php b/src/Controllers/UserController.php index c74940d0..397260ed 100644 --- a/src/Controllers/UserController.php +++ b/src/Controllers/UserController.php @@ -59,7 +59,7 @@ class UserController $page = intval($page); $usersPerPage = intval($this->config->browsing->usersPerPage); $this->context->subTitle = 'browsing users'; - PrivilegesHelper::confirmWithException($this->context->user, Privilege::ListUsers); + PrivilegesHelper::confirmWithException(Privilege::ListUsers); if ($sortStyle == '' or $sortStyle == 'alpha') $sortStyle = 'alpha,asc'; @@ -127,8 +127,7 @@ class UserController public function banAction($name) { $user = Model_User::locate($name); - $secondary = $user->id == $this->context->user->id ? 'own' : 'all'; - PrivilegesHelper::confirmWithException($this->context->user, Privilege::BanUser, $secondary); + PrivilegesHelper::confirmWithException(Privilege::BanUser, PrivilegesHelper::getIdentitySubPrivilege($user)); $user->banned = true; R::store($user); $this->context->transport->success = true; @@ -141,8 +140,7 @@ class UserController public function unbanAction($name) { $user = Model_User::locate($name); - $secondary = $user->id == $this->context->user->id ? 'own' : 'all'; - PrivilegesHelper::confirmWithException($this->context->user, Privilege::BanUser, $secondary); + PrivilegesHelper::confirmWithException(Privilege::BanUser, PrivilegesHelper::getIdentitySubPrivilege($user)); $user->banned = false; R::store($user); $this->context->transport->success = true; @@ -155,7 +153,7 @@ class UserController public function acceptRegistrationAction($name) { $user = Model_User::locate($name); - PrivilegesHelper::confirmWithException($this->context->user, Privilege::AcceptUserRegistration); + PrivilegesHelper::confirmWithException(Privilege::AcceptUserRegistration); $user->staff_confirmed = true; R::store($user); $this->context->transport->success = true; @@ -171,9 +169,8 @@ class UserController public function deleteAction($name) { $user = Model_User::locate($name); - $secondary = $user->id == $this->context->user->id ? 'own' : 'all'; - PrivilegesHelper::confirmWithException($this->context->user, Privilege::ViewUser, $secondary); - PrivilegesHelper::confirmWithException($this->context->user, Privilege::DeleteUser, $secondary); + PrivilegesHelper::confirmWithException(Privilege::ViewUser, PrivilegesHelper::getIdentitySubPrivilege($user)); + PrivilegesHelper::confirmWithException(Privilege::DeleteUser, PrivilegesHelper::getIdentitySubPrivilege($user)); $this->context->handleExceptions = true; $this->context->transport->user = $user; @@ -213,8 +210,7 @@ class UserController $user = Model_User::locate($name); $edited = false; - $secondary = $user->id == $this->context->user->id ? 'own' : 'all'; - PrivilegesHelper::confirmWithException($this->context->user, Privilege::ViewUser, $secondary); + PrivilegesHelper::confirmWithException(Privilege::ViewUser, PrivilegesHelper::getIdentitySubPrivilege($user)); $this->context->handleExceptions = true; $this->context->transport->user = $user; @@ -233,7 +229,7 @@ class UserController if ($suppliedName != '' and $suppliedName != $user->name) { - PrivilegesHelper::confirmWithException($this->context->user, Privilege::ChangeUserName, $secondary); + PrivilegesHelper::confirmWithException(Privilege::ChangeUserName, PrivilegesHelper::getIdentitySubPrivilege($user)); $suppliedName = Model_User::validateUserName($suppliedName); $user->name = $suppliedName; $edited = true; @@ -241,7 +237,7 @@ class UserController if ($suppliedPassword1 != '') { - PrivilegesHelper::confirmWithException($this->context->user, Privilege::ChangeUserPassword, $secondary); + PrivilegesHelper::confirmWithException(Privilege::ChangeUserPassword, PrivilegesHelper::getIdentitySubPrivilege($user)); if ($suppliedPassword1 != $suppliedPassword2) throw new SimpleException('Specified passwords must be the same'); $suppliedPassword = Model_User::validatePassword($suppliedPassword1); @@ -251,7 +247,7 @@ class UserController if ($suppliedEmail != '' and $suppliedEmail != $user->email_confirmed) { - PrivilegesHelper::confirmWithException($this->context->user, Privilege::ChangeUserEmail, $secondary); + PrivilegesHelper::confirmWithException(Privilege::ChangeUserEmail, PrivilegesHelper::getIdentitySubPrivilege($user)); $suppliedEmail = Model_User::validateEmail($suppliedEmail); if ($this->context->user->id == $user->id) { @@ -268,7 +264,7 @@ class UserController if ($suppliedAccessRank != '' and $suppliedAccessRank != $user->access_rank) { - PrivilegesHelper::confirmWithException($this->context->user, Privilege::ChangeUserAccessRank, $secondary); + PrivilegesHelper::confirmWithException(Privilege::ChangeUserAccessRank, PrivilegesHelper::getIdentitySubPrivilege($user)); $suppliedAccessRank = Model_User::validateAccessRank($suppliedAccessRank); $user->access_rank = $suppliedAccessRank; $edited = true; @@ -312,8 +308,7 @@ class UserController if ($page === null) $page = 1; - $secondary = $user->id == $this->context->user->id ? 'own' : 'all'; - PrivilegesHelper::confirmWithException($this->context->user, Privilege::ViewUser, $secondary); + PrivilegesHelper::confirmWithException(Privilege::ViewUser, PrivilegesHelper::getIdentitySubPrivilege($user)); $this->context->stylesheets []= 'user-view.css'; $this->context->stylesheets []= 'post-list.css'; $this->context->stylesheets []= 'paginator.css'; @@ -329,7 +324,7 @@ class UserController /* safety */ $allowedSafety = array_filter(PostSafety::getAll(), function($safety) { - return PrivilegesHelper::confirm($this->context->user, Privilege::ListPosts, PostSafety::toString($safety)) and + return PrivilegesHelper::confirm(Privilege::ListPosts, PostSafety::toString($safety)) and $this->context->user->hasEnabledSafety($safety); }); $dbQuery->where('safety IN (' . R::genSlots($allowedSafety) . ')'); @@ -338,7 +333,7 @@ class UserController /* hidden */ - if (!PrivilegesHelper::confirm($this->context->user, Privilege::ListPosts, 'hidden')) + if (!PrivilegesHelper::confirm(Privilege::ListPosts, 'hidden')) $dbQuery->andNot('hidden'); diff --git a/src/Helpers/PrivilegesHelper.php b/src/Helpers/PrivilegesHelper.php index c80e1174..5c1a9e1e 100644 --- a/src/Helpers/PrivilegesHelper.php +++ b/src/Helpers/PrivilegesHelper.php @@ -10,18 +10,19 @@ class PrivilegesHelper { if (strpos($key, '.') === false) $key .= '.'; - list ($privilegeName, $flag) = explode('.', $key); + list ($privilegeName, $subPrivilegeName) = explode('.', $key); $privilegeName = TextHelper::camelCaseToKebabCase($privilegeName); - $flag = TextHelper::camelCaseToKebabCase($flag); - $key = rtrim($privilegeName . '.' . $flag, '.'); + $subPrivilegeName = TextHelper::camelCaseToKebabCase($subPrivilegeName); + $key = rtrim($privilegeName . '.' . $subPrivilegeName, '.'); $minAccessRank = TextHelper::resolveConstant($minAccessRankName, 'AccessRank'); self::$privileges[$key] = $minAccessRank; } } - public static function confirm($user, $privilege, $flag = null) + public static function confirm($privilege, $subPrivilege = null) { + $user = \Chibi\Registry::getContext()->user; $minAccessRank = AccessRank::Admin; $key = TextHelper::camelCaseToKebabCase(Privilege::toString($privilege)); @@ -29,9 +30,9 @@ class PrivilegesHelper { $minAccessRank = self::$privileges[$key]; } - if ($flag != null) + if ($subPrivilege != null) { - $key2 = $key . '.' . strtolower($flag); + $key2 = $key . '.' . strtolower($subPrivilege); if (isset(self::$privileges[$key2])) { $minAccessRank = self::$privileges[$key2]; @@ -41,14 +42,20 @@ class PrivilegesHelper return intval($user->access_rank) >= $minAccessRank; } - public static function confirmWithException($user, $privilege, $flag = null) + public static function confirmWithException($privilege, $subPrivilege = null) { - if (!self::confirm($user, $privilege, $flag)) + if (!self::confirm($privilege, $subPrivilege)) { throw new SimpleException('Insufficient privileges'); } } + public static function getIdentitySubPrivilege($user) + { + $userFromContext = \Chibi\Registry::getContext()->user; + return $user->id == $userFromContext->id ? 'own' : 'all'; + } + public static function confirmEmail($user) { if (!$user->email_confirmed) diff --git a/src/Views/comment-small.phtml b/src/Views/comment-small.phtml index 935fb1e2..18b9d77c 100644 --- a/src/Views/comment-small.phtml +++ b/src/Views/comment-small.phtml @@ -25,8 +25,7 @@ context->comment->comment_date) ?> - context->comment->commenter->id == $this->context->user->id ? 'own' : 'all' ?> - context->user, Privilege::DeleteComment, $secondary)): ?> + context->comment->commenter))): ?> delete diff --git a/src/Views/layout-normal.phtml b/src/Views/layout-normal.phtml index abb1fc75..d971c52f 100644 --- a/src/Views/layout-normal.phtml +++ b/src/Views/layout-normal.phtml @@ -23,22 +23,22 @@ $nav = []; $nav []= ['Home', \Chibi\UrlHelper::route('index', 'index')]; - if (PrivilegesHelper::confirm($this->context->user, Privilege::ListPosts)) + if (PrivilegesHelper::confirm(Privilege::ListPosts)) $nav []= ['Browse', \Chibi\UrlHelper::route('post', 'list')]; - if (PrivilegesHelper::confirm($this->context->user, Privilege::ListPosts)) + if (PrivilegesHelper::confirm(Privilege::ListPosts)) $nav []= ['Favorites', \Chibi\UrlHelper::route('post', 'favorites')]; - if (PrivilegesHelper::confirm($this->context->user, Privilege::UploadPost)) + if (PrivilegesHelper::confirm(Privilege::UploadPost)) $nav []= ['Upload', \Chibi\UrlHelper::route('post', 'upload')]; - if (PrivilegesHelper::confirm($this->context->user, Privilege::ListComments)) + if (PrivilegesHelper::confirm(Privilege::ListComments)) $nav []= ['Comments', \Chibi\UrlHelper::route('comment', 'list')]; - if (PrivilegesHelper::confirm($this->context->user, Privilege::ListTags)) + if (PrivilegesHelper::confirm(Privilege::ListTags)) $nav []= ['Tags', \Chibi\UrlHelper::route('tag', 'list')]; - if (PrivilegesHelper::confirm($this->context->user, Privilege::ListUsers)) + if (PrivilegesHelper::confirm(Privilege::ListUsers)) $nav []= ['Users', \Chibi\UrlHelper::route('user', 'list')]; if (!$this->context->loggedIn) @@ -67,7 +67,7 @@