pm_block_user.module

  1. 1 pm_block_user/pm_block_user.module
  2. 7-1 pm_block_user/pm_block_user.module
  3. 7-2 pm_block_user/pm_block_user.module
  4. 6-2 pm_block_user/pm_block_user.module

Allows users to block other users from sending them any messages

File

pm_block_user/pm_block_user.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Allows users to block other users from sending them any messages
  5. */
  6. /**
  7. * Disallow blocking private messages from a user.
  8. */
  9. define('PM_BLOCK_USER_DISALLOW_BLOCKING', 0);
  10. /**
  11. * Disallow sending private messages to a user.
  12. */
  13. define('PM_BLOCK_USER_DISALLOW_SENDING', 1);
  14. /**
  15. * Implements hook_help().
  16. */
  17. function pm_block_user_help($path) {
  18. switch ($path) {
  19. case 'admin/settings/messages/block':
  20. return '<p>' . t('This area is used to define user blocking rules for the Privatemsg module. Rules allow control of who may block messages from whom. By default all users are allowed to block messages from anyone else. However, a site may have groups of users that need to contact or get information to others, for example: the site may have administrative staff or be a forum with moderators. Groups of users are defined by roles, which can be managed on the <a href="@roles">roles configuration page</a>.', array('@roles' => url('admin/user/roles'))) . '</p>';
  21. }
  22. }
  23. /**
  24. * Implements hook_menu().
  25. */
  26. function pm_block_user_menu() {
  27. $items['messages/block/%user'] = array(
  28. 'title' => 'Block user messages',
  29. 'page callback' => 'drupal_get_form',
  30. 'page arguments' => array('pm_block_user_form', 2),
  31. 'file' => 'pm_block_user.pages.inc',
  32. 'access callback' => '_pm_block_user_access',
  33. 'access arguments' => array(2),
  34. 'type' => MENU_CALLBACK,
  35. 'weight' => -10,
  36. );
  37. $items['messages/blocked'] = array(
  38. 'title' => 'Blocked users',
  39. 'page callback' => 'drupal_get_form',
  40. 'page arguments' => array('pm_block_user_list'),
  41. 'file' => 'pm_block_user.pages.inc',
  42. 'access callback' => 'privatemsg_user_access',
  43. 'access arguments' => array('read privatemsg'),
  44. 'type' => MENU_LOCAL_TASK,
  45. 'weight' => 10,
  46. );
  47. $items['admin/config/messaging/privatemsg/block'] = array(
  48. 'title' => 'User blocking rules',
  49. 'description' => 'Configure rules for which users may block each other.',
  50. 'page callback' => 'drupal_get_form',
  51. 'page arguments' => array('pm_block_user_settings'),
  52. 'file' => 'pm_block_user.admin.inc',
  53. 'access arguments' => array('administer privatemsg settings'),
  54. 'type' => MENU_LOCAL_TASK,
  55. );
  56. $items['messages/block/js'] = array(
  57. 'title' => 'Javascript block actions form',
  58. 'page callback' => 'pm_block_user_js',
  59. 'file' => 'pm_block_user.admin.inc',
  60. 'access arguments' => array('administer privatemsg settings'),
  61. 'type' => MENU_CALLBACK,
  62. );
  63. $items['messages/user/autocomplete'] = array(
  64. 'page callback' => 'privatemsg_autocomplete',
  65. 'file' => 'privatemsg.pages.inc',
  66. 'file path' => drupal_get_path('module', 'privatemsg'),
  67. 'access callback' => 'privatemsg_user_access',
  68. 'access arguments' => array('write privatemsg'),
  69. 'type' => MENU_CALLBACK,
  70. );
  71. return $items;
  72. }
  73. /**
  74. * Implements hook_theme().
  75. */
  76. function pm_block_user_theme() {
  77. return array(
  78. 'pm_block_user_list' => array(
  79. 'render element' => 'form',
  80. 'file' => 'pm_block_user.pages.inc',
  81. ),
  82. 'pm_block_user_actions' => array(
  83. 'render element' => 'form',
  84. 'file' => 'pm_block_user.admin.inc',
  85. ),
  86. );
  87. }
  88. function pm_block_user_privatemsg_autocomplete_alter(&$matches, $names, $fragment) {
  89. // Remove all types other than user if accessed through
  90. // messages/user/autocomplete.
  91. if (arg(1) == 'user') {
  92. foreach ($matches as $id => $match) {
  93. if ($match->type != 'user') {
  94. unset($matches[$id]);
  95. }
  96. }
  97. }
  98. }
  99. /**
  100. * Theme the user actions form.
  101. *
  102. * @ingroup theming
  103. */
  104. function theme_pm_block_user_actions($form) {
  105. // @todo: Something is wrong, remove this hack.
  106. $form = $form['form'];
  107. $rows = array();
  108. $headers = array(
  109. t('If the author has the role'),
  110. t('And the recipient has the role'),
  111. t('Action'),
  112. t('Enabled'),
  113. '',
  114. );
  115. $form_data = element_children($form);
  116. foreach ($form_data as $key) {
  117. // Build the table row.
  118. $row = array(
  119. 'data' => array(
  120. array('data' => drupal_render($form[$key]['author'])),
  121. array('data' => drupal_render($form[$key]['recipient'])),
  122. array('data' => drupal_render($form[$key]['action'])),
  123. array('data' => drupal_render($form[$key]['enabled'])),
  124. array('data' => drupal_render($form[$key]['remove'])),
  125. ),
  126. );
  127. // Add additional attributes to the row, such as a class for this row.
  128. if (isset($form[$key]['#attributes'])) {
  129. $row = array_merge($row, $form[$key]['#attributes']);
  130. }
  131. $rows[] = $row;
  132. }
  133. // If there are no rows, output some instructions for the user.
  134. if (empty($form_data)) {
  135. $rows[] = array(
  136. array(
  137. 'data' => t("No rules have been added. All users may block private messages from each other. To limit which users may be blocked, click 'Add new rule'."),
  138. 'colspan' => '5',
  139. ),
  140. );
  141. }
  142. $output = theme('table', array(
  143. 'header' => $headers,
  144. 'rows' => $rows
  145. ));
  146. $output .= drupal_render_children($form);
  147. return $output;
  148. }
  149. /**
  150. * Provides access argument for blocking user menu item.
  151. *
  152. * @param $account
  153. * User object representing the account the menu item will block private
  154. * messages from.
  155. *
  156. * @return
  157. * TRUE if the user is allowed to block $account, or FALSE if not.
  158. */
  159. function _pm_block_user_access($account) {
  160. global $user;
  161. if (!privatemsg_user_access('read privatemsg', $user)) {
  162. return FALSE;
  163. }
  164. // Allow to unblock users that are already blocked but the user is now not
  165. // allowed to block anymore.
  166. if (_pm_block_user_rule_exists($account, $user, PM_BLOCK_USER_DISALLOW_BLOCKING) && !pm_block_user_has_blocked($account, $user)) {
  167. return FALSE;
  168. }
  169. return TRUE;
  170. }
  171. /**
  172. * Checks if author is blocked by the recipient.
  173. *
  174. * @param $author
  175. * The user that would send a message.
  176. * @param $recipient
  177. * The user that would receive the message.
  178. * @return
  179. * TRUE if the recipient has blocked the author.
  180. */
  181. function pm_block_user_has_blocked($author, $recipient) {
  182. return db_query('SELECT 1 FROM {pm_block_user} WHERE author = :author AND recipient = :recipient', array(':author' => $author->uid, ':recipient' => $recipient->uid))->fetchField();
  183. }
  184. /**
  185. * Checks whether a rule exists for a given author, recipient and action.
  186. *
  187. * For example: if this is passed User A (who has the admin role), User B (who
  188. * has the authenticated user role) and PM_BLOCK_USER_DISALLOW_BLOCKING
  189. * parameters, and a rule is configured that disallows authenticated users
  190. * blocking admins then this function will return TRUE.
  191. *
  192. * @param $author
  193. * Author user object to check.
  194. * @param $recipient
  195. * Receiver user object to check.
  196. * @param $action
  197. * The action to be taken, defaults to PM_BLOCK_USER_DISALLOW_BLOCKING.
  198. *
  199. * @return
  200. * TRUE if a rule exists for the combination of author recipient and action.
  201. */
  202. function _pm_block_user_rule_exists($author, $recipient, $action = PM_BLOCK_USER_DISALLOW_BLOCKING) {
  203. $block_actions = variable_get('pm_block_user_actions', array());
  204. foreach ($block_actions as $details) {
  205. // If this rule doesn't relate to $action, or it's disabled
  206. // ignore it and go to next loop iteration.
  207. if ($details['action'] != $action || !$details['enabled']) {
  208. continue;
  209. }
  210. // There are no rules governing user one, but user one may have roles that
  211. // affect other users, so these exceptions are narrow in scope.
  212. // Disallow sending affects private message authors.
  213. if ($author->uid == 1 && $action == PM_BLOCK_USER_DISALLOW_SENDING) {
  214. continue;
  215. }
  216. // Disallow blocking affects private message recipients.
  217. if ($recipient->uid == 1 && $action == PM_BLOCK_USER_DISALLOW_BLOCKING) {
  218. continue;
  219. }
  220. // The author has a role matching the rule and so does the recipient.
  221. if (isset($author->roles[$details['author']]) && isset($recipient->roles[$details['recipient']])) {
  222. return TRUE;
  223. }
  224. }
  225. return FALSE;
  226. }
  227. /**
  228. * Implements hook_privatemsg_block_message.
  229. */
  230. function pm_block_user_privatemsg_block_message($author, $recipients, $context = array()) {
  231. $blocked = array();
  232. // Loop through each recipient and ensure there is no rule blocking this
  233. // author from sending them private messages. Use a reference, so when
  234. // privatemsg_user_load_multiple() is needed here the array is updated, to
  235. // avoid additional checks.
  236. foreach (array_keys($recipients) as $id) {
  237. // Only recipients of type user are currently supported.
  238. if (isset($recipients[$id]->type) && $recipients[$id]->type != 'user') {
  239. continue;
  240. }
  241. // Ensure we have a recipient user object which includes roles.
  242. if (!isset($recipients[$id]->roles)) {
  243. $uid = str_replace('user_', '', $id);
  244. $recipients[$id] = array_shift(privatemsg_user_load_multiple($uid));
  245. }
  246. // Note: this is checks whether the author may send the message (see third
  247. // parameter). Further below is a check whether the recipient may block it.
  248. if (_pm_block_user_rule_exists($author, $recipients[$id], PM_BLOCK_USER_DISALLOW_SENDING)) {
  249. $blocked[] = array(
  250. 'recipient' => $id,
  251. 'message' => t('You are not permitted to send messages to !user.', array('!user' => privatemsg_recipient_format($recipients[$id]))),
  252. );
  253. }
  254. }
  255. // Only user recipients are supported for now, remove others.
  256. $user_recipients = array();
  257. foreach ($recipients as $recipient) {
  258. if (empty($recipient->type)) {
  259. $recipient->type = 'user';
  260. $recipient->recipient = $recipient->uid;
  261. }
  262. if ($recipient->type == 'user') {
  263. $user_recipients[$recipient->recipient] = $recipient;
  264. }
  265. }
  266. if (empty($user_recipients)) {
  267. return $blocked;
  268. }
  269. $args = array(
  270. ':author' => $author->uid,
  271. ':recipients' => array_keys($user_recipients)
  272. );
  273. $result = db_query('SELECT recipient FROM {pm_block_user} WHERE author = :author AND recipient IN (:recipients) GROUP BY recipient', $args);
  274. foreach ($result as $row) {
  275. $recipient = $user_recipients[$row->recipient];
  276. // If there's a rule disallowing blocking of this message, send it anyway.
  277. if (_pm_block_user_rule_exists($author, $recipient, PM_BLOCK_USER_DISALLOW_BLOCKING)) {
  278. continue;
  279. }
  280. $blocked[] = array(
  281. 'recipient' => privatemsg_recipient_key($recipient),
  282. 'message' => t('%name has chosen to block messages from you.', array('%name' => privatemsg_recipient_format($recipient, array('plain' => TRUE))))
  283. );
  284. }
  285. return $blocked;
  286. }
  287. function pm_block_user_query_privatemsg_message_load_multiple_alter($query) {
  288. $query->addField('pmbu', 'recipient', 'is_blocked');
  289. $query->leftJoin('pm_block_user', 'pmbu', "base.author = pmbu.author AND pmi.recipient = pmbu.recipient AND pmi.type IN ('user', 'hidden')");
  290. }
  291. /**
  292. * Implements hook_privatemsg_message_view_alter.
  293. */
  294. function pm_block_user_privatemsg_message_view_alter(&$vars) {
  295. global $user;
  296. // @todo: weird, figure out why it is below #message.
  297. $author = $vars['message']->author;
  298. if (_pm_block_user_rule_exists($author, $user, PM_BLOCK_USER_DISALLOW_BLOCKING)) {
  299. return;
  300. }
  301. if (!isset($vars['message']->thread_id)) {
  302. // No thread id, this is probably only a preview
  303. return;
  304. }
  305. $thread_id = $vars['message']->thread_id;
  306. if ($user->uid <> $author->uid) {
  307. if ($vars['message']->is_blocked) {
  308. $vars['message_actions']['unblock_author'] = array('title' => t('Unblock'), 'href' => 'messages/block/' . $author->uid, 'query' => array('destination' => 'messages/view/' . $thread_id));
  309. }
  310. else {
  311. $vars['message_actions']['block_author'] = array('title' => t('Block'), 'href' => 'messages/block/' . $author->uid, 'query' => array('destination' => 'messages/view/' . $thread_id));
  312. }
  313. }
  314. }
  315. /**
  316. * Implements hook_user_cancel().
  317. */
  318. function pm_block_user_user_cancel($edit, $account, $method) {
  319. // Always delete, we don't need to keep anonymous blocking rules.
  320. db_delete('pm_block_user')
  321. ->condition(db_or()
  322. ->condition('author', $account->uid)
  323. ->condition('recipient', $account->uid)
  324. )
  325. ->execute();
  326. }
  327. /**
  328. * Implements hook_query_TAG_alter().
  329. *
  330. * Remove blocked users.
  331. */
  332. function pm_block_user_query_privatemsg_autocomplete_alter($query) {
  333. global $user;
  334. // Assume the users don't have blocked more than a few dozen other users
  335. // but there can be hundreds of thousands of users and make the subquery
  336. // unrelated.
  337. // Create a subquery that gets all users which have blocked the current user.
  338. $blocked = db_select('pm_block_user', 'pmbu')
  339. ->fields('pmbu', array('recipient'))
  340. ->condition('pmbu.author', $user->uid);
  341. // Exclude these from the possible recipients.
  342. $query->condition('u.uid', $blocked, 'NOT IN');
  343. // Block role configurations.
  344. if ($user->uid != 1) {
  345. $rids = array();
  346. $block_actions = variable_get('pm_block_user_actions', array());
  347. foreach ($block_actions as $details) {
  348. // Only continue if the rule is enabled and is a disallow sending rule.
  349. if ($details['action'] != PM_BLOCK_USER_DISALLOW_SENDING || !$details['enabled']) {
  350. continue;
  351. }
  352. // The author (current user) does have a matching role.
  353. if (isset($user->roles[$details['author']])) {
  354. // authenticated user role is not stored in the database but all users
  355. // have it so there can be no valid recipients. Add a condition that is
  356. // always false and bail out because no other rules need to be checked.
  357. if ($details['recipient'] == DRUPAL_AUTHENTICATED_RID) {
  358. $query->addExpression('1 = 0');
  359. return;
  360. }
  361. // Keep role id, will be added to the query later on.
  362. $rids[] = $details['recipient'];;
  363. }
  364. }
  365. // If there are any, add role limitation to the query.
  366. if (!empty($rids)) {
  367. $join_alias = $query->leftJoin('users_roles', 'ur', 'u.uid = ur.uid');
  368. $query->condition(db_or()
  369. ->condition($join_alias . '.rid', $rids, 'NOT IN')
  370. ->isNull($join_alias . '.rid')
  371. );
  372. }
  373. }
  374. }

Functions

Namesort descending Description
pm_block_user_has_blocked Checks if author is blocked by the recipient.
pm_block_user_help Implements hook_help().
pm_block_user_menu Implements hook_menu().
pm_block_user_privatemsg_autocomplete_alter
pm_block_user_privatemsg_block_message Implements hook_privatemsg_block_message.
pm_block_user_privatemsg_message_view_alter Implements hook_privatemsg_message_view_alter.
pm_block_user_query_privatemsg_autocomplete_alter Implements hook_query_TAG_alter().
pm_block_user_query_privatemsg_message_load_multiple_alter
pm_block_user_theme Implements hook_theme().
pm_block_user_user_cancel Implements hook_user_cancel().
theme_pm_block_user_actions Theme the user actions form.
_pm_block_user_access Provides access argument for blocking user menu item.
_pm_block_user_rule_exists Checks whether a rule exists for a given author, recipient and action.

Constants

Namesort descending Description
PM_BLOCK_USER_DISALLOW_BLOCKING Disallow blocking private messages from a user.
PM_BLOCK_USER_DISALLOW_SENDING Disallow sending private messages to a user.