Skip to content

Commit

Permalink
Add model to record privileges from legacy toggles and add form-link-…
Browse files Browse the repository at this point in the history
…workflow to it
  • Loading branch information
sravfeyn committed Jan 17, 2023
1 parent a5494ba commit bf6fbbf
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 1 deletion.
23 changes: 23 additions & 0 deletions corehq/apps/accounting/migrations/0065_toggleprivilegerecord.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.2.16 on 2023-01-17 04:56

import django.contrib.postgres.fields
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('accounting', '0064_add_form_link_workflow_priv'),
]

operations = [
migrations.CreateModel(
name='TogglePrivilegeRecord',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('privilege_slug', models.CharField(max_length=256, unique=True)),
('toggle_slug', models.CharField(max_length=256, unique=True)),
('enabled_domains', django.contrib.postgres.fields.ArrayField(base_field=models.CharField(max_length=256), size=None)),
],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 3.2.16 on 2023-01-17 04:57

from django.db import migrations
from corehq.privileges import FORM_LINK_WORKFLOW
from corehq.util.django_migrations import skip_on_fresh_install


@skip_on_fresh_install
def create_record(apps, schema_editor):
from corehq.apps.accounting.models import TogglePrivilegeRecord
TogglePrivilegeRecord.create_from_toggle(
FORM_LINK_WORKFLOW,
FORM_LINK_WORKFLOW
)


@skip_on_fresh_install
def delete_record(apps, schema_editor):
from corehq.apps.accounting.models import TogglePrivilegeRecord
TogglePrivilegeRecord.get_by_privilege_slug(
FORM_LINK_WORKFLOW,
).delete()


class Migration(migrations.Migration):

dependencies = [
('accounting', '0065_toggleprivilegerecord'),
]


operations = [
migrations.RunPython(create_record, delete_record),
]
53 changes: 53 additions & 0 deletions corehq/apps/accounting/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3847,3 +3847,56 @@ class InvoiceCommunicationHistory(CommunicationHistoryBase):

class CustomerInvoiceCommunicationHistory(CommunicationHistoryBase):
invoice = models.ForeignKey(CustomerInvoice, on_delete=models.PROTECT)


class TogglePrivilegeRecord(models.Model):
"""
A special one-off model to record privileges that
were historically a feature-flag/toggle with domains
that may not have the privilege anymore
"""

privilege_slug = models.CharField(
max_length=256,
unique=True,
)
toggle_slug = models.CharField(
max_length=256,
unique=True,
)
enabled_domains = ArrayField(
models.CharField(max_length=256)
)

class Meta(object):
app_label = 'accounting'

@classmethod
def create_from_toggle(cls, toggle_slug, privilege_slug):
from corehq.toggles import StaticToggle
# StaticToggle could be removed from corehq.toggles
# code base, but the associated Toggle could still exist
# so initialize one here just to fetch enabled domains
domains = StaticToggle(toggle_slug, '', '').get_enabled_domains()
# hardfail with IntegrityError if created again
cls.objects.create(
privilege_slug=privilege_slug,
toggle_slug=toggle_slug,
enabled_domains=domains
)

@classmethod
@quickcache(['privilege_slug'], timeout=60 * 60)
def get_by_privilege_slug(cls, privilege_slug):
try:
return cls.objects.get(privilege_slug=privilege_slug)
except cls.DoesNotExist:
return None

@classmethod
def enabled_for_domain(cls, privilege_slug, domain):
record = cls.get_by_privilege_slug(privilege_slug)
if record:
return domain in record.enabled_domains
else:
return False
6 changes: 5 additions & 1 deletion corehq/apps/accounting/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ def domain_has_privilege_cache_args(domain, privilege_slug, **assignment):

@quickcache(domain_has_privilege_cache_args, timeout=10)
def domain_has_privilege(domain, privilege_slug, **assignment):
from corehq.apps.accounting.models import Subscription
from corehq.apps.accounting.models import Subscription, TogglePrivilegeRecord
if TogglePrivilegeRecord.enabled_for_domain(privilege_slug, domain):
# If it were historically a toggle and was enabled, return True
# regardless of subscription
return True
try:
plan_version = Subscription.get_subscribed_plan_by_domain(domain)
privilege = Role.get_privilege(privilege_slug, assignment)
Expand Down
2 changes: 2 additions & 0 deletions migrations.lock
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ accounting
0062_add_release_management_to_enterprise
0063_replace_linked_projects_ff_with_erm
0064_add_form_link_workflow_priv
0065_toggleprivilegerecord
0066_record_toggle_priv_for_form_link_workflow
admin
0001_initial
0002_logentry_remove_auto_add
Expand Down

0 comments on commit bf6fbbf

Please sign in to comment.