2121use Joomla \CMS \Version ;
2222use Joomla \Database \ParameterType ;
2323use Joomla \Registry \Registry ;
24+ use Joomla \Utilities \ArrayHelper ;
2425
2526// phpcs:disable PSR1.Files.SideEffects
2627\defined ('_JEXEC ' ) or die;
@@ -48,24 +49,23 @@ public function sendNotification($type, $oldVersion): void
4849 {
4950 $ params = ComponentHelper::getParams ('com_joomlaupdate ' );
5051
51- // Load the parameters .
52- $ specificEmail = $ params ->get ('automated_updates_email ' );
52+ // User groups to notify as array. Default is superuser group .
53+ $ emailGroups = $ params ->get ('automated_updates_email_groups ' , 8 , ' array ' );
5354
54- // Let's find out the email addresses to notify
55- $ superUsers = [];
56-
57- if (!empty ($ specificEmail )) {
58- $ superUsers = $ this ->getSuperUsers ($ specificEmail );
59- }
60-
61- if (empty ($ superUsers )) {
62- $ superUsers = $ this ->getSuperUsers ();
55+ // If the emailGroups is not an array, convert it to an array
56+ if (!is_array ($ emailGroups )) {
57+ $ emailGroups = ArrayHelper::toInteger (explode (', ' , $ emailGroups ));
6358 }
59+
60+ // Get all users in these groups who can receive e-mails
61+ $ emailReceivers = $ this ->getEmailReceivers ($ emailGroups );
6462
65- if (empty ($ superUsers )) {
66- throw new \RuntimeException ();
63+ // If no email receivers are found, we do not send any notification
64+ if (empty ($ emailReceivers )) {
65+ return ;
6766 }
6867
68+ // Build the message
6969 $ app = Factory::getApplication ();
7070 $ jLanguage = $ app ->getLanguage ();
7171 $ sitename = $ app ->get ('sitename ' );
@@ -78,112 +78,70 @@ public function sendNotification($type, $oldVersion): void
7878 'url ' => Uri::root (),
7979 ];
8080
81- // Send the emails to the Super Users
82- foreach ($ superUsers as $ superUser ) {
83- $ params = new Registry ($ superUser ->params );
81+ // Send the emails to Recipients
82+ foreach ($ emailReceivers as $ receiver ) {
83+ $ params = new Registry ($ receiver ->params );
8484 $ jLanguage ->load ('com_joomlaupdate ' , JPATH_ADMINISTRATOR , 'en-GB ' , true , true );
8585 $ jLanguage ->load ('com_joomlaupdate ' , JPATH_ADMINISTRATOR , $ params ->get ('admin_language ' , null ), true , true );
8686
8787 $ mailer = new MailTemplate ('com_joomlaupdate.update. ' . $ type , $ jLanguage ->getTag ());
88- $ mailer ->addRecipient ($ superUser ->email );
88+ $ mailer ->addRecipient ($ receiver ->email );
8989 $ mailer ->addTemplateData ($ substitutions );
90+
9091 $ mailer ->send ();
9192 }
93+
94+ exit ;
9295 }
9396
9497 /**
95- * Returns the Super Users email information. If you provide a comma separated $email list
96- * we will check that these emails do belong to Super Users and that they have not blocked
97- * system emails.
98+ * Returns the email information of receivers. Receiver can be any users who is not blocked.
9899 *
99- * @param null|string $email A list of Super Users to email
100+ * @param null|string $email A list of usergroups to email
100101 *
101- * @return array The list of Super User emails
102+ * @return array The list of user emails
102103 *
103104 * @since 5.4.0
104105 */
105- private function getSuperUsers ( $ email = null ): array
106+ private function getEmailReceivers ( $ emailGroups = null ): array
106107 {
107- $ db = $ this ->getDatabase ();
108- $ emails = [];
109-
110- // Convert the email list to an array
111- if (!empty ($ email )) {
112- $ temp = explode (', ' , $ email );
113-
114- foreach ($ temp as $ entry ) {
115- if (!MailHelper::isEmailAddress (trim ($ entry ))) {
116- continue ;
117- }
118-
119- $ emails [] = trim ($ entry );
120- }
121-
122- $ emails = array_unique ($ emails );
123- }
124-
125- // Get a list of groups which have Super User privileges
126- $ ret = [];
127-
128- try {
129- $ rootId = (new Asset ($ db ))->getRootId ();
130- $ rules = Access::getAssetRules ($ rootId )->getData ();
131- $ rawGroups = $ rules ['core.admin ' ]->getData ();
132- $ groups = [];
133-
134- if (empty ($ rawGroups )) {
135- return $ ret ;
136- }
137-
138- foreach ($ rawGroups as $ g => $ enabled ) {
139- if ($ enabled ) {
140- $ groups [] = $ g ;
141- }
142- }
143-
144- if (empty ($ groups )) {
145- return $ ret ;
146- }
147- } catch (\Exception $ exc ) {
148- return $ ret ;
108+ if (empty ($ emailGroups )) {
109+ return [];
149110 }
150-
151- // Get the user IDs of users belonging to the SA groups
152- try {
153- $ query = $ db ->getQuery (true )
154- ->select ($ db ->quoteName ('user_id ' ))
155- ->from ($ db ->quoteName ('#__user_usergroup_map ' ))
156- ->whereIn ($ db ->quoteName ('group_id ' ), $ groups );
157-
158- $ db ->setQuery ($ query );
159- $ userIDs = $ db ->loadColumn (0 );
160-
161- if (empty ($ userIDs )) {
162- return $ ret ;
163- }
164- } catch (\Exception $ exc ) {
165- return $ ret ;
111+
112+ $ emailReceivers = [];
113+
114+ $ groupModel = Factory::getApplication ()->bootComponent ('com_users ' )
115+ ->getMVCFactory ()->createModel ('Group ' , 'Administrator ' );
116+
117+ // Get the emails of all groups in the emailGroups array
118+ foreach ($ emailGroups as $ group ) {
119+ $ usersInGroup = $ groupModel ->getUsersInGroup ($ group );
120+
121+ if (!empty ($ usersInGroup )) {
122+ // Only users with valid email address and not blocked can reveive the email
123+ foreach ($ usersInGroup as $ user ) {
124+
125+ if (MailHelper::isEmailAddress ($ user ->email ) && !$ user ->block ) {
126+ $ user ->email = strtolower (trim ($ user ->email ));
127+
128+ $ exist = false ;
129+
130+ // Check if the email already exists in the emailReceivers array
131+ for ($ i = 0 ; $ i < count ($ emailReceivers ); $ i ++) {
132+ if ($ emailReceivers [$ i ]->email === $ user ->email ) {
133+ $ exist = true ;
134+ break ;
135+ }
136+ }
137+ // Skip if email already exists
138+ if (!$ exist ) {
139+ $ emailReceivers [] = $ user ;
140+ }
141+ }
142+ }
143+ }
166144 }
167-
168- // Get the user information for the Super Administrator users
169- try {
170- $ query = $ db ->getQuery (true )
171- ->select ($ db ->quoteName (['id ' , 'username ' , 'email ' , 'params ' ]))
172- ->from ($ db ->quoteName ('#__users ' ))
173- ->whereIn ($ db ->quoteName ('id ' ), $ userIDs )
174- ->where ($ db ->quoteName ('block ' ) . ' = 0 ' )
175- ->where ($ db ->quoteName ('sendEmail ' ) . ' = 1 ' );
176-
177- if (!empty ($ emails )) {
178- $ lowerCaseEmails = array_map ('strtolower ' , $ emails );
179- $ query ->whereIn ('LOWER( ' . $ db ->quoteName ('email ' ) . ') ' , $ lowerCaseEmails , ParameterType::STRING );
180- }
181-
182- $ ret = $ db ->setQuery ($ query )->loadObjectList ();
183- } catch (\Exception ) {
184- return $ ret ;
185- }
186-
187- return $ ret ;
145+ return $ emailReceivers ;
188146 }
189147}
0 commit comments