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 SecurityComponent 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 SecurityComponent, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class SecurityComponent extends Component{ |
||
|
|
|||
| 13 | |||
| 14 | /** |
||
| 15 | * Default configurations |
||
| 16 | * |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | protected $config = [ |
||
| 20 | 'form' => [], |
||
| 21 | 'requireSecure' => [], |
||
| 22 | 'requirePost' => [], |
||
| 23 | 'requireAjax' => [], |
||
| 24 | 'requireGet' => [], |
||
| 25 | 'validateForm' => true, |
||
| 26 | 'validateCsrfToken' => false |
||
| 27 | ]; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Auth startup |
||
| 31 | * All security checking are done in this method |
||
| 32 | * |
||
| 33 | */ |
||
| 34 | public function startup(){ |
||
| 35 | |||
| 36 | $this->requestRequired(); |
||
| 37 | $this->secureRequired(); |
||
| 38 | |||
| 39 | $this->validateDomain(); |
||
| 40 | |||
| 41 | if($this->request->isPost() && $this->config["validateForm"]){ |
||
| 42 | if(!$this->form($this->config["form"])){ |
||
| 43 | $this->invalidRequest(); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | |||
| 47 | if($this->config["validateCsrfToken"]){ |
||
| 48 | if(!$this->CsrfToken()){ |
||
| 49 | $this->invalidRequest(); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Check & validate from the required HTTP methods, like: Post, Ajax, Get |
||
| 56 | * |
||
| 57 | * If invalid, this will fire invalid request error. |
||
| 58 | * |
||
| 59 | */ |
||
| 60 | private function requestRequired(){ |
||
| 61 | foreach (['Post', 'Ajax', 'Get'] as $method) { |
||
| 62 | $key = 'require' . $method; |
||
| 63 | View Code Duplication | if (!empty($this->config[$key])) { |
|
| 64 | if (in_array($this->request->param('action'), $this->config[$key], true) || $this->config[$key] === ['*']) { |
||
| 65 | if (!$this->request->{"is" . $method}()) { |
||
| 66 | $this->invalidRequest(); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | } |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Check & validate if secured connection is required. |
||
| 75 | * |
||
| 76 | * It calls forceSSL() method in the controller |
||
| 77 | * |
||
| 78 | */ |
||
| 79 | private function secureRequired(){ |
||
| 80 | $key = "requireSecure"; |
||
| 81 | View Code Duplication | if(!empty($this->config[$key])){ |
|
| 82 | if (in_array($this->request->param('action'), $this->config[$key], true) || $this->config[$key] === ['*']) { |
||
| 83 | if (!$this->request->isSSL()) { |
||
| 84 | $this->controller->forceSSL(); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Check & validate if request is coming from the same domain; if equals to $this->request->host() |
||
| 92 | * HTTP referer tells the domain where the request came from. |
||
| 93 | * |
||
| 94 | */ |
||
| 95 | private function validateDomain(){ |
||
| 96 | |||
| 97 | $isValid = true; |
||
| 98 | $referer = $this->request->referer(); |
||
| 99 | |||
| 100 | if($this->request->isPost()){ |
||
| 101 | if(!isset($referer)) { |
||
| 102 | $isValid = false; |
||
| 103 | } else { |
||
| 104 | $referer_host = parse_url($referer, PHP_URL_HOST); |
||
| 105 | $server_host = $this->request->host(); |
||
| 106 | $isValid = ($referer_host === $server_host)? true: false; |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | if(!$isValid){ |
||
| 111 | Logger::log("Request Domain", "User: ". Session::getUserId() ." Request is not coming from the same domain with invalid HTTP referer", __FILE__, __LINE__); |
||
| 112 | $this->invalidRequest(); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Handles invalid request with a 400 Bad Request Error |
||
| 118 | * |
||
| 119 | */ |
||
| 120 | private function invalidRequest(){ |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Sets the actions that require secured connection(SSL) |
||
| 126 | * |
||
| 127 | * @param array $actions |
||
| 128 | */ |
||
| 129 | public function requireSecure($actions = []){ |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Sets the actions that require a POST request |
||
| 135 | * |
||
| 136 | * @param array $actions |
||
| 137 | */ |
||
| 138 | public function requirePost($actions = []){ |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Sets the actions that require a Ajax request |
||
| 144 | * |
||
| 145 | * @param array $actions |
||
| 146 | */ |
||
| 147 | public function requireAjax($actions = []){ |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Sets the actions that require a GET request |
||
| 153 | * |
||
| 154 | * @param array $actions |
||
| 155 | */ |
||
| 156 | public function requireGet($actions = []){ |
||
| 159 | |||
| 160 | /** |
||
| 161 | * validate submitted form |
||
| 162 | * - Unknown fields cannot be added to the form. |
||
| 163 | * - Fields cannot be removed from the form. |
||
| 164 | * |
||
| 165 | * Use $exclude to exclude anything mightn't be sent with the form, like possible empty arrays, checkboxes, radio buttons, ...etc. |
||
| 166 | * By default, the submit field will be excluded. |
||
| 167 | * |
||
| 168 | * @param array $config configuration data |
||
| 169 | * @return boolean |
||
| 170 | */ |
||
| 171 | public function form($config){ |
||
| 203 | |||
| 204 | /** |
||
| 205 | * validate CSRF token |
||
| 206 | * CSRF token can be passed with submitted forms and links associated with sensitive server-side operations. |
||
| 207 | * |
||
| 208 | * In case of GET request, you need to set 'validateCsrfToken' in $config to true. |
||
| 209 | * |
||
| 210 | * @param array $config configuration data |
||
| 211 | * @return boolean |
||
| 212 | */ |
||
| 213 | public function CsrfToken($config = []){ |
||
| 229 | |||
| 230 | } |
||
| 231 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.