Skip to content

Commit 6c61467

Browse files
authored
feat: Add django admin cmd for adding explicit plugin access (#22172)
1 parent 839aed7 commit 6c61467

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import logging
2+
import uuid
3+
4+
import structlog
5+
from django.core.management import CommandError
6+
from django.core.management.base import BaseCommand
7+
8+
from posthog.models import Organization
9+
from posthog.models.plugin import Plugin
10+
11+
logger = structlog.get_logger(__name__)
12+
logger.setLevel(logging.INFO)
13+
14+
15+
class Command(BaseCommand):
16+
help = "Change Plugin private access (either add or remove depending on state)."
17+
18+
def add_arguments(self, parser):
19+
parser.add_argument("--organization-id", default=None, type=uuid.UUID, help="Specify the organization.")
20+
parser.add_argument("--plugin-id", default=None, type=int, help="Specify the plugin.")
21+
parser.add_argument("--live-run", action="store_true", help="Run changes, default is dry-run")
22+
23+
def handle(self, *args, **options):
24+
run(options)
25+
26+
27+
def run(options):
28+
live_run = options["live_run"]
29+
30+
if options["plugin_id"] is None:
31+
raise CommandError("You must specify --plugin-id to run this script")
32+
33+
if options["organization_id"] is None:
34+
raise CommandError("You must specify --organization-id to run this script")
35+
36+
plugin_id = options["plugin_id"]
37+
organization_id = options["organization_id"]
38+
39+
plugin = Plugin.objects.get(pk=plugin_id)
40+
current_organizations = plugin.has_private_access.all()
41+
logger.info(
42+
f"Plugin {plugin.name} is currently explicitly allowed for organizations: [{', '.join([str(org.name) for org in current_organizations])}]"
43+
)
44+
45+
org = Organization.objects.get(pk=organization_id)
46+
org_current_plugins = org.plugin_set.all()
47+
logger.info(
48+
f"Organization {org.name} currently has explicit access to plugins: [{', '.join([str(plugin.name) for plugin in org_current_plugins])}]"
49+
)
50+
51+
has_access = org in current_organizations
52+
logger.info(
53+
f"Target organization {organization_id} is named {org.name} currently {'has access' if has_access else 'does not have access'}"
54+
)
55+
56+
if has_access:
57+
if live_run:
58+
plugin.has_private_access.remove(org)
59+
logger.info("Removed access")
60+
else:
61+
logger.info("Skipping the access removal, pass --live-run to run it")
62+
else:
63+
if live_run:
64+
plugin.has_private_access.add(org)
65+
logger.info("Added access")
66+
else:
67+
logger.info("Skipping the access addition, pass --live-run to run it")

0 commit comments

Comments
 (0)