PageRenderTime 72ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/Base/Controller/Admin.php

https://github.com/ilyich/iqyou
PHP | 126 lines | 98 code | 14 blank | 14 comment | 28 complexity | 999006effa611ba506456b8e3472891e MD5 | raw file
  1. <?php
  2. /**
  3. * Abstract Admin controller
  4. *
  5. * @author vasmik
  6. */
  7. abstract class Base_Controller_Admin extends Base_Controller_App
  8. {
  9. public $_sanitizeUserInput = false;
  10. /**
  11. * Дергает phpDoс у метода и смотрит там значение @access
  12. */
  13. private function getActionAccessLevel($actionName)
  14. {
  15. $reflectionClass = new ReflectionClass($this);
  16. $method = $reflectionClass->getMethod($actionName);
  17. if (!$method) {
  18. return Base_Service_Acl::ADMIN_LEVEL_NONE;
  19. }
  20. $comment = $method->getDocComment();
  21. $matches = array();
  22. Utf::preg_match('/@accessAllow\s+(.+)\s+/', $comment, $matches);
  23. $allowedUsers = isset($matches[1]) ? $matches[1] : '';
  24. $allowedUsers = explode(',', Utf::trim($allowedUsers));
  25. $currentUser = Base_Context::getInstance()->getUser();
  26. if ($currentUser && in_array($currentUser->getId(), $allowedUsers)) {
  27. return Base_Service_Acl::ADMIN_LEVEL_NONE;
  28. }
  29. Utf::preg_match('/@access\s+(none|low|medium|trusted|high|extra)\s+/', $comment, $matches);
  30. $access = isset($matches[1]) ? $matches[1] : '';
  31. //TODO: это костыль, убрать, когда заработает нормально Reflection
  32. if ($actionName == 'ouremailAction') {
  33. return Base_Service_Acl::ADMIN_LEVEL_NONE;
  34. }
  35. $accessMap = array(
  36. 'none' => Base_Service_Acl::ADMIN_LEVEL_NONE,
  37. 'low' => Base_Service_Acl::ADMIN_LEVEL_LOW,
  38. 'medium' => Base_Service_Acl::ADMIN_LEVEL_MEDIUM,
  39. 'trusted' => Base_Service_Acl::ADMIN_LEVEL_TRUSTED,
  40. 'high' => Base_Service_Acl::ADMIN_LEVEL_HIGH,
  41. 'extra' => Base_Service_Acl::ADMIN_LEVEL_EXTRA,
  42. );
  43. return isset($accessMap[$access]) ? $accessMap[$access] : Base_Service_Acl::ADMIN_LEVEL_LOW;
  44. }
  45. /**
  46. * по phpDoc смотрит наличие @accessNoLoginRequest
  47. */
  48. private function needLoginRequest($actionName)
  49. {
  50. $reflectionClass = new ReflectionClass($this);
  51. $method = $reflectionClass->getMethod($actionName);
  52. if (!$method) {
  53. return Base_Service_Acl::ADMIN_LEVEL_NONE;
  54. }
  55. $comment = $method->getDocComment();
  56. return Utf::preg_match('/@accessNoLoginRequest/', $comment) ? false : true;
  57. }
  58. private final function isUserAllowedToView($userAdminLevel, $actionName)
  59. {
  60. $accessLevel = $this->getActionAccessLevel($actionName);
  61. return $userAdminLevel >= $accessLevel;
  62. }
  63. public static function generateAdminHash($url = null)
  64. {
  65. if ($url === null) {
  66. $url = $_SERVER['REQUEST_URI'];
  67. }
  68. $salt = !empty(Base_Application::getInstance()->config['passwd']['admin_page']['admin_hash_salt'])
  69. ? Base_Application::getInstance()->config['passwd']['admin_page']['admin_hash_salt']
  70. : '';
  71. return md5($url . $salt . 'f7O)dq_3#');
  72. }
  73. private static function checkAdminHash()
  74. {
  75. return isset($_POST['admin_hash']) && $_POST['admin_hash'] == self::generateAdminHash();
  76. }
  77. public function preDispatch()
  78. {
  79. parent::preDispatch();
  80. if (PRODUCTION && !$this->delayRightsCheck) {
  81. if (!$this->USER) {
  82. throw new Base_Exception_Error401();
  83. }
  84. // Костыль для девочек финансового сапорта
  85. if( !in_array($this->getRequest()->getActionName(), array('transactionAction', 'getfieldsAction')) || !in_array($this->USER->getId(), array(17312838, 3783404,24496690,4981185, 51570269,61000990, /*для ios и android*/9542992, 45866252, 45973220/*для ios и android*/)) ) {
  86. if (!self::checkAdminHash()) {
  87. $actionLevel = $this->getActionAccessLevel($this->request->getActionName());
  88. if ($actionLevel != Base_Service_Acl::ADMIN_LEVEL_NONE) {
  89. if (!$this->USER) {
  90. throw new Base_Exception_Error401();
  91. }
  92. $userAdminLevel = Base_Service_Acl::getUserAdminLevel($this->USER->getId());
  93. if ($userAdminLevel <= Base_Service_Acl::ADMIN_LEVEL_NONE || !$this->isUserAllowedToView($userAdminLevel, $this->request->getActionName())) {
  94. $this->redirect('/error/?code=1', true);
  95. }
  96. }
  97. }
  98. if (in_array($this->getRequest()->getActionName(), array('transactionAction', 'getfieldsAction')) && in_array($this->USER->getId(), array(3783404,24496690,61000990))) {
  99. Service_AdminLogin::login(Service_AdminLogin::ADMIN_FINANCE);
  100. } else {
  101. // очень нехороший костыль для нитро
  102. if ($this->needLoginRequest($this->request->getActionName()) && $this->getRequest()->getActionName() != 'appstatsAction' && $this->USER && !in_array($this->USER->getId(), array(1578, 1347601))) {
  103. Service_AdminLogin::login();
  104. }
  105. }
  106. }
  107. }
  108. ini_set('display_errors', 'on');
  109. ini_set('memory_limit', '1024M');
  110. set_time_limit(0);
  111. }
  112. }