Skip to content

Commit

Permalink
Merge pull request #1338 from craftcms/bugfix/844-users-field-special…
Browse files Browse the repository at this point in the history
…-sources

search through admins, credentialed and inactive if selected as a field source
  • Loading branch information
angrybrad committed Jul 4, 2023
2 parents b35cc52 + 0a674aa commit 379fd54
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/fields/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Cake\Utility\Hash;
use Craft;
use craft\base\Element as BaseElement;
use craft\elements\db\UserQuery;
use craft\elements\User as UserElement;
use craft\errors\ElementNotFoundException;
use craft\feedme\base\Field;
Expand Down Expand Up @@ -86,6 +87,7 @@ public function parseField(): mixed
// Get source id's for connecting
$groupIds = [];
$isAdmin = false;
$status = null;

if (is_array($sources)) {
// go through sources that start with "group:" and get group uid for those
Expand All @@ -96,10 +98,19 @@ public function parseField(): mixed
}
}

// the other source left in Craft 3 can be 'admins' for which we'll need a separate query
// the other possible source in Craft 4 can be 'admins' for which we'll need a separate query
if (in_array('admins', $sources, true)) {
$isAdmin = true;
}

// the other possible source in Craft 4 can be 'credentialed'
if (in_array(UserQuery::STATUS_CREDENTIALED, $sources, true)) {
$status[] = UserQuery::STATUS_CREDENTIALED;
}
// or 'inactive'
if (in_array(UserElement::STATUS_INACTIVE, $sources, true)) {
$status[] = UserElement::STATUS_INACTIVE;
}
} elseif ($sources === '*') {
$groupIds = null;
}
Expand Down Expand Up @@ -158,6 +169,15 @@ public function parseField(): mixed
$foundElements = array_merge($foundElements, $ids);
}

// If we still have no matches, check based on the credentialed/inactive status
if (!empty($status) && count($ids) === 0) {
unset($criteria['groupId'], $criteria['admin']);
$criteria['status'] = $status;

$ids = $this->_findUsers($criteria);
$foundElements = array_merge($foundElements, $ids);
}

// Check if we should create the element. But only if email is provided (for the moment)
if ((count($ids) == 0) && $create && $match === 'email') {
$foundElements[] = $this->_createElement($dataValue, $groupIds);
Expand Down

0 comments on commit 379fd54

Please sign in to comment.