1616use Joomla \CMS \Form \Form ;
1717use Joomla \CMS \Language \Multilanguage ;
1818use Joomla \CMS \Language \Text ;
19+ use Joomla \CMS \Mail \MailTemplate ;
1920use Joomla \CMS \MVC \Factory \MVCFactoryInterface ;
2021use Joomla \CMS \MVC \Model \AdminModel ;
2122use Joomla \CMS \Plugin \PluginHelper ;
@@ -224,9 +225,8 @@ protected function preprocessForm(Form $form, $data, $group = 'user')
224225 */
225226 public function save ($ data )
226227 {
227- $ pk = (!empty ($ data ['id ' ])) ? $ data ['id ' ] : (int ) $ this ->getState ('user.id ' );
228- $ user = $ this ->getUserFactory ()->loadUserById ($ pk );
229-
228+ $ pk = (!empty ($ data ['id ' ])) ? $ data ['id ' ] : (int ) $ this ->getState ('user.id ' );
229+ $ user = $ this ->getUserFactory ()->loadUserById ($ pk );
230230 $ my = $ this ->getCurrentUser ();
231231 $ iAmSuperAdmin = $ my ->authorise ('core.admin ' );
232232
@@ -292,7 +292,7 @@ public function save($data)
292292 }
293293
294294 // Destroy all active sessions for the user after changing the password or blocking him
295- if (!empty ($ data ['password2 ' ]) || $ data ['block ' ]) {
295+ if (!empty ($ data ['password2 ' ]) || ! empty ( $ data ['block ' ]) ) {
296296 UserHelper::destroyUserSessions ($ user ->id , true );
297297 }
298298
@@ -380,8 +380,8 @@ public function delete(&$pks)
380380 */
381381 public function block (&$ pks , $ value = 1 )
382382 {
383- $ app = Factory::getApplication ();
384- $ user = $ this ->getCurrentUser ();
383+ $ app = Factory::getApplication ();
384+ $ user = $ this ->getCurrentUser ();
385385
386386 // Check if I am a Super Admin
387387 $ iAmSuperAdmin = $ user ->authorise ('core.admin ' );
@@ -484,26 +484,80 @@ public function block(&$pks, $value = 1)
484484 */
485485 public function activate (&$ pks )
486486 {
487- $ user = $ this ->getCurrentUser ();
487+ $ app = Factory::getApplication ();
488+ $ user = $ app ->getIdentity ();
488489
489- // Check if I am a Super Admin
490+ // Check if I am a super admin
490491 $ iAmSuperAdmin = $ user ->authorise ('core.admin ' );
491- $ table = $ this ->getTable ();
492- $ pks = (array ) $ pks ;
492+
493+ // Load user table
494+ $ table = $ this ->getTable ();
495+ $ pks = (array ) $ pks ;
496+
497+ // Compile the user activated notification mail default values.
498+ $ mailData = [];
499+ $ mailData ['siteurl ' ] = \Joomla \CMS \Uri \Uri::root ();
500+ $ mailData ['fromname ' ] = $ app ->get ('fromname ' );
501+ $ mailData ['mailfrom ' ] = $ app ->get ('mailfrom ' );
502+ $ mailData ['sitename ' ] = $ app ->get ('sitename ' );
503+
504+ // Load com_users site language strings, the mail template use it
505+ $ app ->getLanguage ()->load ('com_users ' , JPATH_SITE );
506+
507+ $ sendMailTo = function ($ userData ) use ($ app , $ mailData ) {
508+ $ mailData ['name ' ] = $ userData ['name ' ];
509+ $ mailData ['username ' ] = $ userData ['username ' ];
510+
511+ // Use the default language
512+ $ langTag = ComponentHelper::getParams ('com_languages ' )->get ('site ' , 'en-GB ' );
513+
514+ $ mailer = new MailTemplate ('com_users.registration.user.admin_activated ' , $ langTag );
515+ $ mailer ->addTemplateData ($ mailData );
516+ $ mailer ->addRecipient ($ userData ['email ' ]);
517+
518+ try {
519+ $ return = $ mailer ->send ();
520+ } catch (\Exception $ exception ) {
521+ try {
522+ \Joomla \CMS \Log \Log::add (Text::_ ($ exception ->getMessage ()), \Joomla \CMS \Log \Log::WARNING , 'jerror ' );
523+
524+ $ return = false ;
525+ } catch (\RuntimeException $ exception ) {
526+ $ app ->enqueueMessage (Text::_ ($ exception ->errorMessage ()), $ app ::MSG_WARNING );
527+
528+ $ return = false ;
529+ }
530+ }
531+
532+ // Check for an error.
533+ if ($ return !== true ) {
534+ $ app ->enqueueMessage (Text::_ ('COM_USERS_REGISTRATION_ACTIVATION_NOTIFY_SEND_MAIL_FAILED ' ), $ app ::MSG_WARNING );
535+
536+ return false ;
537+ }
538+
539+ $ app ->enqueueMessage (Text::_ ('COM_USERS_REGISTRATION_ACTIVATION_NOTIFY_SEND_MAIL_SUCCESS ' ), $ app ::MSG_INFO );
540+ return true ;
541+ };
493542
494543 PluginHelper::importPlugin ($ this ->events_map ['save ' ]);
495544
496- // Access checks.
545+ // Activate and send the notification email
497546 foreach ($ pks as $ i => $ pk ) {
498547 if ($ table ->load ($ pk )) {
499- $ old = $ table ->getProperties ();
500- $ allow = $ user ->authorise ('core.edit.state ' , 'com_users ' );
548+ $ prevUserData = $ table ->getProperties ();
549+ $ allow = $ user ->authorise ('core.edit.state ' , 'com_users ' );
501550
502- // Don't allow non-super-admin to delete a super admin
551+ // Don't allow non-super-admin to edit the active status of a super admin
503552 $ allow = (!$ iAmSuperAdmin && Access::check ($ pk , 'core.admin ' )) ? false : $ allow ;
504553
554+ // Ignore activated accounts but check if we can still
555+ // resend the notification email
505556 if (empty ($ table ->activation )) {
506- // Ignore activated accounts.
557+ if (\is_null ($ table ->lastvisitDate )) {
558+ $ sendMailTo ($ prevUserData );
559+ }
560+
507561 unset($ pks [$ i ]);
508562 } elseif ($ allow ) {
509563 $ table ->block = 0 ;
@@ -518,7 +572,7 @@ public function activate(&$pks)
518572 }
519573
520574 // Trigger the before save event.
521- $ result = Factory::getApplication ()->triggerEvent ($ this ->event_before_save , [$ old , false , $ table ->getProperties ()]);
575+ $ result = Factory::getApplication ()->triggerEvent ($ this ->event_before_save , [$ prevUserData , false , $ table ->getProperties ()]);
522576
523577 if (\in_array (false , $ result , true )) {
524578 // Plugin will have to raise it's own error or throw an exception.
@@ -534,6 +588,11 @@ public function activate(&$pks)
534588
535589 // Fire the after save event
536590 Factory::getApplication ()->triggerEvent ($ this ->event_after_save , [$ table ->getProperties (), false , true , null ]);
591+
592+ // Send the email
593+ if (!$ sendMailTo ($ prevUserData )) {
594+ return false ;
595+ }
537596 } catch (\Exception $ e ) {
538597 $ this ->setError ($ e ->getMessage ());
539598
@@ -542,7 +601,7 @@ public function activate(&$pks)
542601 } else {
543602 // Prune items that you can't change.
544603 unset($ pks [$ i ]);
545- Factory:: getApplication () ->enqueueMessage (Text::_ ('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED ' ), 'error ' );
604+ $ app ->enqueueMessage (Text::_ ('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED ' ), 'error ' );
546605 }
547606 }
548607 }
@@ -733,10 +792,10 @@ public function batchUser($groupId, $userIds, $action)
733792 // Remove the users from the group if requested.
734793 if (isset ($ doDelete )) {
735794 /*
736- * First we need to check that the user is part of more than one group
737- * otherwise we will end up with a user that is not part of any group
738- * unless we are moving the user to a new group.
739- */
795+ * First we need to check that the user is part of more than one group
796+ * otherwise we will end up with a user that is not part of any group
797+ * unless we are moving the user to a new group.
798+ */
740799 if ($ doDelete === 'group ' ) {
741800 $ query = $ db ->getQuery (true );
742801 $ query ->select ($ db ->quoteName ('user_id ' ))
@@ -877,8 +936,8 @@ public function getAssignedGroups($userId = null)
877936 $ userId = (!empty ($ userId )) ? $ userId : (int ) $ this ->getState ('user.id ' );
878937
879938 if (empty ($ userId )) {
880- $ result = [];
881- $ form = $ this ->getForm ();
939+ $ result = [];
940+ $ form = $ this ->getForm ();
882941
883942 if ($ form ) {
884943 $ groupsIDs = $ form ->getValue ('groups ' );
0 commit comments