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

/inc/bx/permm.php

https://github.com/chregu/fluxcms
PHP | 205 lines | 125 code | 31 blank | 49 comment | 29 complexity | a7b45638d4dbf373646221107d93b66e MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0, LGPL-2.1
  1. <?php
  2. Class bx_permm {
  3. static private $instance = null;
  4. private $authObj = null;
  5. private $permObj = null;
  6. private $userId = null;
  7. private function __construct($options) {
  8. $this->permm = $options;
  9. if (!isset($options['permModule']) || !$options['permModule']['type'] ) {
  10. $this->permObj = null;
  11. } else {
  12. $this->authObj = bx_permm::factory($options['authModule'], 'auth');
  13. $this->authObj->start();
  14. $this->permObj = bx_permm::factory($options['permModule'], 'perm');
  15. $this->userId = $this->authObj->getUserId();
  16. }
  17. }
  18. public static function getInstance($options = NULL) {
  19. if (!bx_permm::$instance instanceof bx_permm) {
  20. //$options can only be null, if we already have an instance
  21. if ($options == NULL) {
  22. $conf = bx_config::getInstance();
  23. $options = $conf->getConfProperty('permm');
  24. //throw new Exception("You didn't provide any Permmission Options");
  25. }
  26. bx_permm::$instance = new bx_permm($options);
  27. }
  28. return bx_permm::$instance;
  29. }
  30. /**
  31. * Wrapper function for Auth module to start
  32. * authentication process
  33. *
  34. * @return void
  35. * @access public
  36. */
  37. public function start() {
  38. if(method_exists($this->authObj, "start")) {
  39. $this->authObj->start();
  40. $this->userId = $this->authObj->getUserId();
  41. }
  42. }
  43. /**
  44. * Wrapper function for Auth module's getAuth() method
  45. *
  46. * @return boolean true|false
  47. * @access public
  48. */
  49. public function getAuth() {
  50. if (!$this->authObj) {
  51. $this->authObj = bx_permm::factory($this->permm['authModule'], 'auth');
  52. if ($this->authObj) {
  53. $this->authObj->start();
  54. }
  55. }
  56. return $this->authObj->getAuth();
  57. }
  58. /**
  59. * Wrapper function for Auth modules setAuth() method
  60. *
  61. * @param string $username
  62. * @return void
  63. * @access public
  64. */
  65. public function setAuth($username) {
  66. if (!$this->authObj) {
  67. $this->getAuth();
  68. }
  69. $this->authObj->setAuth($username);
  70. }
  71. /**
  72. * Wrapper function for Auth module's logout() method
  73. *
  74. * @return void
  75. * @access public
  76. */
  77. public function logout() {
  78. if (!$this->authObj) {
  79. $this->getAuth();
  80. }
  81. if (!empty($_COOKIE['fluxcms_login'])) {
  82. setcookie('fluxcms_login', '', 0,"/",null,null,true);
  83. unset($_COOKIE['fluxcms_login']);
  84. }
  85. if (method_exists($this->authObj, "logout")) {
  86. $this->authObj->logout();
  87. }
  88. }
  89. public function getUsername() {
  90. if (!$this->authObj) {
  91. $this->getAuth();
  92. }
  93. return $this->authObj->getUsername();
  94. }
  95. public function getUserId() {
  96. return $this->authObj->getUserId();
  97. }
  98. public function getUserGid() {
  99. return $this->authObj->getUserGid();
  100. }
  101. public function getStatus() {
  102. return $this->authObj->getStatus();
  103. }
  104. public function isLoggedIn() {
  105. if ($this->getUsername()) {
  106. return true;
  107. } else {
  108. return false;
  109. }
  110. }
  111. /**
  112. * Factory to instanciate appropriate auth and perm module
  113. *
  114. * @param array $options options contianing type,dsn,...
  115. * @param string $mod module category (auth|perm)
  116. * @return mixed object|null
  117. * @access private
  118. *
  119. */
  120. private function factory($options,$mod) {
  121. if (isset($options['type']) && !empty($options['type'])) {
  122. $module = sprintf("bx_permm_%s_%s", $mod, $options['type']);
  123. if (class_exists($module) && !$this->authObj instanceof $module) {
  124. return new $module($options);
  125. }
  126. }
  127. return NULL;
  128. }
  129. public function isAllowed($uri,$actions) {
  130. if ($this->permObj) {
  131. return $this->permObj->isAllowed($uri,$actions,$this->userId);
  132. } else {
  133. if (in_array('admin',$actions) && !$this->userId) {
  134. // try to get the authObj
  135. $this->getAuth();
  136. // check for a userid
  137. $this->userId = $this->authObj->getUserId();
  138. if ($this->userId) {
  139. return true;
  140. }
  141. return false;
  142. }
  143. return true;
  144. }
  145. }
  146. /*
  147. * Check if the permission systems allows to edit permissions online
  148. */
  149. public function isEditable() {
  150. if ($this->permObj) {
  151. return $this->permObj->isEditable();
  152. } else {
  153. return false;
  154. }
  155. }
  156. public function checkPassword($password) {
  157. return $this->authObj->checkPassword($password);
  158. }
  159. /**
  160. * movePermissions() is called, when a colleciton is moved
  161. *
  162. * @param mixed $from_uri
  163. * @param mixed $to_uri
  164. * @return void
  165. */
  166. public function movePermissions($from_uri, $to_uri)
  167. {
  168. if ($this->permObj && method_exists($this->permObj, "movePermissions")) {
  169. $this->permObj->movePermissions($from_uri, $to_uri);
  170. }
  171. return NULL;
  172. }
  173. }
  174. ?>