Skip to content

Commit

Permalink
Handle protected forums better
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Friedman <[email protected]>
  • Loading branch information
iMattPro committed Jun 27, 2024
1 parent 370dfe0 commit 58f54e0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
1 change: 1 addition & 0 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ services:
class: phpbb\boardannouncements\event\listener
arguments:
- '@phpbb.boardannouncements.manager'
- '@auth'
- '@config'
- '@controller.helper'
- '@language'
Expand Down
38 changes: 24 additions & 14 deletions event/listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace phpbb\boardannouncements\event;

use phpbb\auth\auth;
use phpbb\boardannouncements\ext;
use phpbb\boardannouncements\manager\manager;
use phpbb\config\config;
Expand All @@ -28,6 +29,9 @@ class listener implements EventSubscriberInterface
/** @var manager $manager */
protected $manager;

/** @var auth $auth */
protected $auth;

/** @var config $config */
protected $config;

Expand All @@ -52,7 +56,8 @@ class listener implements EventSubscriberInterface
/**
* Constructor
*
* @param manager $manager
* @param manager $manager Board announcements manager object
* @param auth $auth Auth object
* @param config $config Config object
* @param helper $controller_helper Controller helper object
* @param language $language Language object
Expand All @@ -62,9 +67,10 @@ class listener implements EventSubscriberInterface
* @param string $php_ext PHP extension
* @access public
*/
public function __construct(manager $manager, config $config, helper $controller_helper, language $language, request $request, template $template, user $user, $php_ext)
public function __construct(manager $manager, auth $auth, config $config, helper $controller_helper, language $language, request $request, template $template, user $user, $php_ext)
{
$this->manager = $manager;
$this->auth = $auth;
$this->config = $config;
$this->controller_helper = $controller_helper;
$this->language = $language;
Expand Down Expand Up @@ -107,33 +113,37 @@ public function display_board_announcements()

$is_index = $this->user->page['page_name'] === "index.$this->php_ext";
$current_page = $is_index ? ext::INDEX_ONLY : $this->request->variable('f', 0);
$protected_forums = $this->user->get_passworded_forums();

$board_announcements_data = $this->manager->get_visible_announcements($this->user->data['user_id']);

$board_announcements_data = array_filter($board_announcements_data, function ($data) use ($current_page) {
foreach ($board_announcements_data as $data)
{
$locations = $this->manager->decode_json($data['announcement_locations']);

// Check if announcement has locations specified, and user is at that location
if (!empty($locations) && !in_array($current_page, $locations))
if (!empty($locations))
{
return false;
$invalid_location = !in_array($current_page, $locations);
$is_protected = $current_page > 0 && !empty($protected_forums) && in_array($current_page, $protected_forums);
$no_auth = $current_page > 0 && !$this->auth->acl_get('f_read', $current_page);

// Do not include announcement if user is in a location where it shouldn't be visible
if ($invalid_location || $is_protected || $no_auth)
{
continue;
}
}

// Check if announcement has been dismissed
$cookie_name = $this->config['cookie_name'] . '_ba_' . $data['announcement_id'];
$announcement_dismissed = $this->request->variable($cookie_name, '', true, \phpbb\request\request_interface::COOKIE) == $data['announcement_timestamp'];

// Do not include announcement if it has been dismissed
if ($announcement_dismissed)
{
return false;
continue;
}

return true;
});

// Output board announcement to the template
foreach ($board_announcements_data as $data)
{
// Output board announcement to the template
$this->template->assign_block_vars('board_announcements', [
'BOARD_ANNOUNCEMENT_ID' => $data['announcement_id'],
'S_BOARD_ANNOUNCEMENT_DISMISS' => (bool) $data['announcement_dismissable'],
Expand Down
4 changes: 4 additions & 0 deletions tests/event/listener_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ protected static function setup_extensions()
/** @var \phpbb\boardannouncements\event\listener */
protected $listener;

/** @var \phpbb_mock_notifications_auth */
protected $auth;

/** @var \phpbb\config\config */
protected $config;

Expand Down Expand Up @@ -128,6 +131,7 @@ protected function set_listener()
{
$this->listener = new \phpbb\boardannouncements\event\listener(
$this->manager,
$this->auth,
$this->config,
$this->controller_helper,
$this->language,
Expand Down

0 comments on commit 58f54e0

Please sign in to comment.