1354482-er-user-roles-19.patch 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. diff --git a/plugins/selection/EntityReference_SelectionHandler_Generic.class.php b/plugins/selection/EntityReference_SelectionHandler_Generic.class.php
  2. index 7db4045..f3ff067 100644
  3. --- a/plugins/selection/EntityReference_SelectionHandler_Generic.class.php
  4. +++ b/plugins/selection/EntityReference_SelectionHandler_Generic.class.php
  5. @@ -307,6 +307,27 @@ class EntityReference_SelectionHandler_Generic_node extends EntityReference_Sele
  6. * This only exists to workaround core bugs.
  7. */
  8. class EntityReference_SelectionHandler_Generic_user extends EntityReference_SelectionHandler_Generic {
  9. + /**
  10. + * Implements EntityReferenceHandler::settingsForm().
  11. + */
  12. + public static function settingsForm($field, $instance) {
  13. + $settings = $field['settings']['handler_settings'];
  14. + $form = parent::settingsForm($field, $instance);
  15. + $form['referenceable_roles'] = array(
  16. + '#type' => 'checkboxes',
  17. + '#title' => t('User roles that can be referenced'),
  18. + '#default_value' => isset($settings['referenceable_roles']) ? array_filter($settings['referenceable_roles']) : array(),
  19. + '#options' => user_roles(TRUE),
  20. + );
  21. + $form['referenceable_status'] = array(
  22. + '#type' => 'checkboxes',
  23. + '#title' => t('User status that can be referenced'),
  24. + '#default_value' => isset($settings['referenceable_status']) ? array_filter($settings['referenceable_status']) : array('active' => 'active'),
  25. + '#options' => array('active' => t('Active'), 'blocked' => t('Blocked')),
  26. + );
  27. + return $form;
  28. + }
  29. +
  30. public function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS') {
  31. $query = parent::buildEntityFieldQuery($match, $match_operator);
  32. @@ -315,21 +336,33 @@ class EntityReference_SelectionHandler_Generic_user extends EntityReference_Sele
  33. $query->propertyCondition('name', $match, $match_operator);
  34. }
  35. - // Adding the 'user_access' tag is sadly insufficient for users: core
  36. - // requires us to also know about the concept of 'blocked' and
  37. - // 'active'.
  38. - if (!user_access('administer users')) {
  39. - $query->propertyCondition('status', 1);
  40. + $field = $this->field;
  41. + $settings = $field['settings']['handler_settings'];
  42. + $referenceable_roles = isset($settings['referenceable_roles']) ? array_filter($settings['referenceable_roles']) : array();
  43. + $referenceable_status = isset($settings['referenceable_status']) ? array_filter($settings['referenceable_status']) : array('active' => 'active');
  44. +
  45. + // If this filter is not filled, use the users access permissions.
  46. + if (empty($referenceable_status)) {
  47. + // Adding the 'user_access' tag is sadly insufficient for users: core
  48. + // requires us to also know about the concept of 'blocked' and 'active'.
  49. + if (!user_access('administer users')) {
  50. + $query->propertyCondition('status', 1);
  51. + }
  52. + }
  53. + elseif (count($referenceable_status) == 1) {
  54. + $values = array('active' => 1, 'blocked' => 0);
  55. + $query->propertyCondition('status', $values[key($referenceable_status)]);
  56. }
  57. +
  58. return $query;
  59. }
  60. public function entityFieldQueryAlter(SelectQueryInterface $query) {
  61. + $conditions = &$query->conditions();
  62. if (user_access('administer users')) {
  63. - // In addition, if the user is administrator, we need to make sure to
  64. + // If the user is administrator, we need to make sure to
  65. // match the anonymous user, that doesn't actually have a name in the
  66. // database.
  67. - $conditions = &$query->conditions();
  68. foreach ($conditions as $key => $condition) {
  69. if ($condition['field'] == 'users.name') {
  70. // Remove the condition.
  71. @@ -356,6 +389,19 @@ class EntityReference_SelectionHandler_Generic_user extends EntityReference_Sele
  72. }
  73. }
  74. }
  75. +
  76. + $field = $this->field;
  77. + $settings = $field['settings']['handler_settings'];
  78. + $referenceable_roles = isset($settings['referenceable_roles']) ? array_filter($settings['referenceable_roles']) : array();
  79. + if (!$referenceable_roles || !empty($referenceable_roles[DRUPAL_AUTHENTICATED_RID])) {
  80. + // Return early if "authenticated user" choosen.
  81. + return;
  82. + }
  83. +
  84. + if (!isset($referenceable_roles[DRUPAL_AUTHENTICATED_RID])) {
  85. + $query->join('users_roles', 'users_roles', 'users.uid = users_roles.uid');
  86. + $query->condition('users_roles.rid', array_keys($referenceable_roles), 'IN');
  87. + }
  88. }
  89. }