Skip to content

Commit

Permalink
Properly filter for local admins emails
Browse files Browse the repository at this point in the history
Originally the mail.local_administrators only returned users who
were members of the default builtin local administrators privilege
by virtue of auxiliary group membership. This commit makes the
logic more generic. Since the method was originally written we
expanded user.query output to include information about the
user's roles and so we can select all users with FULL_ADMIN
privileges (which may be granted by non-default privilege sets).
  • Loading branch information
anodos325 committed Sep 13, 2024
1 parent fbe2864 commit 3fa21be
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/middlewared/middlewared/plugins/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,11 +492,11 @@ def _from_addr(self, config):

@private
async def local_administrators_emails(self):
return list(set(
user["email"]
for user in await self.middleware.call("privilege.local_administrators")
if user["email"]
))
return list(set(user["email"] for user in await self.middleware.call("user.query", [
["roles", "rin", "FULL_ADMIN"],
["local", "=", True],
["email", "!=", None]
])))

@private
async def local_administrator_email(self):
Expand Down
27 changes: 27 additions & 0 deletions tests/api2/test_mail_admins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest

from middlewared.test.integration.assets.account import user
from middlewared.test.integration.utils import call

MAILUSER = 'wilbur'
MAILADDR = '[email protected]'
PASSWD = 'abcd1234'


@pytest.fixture(scope='module')
def full_admin_user():
ba_id = call('group.query', [['gid', '=', 544]], {'get': True})['id']
with user({
'username': MAILUSER,
'full_name': MAILUSER,
'group_create': False,
'email': '[email protected]',
'group': ba_id,
'password': PASSWD
}, get_instance=True) as u:
yield u


def test_mail_administrators(full_admin_user):
emails = call('mail.local_administrators_emails')
assert MAILADDR in emails

0 comments on commit 3fa21be

Please sign in to comment.