user.eval.inc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * @file
  4. * Contains rules integration for the user module needed during evaluation.
  5. *
  6. * @addtogroup rules
  7. *
  8. * @{
  9. */
  10. /**
  11. * Condition user: condition to check whether user has particular roles.
  12. */
  13. function rules_condition_user_has_role($account, $roles, $operation = 'AND') {
  14. switch ($operation) {
  15. case 'OR':
  16. foreach ($roles as $rid) {
  17. if (isset($account->roles[$rid])) {
  18. return TRUE;
  19. }
  20. }
  21. return FALSE;
  22. case 'AND':
  23. foreach ($roles as $rid) {
  24. if (!isset($account->roles[$rid])) {
  25. return FALSE;
  26. }
  27. }
  28. return TRUE;
  29. }
  30. }
  31. /**
  32. * Condition: User is blocked.
  33. */
  34. function rules_condition_user_is_blocked($account) {
  35. return $account->status == 0;
  36. }
  37. /**
  38. * Action: Adds roles to a particular user.
  39. */
  40. function rules_action_user_add_role($account, $roles) {
  41. if ($account->uid || !empty($account->is_new)) {
  42. // Get role list (minus the anonymous).
  43. $role_list = user_roles(TRUE);
  44. foreach ($roles as $rid) {
  45. $account->roles[$rid] = $role_list[$rid];
  46. }
  47. if (!empty($account->is_new) && $account->uid) {
  48. // user_save() inserts roles after invoking hook_user_insert() anyway, so
  49. // we skip saving to avoid errors due saving them twice.
  50. return FALSE;
  51. }
  52. }
  53. else {
  54. return FALSE;
  55. }
  56. }
  57. /**
  58. * Action: Remove roles from a given user.
  59. */
  60. function rules_action_user_remove_role($account, $roles) {
  61. if ($account->uid || !empty($account->is_new)) {
  62. foreach ($roles as $rid) {
  63. // If the user has this role, remove it.
  64. if (isset($account->roles[$rid])) {
  65. unset($account->roles[$rid]);
  66. }
  67. }
  68. if (!empty($account->is_new) && $account->uid) {
  69. // user_save() inserts roles after invoking hook_user_insert() anyway, so
  70. // we skip saving to avoid errors due saving them twice.
  71. return FALSE;
  72. }
  73. }
  74. else {
  75. return FALSE;
  76. }
  77. }
  78. /**
  79. * Action: Block a user.
  80. */
  81. function rules_action_user_block($account) {
  82. $account->status = 0;
  83. drupal_session_destroy_uid($account->uid);
  84. }
  85. /**
  86. * Action: Unblock a user.
  87. */
  88. function rules_action_user_unblock($account) {
  89. $account->status = 1;
  90. }
  91. /**
  92. * Action: Send a user account e-mail.
  93. */
  94. function rules_action_user_send_account_email($account, $email_type) {
  95. // If we received an authenticated user account...
  96. if (!empty($account->uid)) {
  97. module_load_include('inc', 'rules', 'modules/user.rules');
  98. $types = rules_user_account_email_options_list();
  99. // Attempt to send the account e-mail.
  100. // This code is adapted from _user_mail_notify().
  101. $params = array('account' => $account);
  102. $language = user_preferred_language($account);
  103. $mail = drupal_mail('user', $email_type, $account->mail, $language, $params);
  104. if ($email_type == 'register_pending_approval') {
  105. // If a user registered requiring admin approval, notify the admin, too.
  106. // We use the site default language for this.
  107. drupal_mail('user', 'register_pending_approval_admin', variable_get('site_mail', ini_get('sendmail_from')), language_default(), $params);
  108. }
  109. $result = empty($mail) ? NULL : $mail['result'];
  110. // Log the success or failure.
  111. if ($result) {
  112. watchdog('rules', '%type e-mail sent to %recipient.', array('%type' => $types[$email_type], '%recipient' => $account->mail));
  113. }
  114. else {
  115. watchdog('rules', 'Failed to send %type e-mail to %recipient.', array('%type' => $types[$email_type], '%recipient' => $account->mail));
  116. }
  117. }
  118. }
  119. /**
  120. * @}
  121. */