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:
| 1 | <?php |
||
| 19 | class SimpleRbacAuthorize extends BaseAuthorize { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Default config for this object. |
||
| 23 | * |
||
| 24 | * - `roleField` - The name of the role field in the user data array that is passed to authorize() |
||
| 25 | * |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | public $_defaultConfig = array( |
||
| 29 | 'roleField' => 'role', |
||
| 30 | 'allowEmptyActionMap' => false, |
||
| 31 | 'allowEmptyPrefixMap' => true, |
||
| 32 | 'undefinedActionsAreAllowed' => false |
||
| 33 | ); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Authorize a user based on his roles |
||
| 37 | * |
||
| 38 | * @param array $user The user to authorize |
||
| 39 | * @param Request $request The request needing authorization. |
||
| 40 | * @return boolean |
||
| 41 | * @throws RuntimeException when the role field does not exist |
||
| 42 | */ |
||
| 43 | public function authorize($user, Request $request) { |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Checks if a role is granted access to a controller and action |
||
| 67 | * |
||
| 68 | * @param array $user |
||
| 69 | * @param Request $request |
||
| 70 | * @return boolean |
||
| 71 | */ |
||
| 72 | public function authorizeByControllerAndAction($user, Request $request) { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Checks if a role is allowed. |
||
| 98 | * |
||
| 99 | * @param array|string $userRoles |
||
| 100 | * @param array $allowedRoles |
||
| 101 | * @return boolean |
||
| 102 | */ |
||
| 103 | protected function _isAllowedRole($userRoles, array $allowedRoles) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Checks if a role is granted access to a prefix route like /admin. |
||
| 120 | * |
||
| 121 | * @param array $roles |
||
| 122 | * @param Request $request |
||
| 123 | * @return boolean |
||
| 124 | */ |
||
| 125 | public function authorizeByPrefix(array $roles, Request $request) { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Gets the controller and action, prefixes the controller with the plugin if there is one |
||
| 139 | * |
||
| 140 | * @param Request $request |
||
| 141 | * @return array |
||
| 142 | */ |
||
| 143 | public function getControllerNameAndAction(Request $request) { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Can be overridden if inherited with a method to fetch this from anywhere, a database for example. |
||
| 156 | * |
||
| 157 | * @return array |
||
| 158 | * @throws RuntimeException |
||
| 159 | */ |
||
| 160 | View Code Duplication | public function getActionMap() { |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Can be overriden if inherited with a method to fetch this from anywhere, a database for exaple |
||
| 170 | * |
||
| 171 | * @return array |
||
| 172 | * @throws \RuntimeException |
||
| 173 | */ |
||
| 174 | View Code Duplication | public function getPrefixMap() { |
|
| 181 | } |
||
| 182 |