mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-22 20:12:00 -06:00
Introduce proxy models for User and Group to organize admin UI
This commit is contained in:
parent
f65b2278f0
commit
90828cedae
@ -1,10 +1,10 @@
|
||||
from django import forms
|
||||
from django.contrib import admin
|
||||
from django.contrib.auth.admin import UserAdmin as UserAdmin_
|
||||
from django.contrib.auth.models import Group, User
|
||||
from django.contrib.auth.models import Group as StockGroup, User as StockUser
|
||||
|
||||
from extras.admin import order_content_types
|
||||
from .models import ObjectPermission, Token, UserConfig
|
||||
from .models import Group, User, ObjectPermission, Token, UserConfig
|
||||
|
||||
|
||||
#
|
||||
@ -12,8 +12,8 @@ from .models import ObjectPermission, Token, UserConfig
|
||||
#
|
||||
|
||||
# Unregister the built-in GroupAdmin and UserAdmin classes so that we can use our custom admin classes below
|
||||
admin.site.unregister(Group)
|
||||
admin.site.unregister(User)
|
||||
admin.site.unregister(StockGroup)
|
||||
admin.site.unregister(StockUser)
|
||||
|
||||
|
||||
@admin.register(Group)
|
||||
|
@ -1,32 +0,0 @@
|
||||
# Generated by Django 3.0.6 on 2020-05-28 18:24
|
||||
|
||||
from django.conf import settings
|
||||
import django.contrib.postgres.fields.jsonb
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('contenttypes', '0002_remove_content_type_name'),
|
||||
('auth', '0011_update_proxy_permissions'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('users', '0006_create_userconfigs'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='ObjectPermission',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False)),
|
||||
('attrs', django.contrib.postgres.fields.jsonb.JSONField(blank=True, null=True)),
|
||||
('can_view', models.BooleanField(default=False)),
|
||||
('can_add', models.BooleanField(default=False)),
|
||||
('can_change', models.BooleanField(default=False)),
|
||||
('can_delete', models.BooleanField(default=False)),
|
||||
('content_types', models.ManyToManyField(limit_choices_to={'app_label__in': ['circuits', 'dcim', 'extras', 'ipam', 'secrets', 'tenancy', 'virtualization']}, related_name='object_permissions', to='contenttypes.ContentType')),
|
||||
('groups', models.ManyToManyField(blank=True, related_name='object_permissions', to='auth.Group')),
|
||||
('users', models.ManyToManyField(blank=True, related_name='object_permissions', to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
]
|
44
netbox/users/migrations/0007_proxy_group_user.py
Normal file
44
netbox/users/migrations/0007_proxy_group_user.py
Normal file
@ -0,0 +1,44 @@
|
||||
# Generated by Django 3.0.6 on 2020-05-29 14:30
|
||||
|
||||
import django.contrib.auth.models
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('auth', '0011_update_proxy_permissions'),
|
||||
('users', '0006_create_userconfigs'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Group',
|
||||
fields=[
|
||||
],
|
||||
options={
|
||||
'proxy': True,
|
||||
'indexes': [],
|
||||
'constraints': [],
|
||||
},
|
||||
bases=('auth.group',),
|
||||
managers=[
|
||||
('objects', django.contrib.auth.models.GroupManager()),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='User',
|
||||
fields=[
|
||||
],
|
||||
options={
|
||||
'proxy': True,
|
||||
'indexes': [],
|
||||
'constraints': [],
|
||||
},
|
||||
bases=('auth.user',),
|
||||
managers=[
|
||||
('objects', django.contrib.auth.models.UserManager()),
|
||||
],
|
||||
),
|
||||
]
|
@ -1,18 +1,16 @@
|
||||
import binascii
|
||||
import os
|
||||
|
||||
from django.contrib.auth.models import Group, User
|
||||
from django.contrib.auth.models import Group as Group_, User as User_
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.contrib.postgres.fields import JSONField
|
||||
from django.core.exceptions import FieldError, ValidationError
|
||||
from django.core.validators import MinLengthValidator
|
||||
from django.db import models
|
||||
from django.db.models import Q
|
||||
from django.db.models.signals import post_save
|
||||
from django.dispatch import receiver
|
||||
from django.utils import timezone
|
||||
|
||||
from utilities.permissions import resolve_permission
|
||||
from utilities.utils import flatten_dict
|
||||
|
||||
|
||||
@ -23,6 +21,30 @@ __all__ = (
|
||||
)
|
||||
|
||||
|
||||
#
|
||||
# Proxy models for admin
|
||||
#
|
||||
|
||||
class Group(Group_):
|
||||
"""
|
||||
Proxy contrib.auth.models.Group for the admin UI
|
||||
"""
|
||||
class Meta:
|
||||
proxy = True
|
||||
|
||||
|
||||
class User(User_):
|
||||
"""
|
||||
Proxy contrib.auth.models.User for the admin UI
|
||||
"""
|
||||
class Meta:
|
||||
proxy = True
|
||||
|
||||
|
||||
#
|
||||
# User preferences
|
||||
#
|
||||
|
||||
class UserConfig(models.Model):
|
||||
"""
|
||||
This model stores arbitrary user-specific preferences in a JSON data structure.
|
||||
@ -143,6 +165,10 @@ def create_userconfig(instance, created, **kwargs):
|
||||
UserConfig(user=instance).save()
|
||||
|
||||
|
||||
#
|
||||
# REST API
|
||||
#
|
||||
|
||||
class Token(models.Model):
|
||||
"""
|
||||
An API token used for user authentication. This extends the stock model to allow each user to have multiple tokens.
|
||||
@ -197,6 +223,10 @@ class Token(models.Model):
|
||||
return True
|
||||
|
||||
|
||||
#
|
||||
# Permissions
|
||||
#
|
||||
|
||||
class ObjectPermission(models.Model):
|
||||
"""
|
||||
A mapping of view, add, change, and/or delete permission for users and/or groups to an arbitrary set of objects
|
||||
|
Loading…
Reference in New Issue
Block a user