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 |
||
| 33 | class SecurityAdmin extends LeftAndMain implements PermissionProvider { |
||
| 34 | |||
| 35 | private static $url_segment = 'security'; |
||
|
|
|||
| 36 | |||
| 37 | private static $url_rule = '/$Action/$ID/$OtherID'; |
||
| 38 | |||
| 39 | private static $menu_title = 'Security'; |
||
| 40 | |||
| 41 | private static $tree_class = 'SilverStripe\\Security\\Group'; |
||
| 42 | |||
| 43 | private static $subitem_class = 'SilverStripe\\Security\\Member'; |
||
| 44 | |||
| 45 | private static $required_permission_codes = 'CMS_ACCESS_SecurityAdmin'; |
||
| 46 | |||
| 47 | private static $allowed_actions = array( |
||
| 48 | 'EditForm', |
||
| 49 | 'MemberImportForm', |
||
| 50 | 'memberimport', |
||
| 51 | 'GroupImportForm', |
||
| 52 | 'groupimport', |
||
| 53 | 'groups', |
||
| 54 | 'users', |
||
| 55 | 'roles' |
||
| 56 | ); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Shortcut action for setting the correct active tab. |
||
| 60 | * |
||
| 61 | * @param HTTPRequest $request |
||
| 62 | * @return HTTPResponse |
||
| 63 | */ |
||
| 64 | public function users($request) { |
||
| 65 | return $this->index($request); |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Shortcut action for setting the correct active tab. |
||
| 70 | * |
||
| 71 | * @param HTTPRequest $request |
||
| 72 | * @return HTTPResponse |
||
| 73 | */ |
||
| 74 | public function groups($request) { |
||
| 75 | return $this->index($request); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Shortcut action for setting the correct active tab. |
||
| 80 | * |
||
| 81 | * @param HTTPRequest $request |
||
| 82 | * @return HTTPResponse |
||
| 83 | */ |
||
| 84 | public function roles($request) { |
||
| 85 | return $this->index($request); |
||
| 86 | } |
||
| 87 | |||
| 88 | public function getEditForm($id = null, $fields = null) { |
||
| 89 | // TODO Duplicate record fetching (see parent implementation) |
||
| 90 | if(!$id) $id = $this->currentPageID(); |
||
| 91 | $form = parent::getEditForm($id); |
||
| 92 | |||
| 93 | // TODO Duplicate record fetching (see parent implementation) |
||
| 94 | $record = $this->getRecord($id); |
||
| 95 | |||
| 96 | if($record && !$record->canView()) { |
||
| 97 | return Security::permissionFailure($this); |
||
| 98 | } |
||
| 99 | |||
| 100 | $memberList = GridField::create( |
||
| 101 | 'Members', |
||
| 102 | false, |
||
| 103 | Member::get(), |
||
| 104 | $memberListConfig = GridFieldConfig_RecordEditor::create() |
||
| 105 | ->addComponent(new GridFieldButtonRow('after')) |
||
| 106 | ->addComponent(new GridFieldExportButton('buttons-after-left')) |
||
| 107 | )->addExtraClass("members_grid"); |
||
| 108 | |||
| 109 | if($record && method_exists($record, 'getValidator')) { |
||
| 110 | $validator = $record->getValidator(); |
||
| 111 | } else { |
||
| 112 | $validator = Member::singleton()->getValidator(); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** @var GridFieldDetailForm $detailForm */ |
||
| 116 | $detailForm = $memberListConfig->getComponentByType('SilverStripe\\Forms\\GridField\\GridFieldDetailForm'); |
||
| 117 | $detailForm |
||
| 118 | ->setValidator($validator); |
||
| 119 | |||
| 120 | $groupList = GridField::create( |
||
| 121 | 'Groups', |
||
| 122 | false, |
||
| 123 | Group::get(), |
||
| 124 | GridFieldConfig_RecordEditor::create() |
||
| 125 | ); |
||
| 126 | /** @var GridFieldDataColumns $columns */ |
||
| 127 | $columns = $groupList->getConfig()->getComponentByType('SilverStripe\\Forms\\GridField\\GridFieldDataColumns'); |
||
| 128 | $columns->setDisplayFields(array( |
||
| 129 | 'Breadcrumbs' => Group::singleton()->fieldLabel('Title') |
||
| 130 | )); |
||
| 131 | $columns->setFieldFormatting(array( |
||
| 132 | 'Breadcrumbs' => function($val, $item) { |
||
| 133 | /** @var Group $item */ |
||
| 134 | return Convert::raw2xml($item->getBreadcrumbs(' > ')); |
||
| 135 | } |
||
| 136 | )); |
||
| 137 | |||
| 138 | $fields = new FieldList( |
||
| 139 | $root = new TabSet( |
||
| 140 | 'Root', |
||
| 141 | $usersTab = new Tab('Users', _t('SecurityAdmin.Users', 'Users'), |
||
| 142 | |||
| 143 | new LiteralField('MembersCautionText', |
||
| 144 | sprintf('<div class="alert alert-warning" role="alert">%s</div>', |
||
| 145 | _t( |
||
| 146 | 'SecurityAdmin.MemberListCaution', |
||
| 147 | 'Caution: Removing members from this list will remove them from all groups and the database' |
||
| 148 | ) |
||
| 149 | ) |
||
| 150 | ), |
||
| 151 | $memberList |
||
| 152 | ), |
||
| 153 | $groupsTab = new Tab('Groups', singleton('SilverStripe\\Security\\Group')->i18n_plural_name(), |
||
| 154 | $groupList |
||
| 155 | ) |
||
| 156 | ), |
||
| 157 | // necessary for tree node selection in LeftAndMain.EditForm.js |
||
| 158 | new HiddenField('ID', false, 0) |
||
| 159 | ); |
||
| 160 | |||
| 161 | // Add import capabilities. Limit to admin since the import logic can affect assigned permissions |
||
| 162 | if(Permission::check('ADMIN')) { |
||
| 163 | $fields->addFieldsToTab('Root.Users', array( |
||
| 164 | new HeaderField('ImportUsersHeader', _t('SecurityAdmin.IMPORTUSERS', 'Import users'), 3), |
||
| 165 | new LiteralField( |
||
| 166 | 'MemberImportFormIframe', |
||
| 167 | sprintf( |
||
| 168 | '<iframe src="%s" id="MemberImportFormIframe" width="100%%" height="250px" frameBorder="0">' |
||
| 169 | . '</iframe>', |
||
| 170 | $this->Link('memberimport') |
||
| 171 | ) |
||
| 172 | ) |
||
| 173 | )); |
||
| 174 | $fields->addFieldsToTab('Root.Groups', array( |
||
| 175 | new HeaderField('ImportGroupsHeader', _t('SecurityAdmin.IMPORTGROUPS', 'Import groups'), 3), |
||
| 176 | new LiteralField( |
||
| 177 | 'GroupImportFormIframe', |
||
| 178 | sprintf( |
||
| 179 | '<iframe src="%s" id="GroupImportFormIframe" width="100%%" height="250px" frameBorder="0">' |
||
| 180 | . '</iframe>', |
||
| 181 | $this->Link('groupimport') |
||
| 182 | ) |
||
| 183 | ) |
||
| 184 | )); |
||
| 185 | } |
||
| 186 | |||
| 187 | // Tab nav in CMS is rendered through separate template |
||
| 188 | $root->setTemplate('SilverStripe\\Forms\\CMSTabSet'); |
||
| 189 | |||
| 190 | // Add roles editing interface |
||
| 191 | $rolesTab = null; |
||
| 192 | if(Permission::check('APPLY_ROLES')) { |
||
| 193 | $rolesField = GridField::create('Roles', |
||
| 194 | false, |
||
| 195 | PermissionRole::get(), |
||
| 196 | GridFieldConfig_RecordEditor::create() |
||
| 197 | ); |
||
| 198 | |||
| 199 | $rolesTab = $fields->findOrMakeTab('Root.Roles', _t('SecurityAdmin.TABROLES', 'Roles')); |
||
| 200 | $rolesTab->push($rolesField); |
||
| 201 | } |
||
| 202 | |||
| 203 | $actionParam = $this->getRequest()->param('Action'); |
||
| 204 | if($actionParam == 'groups') { |
||
| 205 | $groupsTab->addExtraClass('ui-state-active'); |
||
| 206 | } elseif($actionParam == 'users') { |
||
| 207 | $usersTab->addExtraClass('ui-state-active'); |
||
| 208 | } elseif($actionParam == 'roles' && $rolesTab) { |
||
| 209 | $rolesTab->addExtraClass('ui-state-active'); |
||
| 210 | } |
||
| 211 | |||
| 212 | $actions = new FieldList(); |
||
| 213 | |||
| 214 | $form = Form::create( |
||
| 215 | $this, |
||
| 216 | 'EditForm', |
||
| 217 | $fields, |
||
| 218 | $actions |
||
| 219 | )->setHTMLID('Form_EditForm'); |
||
| 220 | $form->addExtraClass('cms-edit-form'); |
||
| 221 | $form->setTemplate($this->getTemplatesWithSuffix('_EditForm')); |
||
| 222 | // Tab nav in CMS is rendered through separate template |
||
| 223 | if($form->Fields()->hasTabSet()) { |
||
| 224 | $form->Fields()->findOrMakeTab('Root')->setTemplate('SilverStripe\\Forms\\CMSTabSet'); |
||
| 225 | } |
||
| 226 | $form->addExtraClass('center ss-tabset cms-tabset ' . $this->BaseCSSClasses()); |
||
| 227 | $form->setAttribute('data-pjax-fragment', 'CurrentForm'); |
||
| 228 | |||
| 229 | $this->extend('updateEditForm', $form); |
||
| 230 | |||
| 231 | return $form; |
||
| 232 | } |
||
| 233 | |||
| 234 | View Code Duplication | public function memberimport() { |
|
| 235 | Requirements::clear(); |
||
| 236 | Requirements::javascript(FRAMEWORK_ADMIN_DIR . '/client/dist/js/bundle-lib.js'); |
||
| 237 | Requirements::javascript(FRAMEWORK_ADMIN_DIR . '/client/dist/js/MemberImportForm.js'); |
||
| 238 | Requirements::css(FRAMEWORK_ADMIN_DIR . '/client/dist/styles/bundle.css'); |
||
| 239 | |||
| 240 | return $this->renderWith('BlankPage', array( |
||
| 241 | 'Form' => $this->MemberImportForm()->forTemplate(), |
||
| 242 | 'Content' => ' ' |
||
| 243 | )); |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @see SecurityAdmin_MemberImportForm |
||
| 248 | * |
||
| 249 | * @return Form |
||
| 250 | */ |
||
| 251 | public function MemberImportForm() { |
||
| 252 | if(!Permission::check('ADMIN')) { |
||
| 253 | return null; |
||
| 254 | } |
||
| 255 | |||
| 256 | /** @var Group $group */ |
||
| 257 | $group = $this->currentPage(); |
||
| 258 | /** @skipUpgrade */ |
||
| 259 | $form = new MemberImportForm( |
||
| 260 | $this, |
||
| 261 | 'MemberImportForm' |
||
| 262 | ); |
||
| 263 | $form->setGroup($group); |
||
| 264 | |||
| 265 | return $form; |
||
| 266 | } |
||
| 267 | |||
| 268 | View Code Duplication | public function groupimport() { |
|
| 279 | |||
| 280 | /** |
||
| 281 | * @see SecurityAdmin_MemberImportForm |
||
| 282 | * |
||
| 283 | * @skipUpgrade |
||
| 284 | * @return Form |
||
| 285 | */ |
||
| 286 | public function GroupImportForm() { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Disable GridFieldDetailForm backlinks for this view, as its |
||
| 297 | */ |
||
| 298 | public function Backlink() { |
||
| 301 | |||
| 302 | public function Breadcrumbs($unlinked = false) { |
||
| 335 | |||
| 336 | public function providePermissions() { |
||
| 364 | } |
||
| 365 |