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

/lib/Flux/Authorization.php

https://github.com/chokoleytdesignoper/fluxcp_choko
PHP | 166 lines | 80 code | 17 blank | 69 comment | 22 complexity | 1d15af51db1d08ba3d062dc8d0e91465 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, BSD-3-Clause
  1. <?php
  2. require_once 'Flux/Error.php';
  3. /**
  4. * The authorization component allows you to find out whether or not the
  5. * the current user is allowed to perform a certain task based on his account
  6. * level.
  7. */
  8. class Flux_Authorization {
  9. /**
  10. * Authorization instance.
  11. *
  12. * @access private
  13. * @var Flux_Authorization
  14. */
  15. private static $auth;
  16. /**
  17. * Access configuration.
  18. *
  19. * @access private
  20. * @var Flux_Config
  21. */
  22. private $config;
  23. /**
  24. * Session data object.
  25. *
  26. * @access private
  27. * @var Flux_SessionData
  28. */
  29. private $session;
  30. /**
  31. * Construct new Flux_Authorization instance.
  32. *
  33. * @param Flux_Config $accessConfig
  34. * @param Flux_SessionData $sessionData
  35. * @access private
  36. */
  37. private function __construct(Flux_Config $accessConfig, Flux_SessionData $sessionData)
  38. {
  39. $this->config = $accessConfig;
  40. $this->session = $sessionData;
  41. }
  42. /**
  43. * Get authorization instance, creates one if it doesn't already exist.
  44. *
  45. * @param Flux_Config $accessConfig
  46. * @param Flux_SessionData $sessionData
  47. * @return Flux_Authorization
  48. * @access public
  49. */
  50. public static function getInstance($accessConfig = null, $sessionData = null)
  51. {
  52. if (!self::$auth) {
  53. self::$auth = new Flux_Authorization($accessConfig, $sessionData);
  54. }
  55. return self::$auth;
  56. }
  57. /**
  58. * Checks whether or not the current user is able to perform a particular
  59. * action based on his/her level.
  60. *
  61. * @param string $moduleName
  62. * @param string $actionName
  63. * @return bool
  64. * @access public
  65. */
  66. public function actionAllowed($moduleName, $actionName = 'index')
  67. {
  68. $accessConfig = $this->config->get('modules');
  69. $accessKeys = array("$moduleName.$actionName", "$moduleName.*");
  70. $accountLevel = $this->session->account->level;
  71. $existentKeys = array();
  72. if ($accessConfig instanceOf Flux_Config) {
  73. foreach ($accessKeys as $accessKey) {
  74. $accessLevel = $accessConfig->get($accessKey);
  75. if (!is_null($accessLevel)) {
  76. $existentKeys[] = $accessKey;
  77. if (($accessLevel == AccountLevel::ANYONE || $accessLevel == $accountLevel ||
  78. ($accessLevel != AccountLevel::UNAUTH && $accessLevel <= $accountLevel))) {
  79. return true;
  80. }
  81. }
  82. }
  83. }
  84. if (empty($existentKeys)) {
  85. return -1;
  86. }
  87. else {
  88. return false;
  89. }
  90. }
  91. /**
  92. * Checks whether or not the current user is allowed to use a particular
  93. * feature based on his/her level.
  94. *
  95. * @param string $featureName
  96. * @return bool
  97. * @access public
  98. */
  99. public function featureAllowed($featureName)
  100. {
  101. $accessConfig = $this->config->get('features');
  102. $accountLevel = $this->session->account->level;
  103. if (($accessConfig instanceOf Flux_Config)) {
  104. $accessLevel = $accessConfig->get($featureName);
  105. if (!is_null($accessLevel) &&
  106. ($accessLevel == AccountLevel::ANYONE || $accessLevel == $accountLevel ||
  107. ($accessLevel != AccountLevel::UNAUTH && $accessLevel <= $accountLevel))) {
  108. return true;
  109. }
  110. }
  111. return false;
  112. }
  113. /**
  114. * Provides convenient getters such as `allowedTo<FeatureName>' and
  115. * `getLevelTo<FeatureName>'.
  116. *
  117. * @access public
  118. */
  119. public function __get($prop)
  120. {
  121. if (preg_match("/^allowedTo(.+)/i", $prop, $m)) {
  122. return $this->featureAllowed($m[1]);
  123. }
  124. elseif (preg_match("/^getLevelTo(.+)/i", $prop, $m)) {
  125. $accessConfig = $this->config->get('features');
  126. if ($accessConfig instanceOf Flux_Config) {
  127. return $accessConfig->get($m[1]);
  128. }
  129. }
  130. }
  131. /**
  132. * Wrapper method for setting and getting values from the access config.
  133. *
  134. * @param string $key
  135. * @param mixed $value
  136. * @param arary $options
  137. * @access public
  138. */
  139. public function config($key, $value = null, $options = array())
  140. {
  141. if (!is_null($value)) {
  142. return $this->config->set($key, $value, $options);
  143. }
  144. else {
  145. return $this->config->get($key);
  146. }
  147. }
  148. }
  149. ?>