|
21 | 21 | use Joomla\CMS\Version; |
22 | 22 | use Joomla\Database\ParameterType; |
23 | 23 | use Joomla\Registry\Registry; |
| 24 | +use Joomla\Utilities\ArrayHelper; |
24 | 25 |
|
25 | 26 | // phpcs:disable PSR1.Files.SideEffects |
26 | 27 | \defined('_JEXEC') or die; |
@@ -48,11 +49,20 @@ public function sendNotification($type, $oldVersion): void |
48 | 49 | { |
49 | 50 | $params = ComponentHelper::getParams('com_joomlaupdate'); |
50 | 51 |
|
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'); |
53 | 54 |
|
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; |
56 | 66 | } |
57 | 67 |
|
58 | 68 | $app = Factory::getApplication(); |
@@ -81,98 +91,54 @@ public function sendNotification($type, $oldVersion): void |
81 | 91 | } |
82 | 92 |
|
83 | 93 | /** |
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. |
87 | 95 | * |
88 | | - * @param null|string $email A list of Super Users to email |
| 96 | + * @param array $emailGroups A list of usergroups to email |
89 | 97 | * |
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. |
91 | 99 | * |
92 | 100 | * @since 5.4.0 |
93 | 101 | */ |
94 | | - private function getSuperUsers($email = null): array |
| 102 | + private function getEmailReceivers($emailGroups): array |
95 | 103 | { |
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 []; |
112 | 106 | } |
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 []; |
125 | 119 | } |
126 | 120 |
|
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 | + } |
130 | 138 | } |
131 | 139 | } |
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; |
155 | 140 | } |
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; |
177 | 143 | } |
178 | 144 | } |
0 commit comments