|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + * phpBB Media Embed PlugIn extension for the phpBB Forum Software package. |
| 5 | + * |
| 6 | + * @copyright (c) 2022 phpBB Limited <https://www.phpbb.com> |
| 7 | + * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +namespace phpbb\mediaembed\migrations; |
| 12 | + |
| 13 | +/** |
| 14 | + * Migration 7: Add missing permissions. |
| 15 | + * |
| 16 | + * If the media embed forum permission has already been set, either by a previous migration |
| 17 | + * or the user, then this migration will not run. Otherwise, it probably means the core forum |
| 18 | + * roles don't exist anymore. So this migration will look for any custom forum roles that |
| 19 | + * exist with permission to use BBCodes and assume those are safe to assign Media Embed |
| 20 | + * permissions to, since it is essentially a BBCode itself. |
| 21 | + */ |
| 22 | +class m7_add_missing_permissions extends \phpbb\db\migration\migration |
| 23 | +{ |
| 24 | + /** |
| 25 | + * If f_mediaembed has already been assigned to any permission role |
| 26 | + * then this migration should not run. |
| 27 | + * |
| 28 | + * {@inheritdoc} |
| 29 | + */ |
| 30 | + public function effectively_installed() |
| 31 | + { |
| 32 | + $sql_array = [ |
| 33 | + 'SELECT' => '*', |
| 34 | + 'FROM' => [ |
| 35 | + $this->table_prefix . 'acl_roles_data' => 'd', |
| 36 | + ], |
| 37 | + 'LEFT_JOIN' => [ |
| 38 | + [ |
| 39 | + 'FROM' => [$this->table_prefix . 'acl_options' => 'o'], |
| 40 | + 'ON' => 'o.auth_option_id = d.auth_option_id', |
| 41 | + ], |
| 42 | + ], |
| 43 | + 'WHERE' => "o.auth_option = 'f_mediaembed'", |
| 44 | + ]; |
| 45 | + |
| 46 | + $sql = $this->db->sql_build_query('SELECT', $sql_array); |
| 47 | + $result = $this->db->sql_query_limit($sql, 1); |
| 48 | + $row = $this->db->sql_fetchrow($result); |
| 49 | + $this->db->sql_freeresult($result); |
| 50 | + |
| 51 | + return $row !== false; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * {@inheritdoc} |
| 56 | + */ |
| 57 | + public static function depends_on() |
| 58 | + { |
| 59 | + return [ |
| 60 | + '\phpbb\mediaembed\migrations\m1_install_data', |
| 61 | + '\phpbb\mediaembed\migrations\m4_permissions', |
| 62 | + '\phpbb\mediaembed\migrations\m6_full_width', |
| 63 | + ]; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * {@inheritdoc} |
| 68 | + */ |
| 69 | + public function update_data() |
| 70 | + { |
| 71 | + $install = []; |
| 72 | + |
| 73 | + foreach ($this->custom_forum_roles() as $role) |
| 74 | + { |
| 75 | + $install[] = ['permission.permission_set', [$role, 'f_mediaembed']]; |
| 76 | + } |
| 77 | + |
| 78 | + return $install; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Find custom forum roles with bbcodes allowed. |
| 83 | + * |
| 84 | + * @return array An array of forum role names |
| 85 | + */ |
| 86 | + public function custom_forum_roles() |
| 87 | + { |
| 88 | + $sql_array = [ |
| 89 | + 'SELECT' => 'roles.role_id, roles.role_name', |
| 90 | + 'FROM' => [ |
| 91 | + $this->table_prefix . 'acl_roles' => 'roles', |
| 92 | + ], |
| 93 | + 'LEFT_JOIN' => [ |
| 94 | + [ |
| 95 | + 'FROM' => [$this->table_prefix . 'acl_roles_data' => 'data'], |
| 96 | + 'ON' => 'roles.role_id = data.role_id', |
| 97 | + ], |
| 98 | + [ |
| 99 | + 'FROM' => [$this->table_prefix . 'acl_options' => 'opts'], |
| 100 | + 'ON' => 'data.auth_option_id = opts.auth_option_id', |
| 101 | + ], |
| 102 | + ], |
| 103 | + 'WHERE' => 'opts.auth_option = "f_bbcode" |
| 104 | + AND data.auth_setting = "1" |
| 105 | + AND roles.role_type = "f_" |
| 106 | + AND ' . $this->db->sql_in_set('roles.role_name', $this->predefined_roles(), true), |
| 107 | + ]; |
| 108 | + |
| 109 | + $sql = $this->db->sql_build_query('SELECT', $sql_array); |
| 110 | + $result = $this->db->sql_query($sql); |
| 111 | + |
| 112 | + $roles = []; |
| 113 | + while ($row = $this->db->sql_fetchrow($result)) |
| 114 | + { |
| 115 | + $roles[$row['role_id']] = $row['role_name']; |
| 116 | + } |
| 117 | + $this->db->sql_freeresult($result); |
| 118 | + |
| 119 | + return $roles; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * An array of phpBB's predefined forum role names |
| 124 | + * |
| 125 | + * @return array |
| 126 | + */ |
| 127 | + protected function predefined_roles() |
| 128 | + { |
| 129 | + return [ |
| 130 | + 'ROLE_FORUM_FULL', |
| 131 | + 'ROLE_FORUM_STANDARD', |
| 132 | + 'ROLE_FORUM_NOACCESS', |
| 133 | + 'ROLE_FORUM_READONLY', |
| 134 | + 'ROLE_FORUM_LIMITED', |
| 135 | + 'ROLE_FORUM_BOT', |
| 136 | + 'ROLE_FORUM_ONQUEUE', |
| 137 | + 'ROLE_FORUM_POLLS', |
| 138 | + 'ROLE_FORUM_LIMITED_POLLS', |
| 139 | + ]; |
| 140 | + } |
| 141 | +} |
0 commit comments