user.rules.inc 8.2 KB

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