From 30d748969fe423a086ca8c184095ea4136813253 Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 4 Nov 2026 14:32:23 +0100 Subject: [PATCH] fix: shorten email permission codenames to fit NetBox action limit NetBox ObjectPermission actions are limited to 30 characters, so assigning manage_softwarelicense_email_notifications or manage_certificate_email_notifications to a group failed validation. Rename them to manage_license_emails and manage_certificate_emails and migrate existing auth permissions in place to keep assignments. Co-Authored-By: Claude Opus 5.5 --- README.md | 4 +- netbox_slm/api/serializers.py | 4 +- ...0017_shorten_email_permission_codenames.py | 48 +++++++++++++++++++ netbox_slm/models.py | 4 +- netbox_slm/views/certificate.py | 2 +- netbox_slm/views/software_license.py | 2 +- 6 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 netbox_slm/migrations/0017_shorten_email_permission_codenames.py diff --git a/README.md b/README.md index 9432f79..62ac6dd 100644 --- a/README.md +++ b/README.md @@ -149,8 +149,8 @@ on the license, the plugin also sends an e-mail to the address stored in the user account; NetBox SMTP must be configured for this. Enabling e-mail delivery can only be changed by administrators or users with -the permission `netbox_slm.manage_softwarelicense_email_notifications` -(licenses) or `netbox_slm.manage_certificate_email_notifications` +the permission `netbox_slm.manage_license_emails` +(licenses) or `netbox_slm.manage_certificate_emails` (certificates). SMTP credentials are maintained only in the NetBox server configuration and are not visible in the plugin. diff --git a/netbox_slm/api/serializers.py b/netbox_slm/api/serializers.py index 3a6c5cb..c95210a 100644 --- a/netbox_slm/api/serializers.py +++ b/netbox_slm/api/serializers.py @@ -66,7 +66,7 @@ class SoftwareLicenseSerializer(NetBoxModelSerializer): if ( "email_notifications" in attrs and request is not None - and not request.user.has_perm("netbox_slm.manage_softwarelicense_email_notifications") + and not request.user.has_perm("netbox_slm.manage_license_emails") ): raise serializers.ValidationError( {"email_notifications": _("You do not have permission to change email notifications.")} @@ -248,7 +248,7 @@ class CertificateSerializer(NetBoxModelSerializer): if ( "email_notifications" in attrs and request is not None - and not request.user.has_perm("netbox_slm.manage_certificate_email_notifications") + and not request.user.has_perm("netbox_slm.manage_certificate_emails") ): raise serializers.ValidationError( {"email_notifications": _("You do not have permission to change email notifications.")} diff --git a/netbox_slm/migrations/0017_shorten_email_permission_codenames.py b/netbox_slm/migrations/0017_shorten_email_permission_codenames.py new file mode 100644 index 0000000..7d177b0 --- /dev/null +++ b/netbox_slm/migrations/0017_shorten_email_permission_codenames.py @@ -0,0 +1,48 @@ +from django.db import migrations + +# NetBox ObjectPermission actions are limited to 30 characters. +RENAMES = [ + ("softwarelicense", "manage_softwarelicense_email_notifications", "manage_license_emails"), + ("certificate", "manage_certificate_email_notifications", "manage_certificate_emails"), +] + + +def rename_permissions(apps, schema_editor, reverse=False): + Permission = apps.get_model("auth", "Permission") + for model, old, new in RENAMES: + if reverse: + old, new = new, old + Permission.objects.filter(content_type__app_label="netbox_slm", content_type__model=model, codename=old).update( + codename=new + ) + + +def reverse_rename_permissions(apps, schema_editor): + rename_permissions(apps, schema_editor, reverse=True) + + +class Migration(migrations.Migration): + dependencies = [ + ("auth", "0001_initial"), + ("netbox_slm", "0016_certificate"), + ] + + operations = [ + migrations.AlterModelOptions( + name="softwarelicense", + options={ + "permissions": [("manage_license_emails", "Can manage license email notifications")], + "verbose_name": "software license", + "verbose_name_plural": "software licenses", + }, + ), + migrations.AlterModelOptions( + name="certificate", + options={ + "permissions": [("manage_certificate_emails", "Can manage certificate email notifications")], + "verbose_name": "certificate", + "verbose_name_plural": "certificates", + }, + ), + migrations.RunPython(rename_permissions, reverse_rename_permissions), + ] diff --git a/netbox_slm/models.py b/netbox_slm/models.py index cea0d06..8f6e24b 100644 --- a/netbox_slm/models.py +++ b/netbox_slm/models.py @@ -327,7 +327,7 @@ class SoftwareLicense(NetBoxModel, ExpirationNotificationModel): verbose_name = _("software license") verbose_name_plural = _("software licenses") permissions = [ - ("manage_softwarelicense_email_notifications", _("Can manage license email notifications")), + ("manage_license_emails", _("Can manage license email notifications")), ] def __str__(self): @@ -459,7 +459,7 @@ class Certificate(NetBoxModel, ExpirationNotificationModel): verbose_name = _("certificate") verbose_name_plural = _("certificates") permissions = [ - ("manage_certificate_email_notifications", _("Can manage certificate email notifications")), + ("manage_certificate_emails", _("Can manage certificate email notifications")), ] def __str__(self): diff --git a/netbox_slm/views/certificate.py b/netbox_slm/views/certificate.py index 85c9295..8cbc567 100644 --- a/netbox_slm/views/certificate.py +++ b/netbox_slm/views/certificate.py @@ -6,7 +6,7 @@ from utilities.views import register_model_view class CertificateEmailNotificationPermissionMixin(EmailNotificationPermissionMixin): - email_permission = "netbox_slm.manage_certificate_email_notifications" + email_permission = "netbox_slm.manage_certificate_emails" @register_model_view(Certificate, "list", path="", detail=False) diff --git a/netbox_slm/views/software_license.py b/netbox_slm/views/software_license.py index e5c524b..35bb8c3 100644 --- a/netbox_slm/views/software_license.py +++ b/netbox_slm/views/software_license.py @@ -8,7 +8,7 @@ from utilities.views import register_model_view class EmailNotificationPermissionMixin: """Prevent users without the dedicated permission from changing SMTP delivery.""" - email_permission = "netbox_slm.manage_softwarelicense_email_notifications" + email_permission = "netbox_slm.manage_license_emails" def dispatch(self, request, *args, **kwargs): form_attribute = "model_form" if hasattr(self, "model_form") else "form"