mirror of
https://github.com/phpbb/phpbb.git
synced 2025-05-30 11:11:23 +02:00
Merge pull request #6009 from iMattPro/ticket/16527
[ticket/16527] Add a role_exists method to permissions migrator tool
This commit is contained in:
commit
1c724d0860
@ -240,6 +240,25 @@ class permission implements \phpbb\db\migration\tool\tool_interface
|
|||||||
$this->auth->acl_clear_prefetch();
|
$this->auth->acl_clear_prefetch();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a permission role exists
|
||||||
|
*
|
||||||
|
* @param string $role_name The role name
|
||||||
|
*
|
||||||
|
* @return int The id of the role if it exists, 0 otherwise
|
||||||
|
*/
|
||||||
|
public function role_exists($role_name)
|
||||||
|
{
|
||||||
|
$sql = 'SELECT role_id
|
||||||
|
FROM ' . ACL_ROLES_TABLE . "
|
||||||
|
WHERE role_name = '" . $this->db->sql_escape($role_name) . "'";
|
||||||
|
$result = $this->db->sql_query($sql);
|
||||||
|
$role_id = (int) $this->db->sql_fetchfield('role_id');
|
||||||
|
$this->db->sql_freeresult($result);
|
||||||
|
|
||||||
|
return $role_id;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a new permission role
|
* Add a new permission role
|
||||||
*
|
*
|
||||||
@ -251,13 +270,7 @@ class permission implements \phpbb\db\migration\tool\tool_interface
|
|||||||
*/
|
*/
|
||||||
public function role_add($role_name, $role_type, $role_description = '')
|
public function role_add($role_name, $role_type, $role_description = '')
|
||||||
{
|
{
|
||||||
$sql = 'SELECT role_id
|
if ($this->role_exists($role_name))
|
||||||
FROM ' . ACL_ROLES_TABLE . "
|
|
||||||
WHERE role_name = '" . $this->db->sql_escape($role_name) . "'";
|
|
||||||
$this->db->sql_query($sql);
|
|
||||||
$role_id = (int) $this->db->sql_fetchfield('role_id');
|
|
||||||
|
|
||||||
if ($role_id)
|
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -290,13 +303,7 @@ class permission implements \phpbb\db\migration\tool\tool_interface
|
|||||||
*/
|
*/
|
||||||
public function role_update($old_role_name, $new_role_name)
|
public function role_update($old_role_name, $new_role_name)
|
||||||
{
|
{
|
||||||
$sql = 'SELECT role_id
|
if (!$this->role_exists($old_role_name))
|
||||||
FROM ' . ACL_ROLES_TABLE . "
|
|
||||||
WHERE role_name = '" . $this->db->sql_escape($old_role_name) . "'";
|
|
||||||
$this->db->sql_query($sql);
|
|
||||||
$role_id = (int) $this->db->sql_fetchfield('role_id');
|
|
||||||
|
|
||||||
if (!$role_id)
|
|
||||||
{
|
{
|
||||||
throw new \phpbb\db\migration\exception('ROLE_NOT_EXIST', $old_role_name);
|
throw new \phpbb\db\migration\exception('ROLE_NOT_EXIST', $old_role_name);
|
||||||
}
|
}
|
||||||
@ -315,13 +322,7 @@ class permission implements \phpbb\db\migration\tool\tool_interface
|
|||||||
*/
|
*/
|
||||||
public function role_remove($role_name)
|
public function role_remove($role_name)
|
||||||
{
|
{
|
||||||
$sql = 'SELECT role_id
|
if (!($role_id = $this->role_exists($role_name)))
|
||||||
FROM ' . ACL_ROLES_TABLE . "
|
|
||||||
WHERE role_name = '" . $this->db->sql_escape($role_name) . "'";
|
|
||||||
$this->db->sql_query($sql);
|
|
||||||
$role_id = (int) $this->db->sql_fetchfield('role_id');
|
|
||||||
|
|
||||||
if (!$role_id)
|
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -381,13 +382,7 @@ class permission implements \phpbb\db\migration\tool\tool_interface
|
|||||||
switch ($type)
|
switch ($type)
|
||||||
{
|
{
|
||||||
case 'role':
|
case 'role':
|
||||||
$sql = 'SELECT role_id
|
if (!($role_id = $this->role_exists($name)))
|
||||||
FROM ' . ACL_ROLES_TABLE . "
|
|
||||||
WHERE role_name = '" . $this->db->sql_escape($name) . "'";
|
|
||||||
$this->db->sql_query($sql);
|
|
||||||
$role_id = (int) $this->db->sql_fetchfield('role_id');
|
|
||||||
|
|
||||||
if (!$role_id)
|
|
||||||
{
|
{
|
||||||
throw new \phpbb\db\migration\exception('ROLE_NOT_EXIST', $name);
|
throw new \phpbb\db\migration\exception('ROLE_NOT_EXIST', $name);
|
||||||
}
|
}
|
||||||
@ -539,13 +534,7 @@ class permission implements \phpbb\db\migration\tool\tool_interface
|
|||||||
switch ($type)
|
switch ($type)
|
||||||
{
|
{
|
||||||
case 'role':
|
case 'role':
|
||||||
$sql = 'SELECT role_id
|
if (!($role_id = $this->role_exists($name)))
|
||||||
FROM ' . ACL_ROLES_TABLE . "
|
|
||||||
WHERE role_name = '" . $this->db->sql_escape($name) . "'";
|
|
||||||
$this->db->sql_query($sql);
|
|
||||||
$role_id = (int) $this->db->sql_fetchfield('role_id');
|
|
||||||
|
|
||||||
if (!$role_id)
|
|
||||||
{
|
{
|
||||||
throw new \phpbb\db\migration\exception('ROLE_NOT_EXIST', $name);
|
throw new \phpbb\db\migration\exception('ROLE_NOT_EXIST', $name);
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,12 @@
|
|||||||
|
|
||||||
class phpbb_dbal_migrator_tool_permission_test extends phpbb_database_test_case
|
class phpbb_dbal_migrator_tool_permission_test extends phpbb_database_test_case
|
||||||
{
|
{
|
||||||
|
/** @var \phpbb\auth\auth */
|
||||||
|
protected $auth;
|
||||||
|
|
||||||
|
/** @var \phpbb\db\migration\tool\permission */
|
||||||
|
protected $tool;
|
||||||
|
|
||||||
public $group_ids = array(
|
public $group_ids = array(
|
||||||
'REGISTERED' => 2,
|
'REGISTERED' => 2,
|
||||||
'GLOBAL_MODERATORS' => 4,
|
'GLOBAL_MODERATORS' => 4,
|
||||||
@ -218,4 +224,22 @@ class phpbb_dbal_migrator_tool_permission_test extends phpbb_database_test_case
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function data_test_permission_role_exists()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array('ROLE_MOD_FULL', true),
|
||||||
|
array('ROLE_USER_FULL', true),
|
||||||
|
array('ROLE_ADMIN_STANDARD', true),
|
||||||
|
array('ROLE_DOES_NOT_EXIST', false),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider data_test_permission_role_exists
|
||||||
|
*/
|
||||||
|
public function test_permission_role_exists($role_name, $expected)
|
||||||
|
{
|
||||||
|
$this->assertEquals($expected, $this->tool->role_exists($role_name));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user