Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature 15492 - Copying of Admin->Authentication->Permissions #15976

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions netbox/users/models/permissions.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import json

from django.contrib.postgres.fields import ArrayField
from django.db import models
from django.urls import reverse
from django.utils.translation import gettext_lazy as _

from users.constants import OBJECTPERMISSION_OBJECT_TYPES

from utilities.querysets import RestrictedQuerySet

__all__ = (
Expand Down Expand Up @@ -47,6 +50,8 @@ class ObjectPermission(models.Model):

objects = RestrictedQuerySet.as_manager()

clone_fields = ['name', 'description', 'enabled', 'object_types', 'actions', 'constraints']

class Meta:
ordering = ['name']
verbose_name = _('permission')
Expand Down Expand Up @@ -81,3 +86,18 @@ def list_constraints(self):

def get_absolute_url(self):
return reverse('users:objectpermission', args=[self.pk])

def clone(self):
attrs = {}

for field_name in getattr(self, 'clone_fields', []):
field = self._meta.get_field(field_name)
field_value = field.value_from_object(self)
if field_value and isinstance(field, models.ManyToManyField):
attrs[field_name] = [v.pk for v in field_value]
elif field_value and isinstance(field, models.JSONField):
attrs[field_name] = json.dumps(field_value)
elif field_value not in (None, ''):
attrs[field_name] = field_value

return attrs