1 <?php
  2   3   4   5   6   7   8 
  9 
 10 defined('JPATH_PLATFORM') or die;
 11 
 12  13  14  15  16  17  18 
 19 class JFormFieldRules extends JFormField
 20 {
 21      22  23  24  25  26 
 27     protected $type = 'Rules';
 28 
 29      30  31  32  33  34 
 35     protected $section;
 36 
 37      38  39  40  41  42 
 43     protected $component;
 44 
 45      46  47  48  49  50 
 51     protected $assetField;
 52 
 53      54  55  56  57  58  59  60  61 
 62     public function __get($name)
 63     {
 64         switch ($name)
 65         {
 66             case 'section':
 67             case 'component':
 68             case 'assetField':
 69                 return $this->$name;
 70         }
 71 
 72         return parent::__get($name);
 73     }
 74 
 75      76  77  78  79  80  81  82  83  84 
 85     public function __set($name, $value)
 86     {
 87         switch ($name)
 88         {
 89             case 'section':
 90             case 'component':
 91             case 'assetField':
 92                 $this->$name = (string) $value;
 93                 break;
 94 
 95             default:
 96                 parent::__set($name, $value);
 97         }
 98     }
 99 
100     101 102 103 104 105 106 107 108 109 110 111 112 113 
114     public function setup(SimpleXMLElement $element, $value, $group = null)
115     {
116         $return = parent::setup($element, $value, $group);
117 
118         if ($return)
119         {
120             $this->section    = $this->element['section'] ? (string) $this->element['section'] : '';
121             $this->component  = $this->element['component'] ? (string) $this->element['component'] : '';
122             $this->assetField = $this->element['asset_field'] ? (string) $this->element['asset_field'] : 'asset_id';
123         }
124 
125         return $return;
126     }
127 
128     129 130 131 132 133 134 135 136 
137     protected function getInput()
138     {
139         JHtml::_('bootstrap.tooltip');
140 
141         
142         JHtml::_('script', 'system/permissions.js', array('version' => 'auto', 'relative' => true));
143 
144         
145         JText::script('ERROR');
146         JText::script('WARNING');
147         JText::script('NOTICE');
148         JText::script('MESSAGE');
149 
150         
151         JText::script('JLIB_JS_AJAX_ERROR_CONNECTION_ABORT');
152         JText::script('JLIB_JS_AJAX_ERROR_NO_CONTENT');
153         JText::script('JLIB_JS_AJAX_ERROR_OTHER');
154         JText::script('JLIB_JS_AJAX_ERROR_PARSE');
155         JText::script('JLIB_JS_AJAX_ERROR_TIMEOUT');
156 
157         
158         $section    = $this->section;
159         $assetField = $this->assetField;
160         $component  = empty($this->component) ? 'root.1' : $this->component;
161 
162         
163         $isGlobalConfig = $component === 'root.1';
164 
165         
166         $actions = JAccess::getActions($component, $section);
167 
168         
169         foreach ($this->element->children() as $el)
170         {
171             if ($el->getName() == 'action')
172             {
173                 $actions[] = (object) array(
174                     'name' => (string) $el['name'],
175                     'title' => (string) $el['title'],
176                     'description' => (string) $el['description'],
177                 );
178             }
179         }
180 
181         
182         
183         $assetId       = $this->form->getValue($assetField);
184         $newItem       = empty($assetId) && $isGlobalConfig === false && $section !== 'component';
185         $parentAssetId = null;
186 
187         
188         if (empty($assetId))
189         {
190             
191             $db = JFactory::getDbo();
192             $query = $db->getQuery(true)
193                 ->select($db->quoteName('id'))
194                 ->from($db->quoteName('#__assets'))
195                 ->where($db->quoteName('name') . ' = ' . $db->quote($component));
196 
197             $db->setQuery($query);
198 
199             $assetId = (int) $db->loadResult();
200 
201             202 203 204 205 206 
207         }
208 
209         
210         if (!$isGlobalConfig)
211         {
212             
213             $db = JFactory::getDbo();
214 
215             $query = $db->getQuery(true)
216                 ->select($db->quoteName('parent_id'))
217                 ->from($db->quoteName('#__assets'))
218                 ->where($db->quoteName('id') . ' = ' . $assetId);
219 
220             $db->setQuery($query);
221 
222             $parentAssetId = (int) $db->loadResult();
223         }
224 
225         
226 
227         
228         $assetRules = JAccess::getAssetRules($assetId, false, false);
229 
230         
231         $groups = $this->getUserGroups();
232 
233         
234         $ajaxUri = JRoute::_('index.php?option=com_config&task=config.store&format=json&' . JSession::getFormToken() . '=1');
235 
236         
237         $html = array();
238 
239         
240         $html[] = '<p class="rule-desc">' . JText::_('JLIB_RULES_SETTINGS_DESC') . '</p>';
241 
242         
243         $html[] = '<div class="tabbable tabs-left" data-ajaxuri="' . $ajaxUri . '" id="permissions-sliders">';
244 
245         
246         $html[] = '<ul class="nav nav-tabs">';
247 
248         foreach ($groups as $group)
249         {
250             
251             $active = (int) $group->value === 1 ? ' class="active"' : '';
252 
253             $html[] = '<li' . $active . '>';
254             $html[] = '<a href="#permission-' . $group->value . '" data-toggle="tab">';
255             $html[] = JLayoutHelper::render('joomla.html.treeprefix', array('level' => $group->level + 1)) . $group->text;
256             $html[] = '</a>';
257             $html[] = '</li>';
258         }
259 
260         $html[] = '</ul>';
261 
262         $html[] = '<div class="tab-content">';
263 
264         
265         foreach ($groups as $group)
266         {
267             
268             $active = (int) $group->value === 1 ? ' active' : '';
269 
270             $html[] = '<div class="tab-pane' . $active . '" id="permission-' . $group->value . '">';
271             $html[] = '<table class="table table-striped">';
272             $html[] = '<thead>';
273             $html[] = '<tr>';
274 
275             $html[] = '<th class="actions" id="actions-th' . $group->value . '">';
276             $html[] = '<span class="acl-action">' . JText::_('JLIB_RULES_ACTION') . '</span>';
277             $html[] = '</th>';
278 
279             $html[] = '<th class="settings" id="settings-th' . $group->value . '">';
280             $html[] = '<span class="acl-action">' . JText::_('JLIB_RULES_SELECT_SETTING') . '</span>';
281             $html[] = '</th>';
282 
283             $html[] = '<th id="aclactionth' . $group->value . '">';
284             $html[] = '<span class="acl-action">' . JText::_('JLIB_RULES_CALCULATED_SETTING') . '</span>';
285             $html[] = '</th>';
286 
287             $html[] = '</tr>';
288             $html[] = '</thead>';
289             $html[] = '<tbody>';
290 
291             
292             $isSuperUserGroup = JAccess::checkGroup($group->value, 'core.admin');
293 
294             foreach ($actions as $action)
295             {
296                 $html[] = '<tr>';
297                 $html[] = '<td headers="actions-th' . $group->value . '">';
298                 $html[] = '<label for="' . $this->id . '_' . $action->name . '_' . $group->value . '" class="hasTooltip" title="'
299                     . JHtml::_('tooltipText', $action->title, $action->description) . '">';
300                 $html[] = JText::_($action->title);
301                 $html[] = '</label>';
302                 $html[] = '</td>';
303 
304                 $html[] = '<td headers="settings-th' . $group->value . '">';
305 
306                 $html[] = '<select onchange="sendPermissions.call(this, event)" data-chosen="true" class="input-small novalidate"'
307                     . ' name="' . $this->name . '[' . $action->name . '][' . $group->value . ']"'
308                     . ' id="' . $this->id . '_' . $action->name . '_' . $group->value . '"'
309                     . ' title="' . strip_tags(JText::sprintf('JLIB_RULES_SELECT_ALLOW_DENY_GROUP', JText::_($action->title), trim($group->text))) . '">';
310 
311                 312 313 314 315 316 
317 
318                 
319                 $assetRule = $newItem === false ? $assetRules->allow($action->name, $group->value) : null;
320 
321                 
322 
323                 
324                 $html[] = '<option value=""' . ($assetRule === null ? ' selected="selected"' : '') . '>'
325                     . JText::_(empty($group->parent_id) && $isGlobalConfig ? 'JLIB_RULES_NOT_SET' : 'JLIB_RULES_INHERITED') . '</option>';
326                 $html[] = '<option value="1"' . ($assetRule === true ? ' selected="selected"' : '') . '>' . JText::_('JLIB_RULES_ALLOWED')
327                     . '</option>';
328                 $html[] = '<option value="0"' . ($assetRule === false ? ' selected="selected"' : '') . '>' . JText::_('JLIB_RULES_DENIED')
329                     . '</option>';
330 
331                 $html[] = '</select>  ';
332 
333                 $html[] = '<span id="icon_' . $this->id . '_' . $action->name . '_' . $group->value . '"' . '></span>';
334                 $html[] = '</td>';
335 
336                 
337                 $html[] = '<td headers="aclactionth' . $group->value . '">';
338 
339                 $result = array();
340 
341                 
342                 $inheritedGroupRule            = JAccess::checkGroup((int) $group->value, $action->name, $assetId);
343                 $inheritedGroupParentAssetRule = !empty($parentAssetId) ? JAccess::checkGroup($group->value, $action->name, $parentAssetId) : null;
344                 $inheritedParentGroupRule      = !empty($group->parent_id) ? JAccess::checkGroup($group->parent_id, $action->name, $assetId) : null;
345 
346                 
347                 if ($isSuperUserGroup)
348                 {
349                     $result['class'] = 'label label-success';
350                     $result['text'] = '<span class="icon-lock icon-white"></span>' . JText::_('JLIB_RULES_ALLOWED_ADMIN');
351                 }
352                 
353                 else
354                 {
355                     
356 
357                     
358                     if ($inheritedGroupRule === null || $inheritedGroupRule === false)
359                     {
360                         $result['class'] = 'label label-important';
361                         $result['text']  = JText::_('JLIB_RULES_NOT_ALLOWED_INHERITED');
362                     }
363                     
364                     else
365                     {
366                         $result['class'] = 'label label-success';
367                         $result['text']  = JText::_('JLIB_RULES_ALLOWED_INHERITED');
368                     }
369 
370                     
371 
372                     373 374 375 376 
377 
378                     
379                     if ($assetRule === false)
380                     {
381                         $result['class'] = 'label label-important';
382                         $result['text']  = JText::_('JLIB_RULES_NOT_ALLOWED');
383                     }
384                     
385                     elseif ($assetRule === true)
386                     {
387                         $result['class'] = 'label label-success';
388                         $result['text']  = JText::_('JLIB_RULES_ALLOWED');
389                     }
390 
391                     
392 
393                     
394                     if (empty($group->parent_id) && $isGlobalConfig === true && $assetRule === null)
395                     {
396                         $result['class'] = 'label label-important';
397                         $result['text']  = JText::_('JLIB_RULES_NOT_ALLOWED_DEFAULT');
398                     }
399 
400                     401 402 403 404 
405                     elseif ($inheritedGroupParentAssetRule === false || $inheritedParentGroupRule === false)
406                     {
407                         $result['class'] = 'label label-important';
408                         $result['text']  = '<span class="icon-lock icon-white"></span>' . JText::_('JLIB_RULES_NOT_ALLOWED_LOCKED');
409                     }
410                 }
411 
412                 $html[] = '<span class="' . $result['class'] . '">' . $result['text'] . '</span>';
413                 $html[] = '</td>';
414                 $html[] = '</tr>';
415             }
416 
417             $html[] = '</tbody>';
418             $html[] = '</table></div>';
419         }
420 
421         $html[] = '</div></div>';
422         $html[] = '<div class="clr"></div>';
423         $html[] = '<div class="alert">';
424 
425         if ($section === 'component' || !$section)
426         {
427             $html[] = JText::_('JLIB_RULES_SETTING_NOTES');
428         }
429         else
430         {
431             $html[] = JText::_('JLIB_RULES_SETTING_NOTES_ITEM');
432         }
433 
434         $html[] = '</div>';
435 
436         return implode("\n", $html);
437     }
438 
439     440 441 442 443 444 445 
446     protected function getUserGroups()
447     {
448         $options = JHelperUsergroups::getInstance()->getAll();
449 
450         foreach ($options as &$option)
451         {
452             $option->value = $option->id;
453             $option->text  = $option->title;
454         }
455 
456         return array_values($options);
457     }
458 }
459