Group_privacy_settings.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Data class for group privacy settings
  4. *
  5. * PHP version 5
  6. *
  7. * @category Data
  8. * @package StatusNet
  9. * @author Evan Prodromou <evan@status.net>
  10. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  11. * @link http://status.net/
  12. *
  13. * StatusNet - the distributed open-source microblogging tool
  14. * Copyright (C) 2011, StatusNet, Inc.
  15. *
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License as published by
  18. * the Free Software Foundation, either version 3 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. */
  29. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. /**
  33. * Data class for group privacy
  34. *
  35. * Stores admin preferences about the group.
  36. *
  37. * @category Action
  38. * @package StatusNet
  39. * @author Evan Prodromou <evan@status.net>
  40. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  41. * @link http://status.net/
  42. *
  43. * @see DB_DataObject
  44. */
  45. class Group_privacy_settings extends Managed_DataObject
  46. {
  47. public $__table = 'group_privacy_settings';
  48. /** ID of the group. */
  49. public $group_id;
  50. /** When to allow privacy: always, sometimes, or never. */
  51. public $allow_privacy;
  52. /** Who can send private messages: everyone, member, admin */
  53. public $allow_sender;
  54. /** row creation timestamp */
  55. public $created;
  56. /** Last-modified timestamp */
  57. public $modified;
  58. /** NEVER is */
  59. const SOMETIMES = -1;
  60. const NEVER = 0;
  61. const ALWAYS = 1;
  62. /** These are bit-mappy, as a hedge against the future. */
  63. const EVERYONE = 1;
  64. const MEMBER = 2;
  65. const ADMIN = 4;
  66. public static function schemaDef()
  67. {
  68. return array(
  69. 'fields' => array(
  70. 'group_id' => array('type' => 'int', 'not null' => true, 'description' => 'group_privacy_settings'),
  71. 'allow_privacy' => array('type' => 'int', 'not null' => true, 'description' => 'sometimes=-1, never=0, always=1'),
  72. 'allow_sender' => array('type' => 'int', 'not null' => true, 'description' => 'list of bit-mappy values in source code'),
  73. 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
  74. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  75. ),
  76. 'primary key' => array('group_id'),
  77. 'foreign keys' => array(
  78. 'group_privacy_settings_group_id_fkey' => array('user_group', array('group_id' => 'id')),
  79. ),
  80. );
  81. }
  82. function forGroup($group)
  83. {
  84. $gps = Group_privacy_settings::getKV('group_id', $group->id);
  85. if (empty($gps)) {
  86. // make a fake one with defaults
  87. $gps = new Group_privacy_settings();
  88. $gps->allow_privacy = Group_privacy_settings::SOMETIMES;
  89. $gps->allow_sender = Group_privacy_settings::MEMBER;
  90. }
  91. return $gps;
  92. }
  93. function ensurePost($user, $group)
  94. {
  95. $gps = self::forGroup($group);
  96. if ($gps->allow_privacy == Group_privacy_settings::NEVER) {
  97. // TRANS: Exception thrown when trying to set group privacy setting if group %s does not allow private messages.
  98. throw new Exception(sprintf(_m('Group %s does not allow private messages.'),
  99. $group->nickname));
  100. }
  101. switch ($gps->allow_sender) {
  102. case Group_privacy_settings::EVERYONE:
  103. $profile = $user->getProfile();
  104. if (Group_block::isBlocked($group, $profile)) {
  105. // TRANS: Exception thrown when trying to send group private message while blocked from that group.
  106. // TRANS: %1$s is a user nickname, %2$s is a group nickname.
  107. throw new Exception(sprintf(_m('User %1$s is blocked from group %2$s.'),
  108. $user->nickname,
  109. $group->nickname));
  110. }
  111. break;
  112. case Group_privacy_settings::MEMBER:
  113. if (!$user->isMember($group)) {
  114. // TRANS: Exception thrown when trying to send group private message while not a member.
  115. // TRANS: %1$s is a user nickname, %2$s is a group nickname.
  116. throw new Exception(sprintf(_m('User %1$s is not a member of group %2$s.'),
  117. $user->nickname,
  118. $group->nickname));
  119. }
  120. break;
  121. case Group_privacy_settings::ADMIN:
  122. if (!$user->isAdmin($group)) {
  123. // TRANS: Exception thrown when trying to send group private message while not a group administrator.
  124. // TRANS: %1$s is a user nickname, %2$s is a group nickname.
  125. throw new Exception(sprintf(_m('User %1$s is not an administrator of group %2$s.'),
  126. $user->nickname,
  127. $group->nickname));
  128. }
  129. break;
  130. default:
  131. // TRANS: Exception thrown when encountering undefined group privacy settings.
  132. // TRANS: %s is a group nickname.
  133. throw new Exception(sprintf(_m('Unknown privacy settings for group %s.'),
  134. $group->nickname));
  135. }
  136. return true;
  137. }
  138. }