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

143 lines 4.6 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 use UserAccessManager\Setup\Database\DatabaseUpdate;
8
9 class DatabaseUpdate1 extends DatabaseUpdate
10 {
11 public function getVersion(): string
12 {
13 return '1.0';
14 }
15
16 private function updateToUserGroupTableUpdate(string $userGroupTable): bool
17 {
18 $alterQuery = "ALTER TABLE {$userGroupTable}
19 ADD read_access TINYTEXT NOT NULL DEFAULT '',
20 ADD write_access TINYTEXT NOT NULL DEFAULT '',
21 ADD ip_range MEDIUMTEXT NULL DEFAULT ''";
22
23 $this->database->query($alterQuery);
24
25 $updateQuery = "UPDATE $userGroupTable SET read_access = 'group', write_access = 'group'";
26 $success = $this->database->query($updateQuery) !== false;
27
28 $selectQuery = "SHOW columns FROM $userGroupTable LIKE 'ip_range'";
29 $dbIpRange = (string) $this->database->getVariable($selectQuery);
30
31 if ($dbIpRange !== 'ip_range') {
32 $alterQuery = "ALTER TABLE $userGroupTable ADD ip_range MEDIUMTEXT NULL DEFAULT ''";
33 $success = $this->database->query($alterQuery) !== false;
34 }
35
36 return $success;
37 }
38
39 private function getObjectSelectQuery(
40 string $objectType,
41 string $userGroupToPost,
42 string $userGroupToCategory,
43 string $userGroupToUser,
44 string $userGroupToRole
45 ): ?string {
46 $addition = '';
47
48 if ($this->objectHandler->isPostType($objectType) === true) {
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 {
62 return null;
63 }
64
65 return "SELECT $dbIdName AS id, group_id AS groupId FROM $database $addition";
66 }
67
68 private function updateToUserGroupToObjectTableUpdate(): bool
69 {
70 $prefix = $this->database->getPrefix();
71
72 $charsetCollate = $this->database->getCharset();
73 $userGroupToObject = $prefix . 'uam_accessgroup_to_object';
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';
78
79 $alterQuery = "ALTER TABLE '$userGroupToObject'
80 CHANGE 'object_id' 'object_id' VARCHAR(64) $charsetCollate";
81 $success = $this->database->query($alterQuery) !== false;
82
83 if ($success === false) {
84 return false;
85 }
86
87 $objectTypes = $this->objectHandler->getObjectTypes();
88
89 foreach ($objectTypes as $objectType) {
90 $query = $this->getObjectSelectQuery(
91 $objectType,
92 $userGroupToPost,
93 $userGroupToCategory,
94 $userGroupToUser,
95 $userGroupToRole
96 );
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 [
113 '%d',
114 '%d',
115 '%s'
116 ]
117 );
118 $success = $success && $insert !== false;
119 }
120 }
121
122 $dropQuery = "DROP TABLE $userGroupToPost,
123 $userGroupToUser,
124 $userGroupToCategory,
125 $userGroupToRole";
126
127 return $success && $this->database->query($dropQuery) !== false;
128 }
129
130 public function update(): bool
131 {
132 $success = true;
133 $userGroupTable = $this->database->getUserGroupTable();
134 $dbUserGroup = $this->database->getVariable("SHOW TABLES LIKE '$userGroupTable'");
135
136 if ($dbUserGroup === $userGroupTable) {
137 $success = $this->updateToUserGroupTableUpdate($userGroupTable);
138 }
139
140 return $success && $this->updateToUserGroupToObjectTableUpdate();
141 }
142 }
143