Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
remove_orphaned_roles
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 3
56
0.00% covered (danger)
0.00%
0 / 1
 depends_on
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 update_data
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 acl_remove_orphaned_roles
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14namespace phpbb\db\migration\data\v33x;
15
16class remove_orphaned_roles extends \phpbb\db\migration\migration
17{
18    public static function depends_on()
19    {
20        return ['\phpbb\db\migration\data\v33x\v335'];
21    }
22
23    public function update_data()
24    {
25        return [
26            ['custom', [[$this, 'acl_remove_orphaned_roles']]],
27        ];
28    }
29
30    public function acl_remove_orphaned_roles()
31    {
32        $role_ids = [];
33        $auth_role_ids = [];
34
35        $sql = 'SELECT auth_role_id
36            FROM ' . ACL_GROUPS_TABLE . '
37            WHERE auth_role_id <> 0
38                AND forum_id = 0';
39        $result = $this->db->sql_query($sql);
40        while ($row = $this->db->sql_fetchrow($result))
41        {
42            $auth_role_ids[] = $row['auth_role_id'];
43        }
44        $this->db->sql_freeresult($result);
45
46        if (count($auth_role_ids))
47        {
48            $sql = 'SELECT role_id
49                FROM ' . ACL_ROLES_TABLE . '
50                WHERE ' . $this->db->sql_in_set('role_id', $auth_role_ids);
51            $result = $this->db->sql_query($sql);
52            while ($row = $this->db->sql_fetchrow($result))
53            {
54                $role_ids[] = $row['role_id'];
55            }
56            $this->db->sql_freeresult($result);
57        }
58
59        $non_existent_role_ids = array_diff($auth_role_ids, $role_ids);
60
61        // Nothing to do, there are no non-existent roles assigned to groups
62        if (empty($non_existent_role_ids))
63        {
64            return true;
65        }
66
67        // Remove assigned non-existent roles from users and groups
68        $sql = 'DELETE FROM ' . ACL_USERS_TABLE . '
69            WHERE ' . $this->db->sql_in_set('auth_role_id', $non_existent_role_ids);
70        $this->db->sql_query($sql);
71
72        $sql = 'DELETE FROM ' . ACL_GROUPS_TABLE . '
73            WHERE ' . $this->db->sql_in_set('auth_role_id', $non_existent_role_ids);
74        $this->db->sql_query($sql);
75
76        $auth = new \phpbb\auth\auth();
77        $auth->acl_clear_prefetch();
78
79        return true;
80    }
81}