Skip to content

Commit ffbf854

Browse files
authored
[5.4] Update notification code improvements (joomla#46071)
* Improve sends update notification code
1 parent 15eb7cd commit ffbf854

File tree

2 files changed

+38
-41
lines changed

2 files changed

+38
-41
lines changed

administrator/components/com_joomlaupdate/src/Model/NotificationModel.php

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,29 @@ public function sendNotification($type, $oldVersion, $newVersion): void
5050
{
5151
$params = ComponentHelper::getParams('com_joomlaupdate');
5252

53-
// Superusergroups as fallback
54-
$superUserGroups = $this->getSuperUserGroups();
53+
// User groups from Send Email to User Groups parameter in Joomla update component
54+
$emailGroups = $params->get('automated_updates_email_groups', []);
5555

56-
if (!\is_array($superUserGroups)) {
57-
$emailGroups = ArrayHelper::toInteger(explode(',', $superUserGroups));
56+
if (!\is_array($emailGroups)) {
57+
$emailGroups = ArrayHelper::toInteger(explode(',', $emailGroups));
5858
}
5959

60-
// User groups from input field
61-
$emailGroups = $params->get('automated_updates_email_groups', $superUserGroups, 'array');
60+
$emailGroups = array_filter($emailGroups);
6261

63-
if (!\is_array($emailGroups)) {
64-
$emailGroups = ArrayHelper::toInteger(explode(',', $emailGroups));
62+
/*
63+
* If no valid user groups configured in Send Email to User Groups parameter, fallback to Super Users
64+
* user group
65+
*/
66+
if ($emailGroups === []) {
67+
$emailGroups = $this->getSuperUserGroups();
6568
}
6669

6770
// Get all users in these groups who can receive emails
6871
$emailReceivers = $this->getEmailReceivers($emailGroups);
6972

70-
// If no email receivers are found, we use superusergroups as fallback
71-
if (empty($emailReceivers)) {
72-
$emailReceivers = $this->getEmailReceivers($superUserGroups);
73+
// Do not process further if no user is configured to receive email
74+
if ($emailReceivers === []) {
75+
return;
7376
}
7477

7578
$app = Factory::getApplication();
@@ -90,6 +93,11 @@ public function sendNotification($type, $oldVersion, $newVersion): void
9093

9194
// Send emails to all receivers
9295
foreach ($emailReceivers as $receiver) {
96+
// If receiver email is invalid for some reason, just ignore it
97+
if (!MailHelper::isEmailAddress($receiver->email)) {
98+
continue;
99+
}
100+
93101
$receiverParams = new Registry($receiver->params);
94102
$receiverLocale = $receiverParams->get('admin_language', $defaultLocale);
95103

@@ -125,48 +133,29 @@ public function sendNotification($type, $oldVersion, $newVersion): void
125133
/**
126134
* Returns the email information of receivers. Receiver can be any user who is not disabled.
127135
*
128-
* @param array $emailGroups A list of usergroups to email
136+
* @param array $emailGroups A list of user groups to email
129137
*
130138
* @return array The list of email receivers. Can be empty if no users are found.
131139
*
132140
* @since 5.4.0
133141
*/
134-
private function getEmailReceivers($emailGroups): array
142+
private function getEmailReceivers(array $emailGroups): array
135143
{
136-
if (empty($emailGroups)) {
137-
return [];
138-
}
139-
140-
$emailReceivers = [];
141-
142-
// Get the users of all groups in the emailGroups
144+
/* @var \Joomla\Component\Users\Administrator\Model\UsersModel $usersModel */
143145
$usersModel = Factory::getApplication()->bootComponent('com_users')
144-
->getMVCFactory()->createModel('Users', 'Administrator');
145-
$usersModel->setState('filter.state', (int) 0); // Only enabled users
146-
147-
foreach ($emailGroups as $group) {
148-
$usersModel->setState('filter.group_id', $group);
149-
150-
$usersInGroup = $usersModel->getItems();
151-
if (empty($usersInGroup)) {
152-
continue;
153-
}
146+
->getMVCFactory()->createModel('Users', 'Administrator', ['ignore_request' => true]);
154147

155-
// Users can be in more than one group. Accept only one entry
156-
foreach ($usersInGroup as $user) {
157-
if (MailHelper::isEmailAddress($user->email) && $user->sendEmail === 1) {
158-
$emailReceivers[$user->id] ??= $user;
159-
}
160-
}
161-
}
148+
$usersModel->setState('filter.state', 0); // Only return enabled users
149+
$usersModel->setState('filter.receiveSystemEmail', 1); // Only return users who receive system emails set to Yes
150+
$usersModel->setState('filter.groups', $emailGroups);
162151

163-
return $emailReceivers;
152+
return $usersModel->getItems() ?: [];
164153
}
165154

166155
/**
167-
* Returns all Super Users
156+
* Returns all user groups with Super User right
168157
*
169-
* @return array The list of super user groups.
158+
* @return array The list of user groups have Super User right
170159
*
171160
* @since 5.4.0
172161
*/
@@ -175,7 +164,7 @@ private function getSuperUserGroups(): array
175164
$groups = UserGroupsHelper::getInstance()->getAll();
176165
$ret = [];
177166

178-
// Find groups with core.admin rights (super users)
167+
// Find groups with core.admin (Super User) right
179168
foreach ($groups as $group) {
180169
if (Access::checkGroup($group->id, 'core.admin')) {
181170
$ret[] = $group->id;

administrator/components/com_users/src/Model/UsersModel.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,14 @@ protected function getListQuery()
360360
}
361361
}
362362

363+
// If the model is set to check receive system email, add to the query.
364+
$receiveSystemEmail = $this->getState('filter.receiveSystemEmail');
365+
366+
if (is_numeric($receiveSystemEmail)) {
367+
$query->where($db->quoteName('a.sendEmail') . ' = :receiveSystemEmail')
368+
->bind(':receiveSystemEmail', $receiveSystemEmail, ParameterType::INTEGER);
369+
}
370+
363371
// Filter the items over the group id if set.
364372
$groupId = $this->getState('filter.group_id');
365373
$groups = $this->getState('filter.groups');

0 commit comments

Comments
 (0)