user.rules.inc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /**
  3. * @file
  4. * 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. * Implements hook_rules_event_info().
  18. */
  19. function rules_user_event_info() {
  20. return array(
  21. 'user_insert' => array(
  22. 'label' => t('After saving a new user account'),
  23. 'group' => t('User'),
  24. 'variables' => array(
  25. 'account' => array('type' => 'user', 'label' => t('registered user')),
  26. ),
  27. 'access callback' => 'rules_user_integration_access',
  28. ),
  29. 'user_update' => array(
  30. 'label' => t('After updating an existing user account'),
  31. 'group' => t('User'),
  32. 'variables' => array(
  33. 'account' => array('type' => 'user', 'label' => t('updated user')),
  34. 'account_unchanged' => array('type' => 'user', 'label' => t('unchanged user'), 'handler' => 'rules_events_entity_unchanged'),
  35. ),
  36. 'access callback' => 'rules_user_integration_access',
  37. ),
  38. 'user_presave' => array(
  39. 'label' => t('Before saving a user account'),
  40. 'group' => t('User'),
  41. 'variables' => array(
  42. 'account' => array('type' => 'user', 'label' => t('saved user'), 'skip save' => TRUE),
  43. 'account_unchanged' => array('type' => 'user', 'label' => t('unchanged user'), 'handler' => 'rules_events_entity_unchanged'),
  44. ),
  45. 'access callback' => 'rules_user_integration_access',
  46. ),
  47. 'user_view' => array(
  48. 'label' => t('User account page is viewed'),
  49. 'group' => t('User'),
  50. 'variables' => array(
  51. 'account' => array('type' => 'user', 'label' => t('viewed user')),
  52. 'view_mode' => array(
  53. 'type' => 'text',
  54. 'label' => t('view mode'),
  55. 'options list' => 'rules_get_entity_view_modes',
  56. // Add the entity-type for the options list callback.
  57. 'options list entity type' => 'user',
  58. ),
  59. ),
  60. 'access callback' => 'rules_user_integration_access',
  61. 'help' => t("Note that if drupal's page cache is enabled, this event won't be generated for pages served from cache."),
  62. ),
  63. 'user_delete' => array(
  64. 'label' => t('After a user account has been deleted'),
  65. 'group' => t('User'),
  66. 'variables' => array(
  67. 'account' => array('type' => 'user', 'label' => t('deleted user')),
  68. ),
  69. 'access callback' => 'rules_user_integration_access',
  70. ),
  71. 'user_login' => array(
  72. 'label' => t('User has logged in'),
  73. 'group' => t('User'),
  74. 'variables' => array(
  75. 'account' => array('type' => 'user', 'label' => t('logged in user')),
  76. ),
  77. 'access callback' => 'rules_user_integration_access',
  78. ),
  79. 'user_logout' => array(
  80. 'label' => t('User has logged out'),
  81. 'group' => t('User'),
  82. 'variables' => array(
  83. 'account' => array('type' => 'user', 'label' => t('logged out user')),
  84. ),
  85. 'access callback' => 'rules_user_integration_access',
  86. ),
  87. );
  88. }
  89. /**
  90. * Options list for user cancel methods.
  91. *
  92. * @todo Use for providing a user_cancel action.
  93. */
  94. function rules_user_cancel_methods() {
  95. module_load_include('inc', 'user', 'user.pages');
  96. foreach (user_cancel_methods() as $method => $form) {
  97. $methods[$method] = $form['#title'];
  98. }
  99. return $methods;
  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. * Implements hook_rules_condition_info() on behalf of the user module.
  113. */
  114. function rules_user_condition_info() {
  115. return array(
  116. 'user_has_role' => array(
  117. 'label' => t('User has role(s)'),
  118. 'parameter' => array(
  119. 'account' => array('type' => 'user', 'label' => t('User')),
  120. 'roles' => array(
  121. 'type' => 'list<integer>',
  122. 'label' => t('Roles'),
  123. 'options list' => 'rules_user_roles_options_list',
  124. ),
  125. 'operation' => array(
  126. 'type' => 'text',
  127. 'label' => t('Match roles'),
  128. 'options list' => 'rules_user_condition_operations',
  129. 'restriction' => 'input',
  130. 'optional' => TRUE,
  131. 'default value' => 'AND',
  132. 'description' => t('If matching against all selected roles, the user must have <em>all</em> the roles selected.'),
  133. ),
  134. ),
  135. 'group' => t('User'),
  136. 'access callback' => 'rules_user_integration_access',
  137. 'base' => 'rules_condition_user_has_role',
  138. ),
  139. 'user_is_blocked' => array(
  140. 'label' => t('User is blocked'),
  141. 'parameter' => array(
  142. 'account' => array('type' => 'user', 'label' => t('User')),
  143. ),
  144. 'group' => t('User'),
  145. 'access callback' => 'rules_user_integration_access',
  146. 'base' => 'rules_condition_user_is_blocked',
  147. ),
  148. );
  149. }
  150. /**
  151. * User has role condition help callback.
  152. */
  153. function rules_condition_user_has_role_help() {
  154. return t('Whether the user has the selected role(s).');
  155. }
  156. /**
  157. * Options list callback for the operation parameter of condition user has role.
  158. */
  159. function rules_user_condition_operations() {
  160. return array(
  161. 'AND' => t('all'),
  162. 'OR' => t('any'),
  163. );
  164. }
  165. /**
  166. * Implements hook_rules_action_info() on behalf of the user module.
  167. */
  168. function rules_user_action_info() {
  169. $defaults = array(
  170. 'parameter' => array(
  171. 'account' => array(
  172. 'type' => 'user',
  173. 'label' => t('User'),
  174. 'description' => t('The user whose roles should be changed.'),
  175. 'save' => TRUE,
  176. ),
  177. 'roles' => array(
  178. 'type' => 'list<integer>',
  179. 'label' => t('Roles'),
  180. 'options list' => 'rules_user_roles_options_list',
  181. ),
  182. ),
  183. 'group' => t('User'),
  184. 'access callback' => 'rules_user_role_change_access',
  185. );
  186. $items['user_add_role'] = $defaults + array(
  187. 'label' => t('Add user role'),
  188. 'base' => 'rules_action_user_add_role',
  189. );
  190. $items['user_remove_role'] = $defaults + array(
  191. 'label' => t('Remove user role'),
  192. 'base' => 'rules_action_user_remove_role',
  193. );
  194. $defaults = array(
  195. 'parameter' => array(
  196. 'account' => array(
  197. 'type' => 'user',
  198. 'label' => t('User'),
  199. 'save' => TRUE,
  200. ),
  201. ),
  202. 'group' => t('User'),
  203. 'access callback' => 'rules_user_integration_access',
  204. );
  205. $items['user_block'] = $defaults + array(
  206. 'label' => t('Block a user'),
  207. 'base' => 'rules_action_user_block',
  208. );
  209. $items['user_unblock'] = $defaults + array(
  210. 'label' => t('Unblock a user'),
  211. 'base' => 'rules_action_user_unblock',
  212. );
  213. $items['user_send_account_email'] = array(
  214. 'label' => t('Send account e-mail'),
  215. 'parameter' => array(
  216. 'account' => array(
  217. 'type' => 'user',
  218. 'label' => t('Account'),
  219. ),
  220. 'email_type' => array(
  221. 'type' => 'text',
  222. 'label' => t('E-mail type'),
  223. 'description' => t("Select the e-mail based on your site's account settings to send to the user."),
  224. 'options list' => 'rules_user_account_email_options_list',
  225. ),
  226. ),
  227. 'group' => t('User'),
  228. 'base' => 'rules_action_user_send_account_email',
  229. 'access callback' => 'rules_user_integration_access',
  230. );
  231. return $items;
  232. }
  233. /**
  234. * User integration role actions access callback.
  235. */
  236. function rules_user_role_change_access() {
  237. return entity_metadata_user_roles() && user_access('administer permissions');
  238. }
  239. /**
  240. * Options list callback for user roles.
  241. */
  242. function rules_user_roles_options_list($element) {
  243. return entity_metadata_user_roles('roles', array(), $element instanceof RulesConditionInterface ? 'view' : 'edit');
  244. }
  245. /**
  246. * Options list callback for user account e-mail types.
  247. *
  248. * @see _user_mail_notify()
  249. */
  250. function rules_user_account_email_options_list() {
  251. return array(
  252. 'register_admin_created' => t('Welcome (new user created by administrator)'),
  253. 'register_no_approval_required' => t('Welcome (no approval required)'),
  254. 'register_pending_approval' => t('Welcome (awaiting approval)'),
  255. 'password_reset' => t('Password recovery'),
  256. 'status_activated' => t('Account activation'),
  257. 'status_blocked' => t('Account blocked'),
  258. 'cancel_confirm' => t('Account cancellation confirmation'),
  259. 'status_canceled' => t('Account canceled'),
  260. );
  261. }
  262. /**
  263. * @}
  264. */