| Total Complexity | 44 |
| Total Lines | 238 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like perms_model 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 perms_model, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class perms_model extends Model { |
||
| 4 | |||
| 5 | private $all_permissions = [ |
||
| 6 | 'read_file', |
||
| 7 | 'write_file', |
||
| 8 | 'manage_user', |
||
| 9 | 'see_global_trash', |
||
| 10 | 'see_history_detail' |
||
| 11 | ]; |
||
| 12 | |||
| 13 | private $implied_permissions = [ |
||
| 14 | "write_file" => ["read_file"], |
||
| 15 | ]; |
||
| 16 | |||
| 17 | private $default_rp = [ |
||
| 18 | "none" => [], |
||
| 19 | "reader" => ['read_file'], |
||
| 20 | "author" => ['write_file', 'see_history_detail'], |
||
| 21 | "admin" => ['write_file', 'manage_user', 'see_global_trash', 'see_history_detail'] |
||
| 22 | ]; |
||
| 23 | |||
| 24 | private $role_permissions = []; |
||
| 25 | |||
| 26 | function __construct() { |
||
| 27 | $this->load_model("jugaad_model"); |
||
| 28 | $this->load_model("auth_model"); |
||
| 29 | |||
| 30 | $this->load_library("db_lib"); |
||
| 31 | |||
| 32 | $this->default_rp["superadmin"] = $this->all_permissions; |
||
| 33 | |||
| 34 | $this->role_permissions = $this->get_role_info(); |
||
| 35 | } |
||
| 36 | |||
| 37 | private function extend_permissions($permissions) { |
||
| 38 | $e_perms = []; |
||
| 39 | |||
| 40 | foreach ($permissions as $perm) { |
||
| 41 | array_push($e_perms, $perm); |
||
| 42 | if (isset($this->implied_permissions[$perm])) { |
||
| 43 | $e_perms = array_merge($e_perms, $this->implied_permissions[$perm]); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | |||
| 47 | return array_unique($e_perms); |
||
| 48 | } |
||
| 49 | |||
| 50 | function get_role_info() { |
||
|
|
|||
| 51 | $perms = []; |
||
| 52 | |||
| 53 | foreach ($this->default_rp as $role => $value) { |
||
| 54 | $perms[$role] = $this->extend_permissions($value); |
||
| 55 | } |
||
| 56 | |||
| 57 | return $perms; |
||
| 58 | } |
||
| 59 | |||
| 60 | function add_user_role($file_id, $user, $role) { |
||
| 61 | // Add admin user for a file |
||
| 62 | if ($file_id === false || !$user) { |
||
| 63 | return false; |
||
| 64 | } |
||
| 65 | return $this->db_lib->prepared_execute( |
||
| 66 | $this->DB->jugaad, |
||
| 67 | "INSERT INTO `file_permissions` (`file_id`, `user`, `role`) VALUES (?, ?, ?)", |
||
| 68 | "iss", |
||
| 69 | [$file_id, $user, $role], |
||
| 70 | false |
||
| 71 | ); |
||
| 72 | } |
||
| 73 | |||
| 74 | function remove_user_role($file_id, $user) { |
||
| 75 | // Remove admin user for a file |
||
| 76 | if ($file_id === false || !$user) { |
||
| 77 | return false; |
||
| 78 | } |
||
| 79 | return $this->db_lib->prepared_execute( |
||
| 80 | $this->DB->jugaad, |
||
| 81 | "DELETE FROM `file_permissions` WHERE `file_id`=? AND `user`=?", |
||
| 82 | "is", |
||
| 83 | [$file_id, $user], |
||
| 84 | false |
||
| 85 | ); |
||
| 86 | } |
||
| 87 | |||
| 88 | function get_default_role($file_id) { |
||
| 106 | } |
||
| 107 | |||
| 108 | function set_default_role($file_id, $role) { |
||
| 119 | ); |
||
| 120 | } |
||
| 121 | |||
| 122 | private function file_get_user_role($file_id, $user) { |
||
| 123 | // Check permissions for a single file |
||
| 124 | if ($file_id === false) { |
||
| 125 | return false; |
||
| 126 | } |
||
| 127 | $stmt = $this->db_lib->prepared_execute( |
||
| 128 | $this->DB->jugaad, |
||
| 129 | "SELECT `role` FROM `file_permissions` WHERE `file_id`=? AND `user`=?", |
||
| 130 | "is", |
||
| 131 | [$file_id, $user] |
||
| 132 | ); |
||
| 133 | if (!$stmt) { |
||
| 134 | return false; |
||
| 135 | } |
||
| 136 | if ($row = $stmt->get_result()->fetch_row()) { |
||
| 137 | return $row[0]; |
||
| 138 | } |
||
| 139 | return false; |
||
| 140 | } |
||
| 141 | |||
| 142 | function get_user_role($file_id, $user) { |
||
| 143 | // Check permissions for file with inherited permissions |
||
| 144 | if ($this->auth_model->is_admin($user)) { |
||
| 145 | return 'superadmin'; |
||
| 146 | } |
||
| 147 | |||
| 148 | if ($file_id === false) { |
||
| 149 | return false; |
||
| 150 | } |
||
| 151 | $orig_file_id = $file_id; |
||
| 152 | do { |
||
| 153 | $file = $this->jugaad_model->get_file($file_id); |
||
| 154 | if ($file === false) { |
||
| 155 | return false; |
||
| 156 | } |
||
| 157 | $role = $this->file_get_user_role($file_id, $user); |
||
| 158 | if ($role !== false) { |
||
| 159 | return $role; |
||
| 160 | } |
||
| 161 | $file_id = $file['parent']; |
||
| 162 | } while ($file_id >= 0); |
||
| 163 | |||
| 164 | return $this->get_default_role($orig_file_id); |
||
| 165 | } |
||
| 166 | |||
| 167 | private function file_get_users($file_id) { |
||
| 168 | // Get list of privileged users for a single file |
||
| 169 | if ($file_id === false) { |
||
| 170 | return false; |
||
| 171 | } |
||
| 172 | $stmt = $this->db_lib->prepared_execute( |
||
| 173 | $this->DB->jugaad, |
||
| 174 | "SELECT `file_id`, `user`, `role` FROM `file_permissions` WHERE `file_id`=?", |
||
| 175 | "i", |
||
| 176 | [$file_id] |
||
| 177 | ); |
||
| 178 | if (!$stmt) { |
||
| 179 | return false; |
||
| 180 | } |
||
| 181 | $user_list = $stmt->get_result()->fetch_all(MYSQLI_ASSOC); |
||
| 182 | return $user_list; |
||
| 183 | } |
||
| 184 | |||
| 185 | function get_user_list($file_id) { |
||
| 186 | // Get list of privileged users for file with inherited permissions |
||
| 187 | if ($file_id === false) { |
||
| 188 | return false; |
||
| 189 | } |
||
| 190 | $user_list = []; |
||
| 191 | do { |
||
| 192 | $file = $this->jugaad_model->get_file($file_id); |
||
| 193 | if ($file === false) { |
||
| 194 | return false; |
||
| 195 | } |
||
| 196 | $file_users = $this->file_get_users($file_id); |
||
| 197 | foreach ($file_users as $user) { |
||
| 198 | if (!isset($user_list[$user['user']])) { |
||
| 199 | $user_list[$user['user']] = $user; |
||
| 200 | } |
||
| 201 | } |
||
| 202 | $file_id = $file['parent']; |
||
| 203 | } while ($file_id >= 0); |
||
| 204 | |||
| 205 | return $user_list; |
||
| 206 | } |
||
| 207 | |||
| 208 | function get_role_permissions($role) { |
||
| 225 | } |
||
| 226 | |||
| 227 | function get_permissions($file_id, $user) { |
||
| 241 | } |
||
| 242 | |||
| 243 | } |
||
| 244 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.