modprincipal.class.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. * Represents a person or system that will access modX.
  12. *
  13. * {@internal Implement a derivative to define the behavior and attributes of
  14. * an actual user or system that is intended to access modX or a modX service.}
  15. *
  16. * @abstract
  17. * @package modx
  18. */
  19. class modPrincipal extends xPDOSimpleObject {
  20. /**
  21. * Stores a collection of key-value pairs identifying policy authority.
  22. * @var array
  23. * @access protected
  24. */
  25. protected $_attributes = null;
  26. /**
  27. * Load attributes of the principal that define access to secured objects.
  28. *
  29. * {@internal Implement this function in derivatives to control how your
  30. * user class uses the MODX ABAC (Attribute-Based Access Control) security
  31. * model}
  32. *
  33. * @abstract
  34. * @access protected
  35. * @param string $target The target modAccess classes to load attributes from.
  36. * @param string $context Context to check within, defaults to current context.
  37. * @param boolean $reload If true, the attributes will be reloaded and the session updated.
  38. */
  39. public function loadAttributes($target, $context = '', $reload = false) {
  40. $this->_attributes = array();
  41. }
  42. /**
  43. * Get the attributes for this principal.
  44. *
  45. * @param array $targets An array of target modAccess classes to load.
  46. * @param string $context The context to check within. Defaults to active context.
  47. * @param boolean $reload If true, the attributes will be reloaded and the session updated.
  48. * @return array An array of attributes on the principal
  49. */
  50. public function getAttributes($targets = array(), $context = '', $reload = false) {
  51. $context = !empty($context) ? $context : $this->xpdo->context->get('key');
  52. if (!is_array($targets) || empty($targets)) {
  53. $targets = explode(',', $this->xpdo->getOption('principal_targets', null, 'modAccessContext,modAccessResourceGroup,modAccessCategory,sources.modAccessMediaSource,modAccessNamespace'));
  54. array_walk($targets, 'trim');
  55. }
  56. $this->loadAttributes($targets, $context, $reload);
  57. return $this->_attributes[$context];
  58. }
  59. }