Skip to content

Commit b8adb1d

Browse files
committed
New logic for e-mail adresses
1 parent b31848b commit b8adb1d

File tree

1 file changed

+51
-85
lines changed

1 file changed

+51
-85
lines changed

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

Lines changed: 51 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Joomla\CMS\Version;
2222
use Joomla\Database\ParameterType;
2323
use Joomla\Registry\Registry;
24+
use Joomla\Utilities\ArrayHelper;
2425

2526
// phpcs:disable PSR1.Files.SideEffects
2627
\defined('_JEXEC') or die;
@@ -48,11 +49,20 @@ public function sendNotification($type, $oldVersion): void
4849
{
4950
$params = ComponentHelper::getParams('com_joomlaupdate');
5051

51-
// Send a notification to all super users
52-
$superUsers = $this->getSuperUsers();
52+
// User groups to notify. Default is superuser group.
53+
$emailGroups = $params->get('automated_updates_email_groups', 8, 'array');
5354

54-
if (empty($superUsers)) {
55-
throw new \RuntimeException();
55+
// If the emailGroups is not an array, convert it to an array
56+
if (!is_array($emailGroups)) {
57+
$emailGroups = ArrayHelper::toInteger(explode(',', $emailGroups));
58+
}
59+
60+
// Get all users in these groups who can receive e-mails
61+
$emailReceivers = $this->getEmailReceivers($emailGroups);
62+
63+
// If no email receivers are found, we do not send any notification
64+
if (empty($emailReceivers)) {
65+
return;
5666
}
5767

5868
$app = Factory::getApplication();
@@ -81,98 +91,54 @@ public function sendNotification($type, $oldVersion): void
8191
}
8292

8393
/**
84-
* Returns the Super Users email information. If you provide a comma separated $email list
85-
* we will check that these emails do belong to Super Users and that they have not blocked
86-
* system emails.
94+
* Returns the email information of receivers. Receiver can be any users who is not blocked.
8795
*
88-
* @param null|string $email A list of Super Users to email
96+
* @param array $emailGroups A list of usergroups to email
8997
*
90-
* @return array The list of Super User emails
98+
* @return array The list of email receivers. Can be empty if no users are found.
9199
*
92100
* @since 5.4.0
93101
*/
94-
private function getSuperUsers($email = null): array
102+
private function getEmailReceivers($emailGroups): array
95103
{
96-
$db = $this->getDatabase();
97-
$emails = [];
98-
99-
// Convert the email list to an array
100-
if (!empty($email)) {
101-
$temp = explode(',', $email);
102-
103-
foreach ($temp as $entry) {
104-
if (!MailHelper::isEmailAddress(trim($entry))) {
105-
continue;
106-
}
107-
108-
$emails[] = trim($entry);
109-
}
110-
111-
$emails = array_unique($emails);
104+
if (empty($emailGroups)) {
105+
return [];
112106
}
113-
114-
// Get a list of groups which have Super User privileges
115-
$ret = [];
116-
117-
try {
118-
$rootId = (new Asset($db))->getRootId();
119-
$rules = Access::getAssetRules($rootId)->getData();
120-
$rawGroups = $rules['core.admin']->getData();
121-
$groups = [];
122-
123-
if (empty($rawGroups)) {
124-
return $ret;
107+
108+
$emailReceivers = [];
109+
110+
$groupModel = Factory::getApplication()->bootComponent('com_users')
111+
->getMVCFactory()->createModel('Group', 'Administrator');
112+
113+
// Get the emails of all groups in the emailGroups
114+
foreach ($emailGroups as $group) {
115+
$usersInGroup = $groupModel->getUsersInGroup($group);
116+
117+
if (empty($usersInGroup)) {
118+
return [];
125119
}
126120

127-
foreach ($rawGroups as $g => $enabled) {
128-
if ($enabled) {
129-
$groups[] = $g;
121+
// Only users with valid email address who are not blocked can receive the email
122+
foreach ($usersInGroup as $user) {
123+
if (MailHelper::isEmailAddress($user->email) && !$user->block) {
124+
$user->email = strtolower(trim($user->email));
125+
126+
// Check if the email already exists in the emailReceivers array
127+
$exist = false;
128+
for ($i = 0; $i < count($emailReceivers); $i++) {
129+
if ($emailReceivers[$i]->email === $user->email) {
130+
$exist = true;
131+
break;
132+
}
133+
}
134+
// Add to the list if it is not already in the list
135+
if (!$exist) {
136+
$emailReceivers[] = $user;
137+
}
130138
}
131139
}
132-
133-
if (empty($groups)) {
134-
return $ret;
135-
}
136-
} catch (\Exception $exc) {
137-
return $ret;
138-
}
139-
140-
// Get the user IDs of users belonging to the SA groups
141-
try {
142-
$query = $db->getQuery(true)
143-
->select($db->quoteName('user_id'))
144-
->from($db->quoteName('#__user_usergroup_map'))
145-
->whereIn($db->quoteName('group_id'), $groups);
146-
147-
$db->setQuery($query);
148-
$userIDs = $db->loadColumn(0);
149-
150-
if (empty($userIDs)) {
151-
return $ret;
152-
}
153-
} catch (\Exception $exc) {
154-
return $ret;
155140
}
156-
157-
// Get the user information for the Super Administrator users
158-
try {
159-
$query = $db->getQuery(true)
160-
->select($db->quoteName(['id', 'username', 'email', 'params']))
161-
->from($db->quoteName('#__users'))
162-
->whereIn($db->quoteName('id'), $userIDs)
163-
->where($db->quoteName('block') . ' = 0')
164-
->where($db->quoteName('sendEmail') . ' = 1');
165-
166-
if (!empty($emails)) {
167-
$lowerCaseEmails = array_map('strtolower', $emails);
168-
$query->whereIn('LOWER(' . $db->quoteName('email') . ')', $lowerCaseEmails, ParameterType::STRING);
169-
}
170-
171-
$ret = $db->setQuery($query)->loadObjectList();
172-
} catch (\Exception) {
173-
return $ret;
174-
}
175-
176-
return $ret;
141+
echo '<pre> emailReceivers: ' . print_r($emailReceivers, true) . '</pre>'; exit;
142+
return $emailReceivers;
177143
}
178144
}

0 commit comments

Comments
 (0)