PageRenderTime 76ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/form/fields/rules.php

https://github.com/realityking/JAJAX
PHP | 294 lines | 177 code | 48 blank | 69 comment | 33 complexity | 3d32de39d0388e0d213a4cb849ceaac7 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Form
  5. *
  6. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. jimport('joomla.html.html');
  11. jimport('joomla.access.access');
  12. jimport('joomla.form.formfield');
  13. /**
  14. * Form Field class for the Joomla Platform.
  15. * Field for assigning permissions to groups for a given asset
  16. *
  17. * @package Joomla.Platform
  18. * @subpackage Form
  19. * @since 11.1
  20. *
  21. * @see JAccess
  22. */
  23. class JFormFieldRules extends JFormField
  24. {
  25. /**
  26. * The form field type.
  27. *
  28. * @var string
  29. * @since 11.1
  30. */
  31. public $type = 'Rules';
  32. /**
  33. * Method to get the field input markup for Access Control Lists.
  34. * Optionally can be associated with a specific component and section.
  35. *
  36. * TODO: Add access check.
  37. *
  38. * @return string The field input markup.
  39. *
  40. * @since 11.1
  41. */
  42. protected function getInput()
  43. {
  44. JHtml::_('behavior.tooltip');
  45. // Initialise some field attributes.
  46. $section = $this->element['section'] ? (string) $this->element['section'] : '';
  47. $component = $this->element['component'] ? (string) $this->element['component'] : '';
  48. $assetField = $this->element['asset_field'] ? (string) $this->element['asset_field'] : 'asset_id';
  49. // Get the actions for the asset.
  50. $actions = JAccess::getActions($component, $section);
  51. // Iterate over the children and add to the actions.
  52. foreach ($this->element->children() as $el)
  53. {
  54. if ($el->getName() == 'action') {
  55. $actions[] = (object) array(
  56. 'name' => (string) $el['name'],
  57. 'title' => (string) $el['title'],
  58. 'description' => (string) $el['description']
  59. );
  60. }
  61. }
  62. // Get the explicit rules for this asset.
  63. if ($section == 'component') {
  64. // Need to find the asset id by the name of the component.
  65. $db = JFactory::getDbo();
  66. $db->setQuery('SELECT id FROM #__assets WHERE name = ' . $db->quote($component));
  67. $assetId = (int) $db->loadResult();
  68. if ($error = $db->getErrorMsg()) {
  69. JError::raiseNotice(500, $error);
  70. }
  71. }
  72. else {
  73. // Find the asset id of the content.
  74. // Note that for global configuration, com_config injects asset_id = 1 into the form.
  75. $assetId = $this->form->getValue($assetField);
  76. }
  77. // Use the compact form for the content rules (deprecated).
  78. //if (!empty($component) && $section != 'component') {
  79. // return JHtml::_('rules.assetFormWidget', $actions, $assetId, $assetId ? null : $component, $this->name, $this->id);
  80. //}
  81. // Full width format.
  82. // Get the rules for just this asset (non-recursive).
  83. $assetRules = JAccess::getAssetRules($assetId);
  84. // Get the available user groups.
  85. $groups = $this->getUserGroups();
  86. // Build the form control.
  87. $curLevel = 0;
  88. // Prepare output
  89. $html = array();
  90. $html[] = '<div id="permissions-sliders" class="pane-sliders">';
  91. $html[] = '<p class="rule-desc">' . JText::_('JLIB_RULES_SETTINGS_DESC') . '</p>';
  92. $html[] = '<ul id="rules">';
  93. // Start a row for each user group.
  94. foreach ($groups as $group)
  95. {
  96. $difLevel = $group->level - $curLevel;
  97. if ($difLevel > 0) {
  98. $html[] = '<li><ul>';
  99. }
  100. else if ($difLevel < 0) {
  101. $html[] = str_repeat('</ul></li>', -$difLevel);
  102. }
  103. $html[] = '<li>';
  104. $html[] = '<div class="panel">';
  105. $html[] = '<h3 class="pane-toggler title"><a href="javascript:void(0);"><span>';
  106. $html[] = str_repeat('<span class="level">|&ndash;</span> ', $curLevel = $group->level) . $group->text;
  107. $html[] = '</span></a></h3>';
  108. $html[] = '<div class="pane-slider content pane-hide">';
  109. $html[] = '<div class="mypanel">';
  110. $html[] = '<table class="group-rules">';
  111. $html[] = '<thead>';
  112. $html[] = '<tr>';
  113. $html[] = '<th class="actions" id="actions-th' . $group->value . '">';
  114. $html[] = '<span class="acl-action">' . JText::_('JLIB_RULES_ACTION') . '</span>';
  115. $html[] = '</th>';
  116. $html[] = '<th class="settings" id="settings-th' . $group->value . '">';
  117. $html[] = '<span class="acl-action">' . JText::_('JLIB_RULES_SELECT_SETTING') . '</span>';
  118. $html[] = '</th>';
  119. // The calculated setting is not shown for the root group of global configuration.
  120. $canCalculateSettings = ($group->parent_id || !empty($component));
  121. if ($canCalculateSettings) {
  122. $html[] = '<th id="aclactionth' . $group->value . '">';
  123. $html[] = '<span class="acl-action">' . JText::_('JLIB_RULES_CALCULATED_SETTING') . '</span>';
  124. $html[] = '</th>';
  125. }
  126. $html[] = '</tr>';
  127. $html[] = '</thead>';
  128. $html[] = '<tbody>';
  129. foreach ($actions as $action)
  130. {
  131. $html[] = '<tr>';
  132. $html[] = '<td headers="actions-th' . $group->value . '">';
  133. $html[] = '<label class="hasTip" for="' . $this->id . '_' . $action->name . '_' . $group->value . '" title="'.htmlspecialchars(JText::_($action->title).'::'.JText::_($action->description), ENT_COMPAT, 'UTF-8').'">';
  134. $html[] = JText::_($action->title);
  135. $html[] = '</label>';
  136. $html[] = '</td>';
  137. $html[] = '<td headers="settings-th' . $group->value . '">';
  138. $html[] = '<select name="' . $this->name . '[' . $action->name . '][' . $group->value . ']" id="' . $this->id . '_' . $action->name . '_' . $group->value . '" title="' . JText::sprintf('JLIB_RULES_SELECT_ALLOW_DENY_GROUP', JText::_($action->title), trim($group->text)) . '">';
  139. $inheritedRule = JAccess::checkGroup($group->value, $action->name, $assetId);
  140. // Get the actual setting for the action for this group.
  141. $assetRule = $assetRules->allow($action->name, $group->value);
  142. // Build the dropdowns for the permissions sliders
  143. // The parent group has "Not Set", all children can rightly "Inherit" from that.
  144. $html[] = '<option value=""' . ($assetRule === null ? ' selected="selected"' : '') . '>' .
  145. JText::_(empty($group->parent_id) && empty($component) ? 'JLIB_RULES_NOT_SET' : 'JLIB_RULES_INHERITED') . '</option>';
  146. $html[] = '<option value="1"' . ($assetRule === true ? ' selected="selected"' : '') . '>' .
  147. JText::_('JLIB_RULES_ALLOWED') . '</option>';
  148. $html[] = '<option value="0"' . ($assetRule === false ? ' selected="selected"' : '') . '>' .
  149. JText::_('JLIB_RULES_DENIED') . '</option>';
  150. $html[] = '</select>&#160; ';
  151. // If this asset's rule is allowed, but the inherited rule is deny, we have a conflict.
  152. if (($assetRule === true) && ($inheritedRule === false)) {
  153. $html[] = JText::_('JLIB_RULES_CONFLICT');
  154. }
  155. $html[] = '</td>';
  156. // Build the Calculated Settings column.
  157. // The inherited settings column is not displayed for the root group in global configuration.
  158. if ($canCalculateSettings) {
  159. $html[] = '<td headers="aclactionth' . $group->value . '">';
  160. // This is where we show the current effective settings considering currrent group, path and cascade.
  161. // Check whether this is a component or global. Change the text slightly.
  162. if (JAccess::checkGroup($group->value, 'core.admin') !== true)
  163. {
  164. if ($inheritedRule === null) {
  165. $html[] = '<span class="icon-16-unset">'.
  166. JText::_('JLIB_RULES_NOT_ALLOWED').'</span>';
  167. }
  168. else if ($inheritedRule === true)
  169. {
  170. $html[] = '<span class="icon-16-allowed">'.
  171. JText::_('JLIB_RULES_ALLOWED').'</span>';
  172. }
  173. else if ($inheritedRule === false) {
  174. if ($assetRule === false) {
  175. $html[] = '<span class="icon-16-denied">'.
  176. JText::_('JLIB_RULES_NOT_ALLOWED').'</span>';
  177. }
  178. else {
  179. $html[] = '<span class="icon-16-denied"><span class="icon-16-locked">'.
  180. JText::_('JLIB_RULES_NOT_ALLOWED_LOCKED').'</span></span>';
  181. }
  182. }
  183. }
  184. else if (!empty($component)) {
  185. $html[] = '<span class="icon-16-allowed"><span class="icon-16-locked">'.
  186. JText::_('JLIB_RULES_ALLOWED_ADMIN').'</span></span>';
  187. }
  188. else {
  189. // Special handling for groups that have global admin because they can't be denied.
  190. // The admin rights can be changed.
  191. if ($action->name === 'core.admin') {
  192. $html[] = '<span class="icon-16-allowed">'.
  193. JText::_('JLIB_RULES_ALLOWED').'</span>';
  194. }
  195. elseif ($inheritedRule === false) {
  196. // Other actions cannot be changed.
  197. $html[] = '<span class="icon-16-denied"><span class="icon-16-locked">'.
  198. JText::_('JLIB_RULES_NOT_ALLOWED_ADMIN_CONFLICT').'</span></span>';
  199. }
  200. else {
  201. $html[] = '<span class="icon-16-allowed"><span class="icon-16-locked">'.
  202. JText::_('JLIB_RULES_ALLOWED_ADMIN').'</span></span>';
  203. }
  204. }
  205. $html[] = '</td>';
  206. }
  207. $html[] = '</tr>';
  208. }
  209. $html[] = '</tbody>';
  210. $html[] = '</table></div>';
  211. $html[] = '</div></div>';
  212. $html[] = '</li>';
  213. }
  214. $html[] = str_repeat('</ul></li>', $curLevel);
  215. $html[] = '</ul><div class="rule-notes">';
  216. if ($section == 'component' || $section == null ) {
  217. $html[] = JText::_('JLIB_RULES_SETTING_NOTES');
  218. } else {
  219. $html[] = JText::_('JLIB_RULES_SETTING_NOTES_ITEM');
  220. }
  221. $html[] = '</div></div>';
  222. $js = "window.addEvent('domready', function(){ new Fx.Accordion($$('div#permissions-sliders.pane-sliders .panel h3.pane-toggler'), $$('div#permissions-sliders.pane-sliders .panel div.pane-slider'), {onActive: function(toggler, i) {toggler.addClass('pane-toggler-down');toggler.removeClass('pane-toggler');i.addClass('pane-down');i.removeClass('pane-hide');Cookie.write('jpanesliders_permissions-sliders".$component."',$$('div#permissions-sliders.pane-sliders .panel h3').indexOf(toggler));},onBackground: function(toggler, i) {toggler.addClass('pane-toggler');toggler.removeClass('pane-toggler-down');i.addClass('pane-hide');i.removeClass('pane-down');},duration: 300,display: ".JRequest::getInt('jpanesliders_permissions-sliders'.$component, 0, 'cookie').",show: ".JRequest::getInt('jpanesliders_permissions-sliders'.$component, 0, 'cookie').", alwaysHide:true, opacity: false}); });";
  223. JFactory::getDocument()->addScriptDeclaration($js);
  224. return implode("\n", $html);
  225. }
  226. /**
  227. * Get a list of the user groups.
  228. *
  229. * @return array
  230. * @since 11.1
  231. */
  232. protected function getUserGroups()
  233. {
  234. // Initialise variables.
  235. $db = JFactory::getDBO();
  236. $query = $db->getQuery(true)
  237. ->select('a.id AS value, a.title AS text, COUNT(DISTINCT b.id) AS level, a.parent_id')
  238. ->from('#__usergroups AS a')
  239. ->leftJoin('`#__usergroups` AS b ON a.lft > b.lft AND a.rgt < b.rgt')
  240. ->group('a.id')
  241. ->order('a.lft ASC');
  242. $db->setQuery($query);
  243. $options = $db->loadObjectList();
  244. return $options;
  245. }
  246. }