user.eval.inc 3.5 KB

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