Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Permission 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Permission, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Permission extends DataObject implements TemplateGlobalProvider { |
||
16 | |||
17 | // the (1) after Type specifies the DB default value which is needed for |
||
18 | // upgrades from older SilverStripe versions |
||
19 | private static $db = array( |
||
|
|||
20 | "Code" => "Varchar(255)", |
||
21 | "Arg" => "Int", |
||
22 | "Type" => "Int(1)" |
||
23 | ); |
||
24 | private static $has_one = array( |
||
25 | "Group" => "Group" |
||
26 | ); |
||
27 | private static $indexes = array( |
||
28 | "Code" => true |
||
29 | ); |
||
30 | private static $defaults = array( |
||
31 | "Type" => 1 |
||
32 | ); |
||
33 | private static $has_many = array(); |
||
34 | |||
35 | private static $many_many = array(); |
||
36 | |||
37 | private static $belongs_many_many = array(); |
||
38 | |||
39 | /** |
||
40 | * This is the value to use for the "Type" field if a permission should be |
||
41 | * granted. |
||
42 | */ |
||
43 | const GRANT_PERMISSION = 1; |
||
44 | |||
45 | /** |
||
46 | * This is the value to use for the "Type" field if a permission should be |
||
47 | * denied. |
||
48 | */ |
||
49 | const DENY_PERMISSION = -1; |
||
50 | |||
51 | /** |
||
52 | * This is the value to use for the "Type" field if a permission should be |
||
53 | * inherited. |
||
54 | */ |
||
55 | const INHERIT_PERMISSION = 0; |
||
56 | |||
57 | |||
58 | /** |
||
59 | * Method to globally disable "strict" checking, which means a permission |
||
60 | * will be granted if the key does not exist at all. |
||
61 | * |
||
62 | * @var bool |
||
63 | */ |
||
64 | private static $declared_permissions = null; |
||
65 | |||
66 | /** |
||
67 | * Linear list of declared permissions in the system. |
||
68 | * |
||
69 | * @var array |
||
70 | */ |
||
71 | private static $declared_permissions_list = null; |
||
72 | |||
73 | /** |
||
74 | * @config |
||
75 | * @var $strict_checking Boolean Method to globally disable "strict" checking, |
||
76 | * which means a permission will be granted if the key does not exist at all. |
||
77 | */ |
||
78 | private static $strict_checking = true; |
||
79 | |||
80 | /** |
||
81 | * Set to false to prevent the 'ADMIN' permission from implying all |
||
82 | * permissions in the system |
||
83 | * |
||
84 | * @config |
||
85 | * @var bool |
||
86 | */ |
||
87 | private static $admin_implies_all = true; |
||
88 | |||
89 | /** |
||
90 | * a list of permission codes which doesn't appear in the Permission list |
||
91 | * when make the {@link PermissionCheckboxSetField} |
||
92 | * @config |
||
93 | * @var array; |
||
94 | */ |
||
95 | private static $hidden_permissions = array(); |
||
96 | |||
97 | /** |
||
98 | * @config These permissions can only be applied by ADMIN users, to prevent |
||
99 | * privilege escalation on group assignments and inheritance. |
||
100 | * @var array |
||
101 | */ |
||
102 | private static $privileged_permissions = array( |
||
103 | 'ADMIN', |
||
104 | 'APPLY_ROLES', |
||
105 | 'EDIT_PERMISSIONS' |
||
106 | ); |
||
107 | |||
108 | /** |
||
109 | * Check that the current member has the given permission. |
||
110 | * |
||
111 | * @param string|array $code Code of the permission to check (case-sensitive) |
||
112 | * @param string $arg Optional argument (e.g. a permissions for a specific page) |
||
113 | * @param int|Member $member Optional member instance or ID. If set to NULL, the permssion |
||
114 | * will be checked for the current user |
||
115 | * @param bool $strict Use "strict" checking (which means a permission |
||
116 | * will be granted if the key does not exist at all)? |
||
117 | * @return int|bool The ID of the permission record if the permission |
||
118 | * exists; FALSE otherwise. If "strict" checking is |
||
119 | * disabled, TRUE will be returned if the permission does not exist at all. |
||
120 | */ |
||
121 | public static function check($code, $arg = "any", $member = null, $strict = true) { |
||
131 | |||
132 | /** |
||
133 | * Permissions cache. The format is a map, where the keys are member IDs, and the values are |
||
134 | * arrays of permission codes. |
||
135 | */ |
||
136 | private static $cache_permissions = array(); |
||
137 | |||
138 | /** |
||
139 | * Flush the permission cache, for example if you have edited group membership or a permission record. |
||
140 | * @todo Call this whenever Group_Members is added to or removed from |
||
141 | */ |
||
142 | public static function flush_permission_cache() { |
||
145 | |||
146 | /** |
||
147 | * Check that the given member has the given permission. |
||
148 | * |
||
149 | * @param int|Member memberID The ID of the member to check. Leave blank for the current member. |
||
150 | * Alternatively you can use a member object. |
||
151 | * @param string|array $code Code of the permission to check (case-sensitive) |
||
152 | * @param string $arg Optional argument (e.g. a permissions for a specific page) |
||
153 | * @param bool $strict Use "strict" checking (which means a permission |
||
154 | * will be granted if the key does not exist at all)? |
||
155 | * @return int|bool The ID of the permission record if the permission |
||
156 | * exists; FALSE otherwise. If "strict" checking is |
||
157 | * disabled, TRUE will be returned if the permission does not exist at all. |
||
158 | */ |
||
159 | public static function checkMember($member, $code, $arg = "any", $strict = true) { |
||
274 | |||
275 | /** |
||
276 | * Get all the 'any' permission codes available to the given member. |
||
277 | * |
||
278 | * @return array |
||
279 | */ |
||
280 | public static function permissions_for_member($memberID) { |
||
311 | |||
312 | |||
313 | /** |
||
314 | * Get the list of groups that the given member belongs to. |
||
315 | * |
||
316 | * Call without an argument to get the groups that the current member |
||
317 | * belongs to. In this case, the results will be session-cached. |
||
318 | * |
||
319 | * @param int $memberID The ID of the member. Leave blank for the current |
||
320 | * member. |
||
321 | * @return array Returns a list of group IDs to which the member belongs |
||
322 | * to or NULL. |
||
323 | */ |
||
324 | public static function groupList($memberID = null) { |
||
356 | |||
357 | |||
358 | /** |
||
359 | * Grant the given permission code/arg to the given group |
||
360 | * |
||
361 | * @param int $groupID The ID of the group |
||
362 | * @param string $code The permission code |
||
363 | * @param string Optional: The permission argument (e.g. a page ID). |
||
364 | * @returns Permission Returns the new permission object. |
||
365 | */ |
||
366 | View Code Duplication | public static function grant($groupID, $code, $arg = "any") { |
|
390 | |||
391 | |||
392 | /** |
||
393 | * Deny the given permission code/arg to the given group |
||
394 | * |
||
395 | * @param int $groupID The ID of the group |
||
396 | * @param string $code The permission code |
||
397 | * @param string Optional: The permission argument (e.g. a page ID). |
||
398 | * @returns Permission Returns the new permission object. |
||
399 | */ |
||
400 | View Code Duplication | public static function deny($groupID, $code, $arg = "any") { |
|
424 | |||
425 | /** |
||
426 | * Returns all members for a specific permission. |
||
427 | * |
||
428 | * @param $code String|array Either a single permission code, or a list of permission codes |
||
429 | * @return SS_List Returns a set of member that have the specified |
||
430 | * permission. |
||
431 | */ |
||
432 | public static function get_members_by_permission($code) { |
||
454 | |||
455 | /** |
||
456 | * Return all of the groups that have one of the given permission codes |
||
457 | * @param $codes array|string Either a single permission code, or an array of permission codes |
||
458 | * @return SS_List The matching group objects |
||
459 | */ |
||
460 | public static function get_groups_by_permission($codes) { |
||
475 | |||
476 | |||
477 | /** |
||
478 | * Get a list of all available permission codes, both defined through the |
||
479 | * {@link PermissionProvider} interface, and all not explicitly defined codes existing |
||
480 | * as a {@link Permission} database record. By default, the results are |
||
481 | * grouped as denoted by {@link Permission_Group}. |
||
482 | * |
||
483 | * @param bool $grouped Group results into an array of permission groups. |
||
484 | * @return array Returns an array of all available permission codes. The |
||
485 | * array indicies are the permission codes as used in |
||
486 | * {@link Permission::check()}. The value is a description |
||
487 | * suitable for using in an interface. |
||
488 | */ |
||
489 | public static function get_codes($grouped = true) { |
||
566 | |||
567 | /** |
||
568 | * Sort permissions based on their sort value, or name |
||
569 | * |
||
570 | */ |
||
571 | public static function sort_permissions($a, $b) { |
||
580 | |||
581 | /** |
||
582 | * add a permission represented by the $code to the {@link slef::$hidden_permissions} list |
||
583 | * |
||
584 | * @deprecated 4.0 Use "Permission.hidden_permissions" config setting instead |
||
585 | * @param $code string - the permissions code |
||
586 | * @return void |
||
587 | */ |
||
588 | public static function add_to_hidden_permissions($code){ |
||
593 | |||
594 | /** |
||
595 | * remove a permission represented by the $code from the {@link slef::$hidden_permissions} list |
||
596 | * |
||
597 | * @deprecated 4.0 Use "Permission.hidden_permissions" config setting instead |
||
598 | * @param $code string - the permissions code |
||
599 | * @return void |
||
600 | */ |
||
601 | public static function remove_from_hidden_permissions($code){ |
||
606 | |||
607 | /** |
||
608 | * Declare an array of permissions for the system. |
||
609 | * |
||
610 | * Permissions can be grouped by nesting arrays. Scalar values are always |
||
611 | * treated as permissions. |
||
612 | * |
||
613 | * @deprecated 4.0 Use "Permission.declared_permissions" config setting instead |
||
614 | * @param array $permArray A (possibly nested) array of permissions to |
||
615 | * declare for the system. |
||
616 | */ |
||
617 | public static function declare_permissions($permArray) { |
||
621 | |||
622 | |||
623 | /** |
||
624 | * Get a linear list of the permissions in the system. |
||
625 | * |
||
626 | * @return array Linear list of declared permissions in the system. |
||
627 | */ |
||
628 | public static function get_declared_permissions_list() { |
||
642 | |||
643 | /** |
||
644 | * Look up the human-readable title for the permission as defined by <code>Permission::declare_permissions</code> |
||
645 | * |
||
646 | * @param $perm Permission code |
||
647 | * @return Label for the given permission, or the permission itself if the label doesn't exist |
||
648 | */ |
||
649 | public static function get_label_for_permission($perm) { |
||
654 | |||
655 | /** |
||
656 | * Recursively traverse the nested list of declared permissions and create |
||
657 | * a linear list. |
||
658 | * |
||
659 | * @param aeeay $declared Nested structure of permissions. |
||
660 | * @param $list List of permissions in the structure. The result will be |
||
661 | * written to this array. |
||
662 | */ |
||
663 | protected static function traverse_declared_permissions($declared, &$list) { |
||
676 | |||
677 | public function onBeforeWrite() { |
||
683 | |||
684 | public static function get_template_global_variables() { |
||
689 | } |
||
690 | |||
758 |