modaccesspolicy.class.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /*
  3. * This file is part of MODX Revolution.
  4. *
  5. * Copyright (c) MODX, LLC. All Rights Reserved.
  6. *
  7. * For complete copyright and license information, see the COPYRIGHT and LICENSE
  8. * files found in the top-level directory of this distribution.
  9. */
  10. /**
  11. * Defines criteria a principal must satisfy in order to access an object.
  12. *
  13. * @property string $name The name of this Policy
  14. * @property string $description The description of this Policy.
  15. * @property int $parent The parent Policy of this Policy. Not currently used.
  16. * @property int $template The Access Policy Template this Policy belongs to
  17. * @property string $class Deprecated
  18. * @property json $data A JSON object that contains all Permissions loaded in this Policy
  19. * @property string $lexicon Optional. The lexicon to load to provide the translated names/descriptions for the
  20. * Permissions included
  21. *
  22. * @see modAccessPolicyTemplate
  23. * @see modAccessPermission
  24. * @package modx
  25. */
  26. class modAccessPolicy extends xPDOSimpleObject {
  27. /**
  28. * Get the permissions for this access policy, in array format.
  29. *
  30. * @return array An array of access permissions for this Policy.
  31. */
  32. public function getPermissions() {
  33. $template = $this->getOne('Template');
  34. if (empty($template)) return array();
  35. /* get permissions for policy */
  36. $c = $this->xpdo->newQuery('modAccessPermission');
  37. $c->sortby('name','ASC');
  38. $permissions = $template->getMany('Permissions',$c);
  39. $data = $this->get('data');
  40. $lexicon = $template->get('lexicon');
  41. $list = array();
  42. /** @var modAccessPermission $permission */
  43. foreach ($permissions as $permission) {
  44. $desc = $permission->get('description');
  45. if (!empty($lexicon) && $this->xpdo->lexicon) {
  46. if (strpos($lexicon,':') !== false) {
  47. $this->xpdo->lexicon->load($lexicon);
  48. } else {
  49. $this->xpdo->lexicon->load('core:'.$lexicon);
  50. }
  51. $desc = $this->xpdo->lexicon($desc);
  52. }
  53. $active = array_key_exists($permission->get('name'),$data) && $data[$permission->get('name')] ? 1 : 0;
  54. $list[] = array(
  55. $permission->get('name'),
  56. $permission->get('description'),
  57. $desc,
  58. $permission->get('value'),
  59. $active,
  60. );
  61. }
  62. return $list;
  63. }
  64. }