PluginProbe
User Access Manager / trunk
User Access Manager vtrunk
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
user-access-manager / src / Setup / Update / DatabaseUpdate1.php

DatabaseUpdate1.php in User Access Manager trunk, at src/Setup/Update/DatabaseUpdate1.php

141 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace UserAccessManager\Setup\Update;
6
7 class DatabaseUpdate1 extends DatabaseUpdate
8 {
9 public function getVersion(): string
10 {
11 return '1.0';
12 }
13
14 private function updateToUserGroupTableUpdate(string $userGroupTable): bool
15 {
16 $alterQuery = "ALTER TABLE `{$userGroupTable}`
17 ADD `read_access` TINYTEXT NOT NULL DEFAULT '',
18 ADD `write_access` TINYTEXT NOT NULL DEFAULT '',
19 ADD `ip_range` MEDIUMTEXT NULL DEFAULT ''";
20
21 $this->database->query($alterQuery);
22
23 $updateQuery = "UPDATE `$userGroupTable` SET `read_access` = 'group', `write_access` = 'group'";
24 $success = $this->database->query($updateQuery) !== false;
25
26 $selectQuery = "SHOW COLUMNS FROM `$userGroupTable` LIKE 'ip_range'";
27 $dbIpRange = (string) $this->database->getVariable($selectQuery);
28
29 if ($dbIpRange !== 'ip_range') {
30 $alterQuery = "ALTER TABLE `$userGroupTable` ADD `ip_range` MEDIUMTEXT NULL DEFAULT ''";
31 $success = $this->database->query($alterQuery) !== false;
32 }
33
34 return $success;
35 }
36
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 {
42 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) {
56 return null;
57 }
58
59 return "SELECT `{$idColumns[$objectType]}` AS `id`, `group_id` AS `groupId`
60 FROM `{$legacyTables[$objectType]}`";
61 }
62
63 /**
64 * The object types whose assignments the legacy tables can hold. getObjectTypes() only
65 * knows post types and taxonomies, so the user and role types have to be added for
66 * their own legacy tables, whose rows would otherwise be dropped unmigrated.
67 *
68 * @return string[]
69 */
70 private function getMigratableObjectTypes(): array
71 {
72 return array_merge($this->objectHandler->getObjectTypes(), ['user', 'role']);
73 }
74
75 private function updateToUserGroupToObjectTableUpdate(): bool
76 {
77 $prefix = $this->database->getPrefix();
78 $charsetCollate = $this->database->getColumnCharset();
79 $userGroupToObject = $prefix . 'uam_accessgroup_to_object';
80 $legacyTables = [
81 'post' => $prefix . 'uam_accessgroup_to_post',
82 'user' => $prefix . 'uam_accessgroup_to_user',
83 'category' => $prefix . 'uam_accessgroup_to_category',
84 'role' => $prefix . 'uam_accessgroup_to_role'
85 ];
86
87 $alterQuery = "ALTER TABLE `$userGroupToObject`
88 CHANGE `object_id` `object_id` VARCHAR(64) $charsetCollate";
89 $success = $this->database->query($alterQuery) !== false;
90
91 if ($success === false) {
92 return false;
93 }
94
95 foreach ($this->getMigratableObjectTypes() as $objectType) {
96 $query = $this->getObjectSelectQuery($objectType, $legacyTables);
97
98 if ($query === null) {
99 continue;
100 }
101
102 $dbObjects = (array) $this->database->getResults($query);
103
104 foreach ($dbObjects as $dbObject) {
105 $insert = $this->database->insert(
106 $userGroupToObject,
107 [
108 'group_id' => $dbObject->groupId,
109 'object_id' => $dbObject->id,
110 'object_type' => $objectType
111 ],
112 // All three are strings, roles carry their name as the object id.
113 [
114 '%s',
115 '%s',
116 '%s'
117 ]
118 );
119 $success = $success && $insert !== false;
120 }
121 }
122
123 $dropQuery = 'DROP TABLE IF EXISTS `' . implode('`, `', $legacyTables) . '`';
124
125 return $success && $this->database->query($dropQuery) !== false;
126 }
127
128 public function update(): bool
129 {
130 $success = true;
131 $userGroupTable = $this->database->getUserGroupTable();
132 $dbUserGroup = $this->database->getVariable("SHOW TABLES LIKE '$userGroupTable'");
133
134 if ($dbUserGroup === $userGroupTable) {
135 $success = $this->updateToUserGroupTableUpdate($userGroupTable);
136 }
137
138 return $success && $this->updateToUserGroupToObjectTableUpdate();
139 }
140 }
141