Projet

Général

Profil

Paste
Télécharger (8,16 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2

    
3
/**
4
 * @file rules integration for the user module
5
 *
6
 * @addtogroup rules
7
 * @{
8
 */
9

    
10
/**
11
 * Implements hook_rules_file_info() on behalf of the user module.
12
 */
13
function rules_user_file_info() {
14
  return array('modules/user.eval');
15
}
16

    
17
/**
18
 * Implementation of hook_rules_event_info().
19
 */
20
function rules_user_event_info() {
21
  return array(
22
    'user_insert' => array(
23
      'label' => t('After saving a new user account'),
24
      'group' => t('User'),
25
      'variables' => array(
26
        'account' => array('type' => 'user', 'label' => t('registered user')),
27
      ),
28
      'access callback' => 'rules_user_integration_access',
29
    ),
30
    'user_update' => array(
31
      'label' => t('After updating an existing user account'),
32
      'group' => t('User'),
33
      'variables' => array(
34
        'account' => array('type' => 'user', 'label' => t('updated user')),
35
        'account_unchanged' => array('type' => 'user', 'label' => t('unchanged user'), 'handler' => 'rules_events_entity_unchanged'),
36
      ),
37
      'access callback' => 'rules_user_integration_access',
38
    ),
39
    'user_presave' => array(
40
      'label' => t('Before saving a user account'),
41
      'group' => t('User'),
42
      'variables' => array(
43
        'account' => array('type' => 'user', 'label' => t('saved user'), 'skip save' => TRUE),
44
        'account_unchanged' => array('type' => 'user', 'label' => t('unchanged user'), 'handler' => 'rules_events_entity_unchanged'),
45
      ),
46
      'access callback' => 'rules_user_integration_access',
47
    ),
48
    'user_view' => array(
49
      'label' => t('User account page is viewed'),
50
      'group' => t('User'),
51
      'variables' => array(
52
        'account' => array('type' => 'user', 'label' => t('viewed user')),
53
        'view_mode' => array(
54
          'type' => 'text',
55
          'label' => t('view mode'),
56
          'options list' => 'rules_get_entity_view_modes',
57
        ),
58
      ),
59
      'access callback' => 'rules_user_integration_access',
60
      'help' => t("Note that if drupal's page cache is enabled, this event won't be generated for pages served from cache."),
61
    ),
62
    'user_delete' => array(
63
      'label' => t('After a user account has been deleted'),
64
      'group' => t('User'),
65
      'variables' => array(
66
        'account' => array('type' => 'user', 'label' => t('deleted user')),
67
      ),
68
      'access callback' => 'rules_user_integration_access',
69
    ),
70
    'user_login' => array(
71
      'label' => t('User has logged in'),
72
      'group' => t('User'),
73
      'variables' => array(
74
        'account' => array('type' => 'user', 'label' => t('logged in user')),
75
      ),
76
      'access callback' => 'rules_user_integration_access',
77
    ),
78
    'user_logout' => array(
79
      'label' => t('User has logged out'),
80
      'group' => t('User'),
81
      'variables' => array(
82
        'account' => array('type' => 'user', 'label' => t('logged out user')),
83
      ),
84
      'access callback' => 'rules_user_integration_access',
85
    ),
86
  );
87
}
88

    
89
/**
90
 * Options list for user cancel methods.
91
 * @todo: Use for providing a user_cancel action.
92
 */
93
function rules_user_cancel_methods() {
94
  module_load_include('inc', 'user', 'user.pages');
95
  foreach (user_cancel_methods() as $method => $form) {
96
    $methods[$method] = $form['#title'];
97
  }
98
  return $methods;
99
}
100

    
101
/**
102
 * User integration access callback.
103
 */
104
function rules_user_integration_access($type, $name) {
105
  if ($type == 'event' || $type == 'condition') {
106
    return entity_access('view', 'user');
107
  }
108
  // Else return admin access.
109
  return user_access('administer users');
110
}
111

    
112
/**
113
 * Implements hook_rules_condition_info() on behalf of the user module.
114
 */
115
function rules_user_condition_info() {
116
  return array(
117
    'user_has_role' => array(
118
      'label' => t('User has role(s)'),
119
      'parameter' => array(
120
        'account' => array('type' => 'user', 'label' => t('User')),
121
        'roles' => array(
122
          'type' => 'list<integer>',
123
          'label' => t('Roles'),
124
          'options list' => 'rules_user_roles_options_list',
125
        ),
126
        'operation' => array(
127
          'type' => 'text',
128
          'label' => t('Match roles'),
129
          'options list' => 'rules_user_condition_operations',
130
          'restriction' => 'input',
131
          'optional' => TRUE,
132
          'default value' => 'AND',
133
          'description' => t('If matching against all selected roles, the user must have <em>all</em> the roles selected.'),
134
        ),
135
      ),
136
      'group' => t('User'),
137
      'access callback' => 'rules_user_integration_access',
138
      'base' => 'rules_condition_user_has_role',
139
    ),
140
    'user_is_blocked' => array(
141
      'label' => t('User is blocked'),
142
      'parameter' => array(
143
        'account' => array('type' => 'user', 'label' => t('User')),
144
      ),
145
      'group' => t('User'),
146
      'access callback' => 'rules_user_integration_access',
147
      'base' => 'rules_condition_user_is_blocked',
148
    ),
149
  );
150
}
151

    
152
/**
153
 * User has role condition help callback.
154
 */
155
function rules_condition_user_has_role_help() {
156
  return t('Whether the user has the selected role(s).');
157
}
158

    
159
/**
160
 * Options list callback for the operation parameter of condition user has role.
161
 */
162
function rules_user_condition_operations() {
163
  return array(
164
    'AND' => t('all'),
165
    'OR' => t('any'),
166
  );
167
}
168

    
169
/**
170
 * Implements hook_rules_action_info() on behalf of the user module.
171
 */
172
function rules_user_action_info() {
173
  $defaults = array(
174
   'parameter' => array(
175
      'account' => array(
176
        'type' => 'user',
177
        'label' => t('User'),
178
        'description' => t('The user whose roles should be changed.'),
179
        'save' => TRUE,
180
      ),
181
      'roles' => array(
182
        'type' => 'list<integer>',
183
        'label' => t('Roles'),
184
        'options list' => 'rules_user_roles_options_list',
185
      ),
186
    ),
187
    'group' => t('User'),
188
    'access callback' => 'rules_user_role_change_access',
189
  );
190
  $items['user_add_role'] = $defaults + array(
191
    'label' => t('Add user role'),
192
    'base' => 'rules_action_user_add_role',
193
  );
194
  $items['user_remove_role'] = $defaults + array(
195
    'label' => t('Remove user role'),
196
    'base' => 'rules_action_user_remove_role',
197
  );
198
  $defaults = array(
199
   'parameter' => array(
200
      'account' => array(
201
        'type' => 'user',
202
        'label' => t('User'),
203
        'save' => TRUE,
204
      ),
205
    ),
206
    'group' => t('User'),
207
    'access callback' => 'rules_user_integration_access',
208
  );
209
  $items['user_block'] = $defaults + array(
210
    'label' => t('Block a user'),
211
    'base' => 'rules_action_user_block',
212
  );
213
  $items['user_unblock'] = $defaults + array(
214
    'label' => t('Unblock a user'),
215
    'base' => 'rules_action_user_unblock',
216
  );
217
  $items['user_send_account_email'] = array(
218
    'label' => t('Send account e-mail'),
219
    'parameter' => array(
220
      'account' => array(
221
        'type' => 'user',
222
        'label' => t('Account'),
223
      ),
224
      'email_type' => array(
225
        'type' => 'text',
226
        'label' => t('E-mail type'),
227
        'description' => t("Select the e-mail based on your site's account settings to send to the user."),
228
        'options list' => 'rules_user_account_email_options_list',
229
      ),
230
    ),
231
    'group' => t('User'),
232
    'base' => 'rules_action_user_send_account_email',
233
    'access callback' => 'rules_user_integration_access',
234
  );
235
  return $items;
236
}
237

    
238
/**
239
 * User integration role actions access callback.
240
 */
241
function rules_user_role_change_access() {
242
  return entity_metadata_user_roles() && user_access('administer permissions');
243
}
244

    
245
/**
246
 * Options list callback for user roles.
247
 */
248
function rules_user_roles_options_list($element) {
249
  return entity_metadata_user_roles('roles', array(), $element instanceof RulesConditionInterface ? 'view' : 'edit');
250
}
251

    
252
/**
253
 * Options list callback for user account e-mail types.
254
 *
255
 * @see _user_mail_notify()
256
 */
257
function rules_user_account_email_options_list() {
258
  return array(
259
    'register_admin_created' => t('Welcome (new user created by administrator)'),
260
    'register_no_approval_required' => t('Welcome (no approval required)'),
261
    'register_pending_approval' => t('Welcome (awaiting approval)'),
262
    'password_reset' => t('Password recovery'),
263
    'status_activated' => t('Account activation'),
264
    'status_blocked' => t('Account blocked'),
265
    'cancel_confirm' => t('Account cancellation confirmation'),
266
    'status_canceled' => t('Account canceled'),
267
  );
268
}
269

    
270
/**
271
 * @}
272
 */