Conditions | 49 |
Paths | 3408 |
Total Lines | 195 |
Code Lines | 124 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
92 | public function Field($properties = array()) |
||
93 | { |
||
94 | $uninheritedCodes = array(); |
||
95 | $inheritedCodes = array(); |
||
96 | $records = ($this->records) ? $this->records : new ArrayList(); |
||
97 | |||
98 | // Get existing values from the form record (assuming the formfield name is a join field on the record) |
||
99 | if (is_object($this->form)) { |
||
100 | $record = $this->form->getRecord(); |
||
101 | if ($record |
||
102 | && ($record instanceof Group || $record instanceof PermissionRole) |
||
103 | && !$records->find('ID', $record->ID) |
||
104 | ) { |
||
105 | $records->push($record); |
||
106 | } |
||
107 | } |
||
108 | |||
109 | // Get all 'inherited' codes not directly assigned to the group (which is stored in $values) |
||
110 | foreach ($records as $record) { |
||
111 | // Get all uninherited permissions |
||
112 | $relationMethod = $this->name; |
||
113 | foreach ($record->$relationMethod() as $permission) { |
||
114 | if (!isset($uninheritedCodes[$permission->Code])) { |
||
115 | $uninheritedCodes[$permission->Code] = array(); |
||
116 | } |
||
117 | $uninheritedCodes[$permission->Code][] = _t( |
||
118 | 'SilverStripe\\Security\\PermissionCheckboxSetField.AssignedTo', |
||
119 | 'assigned to "{title}"', |
||
120 | array('title' => $record->dbObject('Title')->forTemplate()) |
||
121 | ); |
||
122 | } |
||
123 | |||
124 | // Special case for Group records (not PermissionRole): |
||
125 | // Determine inherited assignments |
||
126 | if ($record instanceof Group) { |
||
127 | // Get all permissions from roles |
||
128 | if ($record->Roles()->count()) { |
||
129 | foreach ($record->Roles() as $role) { |
||
130 | /** @var PermissionRole $role */ |
||
131 | foreach ($role->Codes() as $code) { |
||
132 | if (!isset($inheritedCodes[$code->Code])) { |
||
133 | $inheritedCodes[$code->Code] = array(); |
||
134 | } |
||
135 | $inheritedCodes[$code->Code][] = _t( |
||
136 | 'SilverStripe\\Security\\PermissionCheckboxSetField.FromRole', |
||
137 | 'inherited from role "{title}"', |
||
138 | 'A permission inherited from a certain permission role', |
||
139 | array('title' => $role->dbObject('Title')->forTemplate()) |
||
140 | ); |
||
141 | } |
||
142 | } |
||
143 | } |
||
144 | |||
145 | // Get from parent groups |
||
146 | $parentGroups = $record->getAncestors(); |
||
147 | if ($parentGroups) { |
||
148 | foreach ($parentGroups as $parent) { |
||
149 | if (!$parent->Roles()->Count()) { |
||
150 | continue; |
||
151 | } |
||
152 | foreach ($parent->Roles() as $role) { |
||
153 | if ($role->Codes()) { |
||
154 | foreach ($role->Codes() as $code) { |
||
155 | if (!isset($inheritedCodes[$code->Code])) { |
||
156 | $inheritedCodes[$code->Code] = array(); |
||
157 | } |
||
158 | $inheritedCodes[$code->Code][] = _t( |
||
159 | 'SilverStripe\\Security\\PermissionCheckboxSetField.FromRoleOnGroup', |
||
160 | 'inherited from role "{roletitle}" on group "{grouptitle}"', |
||
161 | 'A permission inherited from a role on a certain group', |
||
162 | array( |
||
163 | 'roletitle' => $role->dbObject('Title')->forTemplate(), |
||
164 | 'grouptitle' => $parent->dbObject('Title')->forTemplate() |
||
165 | ) |
||
166 | ); |
||
167 | } |
||
168 | } |
||
169 | } |
||
170 | if ($parent->Permissions()->Count()) { |
||
171 | foreach ($parent->Permissions() as $permission) { |
||
172 | if (!isset($inheritedCodes[$permission->Code])) { |
||
173 | $inheritedCodes[$permission->Code] = array(); |
||
174 | } |
||
175 | $inheritedCodes[$permission->Code][] = |
||
176 | _t( |
||
177 | 'SilverStripe\\Security\\PermissionCheckboxSetField.FromGroup', |
||
178 | 'inherited from group "{title}"', |
||
179 | 'A permission inherited from a certain group', |
||
180 | array('title' => $parent->dbObject('Title')->forTemplate()) |
||
181 | ); |
||
182 | } |
||
183 | } |
||
184 | } |
||
185 | } |
||
186 | } |
||
187 | } |
||
188 | |||
189 | $odd = 0; |
||
190 | $options = ''; |
||
191 | $globalHidden = (array)Config::inst()->get('SilverStripe\\Security\\Permission', 'hidden_permissions'); |
||
192 | if ($this->source) { |
||
193 | $privilegedPermissions = Permission::config()->privileged_permissions; |
||
194 | |||
195 | // loop through all available categorized permissions and see if they're assigned for the given groups |
||
196 | foreach ($this->source as $categoryName => $permissions) { |
||
197 | $options .= "<li><h5>$categoryName</h5></li>"; |
||
198 | foreach ($permissions as $code => $permission) { |
||
199 | if (in_array($code, $this->hiddenPermissions)) { |
||
200 | continue; |
||
201 | } |
||
202 | if (in_array($code, $globalHidden)) { |
||
203 | continue; |
||
204 | } |
||
205 | |||
206 | $value = $permission['name']; |
||
207 | |||
208 | $odd = ($odd + 1) % 2; |
||
209 | $extraClass = $odd ? 'odd' : 'even'; |
||
210 | $extraClass .= ' val' . str_replace(' ', '', $code); |
||
211 | $itemID = $this->ID() . '_' . preg_replace('/[^a-zA-Z0-9]+/', '', $code); |
||
212 | $disabled = $inheritMessage = ''; |
||
213 | $checked = (isset($uninheritedCodes[$code]) || isset($inheritedCodes[$code])) |
||
214 | ? ' checked="checked"' |
||
215 | : ''; |
||
216 | $title = $permission['help'] |
||
217 | ? 'title="' . htmlentities($permission['help'], ENT_COMPAT, 'UTF-8') . '" ' |
||
218 | : ''; |
||
219 | |||
220 | if (isset($inheritedCodes[$code])) { |
||
221 | // disable inherited codes, as any saving logic would be too complicate to express in this |
||
222 | // interface |
||
223 | $disabled = ' disabled="true"'; |
||
224 | $inheritMessage = ' (' . join(', ', $inheritedCodes[$code]) . ')'; |
||
225 | } elseif ($this->records && $this->records->Count() > 1 && isset($uninheritedCodes[$code])) { |
||
226 | // If code assignments are collected from more than one "source group", |
||
227 | // show its origin automatically |
||
228 | $inheritMessage = ' (' . join(', ', $uninheritedCodes[$code]) . ')'; |
||
229 | } |
||
230 | |||
231 | // Disallow modification of "privileged" permissions unless currently logged-in user is an admin |
||
232 | if (!Permission::check('ADMIN') && in_array($code, $privilegedPermissions)) { |
||
233 | $disabled = ' disabled="true"'; |
||
234 | } |
||
235 | |||
236 | // If the field is readonly, always mark as "disabled" |
||
237 | if ($this->readonly) { |
||
238 | $disabled = ' disabled="true"'; |
||
239 | } |
||
240 | |||
241 | $inheritMessage = '<small>' . $inheritMessage . '</small>'; |
||
242 | |||
243 | // If the field is readonly, add a span that will replace the disabled checkbox input |
||
244 | if ($this->readonly) { |
||
245 | $icon = ($checked) ? 'check-mark-circle' : 'cancel-circled'; |
||
246 | $record = is_object($this->form) ? $this->form->getRecord() : false; |
||
247 | // Inherited codes are shown as a gray x |
||
248 | if ($record && $record instanceof Member && |
||
249 | Permission::checkMember($record, 'ADMIN') && $code != 'ADMIN') { |
||
250 | $icon = 'plus-circled'; |
||
251 | } |
||
252 | |||
253 | $options .= "<li class=\"$extraClass\">" |
||
254 | . "<input id=\"$itemID\"$disabled name=\"$this->name[$code]\" type=\"checkbox\"" |
||
255 | . " value=\"$code\"$checked class=\"checkbox\" />" |
||
256 | . "<label {$title}for=\"$itemID\">" |
||
257 | . "<span class=\"font-icon-$icon\"></span>" |
||
258 | . "{$value}{$inheritMessage}</label>" |
||
259 | . "</li>\n"; |
||
260 | } else { |
||
261 | $options .= "<li class=\"$extraClass\">" |
||
262 | . "<input id=\"$itemID\"$disabled name=\"$this->name[$code]\" type=\"checkbox\"" |
||
263 | . " value=\"$code\"$checked class=\"checkbox\" />" |
||
264 | . "<label {$title}for=\"$itemID\">{$value}{$inheritMessage}</label>" |
||
265 | . "</li>\n"; |
||
266 | } |
||
267 | } |
||
268 | } |
||
269 | } |
||
270 | if ($this->readonly) { |
||
271 | $message = _t( |
||
272 | 'SilverStripe\\Security\\Permission.UserPermissionsIntro', |
||
273 | 'Assigning groups to this user will adjust the permissions they have.' |
||
274 | . ' See the groups section for details of permissions on individual groups.' |
||
275 | ); |
||
276 | |||
277 | return |
||
278 | "<ul id=\"{$this->ID()}\" class=\"optionset checkboxsetfield{$this->extraClass()}\">\n" . |
||
279 | "<li class=\"help\">" . $message . "</li>" . |
||
280 | $options . |
||
281 | "</ul>\n"; |
||
282 | } else { |
||
283 | return |
||
284 | "<ul id=\"{$this->ID()}\" class=\"optionset checkboxsetfield{$this->extraClass()}\">\n" . |
||
285 | $options . |
||
286 | "</ul>\n"; |
||
287 | } |
||
356 |