/tine20/Tinebase/Controller/Abstract.php

https://gitlab.com/rsilveira1987/Expresso · PHP · 200 lines · 81 code · 29 blank · 90 comment · 14 complexity · 9b6cba672a252f5225875570850bd27c MD5 · raw file

  1. <?php
  2. /**
  3. * Tine 2.0
  4. *
  5. * @package Tinebase
  6. * @subpackage Controller
  7. * @license http://www.gnu.org/licenses/agpl.html AGPL Version 3
  8. * @author Philipp Schuele <p.schuele@metaways.de>
  9. * @copyright Copyright (c) 2007-2011 Metaways Infosystems GmbH (http://www.metaways.de)
  10. *
  11. */
  12. /**
  13. * controller abstract for applications
  14. *
  15. * @package Tinebase
  16. * @subpackage Controller
  17. */
  18. abstract class Tinebase_Controller_Abstract extends Tinebase_Pluggable_Abstract implements Tinebase_Controller_Interface
  19. {
  20. /**
  21. * default settings
  22. *
  23. * @var array
  24. */
  25. protected $_defaultsSettings = array();
  26. /**
  27. * holds the default Model of this application
  28. * @var string
  29. */
  30. protected static $_defaultModel = NULL;
  31. /**
  32. * application name (is needed in checkRight())
  33. *
  34. * @var string
  35. */
  36. protected $_applicationName = '';
  37. /**
  38. * disable events on demand
  39. *
  40. * @var mixed false => no events filtered, true => all events filtered, array => disable only specific events
  41. */
  42. protected $_disabledEvents = false;
  43. /**
  44. * Plugins for this class family
  45. * Contains:
  46. * '[method]' => '[Complete_Name_Of_Class]'
  47. *
  48. * @var array
  49. */
  50. protected static $_plugins = array();
  51. /**
  52. * generic check admin rights function
  53. * rules:
  54. * - ADMIN right includes all other rights
  55. * - MANAGE_* right includes VIEW_* right
  56. * - results are cached if caching is active (with cache tag 'rights')
  57. *
  58. * @param string $_right to check
  59. * @param boolean $_throwException [optional]
  60. * @param boolean $_includeTinebaseAdmin [optional]
  61. * @return boolean
  62. * @throws Tinebase_Exception_UnexpectedValue
  63. * @throws Tinebase_Exception_AccessDenied
  64. *
  65. * @todo move that to *_Acl_Rights
  66. * @todo include Tinebase admin? atm only the application admin right is checked
  67. * @todo think about moving the caching to Tinebase_Acl_Roles and use only a class cache as it is difficult (and slow?) to invalidate
  68. */
  69. public function checkRight($_right, $_throwException = TRUE, $_includeTinebaseAdmin = TRUE)
  70. {
  71. if (empty($this->_applicationName)) {
  72. throw new Tinebase_Exception_UnexpectedValue('No application name defined!');
  73. }
  74. $right = strtoupper($_right);
  75. $cache = Tinebase_Core::getCache();
  76. $cacheId = Tinebase_Acl_Roles::createCheckRightCacheId(Tinebase_Core::getUser()->getId(), $right, $this->_applicationName);
  77. $result = $cache->load($cacheId);
  78. if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . $cacheId);
  79. if (!$result) {
  80. $applicationRightsClass = $this->_applicationName . '_Acl_Rights';
  81. // array with the rights that should be checked, ADMIN is in it per default
  82. $rightsToCheck = ($_includeTinebaseAdmin) ? array(Tinebase_Acl_Rights::ADMIN) : array();
  83. if (preg_match("/VIEW_([A-Z_]*)/", $right, $matches)) {
  84. // manage right includes view right
  85. $rightsToCheck[] = constant($applicationRightsClass. '::MANAGE_' . $matches[1]);
  86. }
  87. $rightsToCheck[] = constant($applicationRightsClass. '::' . $right);
  88. $result = FALSE;
  89. if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__
  90. . ' Checking rights: ' . print_r($rightsToCheck, TRUE));
  91. foreach ($rightsToCheck as $rightToCheck) {
  92. if (Tinebase_Acl_Roles::getInstance()->hasRight($this->_applicationName, Tinebase_Core::getUser()->getId(), $rightToCheck)) {
  93. $result = TRUE;
  94. break;
  95. }
  96. }
  97. $cache->save($result, $cacheId, array('rights'), Tinebase_Core::get(Tinebase_Core::RIGHTS_CACHE_TIMEOUT));
  98. }
  99. if (!$result && $_throwException) {
  100. throw new Tinebase_Exception_AccessDenied("You are not allowed to $right in application $this->_applicationName !");
  101. }
  102. return $result;
  103. }
  104. /**
  105. * Returns default settings for app
  106. *
  107. * @param boolean $_resolve if some values should be resolved
  108. * @return array settings data
  109. */
  110. public function getConfigSettings($_resolve = FALSE)
  111. {
  112. $appConfig = Tinebase_Config::getAppConfig($this->_applicationName);
  113. if ($appConfig != NULL) {
  114. $settings = $appConfig::getInstance()->get(
  115. Tinebase_Config::APPDEFAULTS,
  116. new Tinebase_Config_Struct($this->_defaultsSettings)
  117. )->toArray();
  118. } else {
  119. $settings = $this->_defaultsSettings;
  120. }
  121. return ($_resolve) ? $this->_resolveConfigSettings($settings) : $settings;
  122. }
  123. /**
  124. * resolve some settings
  125. *
  126. * @param array $_settings
  127. */
  128. protected function _resolveConfigSettings($_settings)
  129. {
  130. return $_settings;
  131. }
  132. /**
  133. * save settings
  134. *
  135. * @param array $_settings
  136. * @return void
  137. */
  138. public function saveConfigSettings($_settings)
  139. {
  140. // only admins are allowed to do this
  141. $this->checkRight(Tinebase_Acl_Rights::ADMIN);
  142. $appConfig = Tinebase_Config::getAppConfig($this->_applicationName);
  143. if ($appConfig !== NULL) {
  144. $appConfig::getInstance()->set(Tinebase_Config::APPDEFAULTS, $_settings);
  145. }
  146. }
  147. /**
  148. * returns the default model of this application
  149. * @return string
  150. */
  151. public static function getDefaultModel()
  152. {
  153. return static::$_defaultModel;
  154. }
  155. /**
  156. * returns controller instance for given $_controllerName
  157. *
  158. * @param string $_controllerName
  159. * @return Tinebase_Controller
  160. */
  161. public static function getController($_controllerName)
  162. {
  163. if (! class_exists($_controllerName)) {
  164. throw new Exception("Controller" . $_controllerName . "not found.");
  165. }
  166. if (!in_array('Tinebase_Controller_Interface', class_implements($_controllerName))) {
  167. throw new Exception("Controller $_controllerName does not implement Tinebase_Controller_Interface.");
  168. }
  169. return call_user_func(array($_controllerName, 'getInstance'));
  170. }
  171. }