13
13
use Joomla \CMS \Access \Access ;
14
14
use Joomla \CMS \Component \ComponentHelper ;
15
15
use Joomla \CMS \Factory ;
16
+ use Joomla \CMS \Helper \UserGroupsHelper ;
16
17
use Joomla \CMS \Mail \MailHelper ;
17
18
use Joomla \CMS \Mail \MailTemplate ;
18
19
use Joomla \CMS \MVC \Model \BaseDatabaseModel ;
19
- use Joomla \CMS \Table \Asset ;
20
20
use Joomla \CMS \Uri \Uri ;
21
21
use Joomla \CMS \Version ;
22
- use Joomla \Database \ParameterType ;
23
22
use Joomla \Registry \Registry ;
23
+ use Joomla \Utilities \ArrayHelper ;
24
24
25
25
// phpcs:disable PSR1.Files.SideEffects
26
26
\defined ('_JEXEC ' ) or die;
@@ -49,11 +49,26 @@ public function sendNotification($type, $oldVersion, $newVersion): void
49
49
{
50
50
$ params = ComponentHelper::getParams ('com_joomlaupdate ' );
51
51
52
- // Send a notification to all super users
53
- $ superUsers = $ this ->getSuperUsers ();
52
+ // Superusergroups as fallback
53
+ $ superUserGroups = $ this ->getSuperUserGroups ();
54
54
55
- if (empty ($ superUsers )) {
56
- throw new \RuntimeException ();
55
+ if (!\is_array ($ superUserGroups )) {
56
+ $ emailGroups = ArrayHelper::toInteger (explode (', ' , $ superUserGroups ));
57
+ }
58
+
59
+ // User groups from input field
60
+ $ emailGroups = $ params ->get ('automated_updates_email_groups ' , $ superUserGroups , 'array ' );
61
+
62
+ if (!\is_array ($ emailGroups )) {
63
+ $ emailGroups = ArrayHelper::toInteger (explode (', ' , $ emailGroups ));
64
+ }
65
+
66
+ // Get all users in these groups who can receive emails
67
+ $ emailReceivers = $ this ->getEmailReceivers ($ emailGroups );
68
+
69
+ // If no email receivers are found, we use superusergroups as fallback
70
+ if (empty ($ emailReceivers )) {
71
+ $ emailReceivers = $ this ->getEmailReceivers ($ superUserGroups );
57
72
}
58
73
59
74
$ app = Factory::getApplication ();
@@ -67,110 +82,77 @@ public function sendNotification($type, $oldVersion, $newVersion): void
67
82
'url ' => Uri::root (),
68
83
];
69
84
70
- // Send the emails to the Super Users
71
- foreach ($ superUsers as $ superUser ) {
72
- $ params = new Registry ($ superUser ->params );
85
+ // Send emails to all receivers
86
+ foreach ($ emailReceivers as $ receiver ) {
87
+ $ params = new Registry ($ receiver ->params );
73
88
$ jLanguage ->load ('com_joomlaupdate ' , JPATH_ADMINISTRATOR , 'en-GB ' , true , true );
74
89
$ jLanguage ->load ('com_joomlaupdate ' , JPATH_ADMINISTRATOR , $ params ->get ('admin_language ' , null ), true , true );
75
90
76
91
$ mailer = new MailTemplate ('com_joomlaupdate.update. ' . $ type , $ jLanguage ->getTag ());
77
- $ mailer ->addRecipient ($ superUser ->email );
92
+ $ mailer ->addRecipient ($ receiver ->email );
78
93
$ mailer ->addTemplateData ($ substitutions );
79
94
$ mailer ->send ();
80
95
}
81
96
}
82
97
83
98
/**
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.
99
+ * Returns the email information of receivers. Receiver can be any user who is not disabled.
87
100
*
88
- * @param null|string $email A list of Super Users to email
101
+ * @param array $emailGroups A list of usergroups to email
89
102
*
90
- * @return array The list of Super User emails
103
+ * @return array The list of email receivers. Can be empty if no users are found.
91
104
*
92
105
* @since 5.4.0
93
106
*/
94
- private function getSuperUsers ( $ email = null ): array
107
+ private function getEmailReceivers ( $ emailGroups ): array
95
108
{
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 );
109
+ if (empty ($ emailGroups )) {
110
+ return [];
112
111
}
113
112
114
- // Get a list of groups which have Super User privileges
115
- $ ret = [];
113
+ $ emailReceivers = [];
116
114
117
- try {
118
- $ rootId = (new Asset ($ db ))->getRootId ();
119
- $ rules = Access::getAssetRules ($ rootId )->getData ();
120
- $ rawGroups = $ rules ['core.admin ' ]->getData ();
121
- $ groups = [];
115
+ // Get the users of all groups in the emailGroups
116
+ $ usersModel = Factory::getApplication ()->bootComponent ('com_users ' )
117
+ ->getMVCFactory ()->createModel ('Users ' , 'Administrator ' );
118
+ $ usersModel ->setState ('filter.state ' , (int ) 0 ); // Only enabled users
122
119
123
- if (empty ($ rawGroups )) {
124
- return $ ret ;
125
- }
120
+ foreach ($ emailGroups as $ group ) {
121
+ $ usersModel ->setState ('filter.group_id ' , $ group );
126
122
127
- foreach ($ rawGroups as $ g => $ enabled ) {
128
- if ($ enabled ) {
129
- $ groups [] = $ g ;
130
- }
123
+ $ usersInGroup = $ usersModel ->getItems ();
124
+ if (empty ($ usersInGroup )) {
125
+ continue ;
131
126
}
132
127
133
- if (empty ($ groups )) {
134
- return $ ret ;
128
+ // Users can be in more than one group. Accept only one entry
129
+ foreach ($ usersInGroup as $ user ) {
130
+ if (MailHelper::isEmailAddress ($ user ->email ) && $ user ->sendEmail === 1 ) {
131
+ $ emailReceivers [$ user ->id ] ??= $ user ;
132
+ }
135
133
}
136
- } catch (\Exception $ exc ) {
137
- return $ ret ;
138
134
}
139
135
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 );
136
+ return $ emailReceivers ;
137
+ }
149
138
150
- if (empty ($ userIDs )) {
151
- return $ ret ;
152
- }
153
- } catch (\Exception $ exc ) {
154
- return $ ret ;
155
- }
139
+ /**
140
+ * Returns all Super Users
141
+ *
142
+ * @return array The list of super user groups.
143
+ *
144
+ * @since 5.4.0
145
+ */
146
+ private function getSuperUserGroups (): array
147
+ {
148
+ $ groups = UserGroupsHelper::getInstance ()->getAll ();
149
+ $ ret = [];
156
150
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 );
151
+ // Find groups with core.admin rights (super users)
152
+ foreach ($ groups as $ group ) {
153
+ if (Access::checkGroup ($ group ->id , 'core.admin ' )) {
154
+ $ ret [] = $ group ->id ;
169
155
}
170
-
171
- $ ret = $ db ->setQuery ($ query )->loadObjectList ();
172
- } catch (\Exception ) {
173
- return $ ret ;
174
156
}
175
157
176
158
return $ ret ;
0 commit comments