[ticket/291] Add modules_helper and use for permission checks

B3P-291
This commit is contained in:
Marc Alexander
2014-07-07 15:16:29 +02:00
parent 0ae979e34a
commit 40f630a55d
10 changed files with 125 additions and 115 deletions

View File

@@ -8,6 +8,7 @@ services:
- @template - @template
- @dbal.conn - @dbal.conn
- @pagination - @pagination
- @board3.portal.modules_helper
- @request - @request
- %core.php_ext% - %core.php_ext%
- %core.root_path% - %core.root_path%
@@ -179,6 +180,7 @@ services:
- @config - @config
- @dbal.conn - @dbal.conn
- @pagination - @pagination
- @board3.portal.modules_helper
- @request - @request
- @template - @template
- %core.root_path% - %core.root_path%

View File

@@ -49,5 +49,9 @@ services:
board3.portal.helper: board3.portal.helper:
class: board3\portal\includes\helper class: board3\portal\includes\helper
arguments: arguments:
- @auth
- @board3.module_collection - @board3.module_collection
board3.portal.modules_helper:
class: board3\portal\includes\modules_helper
arguments:
- @auth

View File

@@ -93,7 +93,7 @@ function obtain_portal_modules()
// fetch post for news & announce // fetch post for news & announce
function phpbb_fetch_posts($module_id, $forum_from, $permissions, $number_of_posts, $text_length, $time, $type, $start = 0, $invert = false) function phpbb_fetch_posts($module_id, $forum_from, $permissions, $number_of_posts, $text_length, $time, $type, $start = 0, $invert = false)
{ {
global $db, $phpbb_root_path, $auth, $user, $bbcode_bitfield, $bbcode, $portal_config, $config, $cache; global $db, $phpbb_root_path, $auth, $user, $bbcode_bitfield, $bbcode, $portal_config, $config, $cache, $phpbb_container;
$posts = $update_count = array(); $posts = $update_count = array();
$post_time = ($time == 0) ? '' : 'AND t.topic_time > ' . (time() - $time * 86400); $post_time = ($time == 0) ? '' : 'AND t.topic_time > ' . (time() - $time * 86400);
@@ -101,15 +101,10 @@ function phpbb_fetch_posts($module_id, $forum_from, $permissions, $number_of_pos
$str_where = ''; $str_where = '';
$topic_icons = array(0); $topic_icons = array(0);
$have_icons = 0; $have_icons = 0;
$modules_helper = $phpbb_container->get('board3.portal.modules_helper');
if ($permissions == true) // Get disallowed forums
{ $disallow_access = $modules_helper->get_disallowed_forums($permissions);
$disallow_access = array_unique(array_keys($auth->acl_getf('!f_read', true)));
}
else
{
$disallow_access = array();
}
if ($invert == true) if ($invert == true)
{ {

View File

@@ -11,12 +11,6 @@ namespace board3\portal\includes;
class helper class helper
{ {
/**
* Auth object
* @var \phpbb\auth\auth
*/
private $auth;
/** /**
* Board3 Modules service collection * Board3 Modules service collection
* @var \phpbb\di\service_collection * @var \phpbb\di\service_collection
@@ -27,13 +21,11 @@ class helper
* Constructor * Constructor
* NOTE: The parameters of this method must match in order and type with * NOTE: The parameters of this method must match in order and type with
* the dependencies defined in the services.yml file for this service. * the dependencies defined in the services.yml file for this service.
* @param \phpbb\auth\auth $auth Auth object
* @param \phpbb\di\service_collection $modules Board3 Modules service * @param \phpbb\di\service_collection $modules Board3 Modules service
* collection * collection
*/ */
public function __construct($auth, $modules) public function __construct($modules)
{ {
$this->auth = $auth;
$this->register_modules($modules); $this->register_modules($modules);
} }
@@ -72,24 +64,4 @@ class helper
return false; return false;
} }
} }
/**
* Get an array of disallowed forums
*
* @param bool $disallow_access Whether the array for disallowing access
* should be filled
*/
public function get_disallowed_forums($disallow_access)
{
if ($disallow_access == true)
{
$disallow_access = array_unique(array_keys($this->auth->acl_getf('!f_read', true)));
}
else
{
$disallow_access = array();
}
return $disallow_access;
}
} }

View File

@@ -0,0 +1,50 @@
<?php
/**
*
* @package Board3 Portal v2.1
* @copyright (c) 2014 Board3 Group ( www.board3.de )
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
namespace board3\portal\includes;
class modules_helper
{
/**
* Auth object
* @var \phpbb\auth\auth
*/
protected $auth;
/**
* Constructor
* NOTE: The parameters of this method must match in order and type with
* the dependencies defined in the services.yml file for this service.
* @param \phpbb\auth\auth $auth Auth object
*/
public function __construct($auth)
{
$this->auth = $auth;
}
/**
* Get an array of disallowed forums
*
* @param bool $disallow_access Whether the array for disallowing access
* should be filled
*/
public function get_disallowed_forums($disallow_access)
{
if ($disallow_access == true)
{
$disallow_access = array_unique(array_keys($this->auth->acl_getf('!f_read', true)));
}
else
{
$disallow_access = array();
}
return $disallow_access;
}
}

View File

@@ -59,6 +59,9 @@ class announcements extends module_base
/** @var \phpbb\pagination */ /** @var \phpbb\pagination */
protected $pagination; protected $pagination;
/** @var \board3\portal\includes\modules_helper */
protected $modules_helper;
/** @var \phpbb\request\request */ /** @var \phpbb\request\request */
protected $request; protected $request;
@@ -80,12 +83,13 @@ class announcements extends module_base
* @param \phpbb\template $template phpBB template * @param \phpbb\template $template phpBB template
* @param \phpbb\db\driver $db Database driver * @param \phpbb\db\driver $db Database driver
* @param \phpbb\pagination $pagination phpBB pagination * @param \phpbb\pagination $pagination phpBB pagination
* @param \board3\portal\includes\modules_helper $modules_helper Portal modules helper
* @param \phpbb\request\request $request phpBB request * @param \phpbb\request\request $request phpBB request
* @param string $phpEx php file extension * @param string $phpEx php file extension
* @param string $phpbb_root_path phpBB root path * @param string $phpbb_root_path phpBB root path
* @param \phpbb\user $user phpBB user object * @param \phpbb\user $user phpBB user object
*/ */
public function __construct($auth, $cache, $config, $template, $db, $pagination, $request, $phpEx, $phpbb_root_path, $user) public function __construct($auth, $cache, $config, $template, $db, $pagination, $modules_helper, $request, $phpEx, $phpbb_root_path, $user)
{ {
$this->auth = $auth; $this->auth = $auth;
$this->cache = $cache; $this->cache = $cache;
@@ -93,6 +97,7 @@ class announcements extends module_base
$this->template = $template; $this->template = $template;
$this->db = $db; $this->db = $db;
$this->pagination = $pagination; $this->pagination = $pagination;
$this->modules_helper = $modules_helper;
$this->request = $request; $this->request = $request;
$this->php_ext = $phpEx; $this->php_ext = $phpEx;
$this->phpbb_root_path = $phpbb_root_path; $this->phpbb_root_path = $phpbb_root_path;
@@ -137,14 +142,8 @@ class announcements extends module_base
$str_where = ''; $str_where = '';
if($permissions == true) // Get disallowed forums
{ $disallow_access = $this->modules_helper->get_disallowed_forums($permissions);
$disallow_access = array_unique(array_keys($this->auth->acl_getf('!f_read', true)));
}
else
{
$disallow_access = array();
}
if($this->config['board3_announcements_forum_exclude_' . $module_id] == true) if($this->config['board3_announcements_forum_exclude_' . $module_id] == true)
{ {

View File

@@ -56,6 +56,9 @@ class news extends module_base
/** @var \phpbb\pagination */ /** @var \phpbb\pagination */
protected $pagination; protected $pagination;
/** @var \board3\portal\includes\modules_helper */
protected $modules_helper;
/** @var \phpbb\request\request */ /** @var \phpbb\request\request */
protected $request; protected $request;
@@ -79,19 +82,21 @@ class news extends module_base
* @param \phpbb\config\config $config phpBB config * @param \phpbb\config\config $config phpBB config
* @param \phpbb\db\driver $db phpBB db driver * @param \phpbb\db\driver $db phpBB db driver
* @param \phpbb\pagination $pagination phpBB pagination * @param \phpbb\pagination $pagination phpBB pagination
* @param \board3\portal\includes\modules_helper $modules_helper Portal modules helper
* @param \phpbb\request\request $request phpBB request * @param \phpbb\request\request $request phpBB request
* @param \phpbb\template $template phpBB template * @param \phpbb\template $template phpBB template
* @param string $phpbb_root_path phpBB root path * @param string $phpbb_root_path phpBB root path
* @param string $phpEx php file extension * @param string $phpEx php file extension
* @param \phpbb\user $user phpBB user object * @param \phpbb\user $user phpBB user object
*/ */
public function __construct($auth, $cache, $config, $db, $pagination, $request, $template, $phpbb_root_path, $phpEx, $user) public function __construct($auth, $cache, $config, $db, $pagination, $modules_helper, $request, $template, $phpbb_root_path, $phpEx, $user)
{ {
$this->auth = $auth; $this->auth = $auth;
$this->cache = $cache; $this->cache = $cache;
$this->config = $config; $this->config = $config;
$this->db = $db; $this->db = $db;
$this->pagination = $pagination; $this->pagination = $pagination;
$this->modules_helper = $modules_helper;
$this->request = $request; $this->request = $request;
$this->template = $template; $this->template = $template;
$this->phpbb_root_path = $phpbb_root_path; $this->phpbb_root_path = $phpbb_root_path;
@@ -134,14 +139,8 @@ class news extends module_base
$str_where = ''; $str_where = '';
if($permissions == true) // Get disallowed forums
{ $disallow_access = $this->modules_helper->get_disallowed_forums($permissions);
$disallow_access = array_unique(array_keys($this->auth->acl_getf('!f_read', true)));
}
else
{
$disallow_access = array();
}
if($this->config['board3_news_exclude_' . $module_id] == true) if($this->config['board3_news_exclude_' . $module_id] == true)
{ {

View File

@@ -1,57 +0,0 @@
<?php
/**
*
* @package testing
* @copyright (c) Board3 Group ( www.board3.de )
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
class board3_includes_helper_database_test extends \board3\portal\tests\testframework\database_test_case
{
protected $portal_helper;
protected $modules;
public function getDataSet()
{
return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/auth.xml');
}
public function setUp()
{
parent::setUp();
$config = new \phpbb\config\config(array());
$this->modules = array(
'\board3\portal\modules\link_us' => new \board3\portal\modules\link_us($config, new \board3\portal\tests\mock\template($this), new \board3\portal\tests\mock\user),
);
$auth = new \phpbb\auth\auth();
$this->portal_helper = $this->get_portal_helper($auth, $this->modules);
}
protected function get_portal_helper($auth, $modules)
{
$this->portal_helper = new \board3\portal\includes\helper($auth, $modules);
return $this->portal_helper;
}
public function data_get_disallowed_forums()
{
return array(
array(array(), false),
array(array(0 => 1, 1 => 2), true),
);
}
/**
* @dataProvider data_get_disallowed_forums
*/
public function test_get_disallowed_forums($expected, $input)
{
$this->assertEquals($expected, $this->portal_helper->get_disallowed_forums($input));
}
}

View File

@@ -26,7 +26,7 @@ class board3_includes_helper_test extends \board3\portal\tests\testframework\tes
protected function get_portal_helper($modules) protected function get_portal_helper($modules)
{ {
$this->portal_helper = new \board3\portal\includes\helper(array(), $modules); $this->portal_helper = new \board3\portal\includes\helper($modules);
return $this->portal_helper; return $this->portal_helper;
} }

View File

@@ -0,0 +1,46 @@
<?php
/**
*
* @package testing
* @copyright (c) Board3 Group ( www.board3.de )
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
class board3_includes_modules_helper_test extends \board3\portal\tests\testframework\database_test_case
{
protected $modules_helper;
protected $modules;
public function getDataSet()
{
return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/auth.xml');
}
public function setUp()
{
parent::setUp();
$auth = new \phpbb\auth\auth();
$this->modules_helper = new \board3\portal\includes\modules_helper($auth);
}
public function data_get_disallowed_forums()
{
return array(
array(array(), false),
array(array(0 => 1, 1 => 2), true),
);
}
/**
* @dataProvider data_get_disallowed_forums
*/
public function test_get_disallowed_forums($expected, $input)
{
$this->assertEquals($expected, $this->modules_helper->get_disallowed_forums($input));
}
}