Skip to content

Commit

Permalink
feat: Add django admin cmd for adding explicit plugin access (#22172)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiina303 committed May 9, 2024
1 parent 839aed7 commit 6c61467
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions posthog/management/commands/change_plugin_private_access.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import logging
import uuid

import structlog
from django.core.management import CommandError
from django.core.management.base import BaseCommand

from posthog.models import Organization
from posthog.models.plugin import Plugin

logger = structlog.get_logger(__name__)
logger.setLevel(logging.INFO)


class Command(BaseCommand):
help = "Change Plugin private access (either add or remove depending on state)."

def add_arguments(self, parser):
parser.add_argument("--organization-id", default=None, type=uuid.UUID, help="Specify the organization.")
parser.add_argument("--plugin-id", default=None, type=int, help="Specify the plugin.")
parser.add_argument("--live-run", action="store_true", help="Run changes, default is dry-run")

def handle(self, *args, **options):
run(options)


def run(options):
live_run = options["live_run"]

if options["plugin_id"] is None:
raise CommandError("You must specify --plugin-id to run this script")

if options["organization_id"] is None:
raise CommandError("You must specify --organization-id to run this script")

plugin_id = options["plugin_id"]
organization_id = options["organization_id"]

plugin = Plugin.objects.get(pk=plugin_id)
current_organizations = plugin.has_private_access.all()
logger.info(
f"Plugin {plugin.name} is currently explicitly allowed for organizations: [{', '.join([str(org.name) for org in current_organizations])}]"
)

org = Organization.objects.get(pk=organization_id)
org_current_plugins = org.plugin_set.all()
logger.info(
f"Organization {org.name} currently has explicit access to plugins: [{', '.join([str(plugin.name) for plugin in org_current_plugins])}]"
)

has_access = org in current_organizations
logger.info(
f"Target organization {organization_id} is named {org.name} currently {'has access' if has_access else 'does not have access'}"
)

if has_access:
if live_run:
plugin.has_private_access.remove(org)
logger.info("Removed access")
else:
logger.info("Skipping the access removal, pass --live-run to run it")
else:
if live_run:
plugin.has_private_access.add(org)
logger.info("Added access")
else:
logger.info("Skipping the access addition, pass --live-run to run it")

0 comments on commit 6c61467

Please sign in to comment.