| Total Complexity | 76 |
| Total Lines | 389 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like PrivilegesTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PrivilegesTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | trait PrivilegesTrait |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Grabs an array of users and their privileges for an object, |
||
| 16 | * given its type. |
||
| 17 | * |
||
| 18 | * @param string $object The name of the object whose privileges are to be retrieved |
||
| 19 | * @param string $type The type of the object (eg. database, schema, relation, function or language) |
||
| 20 | * @param null|string $table Optional, column's table if type = column |
||
| 21 | * |
||
| 22 | * @return array|int Privileges array or error code |
||
| 23 | * - -1 invalid type |
||
| 24 | * - -2 object not found |
||
| 25 | * - -3 unknown privilege type |
||
| 26 | */ |
||
| 27 | public function getPrivileges($object, $type, $table = null) |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Internal function used for parsing ACLs. |
||
| 97 | * |
||
| 98 | * @param string $acl The ACL to parse (of type aclitem[]) |
||
| 99 | * |
||
| 100 | * @return array|int Privileges array or integer with error code |
||
| 101 | * |
||
| 102 | * @internal bool $in_quotes toggles acl in_quotes attribute |
||
| 103 | */ |
||
| 104 | protected function parseACL($acl) |
||
| 105 | { |
||
| 106 | // Take off the first and last characters (the braces) |
||
| 107 | $acl = substr($acl, 1, strlen($acl) - 2); |
||
| 108 | |||
| 109 | // Pick out individual ACE's by carefully parsing. This is necessary in order |
||
| 110 | // to cope with usernames and stuff that contain commas |
||
| 111 | $aces = []; |
||
| 112 | $i = $j = 0; |
||
| 113 | $in_quotes = false; |
||
| 114 | while ($i < strlen($acl)) { |
||
| 115 | // If current char is a double quote and it's not escaped, then |
||
| 116 | // enter quoted bit |
||
| 117 | $char = substr($acl, $i, 1); |
||
| 118 | if ($char == '"' && ($i == 0 || substr($acl, $i - 1, 1) != '\\')) { |
||
| 119 | $in_quotes = !$in_quotes; |
||
| 120 | } elseif ($char == ',' && !$in_quotes) { |
||
| 121 | // Add text so far to the array |
||
| 122 | $aces[] = substr($acl, $j, $i - $j); |
||
| 123 | $j = $i + 1; |
||
| 124 | } |
||
| 125 | ++$i; |
||
| 126 | } |
||
| 127 | // Add final text to the array |
||
| 128 | $aces[] = substr($acl, $j); |
||
| 129 | |||
| 130 | // Create the array to be returned |
||
| 131 | $temp = []; |
||
| 132 | |||
| 133 | // For each ACE, generate an entry in $temp |
||
| 134 | foreach ($aces as $v) { |
||
| 135 | // If the ACE begins with a double quote, strip them off both ends |
||
| 136 | // and unescape backslashes and double quotes |
||
| 137 | // $unquote = false; |
||
| 138 | if (strpos($v, '"') === 0) { |
||
| 139 | $v = substr($v, 1, strlen($v) - 2); |
||
| 140 | $v = str_replace('\\"', '"', $v); |
||
| 141 | $v = str_replace('\\\\', '\\', $v); |
||
| 142 | } |
||
| 143 | |||
| 144 | // Figure out type of ACE (public, user or group) |
||
| 145 | if (strpos($v, '=') === 0) { |
||
| 146 | $atype = 'public'; |
||
| 147 | } else { |
||
| 148 | if ($this->hasRoles()) { |
||
| 149 | $atype = 'role'; |
||
| 150 | } else { |
||
| 151 | if (strpos($v, 'group ') === 0) { |
||
| 152 | $atype = 'group'; |
||
| 153 | // Tear off 'group' prefix |
||
| 154 | $v = substr($v, 6); |
||
| 155 | } else { |
||
| 156 | $atype = 'user'; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | // Break on unquoted equals sign... |
||
| 162 | $i = 0; |
||
| 163 | $in_quotes = false; |
||
| 164 | $entity = null; |
||
| 165 | $chars = null; |
||
| 166 | while ($i < strlen($v)) { |
||
| 167 | // If current char is a double quote and it's not escaped, then |
||
| 168 | // enter quoted bit |
||
| 169 | $char = substr($v, $i, 1); |
||
| 170 | $next_char = substr($v, $i + 1, 1); |
||
| 171 | if ($char == '"' && ($i == 0 || $next_char != '"')) { |
||
| 172 | $in_quotes = !$in_quotes; |
||
| 173 | } elseif ($char == '"' && $next_char == '"') { |
||
| 174 | // Skip over escaped double quotes |
||
| 175 | ++$i; |
||
| 176 | } elseif ($char == '=' && !$in_quotes) { |
||
| 177 | // Split on current equals sign |
||
| 178 | $entity = substr($v, 0, $i); |
||
| 179 | $chars = substr($v, $i + 1); |
||
| 180 | |||
| 181 | break; |
||
| 182 | } |
||
| 183 | ++$i; |
||
| 184 | } |
||
| 185 | |||
| 186 | // Check for quoting on entity name, and unescape if necessary |
||
| 187 | if (strpos($entity, '"') === 0) { |
||
| 188 | $entity = substr($entity, 1, strlen($entity) - 2); |
||
| 189 | $entity = str_replace('""', '"', $entity); |
||
| 190 | } |
||
| 191 | |||
| 192 | // New row to be added to $temp |
||
| 193 | // (type, grantee, privileges, grantor, grant option? |
||
| 194 | $row = [$atype, $entity, [], '', []]; |
||
| 195 | |||
| 196 | // Loop over chars and add privs to $row |
||
| 197 | for ($i = 0; $i < strlen($chars); ++$i) { |
||
| 198 | // Append to row's privs list the string representing |
||
| 199 | // the privilege |
||
| 200 | $char = substr($chars, $i, 1); |
||
| 201 | if ($char == '*') { |
||
| 202 | $row[4][] = $this->privmap[substr($chars, $i - 1, 1)]; |
||
| 203 | } elseif ($char == '/') { |
||
| 204 | $grantor = substr($chars, $i + 1); |
||
| 205 | // Check for quoting |
||
| 206 | if (strpos($grantor, '"') === 0) { |
||
| 207 | $grantor = substr($grantor, 1, strlen($grantor) - 2); |
||
| 208 | $grantor = str_replace('""', '"', $grantor); |
||
| 209 | } |
||
| 210 | $row[3] = $grantor; |
||
| 211 | |||
| 212 | break; |
||
| 213 | } else { |
||
| 214 | if (!isset($this->privmap[$char])) { |
||
| 215 | return -3; |
||
| 216 | } |
||
| 217 | |||
| 218 | $row[2][] = $this->privmap[$char]; |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | // Append row to temp |
||
| 223 | $temp[] = $row; |
||
| 224 | } |
||
| 225 | |||
| 226 | return $temp; |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Grants a privilege to a user, group or public. |
||
| 231 | * |
||
| 232 | * @param string $mode 'GRANT' or 'REVOKE'; |
||
| 233 | * @param mixed $type The type of object |
||
| 234 | * @param string $object The name of the object |
||
| 235 | * @param bool $public True to grant to public, false otherwise |
||
| 236 | * @param mixed $usernames the array of usernames to grant privs to |
||
| 237 | * @param mixed $groupnames the array of group names to grant privs to |
||
| 238 | * @param mixed $privileges The array of privileges to grant (eg. ('SELECT', 'ALL PRIVILEGES', etc.) ) |
||
| 239 | * @param bool $grantoption True if has grant option, false otherwise |
||
| 240 | * @param bool $cascade True for cascade revoke, false otherwise |
||
| 241 | * @param string $table the column's table if type=column |
||
| 242 | * |
||
| 243 | * @return int 0 if operation was successful |
||
| 244 | */ |
||
| 245 | public function setPrivileges( |
||
| 374 | } |
||
| 375 | |||
| 376 | abstract public function fieldClean(&$str); |
||
| 377 | |||
| 378 | abstract public function beginTransaction(); |
||
| 379 | |||
| 380 | abstract public function rollbackTransaction(); |
||
| 381 | |||
| 382 | abstract public function endTransaction(); |
||
| 383 | |||
| 384 | abstract public function execute($sql); |
||
| 385 | |||
| 386 | abstract public function setComment($obj_type, $obj_name, $table, $comment, $basetype = null); |
||
| 387 | |||
| 388 | abstract public function selectSet($sql); |
||
| 389 | |||
| 390 | abstract public function clean(&$str); |
||
| 391 | |||
| 392 | abstract public function hasGrantOption(); |
||
| 393 | |||
| 394 | abstract public function getFunction($function_oid); |
||
| 395 | |||
| 396 | abstract public function fieldArrayClean(&$arr); |
||
| 397 | |||
| 398 | abstract public function selectField($sql, $field); |
||
| 399 | |||
| 400 | abstract public function hasRoles(); |
||
| 401 | } |
||
| 402 |