user.rules.inc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. return $items;
  209. }
  210. /**
  211. * User integration role actions access callback.
  212. */
  213. function rules_user_role_change_access() {
  214. return entity_metadata_user_roles() && user_access('administer permissions');
  215. }
  216. /**
  217. * Options list callback for user roles.
  218. */
  219. function rules_user_roles_options_list($element) {
  220. return entity_metadata_user_roles('roles', array(), $element instanceof RulesConditionInterface ? 'view' : 'edit');
  221. }
  222. /**
  223. * @}
  224. */