Access.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace ColdTrick\SiteAnnouncements;
  3. class Access {
  4. /**
  5. * Change the access options for site announcements
  6. *
  7. * @param string $hook the name of the hook
  8. * @param string $type the type of the hook
  9. * @param array $returnvalue current returnvalue
  10. * @param array $params supplied params
  11. *
  12. * @return array
  13. */
  14. public static function userWriteCollections($hook, $type, $returnvalue, $params) {
  15. if (empty($params) || !is_array($params)) {
  16. return $returnvalue;
  17. }
  18. $input_params = elgg_extract('input_params', $params);
  19. if (empty($input_params) || !is_array($input_params)) {
  20. return $returnvalue;
  21. }
  22. $type = elgg_extract('entity_type', $input_params);
  23. $subtype = elgg_extract('entity_subtype', $input_params);
  24. if (($type !== 'object') || ($subtype !== SITE_ANNOUNCEMENT_SUBTYPE)) {
  25. return $returnvalue;
  26. }
  27. $allowed_values = array(
  28. ACCESS_LOGGED_IN,
  29. ACCESS_PUBLIC
  30. );
  31. foreach ($returnvalue as $index => $value) {
  32. if (!in_array($index, $allowed_values)) {
  33. unset($returnvalue[$index]);
  34. }
  35. }
  36. return $returnvalue;
  37. }
  38. /**
  39. * Check the container write permissions
  40. *
  41. * @param string $hook the name of the hook
  42. * @param string $type the type of the hook
  43. * @param array $returnvalue current returnvalue
  44. * @param array $params supplied params
  45. *
  46. * @return bool
  47. */
  48. public static function containerPermissionsCheck($hook, $type, $returnvalue, $params) {
  49. if ($returnvalue) {
  50. // already allowed
  51. return $returnvalue;
  52. }
  53. if (empty($params) || !is_array($params)) {
  54. return $returnvalue;
  55. }
  56. $user = elgg_extract('user', $params);
  57. $subtype = elgg_extract('subtype', $params);
  58. if (empty($user) || !elgg_instanceof($user, 'user')) {
  59. return $returnvalue;
  60. }
  61. if (empty($subtype) || ($subtype !== SITE_ANNOUNCEMENT_SUBTYPE)) {
  62. return $returnvalue;
  63. }
  64. if (site_announcements_is_editor($user)) {
  65. return true;
  66. }
  67. return $returnvalue;
  68. }
  69. /**
  70. * Check the write permissions
  71. *
  72. * @param string $hook the name of the hook
  73. * @param string $type the type of the hook
  74. * @param array $returnvalue current returnvalue
  75. * @param array $params supplied params
  76. *
  77. * @return bool
  78. */
  79. public static function permissionsCheck($hook, $type, $returnvalue, $params) {
  80. if ($returnvalue) {
  81. // already allowed
  82. return $returnvalue;
  83. }
  84. if (empty($params) || !is_array($params)) {
  85. return $returnvalue;
  86. }
  87. $user = elgg_extract('user', $params);
  88. $entity = elgg_extract('entity', $params);
  89. if (empty($user) || !elgg_instanceof($user, 'user')) {
  90. return $returnvalue;
  91. }
  92. if (empty($entity) || !elgg_instanceof($entity, 'object', SITE_ANNOUNCEMENT_SUBTYPE)) {
  93. return $returnvalue;
  94. }
  95. if (site_announcements_is_editor($user)) {
  96. return true;
  97. }
  98. return $returnvalue;
  99. }
  100. /**
  101. * Check the can comment
  102. *
  103. * @param string $hook the name of the hook
  104. * @param string $type the type of the hook
  105. * @param array $returnvalue current returnvalue
  106. * @param array $params supplied params
  107. *
  108. * @return bool
  109. */
  110. public static function commentPermissionsCheck($hook, $type, $returnvalue, $params) {
  111. if (empty($params) || !is_array($params)) {
  112. return $returnvalue;
  113. }
  114. $entity = elgg_extract('entity', $params);
  115. if (empty($entity) || !elgg_instanceof($entity, 'object', SITE_ANNOUNCEMENT_SUBTYPE)) {
  116. return $returnvalue;
  117. }
  118. return false;
  119. }
  120. }