PageRenderTime 21ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/jojo_core/classes/Jojo/Field/Permissions.php

http://jojocms.googlecode.com/
PHP | 256 lines | 170 code | 38 blank | 48 comment | 25 complexity | 9c8d868b26bc081060982f69319d6814 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, LGPL-2.0, CC-BY-SA-3.0, MIT
  1. <?php
  2. /**
  3. * Jojo CMS
  4. * ================
  5. *
  6. * Copyright 2007-2008 Harvey Kane <code@ragepank.com>
  7. * Copyright 2007-2008 Michael Holt <code@gardyneholt.co.nz>
  8. * Copyright 2007 Melanie Schulz <mel@gardyneholt.co.nz>
  9. *
  10. * See the enclosed file license.txt for license information (LGPL). If you
  11. * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  12. *
  13. * @author Harvey Kane <code@ragepank.com>
  14. * @author Michael Cochrane <mikec@jojocms.org>
  15. * @author Melanie Schulz <mel@gardyneholt.co.nz>
  16. * @license http://www.fsf.org/copyleft/lgpl.html GNU Lesser General Public License
  17. * @link http://www.jojocms.org JojoCMS
  18. * @package jojo_core
  19. */
  20. class Jojo_Field_permissions extends Jojo_Field
  21. {
  22. var $_table;
  23. var $_permOptions = array(
  24. 'show' => "Show",
  25. 'view' => "View",
  26. 'edit' => "Edit",
  27. 'add' => "Add",
  28. 'delete' => "Delete"
  29. );
  30. function __construct($fielddata = array())
  31. {
  32. parent::__construct($fielddata);
  33. }
  34. function displayedit()
  35. {
  36. /* Get group names */
  37. $groups = array('everyone' => 'All visitors');
  38. $res = Jojo::selectQuery("SELECT * FROM {usergroups} ORDER BY gr_name");
  39. foreach($res as $g) {
  40. $groups[$g['groupid']] = $g['gr_name'];
  41. }
  42. /* joined permissions table */
  43. /* Display values for this record */
  44. Jojo::assignTemplateVar('perms', $this->_getThisPerms());
  45. Jojo::assignTemplateVar('defaultperms', $this->_getInheritedPerms());
  46. /* Output permissions for each group */
  47. if ($this->readonly != 'yes') {
  48. foreach($groups as $group => $groupname) {
  49. foreach($this->_permOptions as $perm => $name) {
  50. $permName = $group . '.' . $perm;
  51. Jojo::assignTemplateVar('permName',$permName);
  52. if (isset($defaultperms[$group]) && isset($defaultperms[$group][$perm])) {
  53. Jojo::assignTemplateVar('defaultperms_group_perm',isset($defaultperms[$group][$perm]));
  54. }
  55. if (isset($perms[$group]) && isset($perms[$group][$perm])) {
  56. Jojo::assignTemplateVar('perms_group_perm',isset($perms[$group][$perm]));
  57. }
  58. }
  59. }
  60. }
  61. Jojo::assignTemplateVar('fd_field', $this->fd_field);
  62. Jojo::assignTemplateVar('readonly', $this->readonly);
  63. Jojo::assignTemplateVar('groups', $groups);
  64. Jojo::assignTemplateVar('_permOptions', $this->_permOptions);
  65. Jojo::assignTemplateVar('defaultperms_group', isset($defaultperms[$group]));
  66. Jojo::assignTemplateVar('perms_group', isset($perms[$group]));
  67. return Jojo::fetchTemplate('admin/fields/permissions.tpl');
  68. }
  69. function displayview()
  70. {
  71. /* Get group names */
  72. $groups = array('everyone' => 'All visitors');
  73. $res = Jojo::selectQuery("SELECT * FROM {usergroups} ORDER BY gr_name");
  74. foreach($res as $g) {
  75. $groups[$g['groupid']] = $g['gr_name'];
  76. }
  77. /* joined permissions table */
  78. /* Display values for this record */
  79. Jojo::assignTemplateVar('perms', $this->_getThisPerms());
  80. Jojo::assignTemplateVar('defaultperms', $this->_getInheritedPerms());
  81. /* Output permissions for each group */
  82. if ($this->readonly != 'yes') {
  83. foreach($groups as $group => $groupname) {
  84. foreach($this->_permOptions as $perm => $name) {
  85. $permName = $group . '.' . $perm;
  86. Jojo::assignTemplateVar('permName',$permName);
  87. if (isset($defaultperms[$group]) && isset($defaultperms[$group][$perm])) {
  88. Jojo::assignTemplateVar('defaultperms_group_perm',isset($defaultperms[$group][$perm]));
  89. }
  90. if (isset($perms[$group]) && isset($perms[$group][$perm])) {
  91. Jojo::assignTemplateVar('perms_group_perm',isset($perms[$group][$perm]));
  92. }
  93. }
  94. }
  95. }
  96. Jojo::assignTemplateVar('fd_field', $this->fd_field);
  97. Jojo::assignTemplateVar('readonly', $this->readonly);
  98. Jojo::assignTemplateVar('groups', $groups);
  99. Jojo::assignTemplateVar('_permOptions', $this->_permOptions);
  100. Jojo::assignTemplateVar('defaultperms_group', isset($defaultperms[$group]));
  101. Jojo::assignTemplateVar('perms_group', isset($perms[$group]));
  102. return Jojo::fetchTemplate('admin/fields/permissions-view.tpl');
  103. }
  104. function setValue($newvalue)
  105. {
  106. $this->value = $newvalue;
  107. $this->checkvalue();
  108. return true;
  109. }
  110. function checkvalue()
  111. {
  112. /* Change from an array to a text string of permissions */
  113. $value = "";
  114. if (is_array($this->value)) {
  115. foreach($this->value as $permName => $permValue) {
  116. if ($permValue == 'Y') {
  117. $value .= "$permName = 1\n";
  118. } elseif ($permValue == 'N') {
  119. $value .= "$permName = 0\n";
  120. }
  121. }
  122. } else {
  123. $permValue = $this->value;
  124. if ($permValue == 'Y') {
  125. $value .= "$permName = 1\n";
  126. } elseif ($permValue == 'N') {
  127. $value .= "$permName = 0\n";
  128. } else {
  129. $value = $this->value;
  130. }
  131. }
  132. /* Set the value to the string */
  133. $this->value = $value;
  134. return true;
  135. }
  136. /* Get the Permissions of this record */
  137. function _getThisPerms()
  138. {
  139. $perms = array();
  140. /* Split the text into different parts */
  141. preg_match_all("/([a-zA-Z0-9]+)\.([a-zA-Z]+)[\s=]+([01])+/", $this->value, $parts);
  142. /* Assemble the results */
  143. foreach($parts[0] as $k => $v) {
  144. $perms[$parts[1][$k]][$parts[2][$k]] = ($parts[3][$k] == 1);
  145. }
  146. /* Return the perms */
  147. return $perms;
  148. }
  149. /* Get the inherited permissions for this record, does not
  150. include the permissions of thie record */
  151. function _getInheritedPerms()
  152. {
  153. $perms = array();
  154. /* Get Permissions from parents */
  155. $record = $this->table->getRecordID();
  156. //echo 'record='.$record;
  157. while($record > 0) {
  158. $record = $this->_getParentID($record);
  159. $perms[$record] = $this->_getRecordPermissions($record);
  160. }
  161. /* Merge all permissions */
  162. // Start with table permissions
  163. $mergedPerms = $this->table->getDefaultPermissions();
  164. // Merge parent permissions in order from root to record
  165. $perms = array_reverse($perms);
  166. foreach ($perms as $perm) {
  167. $mergedPerms = $this->_mergePerms($mergedPerms, $perm);
  168. }
  169. /* Return merged permissions */
  170. return $mergedPerms;
  171. }
  172. /* Merge Permissions, with $second taking higher precidence */
  173. function _mergePerms($first, $second)
  174. {
  175. $merged = $first;
  176. foreach ($second as $group => $groupPerms) {
  177. foreach($groupPerms as $permName => $permValue) {
  178. $merged[$group][$permName] = $permValue;
  179. }
  180. }
  181. return $merged;
  182. }
  183. /* Get the permissions of a particular record */
  184. function _getRecordPermissions($record)
  185. {
  186. /* Create and execute query */
  187. $query = sprintf("SELECT %s AS perms FROM {%s} WHERE %s = '%s';",
  188. $this->fd_field,
  189. $this->fd_table,
  190. $this->table->getOption('primarykey'),
  191. $record);
  192. $res = Jojo::selectQuery($query);
  193. /* Split the result into different parts */
  194. preg_match_all("/([a-zA-Z0-9]+)\.([a-zA-Z]+)[\s=]+([01])+/", isset($res[0]['perms']) ? $res[0]['perms'] : '', $parts);
  195. /* Assemble the results */
  196. $perms = array();
  197. foreach($parts[0] as $k => $v) {
  198. $perms[$parts[1][$k]][$parts[2][$k]] = ($parts[3][$k] == 1);
  199. }
  200. /* Return the perms */
  201. return $perms;
  202. }
  203. /* Get the parent of a particular record */
  204. function _getParentID($record)
  205. {
  206. /* Check if this table has parents */
  207. if ($this->table->getOption('parentfield') == "") {
  208. return 0;
  209. }
  210. $query = sprintf("SELECT %s AS parent FROM {%s} WHERE %s = '%s';",
  211. $this->table->getOption('parentfield'),
  212. $this->fd_table,
  213. $this->table->getOption('primarykey'),
  214. $record);
  215. $res = Jojo::selectQuery($query);
  216. return ($res[0]['parent']);
  217. }
  218. }