Projet

Général

Profil

Paste
Télécharger (3,46 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / rules / modules / user.eval.inc @ 76e2e7c3

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains rules integration for the user module needed during evaluation.
6
 *
7
 * @addtogroup rules
8
 * @{
9
 */
10

    
11
/**
12
 * Condition user: condition to check whether user has particular roles
13
 */
14
function rules_condition_user_has_role($account, $roles, $operation = 'AND') {
15
  switch ($operation) {
16
    case 'OR':
17
      foreach ($roles as $rid) {
18
        if (isset($account->roles[$rid])) {
19
          return TRUE;
20
        }
21
      }
22
      return FALSE;
23

    
24
    case 'AND':
25
      foreach ($roles as $rid) {
26
        if (!isset($account->roles[$rid])) {
27
          return FALSE;
28
        }
29
      }
30
      return TRUE;
31
  }
32
}
33

    
34
/**
35
 * Condition: User is blocked.
36
 */
37
function rules_condition_user_is_blocked($account) {
38
  return $account->status == 0;
39
}
40

    
41
/**
42
 * Action: Adds roles to a particular user.
43
 */
44
function rules_action_user_add_role($account, $roles) {
45
  if ($account->uid || !empty($account->is_new)) {
46
    // Get role list (minus the anonymous)
47
    $role_list = user_roles(TRUE);
48

    
49
    foreach ($roles as $rid) {
50
      $account->roles[$rid] = $role_list[$rid];
51
    }
52
    if (!empty($account->is_new) && $account->uid) {
53
      // user_save() inserts roles after invoking hook_user_insert() anyway, so
54
      // we skip saving to avoid errors due saving them twice.
55
      return FALSE;
56
    }
57
  }
58
  else {
59
    return FALSE;
60
  }
61
}
62

    
63
/**
64
 * Action: Remove roles from a given user.
65
 */
66
function rules_action_user_remove_role($account, $roles) {
67
  if ($account->uid || !empty($account->is_new)) {
68
    foreach ($roles as $rid) {
69
      // If the user has this role, remove it.
70
      if (isset($account->roles[$rid])) {
71
        unset($account->roles[$rid]);
72
      }
73
    }
74
    if (!empty($account->is_new) && $account->uid) {
75
      // user_save() inserts roles after invoking hook_user_insert() anyway, so
76
      // we skip saving to avoid errors due saving them twice.
77
      return FALSE;
78
    }
79
  }
80
  else {
81
    return FALSE;
82
  }
83
}
84

    
85
/**
86
 * Action: Block a user.
87
 */
88
function rules_action_user_block($account) {
89
  $account->status = 0;
90
  drupal_session_destroy_uid($account->uid);
91
}
92

    
93
/**
94
 * Action: Unblock a user.
95
 */
96
function rules_action_user_unblock($account) {
97
  $account->status = 1;
98
}
99

    
100
/**
101
 * Action: Send a user account e-mail.
102
 */
103
function rules_action_user_send_account_email($account, $email_type) {
104
  // If we received an authenticated user account...
105
  if (!empty($account->uid)) {
106
    module_load_include('inc', 'rules', 'modules/user.rules');
107
    $types = rules_user_account_email_options_list();
108

    
109
    // Attempt to send the account e-mail.
110
    // This code is adapted from _user_mail_notify().
111
    $params = array('account' => $account);
112
    $language = user_preferred_language($account);
113
    $mail = drupal_mail('user', $email_type, $account->mail, $language, $params);
114
    if ($email_type == 'register_pending_approval') {
115
      // If a user registered requiring admin approval, notify the admin, too.
116
      // We use the site default language for this.
117
      drupal_mail('user', 'register_pending_approval_admin', variable_get('site_mail', ini_get('sendmail_from')), language_default(), $params);
118
    }
119

    
120
    $result = empty($mail) ? NULL : $mail['result'];
121

    
122
    // Log the success or failure.
123
    if ($result) {
124
      watchdog('rules', '%type e-mail sent to %recipient.', array('%type' => $types[$email_type], '%recipient' => $account->mail));
125
    }
126
    else {
127
      watchdog('rules', 'Failed to send %type e-mail to %recipient.', array('%type' => $types[$email_type], '%recipient' => $account->mail));
128
    }
129
  }
130
}
131

    
132
/**
133
 * @}
134
 */