PluginProbe
User Access Manager / 2.3.18
User Access Manager v2.3.18
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 2.3.18, at src/Setup/Update/DatabaseUpdate1.php

127 lines 4.2 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 FROM {$legacyTables[$objectType]}";
60 }
61
62 private function updateToUserGroupToObjectTableUpdate(): bool
63 {
64 $prefix = $this->database->getPrefix();
65 $charsetCollate = $this->database->getCharset();
66 $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 ];
73
74 $alterQuery = "ALTER TABLE '$userGroupToObject'
75 CHANGE 'object_id' 'object_id' VARCHAR(64) $charsetCollate";
76 $success = $this->database->query($alterQuery) !== false;
77
78 if ($success === false) {
79 return false;
80 }
81
82 foreach ($this->objectHandler->getObjectTypes() as $objectType) {
83 $query = $this->getObjectSelectQuery($objectType, $legacyTables);
84
85 if ($query === null) {
86 continue;
87 }
88
89 $dbObjects = (array) $this->database->getResults($query);
90
91 foreach ($dbObjects as $dbObject) {
92 $insert = $this->database->insert(
93 $userGroupToObject,
94 [
95 'group_id' => $dbObject->groupId,
96 'object_id' => $dbObject->id,
97 'object_type' => $objectType
98 ],
99 [
100 '%d',
101 '%d',
102 '%s'
103 ]
104 );
105 $success = $success && $insert !== false;
106 }
107 }
108
109 $dropQuery = 'DROP TABLE ' . implode(', ', $legacyTables);
110
111 return $success && $this->database->query($dropQuery) !== false;
112 }
113
114 public function update(): bool
115 {
116 $success = true;
117 $userGroupTable = $this->database->getUserGroupTable();
118 $dbUserGroup = $this->database->getVariable("SHOW TABLES LIKE '$userGroupTable'");
119
120 if ($dbUserGroup === $userGroupTable) {
121 $success = $this->updateToUserGroupTableUpdate($userGroupTable);
122 }
123
124 return $success && $this->updateToUserGroupToObjectTableUpdate();
125 }
126 }
127