1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-07-30 21:40:43 +02:00

Merge branch '3.3.x'

This commit is contained in:
Marc Alexander
2021-12-03 21:08:15 +01:00
4 changed files with 359 additions and 12 deletions

View File

@@ -0,0 +1,74 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\db\migration\data\v33x;
class remove_orphaned_roles extends \phpbb\db\migration\migration
{
static public function depends_on()
{
return ['\phpbb\db\migration\data\v33x\v335'];
}
public function update_data()
{
return [
['custom', [[$this, 'acl_remove_orphaned_roles']]],
];
}
public function acl_remove_orphaned_roles()
{
$role_ids = [];
$sql = 'SELECT auth_role_id
FROM ' . ACL_GROUPS_TABLE . '
WHERE auth_role_id <> 0
AND forum_id = 0';
$result = $this->db->sql_query($sql);
$auth_role_ids = array_keys($this->db->sql_fetchrowset($result));
$this->db->sql_freeresult($result);
if (count($auth_role_ids))
{
$sql = 'SELECT role_id
FROM ' . ACL_ROLES_TABLE . '
WHERE ' . $this->db->sql_in_set('role_id', $auth_role_ids);
$result = $this->db->sql_query($sql);
$role_ids = array_keys($this->db->sql_fetchrowset($result));
$this->db->sql_freeresult($result);
}
$non_existent_role_ids = array_diff($auth_role_ids, $role_ids);
// Nothing to do, there are no non-existent roles assigned to groups
if (empty($non_existent_role_ids))
{
return true;
}
// Remove assigned non-existent roles from users and groups
$sql = 'DELETE FROM ' . ACL_USERS_TABLE . '
WHERE ' . $this->db->sql_in_set('auth_role_id', $non_existent_role_ids);
$this->db->sql_query($sql);
$sql = 'DELETE FROM ' . ACL_GROUPS_TABLE . '
WHERE ' . $this->db->sql_in_set('auth_role_id', $non_existent_role_ids);
$this->db->sql_query($sql);
$auth = new \phpbb\auth\auth();
$auth->acl_clear_prefetch();
return true;
}
}

View File

@@ -21,6 +21,9 @@ class permission implements \phpbb\db\migration\tool\tool_interface
/** @var \phpbb\auth\auth */
protected $auth;
/** @var \includes\acp\auth\auth_admin */
protected $auth_admin;
/** @var \phpbb\cache\service */
protected $cache;
@@ -49,6 +52,12 @@ class permission implements \phpbb\db\migration\tool\tool_interface
$this->auth = $auth;
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext;
if (!class_exists('auth_admin'))
{
include($this->phpbb_root_path . 'includes/acp/auth.' . $this->php_ext);
}
$this->auth_admin = new \auth_admin();
}
/**
@@ -115,12 +124,6 @@ class permission implements \phpbb\db\migration\tool\tool_interface
return;
}
if (!class_exists('auth_admin'))
{
include($this->phpbb_root_path . 'includes/acp/auth.' . $this->php_ext);
}
$auth_admin = new \auth_admin();
// We have to add a check to see if the !$global (if global, local, and if local, global) permission already exists. If it does, acl_add_option currently has a bug which would break the ACL system, so we are having a work-around here.
if ($this->exists($auth_option, !$global))
{
@@ -137,19 +140,19 @@ class permission implements \phpbb\db\migration\tool\tool_interface
{
if ($global)
{
$auth_admin->acl_add_option(array('global' => array($auth_option)));
$this->auth_admin->acl_add_option(array('global' => array($auth_option)));
}
else
{
$auth_admin->acl_add_option(array('local' => array($auth_option)));
$this->auth_admin->acl_add_option(array('local' => array($auth_option)));
}
}
// The permission has been added, now we can copy it if needed
if ($copy_from && isset($auth_admin->acl_options['id'][$copy_from]))
if ($copy_from && isset($this->auth_admin->acl_options['id'][$copy_from]))
{
$old_id = $auth_admin->acl_options['id'][$copy_from];
$new_id = $auth_admin->acl_options['id'][$auth_option];
$old_id = $this->auth_admin->acl_options['id'][$copy_from];
$new_id = $this->auth_admin->acl_options['id'][$auth_option];
$tables = array(ACL_GROUPS_TABLE, ACL_ROLES_DATA_TABLE, ACL_USERS_TABLE);
@@ -174,7 +177,7 @@ class permission implements \phpbb\db\migration\tool\tool_interface
}
}
$auth_admin->acl_clear_prefetch();
$this->auth_admin->acl_clear_prefetch();
}
}
@@ -288,6 +291,8 @@ class permission implements \phpbb\db\migration\tool\tool_interface
$sql = 'INSERT INTO ' . ACL_ROLES_TABLE . ' ' . $this->db->sql_build_array('INSERT', $sql_ary);
$this->db->sql_query($sql);
return $this->db->sql_nextid();
}
/**
@@ -324,6 +329,66 @@ class permission implements \phpbb\db\migration\tool\tool_interface
return;
}
// Get the role type
$sql = 'SELECT role_type
FROM ' . ACL_ROLES_TABLE . '
WHERE role_id = ' . (int) $role_id;
$result = $this->db->sql_query($sql);
$role_type = $this->db->sql_fetchfield('role_type');
$this->db->sql_freeresult($result);
// Get complete auth array
$sql = 'SELECT auth_option, auth_option_id
FROM ' . ACL_OPTIONS_TABLE . "
WHERE auth_option " . $this->db->sql_like_expression($role_type . $this->db->get_any_char());
$result = $this->db->sql_query($sql);
$auth_settings = [];
while ($row = $this->db->sql_fetchrow($result))
{
$auth_settings[$row['auth_option']] = ACL_NO;
}
$this->db->sql_freeresult($result);
// Get the role auth settings we need to re-set...
$sql = 'SELECT o.auth_option, r.auth_setting
FROM ' . ACL_ROLES_DATA_TABLE . ' r, ' . ACL_OPTIONS_TABLE . ' o
WHERE o.auth_option_id = r.auth_option_id
AND r.role_id = ' . (int) $role_id;
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
$auth_settings[$row['auth_option']] = $row['auth_setting'];
}
$this->db->sql_freeresult($result);
// Get role assignments
$hold_ary = $this->auth_admin->get_role_mask($role_id);
// Re-assign permissions
foreach ($hold_ary as $forum_id => $forum_ary)
{
if (isset($forum_ary['users']))
{
$this->auth_admin->acl_set('user', $forum_id, $forum_ary['users'], $auth_settings, 0, false);
}
if (isset($forum_ary['groups']))
{
$this->auth_admin->acl_set('group', $forum_id, $forum_ary['groups'], $auth_settings, 0, false);
}
}
// Remove role from users and groups just to be sure (happens through acl_set)
$sql = 'DELETE FROM ' . ACL_USERS_TABLE . '
WHERE auth_role_id = ' . $role_id;
$this->db->sql_query($sql);
$sql = 'DELETE FROM ' . ACL_GROUPS_TABLE . '
WHERE auth_role_id = ' . $role_id;
$this->db->sql_query($sql);
$sql = 'DELETE FROM ' . ACL_ROLES_DATA_TABLE . '
WHERE role_id = ' . $role_id;
$this->db->sql_query($sql);
@@ -422,6 +487,11 @@ class permission implements \phpbb\db\migration\tool\tool_interface
WHERE role_id = ' . $role_id;
$this->db->sql_query($sql);
$role_data = $this->db->sql_fetchrow();
if (!$role_data)
{
throw new \phpbb\db\migration\exception('ROLE_ASSIGNED_NOT_EXIST', $name, $role_id);
}
$role_name = $role_data['role_name'];
$role_type = $role_data['role_type'];
@@ -568,6 +638,10 @@ class permission implements \phpbb\db\migration\tool\tool_interface
WHERE role_id = ' . $role_id;
$this->db->sql_query($sql);
$role_name = $this->db->sql_fetchfield('role_name');
if (!$role_name)
{
throw new \phpbb\db\migration\exception('ROLE_ASSIGNED_NOT_EXIST', $name, $role_id);
}
return $this->permission_unset($role_name, $auth_option, 'role');
}