Projet

Général

Profil

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

root / drupal7 / sites / all / modules / rules / modules / user.eval.inc @ 950416da

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

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

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

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

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

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

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

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

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

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

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

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

    
133
/**
134
 * @}
135
 */