PageRenderTime 53ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/tine20/Tinebase/Controller/Abstract.php

https://github.com/testruby/Tine-2.0-Open-Source-Groupware-and-CRM
PHP | 176 lines | 73 code | 24 blank | 79 comment | 9 complexity | 59e71d1533ec0b112a1b7d4e467ecb67 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 implements Tinebase_Controller_Interface
  19. {
  20. /**
  21. * default settings
  22. *
  23. * @var array
  24. */
  25. protected $_defaultsSettings = array();
  26. /**
  27. * application name (is needed in checkRight())
  28. *
  29. * @var string
  30. */
  31. protected $_applicationName = '';
  32. /**
  33. * the current account
  34. *
  35. * @var Tinebase_Model_User
  36. */
  37. protected $_currentAccount = NULL;
  38. /**
  39. * disable events on demand
  40. *
  41. * @var mixed false => no events filtered, true => all events filtered, array => disable only specific events
  42. */
  43. protected $_disabledEvents = false;
  44. /**
  45. * generic check admin rights function
  46. * rules:
  47. * - ADMIN right includes all other rights
  48. * - MANAGE_* right includes VIEW_* right
  49. * - results are cached if caching is active (with cache tag 'rights')
  50. *
  51. * @param string $_right to check
  52. * @param boolean $_throwException [optional]
  53. * @param boolean $_includeTinebaseAdmin [optional]
  54. * @return boolean
  55. * @throws Tinebase_Exception_UnexpectedValue
  56. * @throws Tinebase_Exception_AccessDenied
  57. *
  58. * @todo move that to *_Acl_Rights
  59. */
  60. public function checkRight($_right, $_throwException = TRUE, $_includeTinebaseAdmin = TRUE)
  61. {
  62. if (empty($this->_applicationName)) {
  63. throw new Tinebase_Exception_UnexpectedValue('No application name defined!');
  64. }
  65. $right = strtoupper($_right);
  66. $cache = Tinebase_Core::get(Tinebase_Core::CACHE);
  67. $cacheId = convertCacheId('checkRight' . Tinebase_Core::getUser()->getId() . $_right . $this->_applicationName);
  68. $result = $cache->load($cacheId);
  69. if (!$result) {
  70. $applicationRightsClass = $this->_applicationName . '_Acl_Rights';
  71. // array with the rights that should be checked, ADMIN is in it per default
  72. $rightsToCheck = ($_includeTinebaseAdmin) ? array(Tinebase_Acl_Rights::ADMIN) : array();
  73. if (preg_match("/MANAGE_/", $right)) {
  74. $rightsToCheck[] = constant($applicationRightsClass. '::' . $right);
  75. }
  76. if (preg_match("/VIEW_([A-Z_]*)/", $right, $matches)) {
  77. $rightsToCheck[] = constant($applicationRightsClass. '::' . $right);
  78. // manage right includes view right
  79. $rightsToCheck[] = constant($applicationRightsClass. '::MANAGE_' . $matches[1]);
  80. }
  81. $result = FALSE;
  82. foreach ($rightsToCheck as $rightToCheck) {
  83. //echo "check right: " . $rightToCheck;
  84. if (Tinebase_Acl_Roles::getInstance()->hasRight($this->_applicationName, Tinebase_Core::getUser()->getId(), $rightToCheck)) {
  85. $result = TRUE;
  86. break;
  87. }
  88. }
  89. $cache->save($result, $cacheId, array('rights'), 120);
  90. }
  91. if (!$result && $_throwException) {
  92. throw new Tinebase_Exception_AccessDenied("You are not allowed to $right in application $this->_applicationName !");
  93. }
  94. return $result;
  95. }
  96. /**
  97. * Returns default settings for app
  98. *
  99. * @param boolean $_resolve if some values should be resolved
  100. * @return array settings data
  101. */
  102. public function getConfigSettings($_resolve = FALSE)
  103. {
  104. $settings = Tinebase_Config::getInstance()->getConfigAsArray(
  105. Tinebase_Config::APPDEFAULTS,
  106. $this->_applicationName,
  107. $this->_defaultsSettings
  108. );
  109. return ($_resolve) ? $this->_resolveConfigSettings($settings) : $settings;
  110. }
  111. /**
  112. * resolve some settings
  113. *
  114. * @param array $_settings
  115. */
  116. protected function _resolveConfigSettings($_settings)
  117. {
  118. return $_settings;
  119. }
  120. /**
  121. * save settings
  122. *
  123. * @param array $_settings
  124. * @return void
  125. */
  126. public function saveConfigSettings($_settings)
  127. {
  128. // only admins are allowed to do this
  129. $this->checkRight(Tinebase_Acl_Rights::ADMIN);
  130. Tinebase_Config::getInstance()->setConfigForApplication(
  131. Tinebase_Config::APPDEFAULTS,
  132. Zend_Json::encode($_settings),
  133. $this->_applicationName
  134. );
  135. }
  136. /**
  137. * returns controller instance for given $_controllerName
  138. *
  139. * @param string $_controllerName
  140. * @return Tinebase_Controller
  141. */
  142. public static function getController($_controllerName)
  143. {
  144. if (! class_exists($_controllerName)) {
  145. throw new Exception("Controller" . $_controllerName . "not found.");
  146. }
  147. if (!in_array('Tinebase_Controller_Interface', class_implements($_controllerName))) {
  148. throw new Exception("Controller $_controllerName does not implement Tinebase_Controller_Interface.");
  149. }
  150. return call_user_func(array($_controllerName, 'getInstance'));
  151. }
  152. }