access.class.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Definition of the Access class
  5. * @package chamilo.library
  6. */
  7. /**
  8. * Authorize or deny calls.
  9. *
  10. *
  11. * Note:
  12. *
  13. * This class stores locally the security token so that the current call
  14. * can still be validated after generating the new token.
  15. *
  16. * The new security token is generated only on first call. Successive calls
  17. * return the same token. This ensure that different parts of the application
  18. * (form, javascript for javascript, etc) can get access to the same token.
  19. *
  20. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
  21. */
  22. abstract class Access
  23. {
  24. const SEC_TOKEN = 'sec_token';
  25. /**
  26. * Return view and edit access.
  27. *
  28. * @return \Access
  29. * @assert () !== null
  30. */
  31. public static function all()
  32. {
  33. return AccessAll::instance();
  34. }
  35. /**
  36. * Return no access.
  37. *
  38. * @return \Access
  39. * @assert () === null
  40. */
  41. public static function forbidden()
  42. {
  43. AccessForbidden::instance();
  44. }
  45. protected $session_token;
  46. protected $token;
  47. /**
  48. * Returns true if security token is valid, false otherwise.
  49. *
  50. * @return bool
  51. * @assert () === false
  52. */
  53. public function is_token_valid()
  54. {
  55. $call_token = Request::get_security_token();
  56. if (empty($call_token)) {
  57. return false;
  58. }
  59. $session_token = $this->get_session_token();
  60. return $session_token == $call_token;
  61. }
  62. /**
  63. * Returns the token contained in the session.
  64. * Stores the token for further reuse so that it can be changed in session.
  65. *
  66. * @return string
  67. * @assert () !== null
  68. */
  69. public function get_session_token()
  70. {
  71. if (empty($this->session_token)) {
  72. $key = self::SEC_TOKEN;
  73. $this->session_token = isset($_SESSION[$key]) ? $_SESSION[$key] : '';
  74. }
  75. return $this->session_token;
  76. }
  77. /*
  78. * On first call generate a new security token and save it in session.
  79. * On successful calls returns the same (new) token (function is repeatable).
  80. * If user do not have the right to edit, returns a blank (invalid) token.
  81. *
  82. * Stores the existing session token before saving the new one so that
  83. * the current call can still be validated after calling this function.
  84. * @assert () === ''
  85. */
  86. public function get_token()
  87. {
  88. if (!$this->can_edit()) {
  89. return '';
  90. }
  91. if ($this->token) {
  92. return $this->token;
  93. }
  94. $this->session_token = $this->get_session_token();
  95. $this->token = \Security::get_token();
  96. }
  97. /**
  98. * Returns true if the user has the right to edit.
  99. *
  100. * @return boolean
  101. */
  102. public abstract function can_edit();
  103. /**
  104. * Returns true if the current user has the right to view
  105. *
  106. * @return boolean
  107. */
  108. public abstract function can_view();
  109. /**
  110. * Returns whether this access is authorized or not. Synonym for can_view()
  111. * @assert () === false
  112. */
  113. public function authorize()
  114. {
  115. return $this->can_view();
  116. }
  117. }
  118. /**
  119. * Authorize access and view access.
  120. */
  121. class AccessAll extends Access
  122. {
  123. /**
  124. * Return the instance.
  125. *
  126. * @return \Access
  127. */
  128. public static function instance()
  129. {
  130. static $result = null;
  131. if (empty($result)) {
  132. $result = new self();
  133. }
  134. return $result;
  135. }
  136. private function __construct()
  137. {
  138. }
  139. public function can_edit()
  140. {
  141. return true;
  142. }
  143. public function can_view()
  144. {
  145. return true;
  146. }
  147. public function authorize()
  148. {
  149. return true;
  150. }
  151. }
  152. /**
  153. * Authorizev view access only
  154. */
  155. class AccessForbidden extends Access
  156. {
  157. /**
  158. * Return the instance.
  159. *
  160. * @return \AccessView
  161. */
  162. public static function instance()
  163. {
  164. static $result = null;
  165. if (empty($result)) {
  166. $result = new self();
  167. }
  168. return $result;
  169. }
  170. private function __construct()
  171. {
  172. }
  173. public function can_edit()
  174. {
  175. return false;
  176. }
  177. public function can_view()
  178. {
  179. return false;
  180. }
  181. public function authorize()
  182. {
  183. return false;
  184. }
  185. }