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

186 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * DatabaseUpdate1.php
4 *
5 * The DatabaseUpdate1 class file.
6 *
7 * PHP versions 5
8 *
9 * @author Alexander Schneider <alexanderschneider85@gmail.com>
10 * @copyright 2008-2017 Alexander Schneider
11 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
12 * @version SVN: $id$
13 * @link http://wordpress.org/extend/plugins/user-access-manager/
14 */
15
16 declare(strict_types=1);
17
18 namespace UserAccessManager\Setup\Update;
19
20 use UserAccessManager\Setup\Database\DatabaseUpdate;
21
22 /**
23 * Class DatabaseUpdate1
24 *
25 * @package UserAccessManager\Setup\Update
26 */
27 class DatabaseUpdate1 extends DatabaseUpdate
28 {
29 /**
30 * Returns the version.
31 * @return string
32 */
33 public function getVersion(): string
34 {
35 return '1.0';
36 }
37
38 /**
39 * Updates the user group table to version 1.0.
40 * @param string $userGroupTable
41 * @return bool
42 */
43 private function updateToUserGroupTableUpdate(string $userGroupTable): bool
44 {
45 $alterQuery = "ALTER TABLE {$userGroupTable}
46 ADD read_access TINYTEXT NOT NULL DEFAULT '',
47 ADD write_access TINYTEXT NOT NULL DEFAULT '',
48 ADD ip_range MEDIUMTEXT NULL DEFAULT ''";
49
50 $this->database->query($alterQuery);
51
52 $updateQuery = "UPDATE {$userGroupTable} SET read_access = 'group', write_access = 'group'";
53 $success = $this->database->query($updateQuery) !== false;
54
55 $selectQuery = "SHOW columns FROM {$userGroupTable} LIKE 'ip_range'";
56 $dbIpRange = (string) $this->database->getVariable($selectQuery);
57
58 if ($dbIpRange !== 'ip_range') {
59 $alterQuery = "ALTER TABLE {$userGroupTable} ADD ip_range MEDIUMTEXT NULL DEFAULT ''";
60 $success = $this->database->query($alterQuery) !== false;
61 }
62
63 return $success;
64 }
65
66 /**
67 * Returns the select query for the object type.
68 * @param string $objectType
69 * @param string $userGroupToPost
70 * @param string $userGroupToCategory
71 * @param string $userGroupToUser
72 * @param string $userGroupToRole
73 * @return null|string
74 */
75 private function getObjectSelectQuery(
76 string $objectType,
77 string $userGroupToPost,
78 string $userGroupToCategory,
79 string $userGroupToUser,
80 string $userGroupToRole
81 ): ?string {
82 $addition = '';
83
84 if ($this->objectHandler->isPostType($objectType) === true) {
85 $dbIdName = 'post_id';
86 $database = $userGroupToPost . ', ' . $this->database->getPostsTable();
87 $addition = " WHERE post_id = ID AND post_type = '{$objectType}'";
88 } elseif ($objectType === 'category') {
89 $dbIdName = 'category_id';
90 $database = $userGroupToCategory;
91 } elseif ($objectType === 'user') {
92 $dbIdName = 'user_id';
93 $database = $userGroupToUser;
94 } elseif ($objectType === 'role') {
95 $dbIdName = 'role_name';
96 $database = $userGroupToRole;
97 } else {
98 return null;
99 }
100
101 return "SELECT {$dbIdName} AS id, group_id AS groupId FROM {$database} {$addition}";
102 }
103
104 /**
105 * Updates the user group to object table to version 1.0.
106 */
107 private function updateToUserGroupToObjectTableUpdate(): bool
108 {
109 $prefix = $this->database->getPrefix();
110
111 $charsetCollate = $this->database->getCharset();
112 $userGroupToObject = $prefix . 'uam_accessgroup_to_object';
113 $userGroupToPost = $prefix . 'uam_accessgroup_to_post';
114 $userGroupToUser = $prefix . 'uam_accessgroup_to_user';
115 $userGroupToCategory = $prefix . 'uam_accessgroup_to_category';
116 $userGroupToRole = $prefix . 'uam_accessgroup_to_role';
117
118 $alterQuery = "ALTER TABLE '{$userGroupToObject}'
119 CHANGE 'object_id' 'object_id' VARCHAR(64) {$charsetCollate}";
120 $success = $this->database->query($alterQuery) !== false;
121
122 if ($success === false) {
123 return false;
124 }
125
126 $objectTypes = $this->objectHandler->getObjectTypes();
127
128 foreach ($objectTypes as $objectType) {
129 $query = $this->getObjectSelectQuery(
130 $objectType,
131 $userGroupToPost,
132 $userGroupToCategory,
133 $userGroupToUser,
134 $userGroupToRole
135 );
136
137 if ($query === null) {
138 continue;
139 }
140
141 $dbObjects = (array) $this->database->getResults($query);
142
143 foreach ($dbObjects as $dbObject) {
144 $insert = $this->database->insert(
145 $userGroupToObject,
146 [
147 'group_id' => $dbObject->groupId,
148 'object_id' => $dbObject->id,
149 'object_type' => $objectType
150 ],
151 [
152 '%d',
153 '%d',
154 '%s'
155 ]
156 );
157 $success = $success && $insert !== false;
158 }
159 }
160
161 $dropQuery = "DROP TABLE {$userGroupToPost},
162 {$userGroupToUser},
163 {$userGroupToCategory},
164 {$userGroupToRole}";
165
166 return $success && $this->database->query($dropQuery) !== false;
167 }
168
169 /**
170 * Executes the update.
171 * @return bool
172 */
173 public function update(): bool
174 {
175 $success = true;
176 $userGroupTable = $this->database->getUserGroupTable();
177 $dbUserGroup = $this->database->getVariable("SHOW TABLES LIKE '{$userGroupTable}'");
178
179 if ($dbUserGroup === $userGroupTable) {
180 $success = $this->updateToUserGroupTableUpdate($userGroupTable);
181 }
182
183 return $success && $this->updateToUserGroupToObjectTableUpdate();
184 }
185 }
186