| @@ -3,8 +3,10 @@ | ||
| 3 | 3 | declare(strict_types=1); |
| 4 | 4 | |
| 5 | 5 | namespace UserAccessManager\Setup\Update; |
| 6 | 6 | |
| 7 | +use UserAccessManager\Setup\Database\DatabaseUpdate; | |
| 8 | + | |
| 7 | 9 | class DatabaseUpdate1 extends DatabaseUpdate |
| 8 | 10 | { |
| 9 | 11 | public function getVersion(): string |
| 10 | 12 | { |
| @@ -33,44 +35,47 @@ | ||
| 33 | 35 | |
| 34 | 36 | return $success; |
| 35 | 37 | } |
| 36 | 38 | |
| 37 | - /** | |
| 38 | - * @param string[] $legacyTables Legacy per-object-type tables, keyed by object type. | |
| 39 | - */ | |
| 40 | - private function getObjectSelectQuery(string $objectType, array $legacyTables): ?string | |
| 41 | - { | |
| 39 | + private function getObjectSelectQuery( | |
| 40 | + string $objectType, | |
| 41 | + string $userGroupToPost, | |
| 42 | + string $userGroupToCategory, | |
| 43 | + string $userGroupToUser, | |
| 44 | + string $userGroupToRole | |
| 45 | + ): ?string { | |
| 46 | + $addition = ''; | |
| 47 | + | |
| 42 | 48 | if ($this->objectHandler->isPostType($objectType) === true) { |
| 43 | - $source = $legacyTables['post'] . ', ' . $this->database->getPostsTable(); | |
| 44 | - | |
| 45 | - return "SELECT post_id AS id, group_id AS groupId FROM $source" | |
| 46 | - . " WHERE post_id = ID AND post_type = '$objectType'"; | |
| 47 | - } | |
| 48 | - | |
| 49 | - $idColumns = [ | |
| 50 | - 'category' => 'category_id', | |
| 51 | - 'user' => 'user_id', | |
| 52 | - 'role' => 'role_name' | |
| 53 | - ]; | |
| 54 | - | |
| 55 | - if (isset($idColumns[$objectType]) === false) { | |
| 49 | + $dbIdName = 'post_id'; | |
| 50 | + $database = $userGroupToPost . ', ' . $this->database->getPostsTable(); | |
| 51 | + $addition = " WHERE post_id = ID AND post_type = '$objectType'"; | |
| 52 | + } elseif ($objectType === 'category') { | |
| 53 | + $dbIdName = 'category_id'; | |
| 54 | + $database = $userGroupToCategory; | |
| 55 | + } elseif ($objectType === 'user') { | |
| 56 | + $dbIdName = 'user_id'; | |
| 57 | + $database = $userGroupToUser; | |
| 58 | + } elseif ($objectType === 'role') { | |
| 59 | + $dbIdName = 'role_name'; | |
| 60 | + $database = $userGroupToRole; | |
| 61 | + } else { | |
| 56 | 62 | return null; |
| 57 | 63 | } |
| 58 | 64 | |
| 59 | - return "SELECT {$idColumns[$objectType]} AS id, group_id AS groupId FROM {$legacyTables[$objectType]}"; | |
| 65 | + return "SELECT $dbIdName AS id, group_id AS groupId FROM $database $addition"; | |
| 60 | 66 | } |
| 61 | 67 | |
| 62 | 68 | private function updateToUserGroupToObjectTableUpdate(): bool |
| 63 | 69 | { |
| 64 | 70 | $prefix = $this->database->getPrefix(); |
| 71 | + | |
| 65 | 72 | $charsetCollate = $this->database->getCharset(); |
| 66 | 73 | $userGroupToObject = $prefix . 'uam_accessgroup_to_object'; |
| 67 | - $legacyTables = [ | |
| 68 | - 'post' => $prefix . 'uam_accessgroup_to_post', | |
| 69 | - 'user' => $prefix . 'uam_accessgroup_to_user', | |
| 70 | - 'category' => $prefix . 'uam_accessgroup_to_category', | |
| 71 | - 'role' => $prefix . 'uam_accessgroup_to_role' | |
| 72 | - ]; | |
| 74 | + $userGroupToPost = $prefix . 'uam_accessgroup_to_post'; | |
| 75 | + $userGroupToUser = $prefix . 'uam_accessgroup_to_user'; | |
| 76 | + $userGroupToCategory = $prefix . 'uam_accessgroup_to_category'; | |
| 77 | + $userGroupToRole = $prefix . 'uam_accessgroup_to_role'; | |
| 73 | 78 | |
| 74 | 79 | $alterQuery = "ALTER TABLE '$userGroupToObject' |
| 75 | 80 | CHANGE 'object_id' 'object_id' VARCHAR(64) $charsetCollate"; |
| 76 | 81 | $success = $this->database->query($alterQuery) !== false; |
| @@ -78,11 +83,19 @@ | ||
| 78 | 83 | if ($success === false) { |
| 79 | 84 | return false; |
| 80 | 85 | } |
| 81 | 86 | |
| 82 | - foreach ($this->objectHandler->getObjectTypes() as $objectType) { | |
| 83 | - $query = $this->getObjectSelectQuery($objectType, $legacyTables); | |
| 87 | + $objectTypes = $this->objectHandler->getObjectTypes(); | |
| 84 | 88 | |
| 89 | + foreach ($objectTypes as $objectType) { | |
| 90 | + $query = $this->getObjectSelectQuery( | |
| 91 | + $objectType, | |
| 92 | + $userGroupToPost, | |
| 93 | + $userGroupToCategory, | |
| 94 | + $userGroupToUser, | |
| 95 | + $userGroupToRole | |
| 96 | + ); | |
| 97 | + | |
| 85 | 98 | if ($query === null) { |
| 86 | 99 | continue; |
| 87 | 100 | } |
| 88 | 101 | |
| @@ -105,9 +118,12 @@ | ||
| 105 | 118 | $success = $success && $insert !== false; |
| 106 | 119 | } |
| 107 | 120 | } |
| 108 | 121 | |
| 109 | - $dropQuery = 'DROP TABLE ' . implode(', ', $legacyTables); | |
| 122 | + $dropQuery = "DROP TABLE $userGroupToPost, | |
| 123 | + $userGroupToUser, | |
| 124 | + $userGroupToCategory, | |
| 125 | + $userGroupToRole"; | |
| 110 | 126 | |
| 111 | 127 | return $success && $this->database->query($dropQuery) !== false; |
| 112 | 128 | } |
| 113 | 129 | |