0

Where a component wants to notify a super admin of some event, what is the best way to obtain a super admin email address?

(For background, I originally coded this as a database query, looking for users who were members of the group with title "Super Users". However, I discovered that this only works for an English site as the group title is altered to the local language. It would be possible to look for members of the group with ID 8, but hard coding a number seems a poor solution).

4
  • Why would hard coding a number be a poor solution? You can add a comment in the code to indicate who group 8 refers to. If you ever think you might need it to email some other group how about making the group a parameter that can be configured in the component's admin menu?
    – Huw Evans
    Commented Sep 4, 2022 at 14:48
  • Because the numbering used for groups is internal to Joomla. I don't know that there is any commitment to it not changing at some arbitrary time.
    – mbrampton
    Commented Sep 5, 2022 at 16:34
  • Have you considered using the "receive system email" flag on the user records? I assuming you don't have too many Super Users and all receive the system notifications? Commented Sep 6, 2022 at 9:17
  • Great idea, I guess that is what it is for!
    – mbrampton
    Commented Sep 8, 2022 at 4:53

2 Answers 2

1

Based on the comment by @Glen Arkell the most sensible thing seems to be to send admin emails to people who have the "receive system email" flag set.

0

There is no helper function for this but there are a few places in core extensions where this is done. The first part is getting the user groups with super admin permissions:

use Joomla\CMS\Access\Access;
use Joomla\CMS\Table\Table;

// Get root asset ID
$rootId = Table::getInstance('Asset')->getRootId();

// Get root asset rules.
$rules = Access::getAssetRules($rootId)->getData();

// Get root asset user groups.
$groups = $rules['core.admin'] ? $rules['core.admin']->getData() : [];

// Filter out disallowed groups.
$superUserGroups = array_filter($groups);

Once you have these, make a database query to get the emails:

use Joomla\CMS\Factory;

if ($superUserGroups)
{
    $db = Factory::getDbo(true);
    $query = $db->getQuery(true)
        ->select('u.email')
        ->from('#__users AS u')
        ->join(
            'INNER',
            '#__user_usergroup_map AS g',
            'u.id = g.user_id',
        )
        ->whereIn('g.group_id', $superUserGroups);

    // Load the email addresses and remove duplicates.
    $superUserEmails = array_unique($db->setQuery($query)->loadColumn());
}

You can, of course, modify the above query with additional conditions to ensure only unblocked users and/or only users with "Receive System Email" option are selected:

$query->where('u.block = 0')
    ->where('u.sendEmail = 1');

Also instead of removing duplicates with array_unique(), the query can be modified to use DISTINCT or GROUP clause to retrieve a unique list in the first place. But this would probably be slower in most cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.