Skip to content

Commit

Permalink
Merge pull request #177 from iMattPro/admin-perms
Browse files Browse the repository at this point in the history
Add ACP Permissions
  • Loading branch information
iMattPro committed Jun 18, 2022
2 parents 0344a28 + 959d18d commit 781ce8c
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 7 deletions.
4 changes: 2 additions & 2 deletions acp/main_info.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public function module()
'modes' => array(
'manage' => array(
'title' => 'ACP_MANAGE_ADS_TITLE',
'auth' => 'ext_phpbb/ads && acl_a_board',
'auth' => 'ext_phpbb/ads && acl_a_phpbb_ads_m',
'cat' => array('ACP_PHPBB_ADS_TITLE')
),
'settings' => array(
'title' => 'ACP_ADS_SETTINGS_TITLE',
'auth' => 'ext_phpbb/ads && acl_a_board',
'auth' => 'ext_phpbb/ads && acl_a_phpbb_ads_s',
'cat' => array('ACP_PHPBB_ADS_TITLE')
),
),
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"extra": {
"display-name": "Advertisement Management",
"soft-require": {
"phpbb/phpbb": ">=3.2.1"
"phpbb/phpbb": ">=3.3.2"
},
"version-check": {
"host": "www.phpbb.com",
Expand Down
2 changes: 2 additions & 0 deletions event/main_listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ public function __construct(\phpbb\template\template $template, \phpbb\template\
public function set_permissions($event)
{
$event->update_subarray('permissions', 'u_phpbb_ads', ['lang' => 'ACL_U_PHPBB_ADS', 'cat' => 'misc']);
$event->update_subarray('permissions', 'a_phpbb_ads_m', ['lang' => 'ACL_A_PHPBB_ADS_M', 'cat' => 'misc']);
$event->update_subarray('permissions', 'a_phpbb_ads_s', ['lang' => 'ACL_A_PHPBB_ADS_S', 'cat' => 'misc']);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions ext.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class ext extends \phpbb\extension\base
/**
* {@inheritdoc}
*
* Require phpBB 3.2.1 due to use of $event->update_subarray()
* Requires phpBB 3.3.2 due to using role_exists check in permission migration.
*/
public function is_enableable()
{
return phpbb_version_compare(PHPBB_VERSION, '3.2.1', '>=');
return phpbb_version_compare(PHPBB_VERSION, '3.3.2', '>=');
}
}
2 changes: 2 additions & 0 deletions language/en/permissions_ads.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@

$lang = array_merge($lang, array(
'ACL_U_PHPBB_ADS' => 'Can view own advertisement management statistics',
'ACL_A_PHPBB_ADS_M' => 'Can manage phpBB Advertisement ads',
'ACL_A_PHPBB_ADS_S' => 'Can manage phpBB Advertisement settings',
));
92 changes: 92 additions & 0 deletions migrations/v20x/m4_admin_permission.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
*
* Advertisement management. An extension for the phpBB Forum Software package.
*
* @copyright (c) 2022 phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
*/

namespace phpbb\ads\migrations\v20x;

/**
* Migration stage 4: Add Admin Permission
*/
class m4_admin_permission extends \phpbb\db\migration\container_aware_migration
{
/**
* {@inheritdoc
*/
public function effectively_installed()
{
$sql = 'SELECT * FROM ' . $this->table_prefix . "acl_options
WHERE auth_option = 'a_phpbb_ads_m' OR auth_option = 'a_phpbb_ads_s'";
$result = $this->db->sql_query_limit($sql, 1);
$row = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);

return $row !== false;
}

/**
* {@inheritDoc}
*/
public static function depends_on()
{
return [
'\phpbb\ads\migrations\v10x\m1_initial_schema',
'\phpbb\ads\migrations\v10x\m2_acp_module',
'\phpbb\ads\migrations\v20x\m3_add_start_date',
];
}

/**
* {@inheritDoc}
*/
public function update_data()
{
return [
// Add permission
['permission.add', ['a_phpbb_ads_m', true]],
['permission.add', ['a_phpbb_ads_s', true]],

// Set permissions
['if', [
['permission.role_exists', ['ROLE_ADMIN_FULL']],
['permission.permission_set', ['ROLE_ADMIN_FULL', 'a_phpbb_ads_m']],
]],
['if', [
['permission.role_exists', ['ROLE_ADMIN_FULL']],
['permission.permission_set', ['ROLE_ADMIN_FULL', 'a_phpbb_ads_s']],
]],
['if', [
['permission.role_exists', ['ROLE_ADMIN_STANDARD']],
['permission.permission_set', ['ROLE_ADMIN_STANDARD', 'a_phpbb_ads_m']],
]],
['if', [
['permission.role_exists', ['ROLE_ADMIN_STANDARD']],
['permission.permission_set', ['ROLE_ADMIN_STANDARD', 'a_phpbb_ads_s']],
]],

// Update module auth
['custom', [[$this, 'update_acp_module_auth']]],
];
}

/**
* Update module auth manually, because "module.remove" tool causes problems when deleting extension.
*/
public function update_acp_module_auth()
{
$sql = 'UPDATE ' . $this->container->getParameter('tables.modules') . "
SET module_auth = 'ext_phpbb/ads && acl_a_phpbb_ads_m'
WHERE module_langname = 'ACP_MANAGE_ADS_TITLE'";
$this->db->sql_query($sql);

$sql = 'UPDATE ' . $this->container->getParameter('tables.modules') . "
SET module_auth = 'ext_phpbb/ads && acl_a_phpbb_ads_s'
WHERE module_langname = 'ACP_ADS_SETTINGS_TITLE'";
$this->db->sql_query($sql);
}
}
4 changes: 2 additions & 2 deletions tests/acp/acp_module_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ public function test_module_info()
'modes' => array(
'manage' => array(
'title' => 'ACP_MANAGE_ADS_TITLE',
'auth' => 'ext_phpbb/ads && acl_a_board',
'auth' => 'ext_phpbb/ads && acl_a_phpbb_ads_m',
'cat' => array('ACP_PHPBB_ADS_TITLE')
),
'settings' => array(
'title' => 'ACP_ADS_SETTINGS_TITLE',
'auth' => 'ext_phpbb/ads && acl_a_board',
'auth' => 'ext_phpbb/ads && acl_a_phpbb_ads_s',
'cat' => array('ACP_PHPBB_ADS_TITLE')
),
),
Expand Down
2 changes: 2 additions & 0 deletions tests/event/set_permissions_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public function test_set_permissions()

self::assertEquals(array(
'u_phpbb_ads' => array('lang' => 'ACL_U_PHPBB_ADS', 'cat' => 'misc'),
'a_phpbb_ads_m' => array('lang' => 'ACL_A_PHPBB_ADS_M', 'cat' => 'misc'),
'a_phpbb_ads_s' => array('lang' => 'ACL_A_PHPBB_ADS_S', 'cat' => 'misc'),
), $permissions);
}
}
42 changes: 42 additions & 0 deletions tests/ext_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
*
* Advertisement management. An extension for the phpBB Forum Software package.
*
* @copyright (c) 2022 phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
*/

namespace phpbb\ads\tests;

class ext_test extends \phpbb_test_case
{
public function test_ext()
{
/** @var \PHPUnit\Framework\MockObject\MockObject|\Symfony\Component\DependencyInjection\ContainerInterface */
$container = $this->getMockBuilder('\Symfony\Component\DependencyInjection\ContainerInterface')
->disableOriginalConstructor()
->getMock();

/** @var \PHPUnit\Framework\MockObject\MockObject|\phpbb\finder */
$extension_finder = $this->getMockBuilder('\phpbb\finder')
->disableOriginalConstructor()
->getMock();

/** @var \PHPUnit\Framework\MockObject\MockObject|\phpbb\db\migrator */
$migrator = $this->getMockBuilder('\phpbb\db\migrator')
->disableOriginalConstructor()
->getMock();

$ext = new \phpbb\ads\ext(
$container,
$extension_finder,
$migrator,
'phpbb/ads',
''
);

self::assertTrue($ext->is_enableable());
}
}

0 comments on commit 781ce8c

Please sign in to comment.