PageRenderTime 25ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/lankerd/paGO---Testing-Site
PHP | 415 lines | 235 code | 62 blank | 118 comment | 25 complexity | 494c97541dec96871b2b19aebd88a0dc MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Form
  5. *
  6. * @copyright Copyright (C) 2005 - 2016 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. /**
  11. * Form Field class for the Joomla Platform.
  12. * Field for assigning permissions to groups for a given asset
  13. *
  14. * @see JAccess
  15. * @since 11.1
  16. */
  17. class JFormFieldRules extends JFormField
  18. {
  19. /**
  20. * The form field type.
  21. *
  22. * @var string
  23. * @since 11.1
  24. */
  25. protected $type = 'Rules';
  26. /**
  27. * The section.
  28. *
  29. * @var string
  30. * @since 3.2
  31. */
  32. protected $section;
  33. /**
  34. * The component.
  35. *
  36. * @var string
  37. * @since 3.2
  38. */
  39. protected $component;
  40. /**
  41. * The assetField.
  42. *
  43. * @var string
  44. * @since 3.2
  45. */
  46. protected $assetField;
  47. /**
  48. * Method to get certain otherwise inaccessible properties from the form field object.
  49. *
  50. * @param string $name The property name for which to the the value.
  51. *
  52. * @return mixed The property value or null.
  53. *
  54. * @since 3.2
  55. */
  56. public function __get($name)
  57. {
  58. switch ($name)
  59. {
  60. case 'section':
  61. case 'component':
  62. case 'assetField':
  63. return $this->$name;
  64. }
  65. return parent::__get($name);
  66. }
  67. /**
  68. * Method to set certain otherwise inaccessible properties of the form field object.
  69. *
  70. * @param string $name The property name for which to the the value.
  71. * @param mixed $value The value of the property.
  72. *
  73. * @return void
  74. *
  75. * @since 3.2
  76. */
  77. public function __set($name, $value)
  78. {
  79. switch ($name)
  80. {
  81. case 'section':
  82. case 'component':
  83. case 'assetField':
  84. $this->$name = (string) $value;
  85. break;
  86. default:
  87. parent::__set($name, $value);
  88. }
  89. }
  90. /**
  91. * Method to attach a JForm object to the field.
  92. *
  93. * @param SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
  94. * @param mixed $value The form field value to validate.
  95. * @param string $group The field name group control value. This acts as as an array container for the field.
  96. * For example if the field has name="foo" and the group value is set to "bar" then the
  97. * full field name would end up being "bar[foo]".
  98. *
  99. * @return boolean True on success.
  100. *
  101. * @see JFormField::setup()
  102. * @since 3.2
  103. */
  104. public function setup(SimpleXMLElement $element, $value, $group = null)
  105. {
  106. $return = parent::setup($element, $value, $group);
  107. if ($return)
  108. {
  109. $this->section = $this->element['section'] ? (string) $this->element['section'] : '';
  110. $this->component = $this->element['component'] ? (string) $this->element['component'] : '';
  111. $this->assetField = $this->element['asset_field'] ? (string) $this->element['asset_field'] : 'asset_id';
  112. }
  113. return $return;
  114. }
  115. /**
  116. * Method to get the field input markup for Access Control Lists.
  117. * Optionally can be associated with a specific component and section.
  118. *
  119. * @return string The field input markup.
  120. *
  121. * @since 11.1
  122. * @todo: Add access check.
  123. */
  124. protected function getInput()
  125. {
  126. JHtml::_('bootstrap.tooltip');
  127. // Add Javascript for permission change
  128. JHtml::_('script', 'media/system/js/permissions.min.js', false, false, false, false, true);
  129. // Add JText for error messages
  130. JText::script('JLIB_RULES_REQUEST_FAILURE');
  131. JText::script('JLIB_RULES_SAVE_BEFORE_CHANGE_PERMISSIONS');
  132. JText::script('JLIB_RULES_REQUEST_FAILURE');
  133. // Initialise some field attributes.
  134. $section = $this->section;
  135. $component = $this->component;
  136. $assetField = $this->assetField;
  137. // Get the actions for the asset.
  138. $actions = JAccess::getActions($component, $section);
  139. // Iterate over the children and add to the actions.
  140. foreach ($this->element->children() as $el)
  141. {
  142. if ($el->getName() == 'action')
  143. {
  144. $actions[] = (object) array('name' => (string) $el['name'], 'title' => (string) $el['title'],
  145. 'description' => (string) $el['description']);
  146. }
  147. }
  148. // Get the explicit rules for this asset.
  149. if ($section == 'component')
  150. {
  151. // Need to find the asset id by the name of the component.
  152. $db = JFactory::getDbo();
  153. $query = $db->getQuery(true)
  154. ->select($db->quoteName('id'))
  155. ->from($db->quoteName('#__assets'))
  156. ->where($db->quoteName('name') . ' = ' . $db->quote($component));
  157. $db->setQuery($query);
  158. $assetId = (int) $db->loadResult();
  159. }
  160. else
  161. {
  162. // Find the asset id of the content.
  163. // Note that for global configuration, com_config injects asset_id = 1 into the form.
  164. $assetId = $this->form->getValue($assetField);
  165. }
  166. // Full width format.
  167. // Get the rules for just this asset (non-recursive).
  168. $assetRules = JAccess::getAssetRules($assetId);
  169. // Get the available user groups.
  170. $groups = $this->getUserGroups();
  171. // Prepare output
  172. $html = array();
  173. // Description
  174. $html[] = '<p class="rule-desc">' . JText::_('JLIB_RULES_SETTINGS_DESC') . '</p>';
  175. // Begin tabs
  176. $html[] = '<div id="permissions-sliders" class="tabbable tabs-left">';
  177. // Building tab nav
  178. $html[] = '<ul class="nav nav-tabs">';
  179. foreach ($groups as $group)
  180. {
  181. // Initial Active Tab
  182. $active = "";
  183. if ($group->value == 1)
  184. {
  185. $active = "active";
  186. }
  187. $html[] = '<li class="' . $active . '">';
  188. $html[] = '<a href="#permission-' . $group->value . '" data-toggle="tab">';
  189. $html[] = str_repeat('<span class="level">&ndash;</span> ', $curLevel = $group->level) . $group->text;
  190. $html[] = '</a>';
  191. $html[] = '</li>';
  192. }
  193. $html[] = '</ul>';
  194. $html[] = '<div class="tab-content">';
  195. // Start a row for each user group.
  196. foreach ($groups as $group)
  197. {
  198. // Initial Active Pane
  199. $active = "";
  200. if ($group->value == 1)
  201. {
  202. $active = " active";
  203. }
  204. $html[] = '<div class="tab-pane' . $active . '" id="permission-' . $group->value . '">';
  205. $html[] = '<table class="table table-striped">';
  206. $html[] = '<thead>';
  207. $html[] = '<tr>';
  208. $html[] = '<th class="actions" id="actions-th' . $group->value . '">';
  209. $html[] = '<span class="acl-action">' . JText::_('JLIB_RULES_ACTION') . '</span>';
  210. $html[] = '</th>';
  211. $html[] = '<th class="settings" id="settings-th' . $group->value . '">';
  212. $html[] = '<span class="acl-action">' . JText::_('JLIB_RULES_SELECT_SETTING') . '</span>';
  213. $html[] = '</th>';
  214. // The calculated setting is not shown for the root group of global configuration.
  215. $canCalculateSettings = ($group->parent_id || !empty($component));
  216. if ($canCalculateSettings)
  217. {
  218. $html[] = '<th id="aclactionth' . $group->value . '">';
  219. $html[] = '<span class="acl-action">' . JText::_('JLIB_RULES_CALCULATED_SETTING') . '</span>';
  220. $html[] = '</th>';
  221. }
  222. $html[] = '</tr>';
  223. $html[] = '</thead>';
  224. $html[] = '<tbody>';
  225. foreach ($actions as $action)
  226. {
  227. $html[] = '<tr>';
  228. $html[] = '<td headers="actions-th' . $group->value . '">';
  229. $html[] = '<label for="' . $this->id . '_' . $action->name . '_' . $group->value . '" class="hasTooltip" title="'
  230. . JHtml::_('tooltipText', JText::_($action->title), JText::_($action->description)) . '">';
  231. $html[] = JText::_($action->title);
  232. $html[] = '</label>';
  233. $html[] = '</td>';
  234. $html[] = '<td headers="settings-th' . $group->value . '">';
  235. $html[] = '<select onchange="sendPermissions.call(this, event)" data-chosen="true" class="input-small"'
  236. . ' name="' . $this->name . '[' . $action->name . '][' . $group->value . ']"'
  237. . ' id="' . $this->id . '_' . $action->name . '_' . $group->value . '"'
  238. . ' title="' . JText::sprintf('JLIB_RULES_SELECT_ALLOW_DENY_GROUP', JText::_($action->title), trim($group->text)) . '">';
  239. $inheritedRule = JAccess::checkGroup($group->value, $action->name, $assetId);
  240. // Get the actual setting for the action for this group.
  241. $assetRule = $assetRules->allow($action->name, $group->value);
  242. // Build the dropdowns for the permissions sliders
  243. // The parent group has "Not Set", all children can rightly "Inherit" from that.
  244. $html[] = '<option value=""' . ($assetRule === null ? ' selected="selected"' : '') . '>'
  245. . JText::_(empty($group->parent_id) && empty($component) ? 'JLIB_RULES_NOT_SET' : 'JLIB_RULES_INHERITED') . '</option>';
  246. $html[] = '<option value="1"' . ($assetRule === true ? ' selected="selected"' : '') . '>' . JText::_('JLIB_RULES_ALLOWED')
  247. . '</option>';
  248. $html[] = '<option value="0"' . ($assetRule === false ? ' selected="selected"' : '') . '>' . JText::_('JLIB_RULES_DENIED')
  249. . '</option>';
  250. $html[] = '</select>&#160; ';
  251. // If this asset's rule is allowed, but the inherited rule is deny, we have a conflict.
  252. if (($assetRule === true) && ($inheritedRule === false))
  253. {
  254. $html[] = JText::_('JLIB_RULES_CONFLICT');
  255. }
  256. $html[] = '<span id="icon_' . $this->id . '_' . $action->name . '_' . $group->value . '"' . '></span>';
  257. $html[] = '</td>';
  258. // Build the Calculated Settings column.
  259. // The inherited settings column is not displayed for the root group in global configuration.
  260. if ($canCalculateSettings)
  261. {
  262. $html[] = '<td headers="aclactionth' . $group->value . '">';
  263. // This is where we show the current effective settings considering currrent group, path and cascade.
  264. // Check whether this is a component or global. Change the text slightly.
  265. if (JAccess::checkGroup($group->value, 'core.admin', $assetId) !== true)
  266. {
  267. if ($inheritedRule === null)
  268. {
  269. $html[] = '<span class="label label-important">' . JText::_('JLIB_RULES_NOT_ALLOWED') . '</span>';
  270. }
  271. elseif ($inheritedRule === true)
  272. {
  273. $html[] = '<span class="label label-success">' . JText::_('JLIB_RULES_ALLOWED') . '</span>';
  274. }
  275. elseif ($inheritedRule === false)
  276. {
  277. if ($assetRule === false)
  278. {
  279. $html[] = '<span class="label label-important">' . JText::_('JLIB_RULES_NOT_ALLOWED') . '</span>';
  280. }
  281. else
  282. {
  283. $html[] = '<span class="label"><span class="icon-lock icon-white"></span> ' . JText::_('JLIB_RULES_NOT_ALLOWED_LOCKED')
  284. . '</span>';
  285. }
  286. }
  287. }
  288. elseif (!empty($component))
  289. {
  290. $html[] = '<span class="label label-success"><span class="icon-lock icon-white"></span> ' . JText::_('JLIB_RULES_ALLOWED_ADMIN')
  291. . '</span>';
  292. }
  293. else
  294. {
  295. // Special handling for groups that have global admin because they can't be denied.
  296. // The admin rights can be changed.
  297. if ($action->name === 'core.admin')
  298. {
  299. $html[] = '<span class="label label-success">' . JText::_('JLIB_RULES_ALLOWED') . '</span>';
  300. }
  301. elseif ($inheritedRule === false)
  302. {
  303. // Other actions cannot be changed.
  304. $html[] = '<span class="label label-important"><span class="icon-lock icon-white"></span> '
  305. . JText::_('JLIB_RULES_NOT_ALLOWED_ADMIN_CONFLICT') . '</span>';
  306. }
  307. else
  308. {
  309. $html[] = '<span class="label label-success"><span class="icon-lock icon-white"></span> ' . JText::_('JLIB_RULES_ALLOWED_ADMIN')
  310. . '</span>';
  311. }
  312. }
  313. $html[] = '</td>';
  314. }
  315. $html[] = '</tr>';
  316. }
  317. $html[] = '</tbody>';
  318. $html[] = '</table></div>';
  319. }
  320. $html[] = '</div></div>';
  321. $html[] = '<div class="alert">';
  322. if ($section == 'component' || $section == null)
  323. {
  324. $html[] = JText::_('JLIB_RULES_SETTING_NOTES');
  325. }
  326. else
  327. {
  328. $html[] = JText::_('JLIB_RULES_SETTING_NOTES_ITEM');
  329. }
  330. $html[] = '</div>';
  331. return implode("\n", $html);
  332. }
  333. /**
  334. * Get a list of the user groups.
  335. *
  336. * @return array
  337. *
  338. * @since 11.1
  339. */
  340. protected function getUserGroups()
  341. {
  342. $db = JFactory::getDbo();
  343. $query = $db->getQuery(true)
  344. ->select('a.id AS value, a.title AS text, COUNT(DISTINCT b.id) AS level, a.parent_id')
  345. ->from('#__usergroups AS a')
  346. ->join('LEFT', $db->quoteName('#__usergroups') . ' AS b ON a.lft > b.lft AND a.rgt < b.rgt')
  347. ->group('a.id, a.title, a.lft, a.rgt, a.parent_id')
  348. ->order('a.lft ASC');
  349. $db->setQuery($query);
  350. $options = $db->loadObjectList();
  351. return $options;
  352. }
  353. }