1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-07-31 22:10:45 +02:00

[ticket/16527] Add a role_exists method to permissions migrator tool

PHPBB3-16527
This commit is contained in:
Matt Friedman
2020-06-14 11:02:40 -07:00
parent 8bb9a9803b
commit b69507be8a
2 changed files with 48 additions and 35 deletions

View File

@@ -240,6 +240,25 @@ class permission implements \phpbb\db\migration\tool\tool_interface
$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
*
@@ -251,13 +270,7 @@ class permission implements \phpbb\db\migration\tool\tool_interface
*/
public function role_add($role_name, $role_type, $role_description = '')
{
$sql = 'SELECT role_id
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)
if ($this->role_exists($role_name))
{
return;
}
@@ -290,13 +303,7 @@ class permission implements \phpbb\db\migration\tool\tool_interface
*/
public function role_update($old_role_name, $new_role_name)
{
$sql = 'SELECT role_id
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)
if (!$this->role_exists($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)
{
$sql = 'SELECT role_id
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)
if (!($role_id = $this->role_exists($role_name)))
{
return;
}
@@ -381,13 +382,7 @@ class permission implements \phpbb\db\migration\tool\tool_interface
switch ($type)
{
case 'role':
$sql = 'SELECT role_id
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)
if (!($role_id = $this->role_exists($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)
{
case 'role':
$sql = 'SELECT role_id
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)
if (!($role_id = $this->role_exists($name)))
{
throw new \phpbb\db\migration\exception('ROLE_NOT_EXIST', $name);
}