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

189 lines 5.6 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 namespace UserAccessManager\Setup\Update;
16
17 use UserAccessManager\Setup\Database\DatabaseUpdate;
18
19 /**
20 * Class DatabaseUpdate1
21 *
22 * @package UserAccessManager\Setup\Update
23 */
24 class DatabaseUpdate1 extends DatabaseUpdate
25 {
26 /**
27 * Returns the version.
28 *
29 * @return string
30 */
31 public function getVersion()
32 {
33 return '1.0';
34 }
35
36 /**
37 * Updates the user group table to version 1.0.
38 *
39 * @param string $userGroupTable
40 *
41 * @return bool
42 */
43 private function updateToUserGroupTableUpdate($userGroupTable)
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 *
69 * @param string $objectType
70 * @param string $userGroupToPost
71 * @param string $userGroupToCategory
72 * @param string $userGroupToUser
73 * @param string $userGroupToRole
74 *
75 * @return null|string
76 */
77 private function getObjectSelectQuery(
78 $objectType,
79 $userGroupToPost,
80 $userGroupToCategory,
81 $userGroupToUser,
82 $userGroupToRole
83 ) {
84 $addition = '';
85
86 if ($this->objectHandler->isPostType($objectType) === true) {
87 $dbIdName = 'post_id';
88 $database = $userGroupToPost.', '.$this->database->getPostsTable();
89 $addition = " WHERE post_id = ID AND post_type = '{$objectType}'";
90 } elseif ($objectType === 'category') {
91 $dbIdName = 'category_id';
92 $database = $userGroupToCategory;
93 } elseif ($objectType === 'user') {
94 $dbIdName = 'user_id';
95 $database = $userGroupToUser;
96 } elseif ($objectType === 'role') {
97 $dbIdName = 'role_name';
98 $database = $userGroupToRole;
99 } else {
100 return null;
101 }
102
103 return "SELECT {$dbIdName} AS id, group_id AS groupId FROM {$database} {$addition}";
104 }
105
106 /**
107 * Updates the user group to object table to version 1.0.
108 */
109 private function updateToUserGroupToObjectTableUpdate()
110 {
111 $prefix = $this->database->getPrefix();
112
113 $charsetCollate = $this->database->getCharset();
114 $userGroupToObject = $prefix.'uam_accessgroup_to_object';
115 $userGroupToPost = $prefix.'uam_accessgroup_to_post';
116 $userGroupToUser = $prefix.'uam_accessgroup_to_user';
117 $userGroupToCategory = $prefix.'uam_accessgroup_to_category';
118 $userGroupToRole = $prefix.'uam_accessgroup_to_role';
119
120 $alterQuery = "ALTER TABLE '{$userGroupToObject}'
121 CHANGE 'object_id' 'object_id' VARCHAR(64) {$charsetCollate}";
122 $success = $this->database->query($alterQuery) !== false;
123
124 if ($success === false) {
125 return false;
126 }
127
128 $objectTypes = $this->objectHandler->getObjectTypes();
129
130 foreach ($objectTypes as $objectType) {
131 $query = $this->getObjectSelectQuery(
132 $objectType,
133 $userGroupToPost,
134 $userGroupToCategory,
135 $userGroupToUser,
136 $userGroupToRole
137 );
138
139 if ($query === null) {
140 continue;
141 }
142
143 $dbObjects = (array)$this->database->getResults($query);
144
145 foreach ($dbObjects as $dbObject) {
146 $insert = $this->database->insert(
147 $userGroupToObject,
148 [
149 'group_id' => $dbObject->groupId,
150 'object_id' => $dbObject->id,
151 'object_type' => $objectType
152 ],
153 [
154 '%d',
155 '%d',
156 '%s'
157 ]
158 );
159 $success = $success && $insert !== false;
160 }
161 }
162
163 $dropQuery = "DROP TABLE {$userGroupToPost},
164 {$userGroupToUser},
165 {$userGroupToCategory},
166 {$userGroupToRole}";
167
168 return $success && $this->database->query($dropQuery) !== false;
169 }
170
171 /**
172 * Executes the update.
173 *
174 * @return bool
175 */
176 public function update()
177 {
178 $success = true;
179 $userGroupTable = $this->database->getUserGroupTable();
180 $dbUserGroup = $this->database->getVariable("SHOW TABLES LIKE '{$userGroupTable}'");
181
182 if ($dbUserGroup === $userGroupTable) {
183 $success = $this->updateToUserGroupTableUpdate($userGroupTable);
184 }
185
186 return $success && $this->updateToUserGroupToObjectTableUpdate();
187 }
188 }
189