Skip to content

Commit a6fb94d

Browse files
authored
Merge pull request #36886 from dimagi/ay/new-superuser-access-to-edit-ff
Grant Edit access to all feature flag categories when creating a new superuser from command line.
2 parents 2f012e4 + aa488c5 commit a6fb94d

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

corehq/apps/domain/management/commands/make_superuser.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
from corehq.apps.hqadmin.views.users import send_email_notif
99
from corehq.util.signals import signalcommand
10+
from corehq.toggles import ALL_TAGS
11+
from corehq.toggles.sql_models import ToggleEditPermission
12+
1013

1114
logger = logging.getLogger(__name__)
1215

@@ -30,8 +33,11 @@ def get_password_from_user():
3033
def handle(self, username, **options):
3134
if not settings.ALLOW_MAKE_SUPERUSER_COMMAND:
3235
from dimagi.utils.web import get_site_domain
33-
raise CommandError(f"""You cannot run this command in SaaS Enviornments.
34-
Use https://{get_site_domain()}/hq/admin/superuser_management/ for granting superuser permissions""")
36+
raise CommandError(
37+
"You cannot run this command in SaaS environments. "
38+
f"Use https://{get_site_domain()}/hq/admin/superuser_management/ "
39+
"for granting superuser permissions."
40+
)
3541
from corehq.apps.users.models import WebUser
3642
try:
3743
validate_email(username)
@@ -55,6 +61,8 @@ def handle(self, username, **options):
5561
couch_user.is_staff = True
5662
couch_user.can_assign_superuser = True
5763

64+
toggle_permission_changes = self.grant_all_tags_edit_permissions(couch_user.username)
65+
5866
if is_superuser_changed or is_staff_changed or can_assign_superuser_changed:
5967
couch_user.save()
6068

@@ -81,4 +89,22 @@ def handle(self, username, **options):
8189
logger.info("✓ User {} can assign superuser privilege".format(couch_user.username))
8290
fields_changed['same_management_privilege'] = couch_user.can_assign_superuser
8391

92+
if toggle_permission_changes['added']:
93+
logger.info("→ User {} can now edit all feature flags".format(couch_user.username))
94+
fields_changed['toggle_edit_permissions'] = toggle_permission_changes
95+
else:
96+
logger.info("✓ User {} can edit all feature flags".format(couch_user.username))
97+
8498
send_email_notif([fields_changed], changed_by_user='The make_superuser command')
99+
100+
@staticmethod
101+
def grant_all_tags_edit_permissions(username):
102+
toggle_permission_changes = {'added': [], 'removed': []}
103+
for tag in ALL_TAGS:
104+
toggle_permission = ToggleEditPermission.objects.get_by_tag_slug(tag.slug)
105+
if not toggle_permission:
106+
toggle_permission = ToggleEditPermission(tag_slug=tag.slug)
107+
if username not in toggle_permission.enabled_users:
108+
toggle_permission.add_users([username])
109+
toggle_permission_changes['added'].append(tag.name)
110+
return toggle_permission_changes

corehq/apps/domain/management/commands/tests/test_make_superuser.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,22 @@
77
from corehq.apps.users.models import FakeUser
88

99

10+
@patch("corehq.apps.domain.management.commands.make_superuser.Command.grant_all_tags_edit_permissions")
1011
class TestEmailValidation(SimpleTestCase):
1112
"""Tests the expected behavior of the management command's use of the
1213
`email_validator` library.
1314
"""
1415

15-
def test_make_superuser(self):
16+
def test_make_superuser(self, *args):
1617
with patch_fake_webuser():
1718
call_command("make_superuser", "[email protected]") # does not raise
1819

19-
def test_make_superuser_allows_special_domains(self):
20+
def test_make_superuser_allows_special_domains(self, *args):
2021
# as-built with email_validator version 1.1.3
2122
with patch_fake_webuser():
2223
call_command("make_superuser", "[email protected]") # does not raise
2324

24-
def test_make_superuser_rejects_invalid_email_syntax(self):
25+
def test_make_superuser_rejects_invalid_email_syntax(self, *args):
2526
with patch_fake_webuser(), self.assertRaises(CommandError) as test:
2627
call_command("make_superuser", "somebody_at_dimagi.com")
2728
self.assertIsInstance(test.exception.__cause__, ValidationError)

0 commit comments

Comments
 (0)