Skip to content

Commit

Permalink
Add permission to access password protected nodes, small refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
West14 committed Jan 25, 2021
1 parent 3703548 commit 23c030e
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 30 deletions.
4 changes: 3 additions & 1 deletion Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Listener
{
public static function forumEntityStructure(\XF\Mvc\Entity\Manager $em, \XF\Mvc\Entity\Structure &$structure)
{
$structure->columns['wfp_password'] = ['type' => Entity::STR, 'maxLength' => 64, 'nullable' => true, 'default' => null];
$structure->columns['wfp_password'] = [
'type' => Entity::STR, 'maxLength' => 64, 'nullable' => true, 'default' => null
];
}
}
5 changes: 4 additions & 1 deletion XF/Entity/Forum.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ public function isPasswordAccessGranted()
$nodeId = $this->node_id;

if (\XF::visitor()->hasNodePermission($nodeId, 'wfpBypass'))
{
return true;
}

$data = \XF::session()->get('wfp_data');

if ($data && isset($data[$nodeId]))
{
if ($this->wfp_password == $data[$nodeId])
{
return true;
}
}
return false;
}
Expand Down
16 changes: 3 additions & 13 deletions XF/Pub/Controller/Forum.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class Forum extends XFCP_Forum
{
use ForumPasswordTrait;

/**
* @param ParameterBag $params
* @return \XF\Mvc\Reply\Redirect|\XF\Mvc\Reply\View
Expand Down Expand Up @@ -51,19 +53,7 @@ protected function assertViewableForum($nodeIdOrName, array $extraWith = [])
/** @var \West\ForumPassword\XF\Entity\Forum $parent */
$parent = parent::assertViewableForum($nodeIdOrName, $extraWith);

if ($parent->wfp_password)
{
if (!$parent->isPasswordAccessGranted())
{
throw $this->exception(
$this->rerouteController(
'XF:Forum',
'Password',
$parent->toArray()
)
);
}
}
$this->checkForumPasswordAccess($parent);

return $parent;
}
Expand Down
38 changes: 38 additions & 0 deletions XF/Pub/Controller/ForumPasswordTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Created by PhpStorm.
* User: Andriy
* Date: 25.01.2021
* Time: 13:55
* Made with <3 by West from TechGate Studio
*/

namespace West\ForumPassword\XF\Pub\Controller;


trait ForumPasswordTrait
{
protected function checkForumPasswordAccess(\West\ForumPassword\XF\Entity\Forum $forum)
{
if ($forum->wfp_password)
{
if (\XF::visitor()->hasPermission('general', 'wfpAccessProtectedNodes'))
{
if (!$forum->isPasswordAccessGranted())
{
throw $this->exception(
$this->rerouteController(
'XF:Forum',
'Password',
$forum->toArray()
)
);
}
}
else
{
throw $this->exception($this->noPermission());
}
}
}
}
17 changes: 4 additions & 13 deletions XF/Pub/Controller/Thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,15 @@

class Thread extends XFCP_Thread
{
use ForumPasswordTrait;

protected function assertViewableThread($threadId, array $extraWith = [])
{
$parent = parent::assertViewableThread($threadId, $extraWith);

/** @var \West\ForumPassword\XF\Entity\Forum $forum */
$forum = $parent->Forum;
if ($forum->wfp_password)
{
if (!$forum->isPasswordAccessGranted())
{
throw $this->exception(
$this->rerouteController(
'XF:Forum',
'Password',
$forum->toArray()
)
);
}
}
$this->checkForumPasswordAccess($forum);

return $parent;
}
Expand Down
3 changes: 3 additions & 0 deletions _output/permissions/_metadata.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"forum-wfpBypass.json": {
"hash": "a9accb917b5ccdee70cd3edca0a0499e"
},
"general-wfpAccessProtectedNodes.json": {
"hash": "1732141476333360460c0d3c87d0711f"
}
}
6 changes: 6 additions & 0 deletions _output/permissions/general-wfpAccessProtectedNodes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"permission_type": "flag",
"interface_group_id": "generalPermissions",
"display_order": 100600,
"depend_permission_id": ""
}
6 changes: 6 additions & 0 deletions _output/phrases/_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
"version_string": "1.0.0",
"hash": "da0146061bd27a8ec89d2ff32ef92654"
},
"permission.general_wfpAccessProtectedNodes.txt": {
"global_cache": false,
"version_id": 1000070,
"version_string": "1.0.0",
"hash": "37643a4b9b1cbd4127570885db0b7ca0"
},
"wfp_password_required.txt": {
"global_cache": true,
"version_id": 1000010,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[West: Forum Password] Access password protected nodes
4 changes: 2 additions & 2 deletions addon.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"legacy_addon_id": "",
"title": "[West] Forum Password",
"description": "Adds functionality which allows to protect the forum with a password.",
"version_id": 1000070,
"version_string": "1.0.0",
"version_id": 1000170,
"version_string": "1.0.1",
"dev": "West",
"dev_url": "https://t.me/West99",
"faq_url": "",
Expand Down

0 comments on commit 23c030e

Please sign in to comment.