mirror of
https://github.com/netbox-community/netbox.git
synced 2026-01-11 06:12:16 -06:00
Some checks failed
CI / build (20.x, 3.12) (push) Has been cancelled
CI / build (20.x, 3.13) (push) Has been cancelled
CodeQL / Analyze (${{ matrix.language }}) (none, actions) (push) Has been cancelled
CodeQL / Analyze (${{ matrix.language }}) (none, javascript-typescript) (push) Has been cancelled
CodeQL / Analyze (${{ matrix.language }}) (none, python) (push) Has been cancelled
* feat(users): Add support for enabling/disabling Tokens Introduce an `enabled` flag on the `Token` model to allow temporarily revoking API tokens without deleting them. Update forms, serializers, and views to expose the new field. Enforce the `enabled` flag in token authentication. Add model, API, and authentication tests for the new behavior. Fixes #20834 * Fix authentication test --------- Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>
89 lines
2.2 KiB
Python
89 lines
2.2 KiB
Python
from django import forms
|
|
from django.utils.translation import gettext as _
|
|
from users.models import *
|
|
from users.choices import TokenVersionChoices
|
|
from utilities.forms import CSVModelForm
|
|
from utilities.forms.fields import CSVModelChoiceField, CSVModelMultipleChoiceField
|
|
|
|
|
|
__all__ = (
|
|
'GroupImportForm',
|
|
'OwnerGroupImportForm',
|
|
'OwnerImportForm',
|
|
'UserImportForm',
|
|
'TokenImportForm',
|
|
)
|
|
|
|
|
|
class GroupImportForm(CSVModelForm):
|
|
|
|
class Meta:
|
|
model = Group
|
|
fields = ('name', 'description')
|
|
|
|
|
|
class UserImportForm(CSVModelForm):
|
|
|
|
class Meta:
|
|
model = User
|
|
fields = (
|
|
'username', 'first_name', 'last_name', 'email', 'password', 'is_active', 'is_superuser'
|
|
)
|
|
|
|
def save(self, *args, **kwargs):
|
|
# Set the hashed password
|
|
self.instance.set_password(self.cleaned_data.get('password'))
|
|
|
|
return super().save(*args, **kwargs)
|
|
|
|
|
|
class TokenImportForm(CSVModelForm):
|
|
version = forms.ChoiceField(
|
|
choices=TokenVersionChoices,
|
|
initial=TokenVersionChoices.V2,
|
|
required=False,
|
|
help_text=_("Specify version 1 or 2 (v2 will be used by default)")
|
|
)
|
|
token = forms.CharField(
|
|
label=_('Token'),
|
|
required=False,
|
|
help_text=_("If no token is provided, one will be generated automatically.")
|
|
)
|
|
|
|
class Meta:
|
|
model = Token
|
|
fields = ('user', 'version', 'token', 'enabled', 'write_enabled', 'expires', 'description',)
|
|
|
|
|
|
class OwnerGroupImportForm(CSVModelForm):
|
|
|
|
class Meta:
|
|
model = OwnerGroup
|
|
fields = (
|
|
'name', 'description',
|
|
)
|
|
|
|
|
|
class OwnerImportForm(CSVModelForm):
|
|
group = CSVModelChoiceField(
|
|
queryset=OwnerGroup.objects.all(),
|
|
required=False,
|
|
to_field_name='name',
|
|
)
|
|
user_groups = CSVModelMultipleChoiceField(
|
|
queryset=Group.objects.all(),
|
|
required=False,
|
|
to_field_name='name',
|
|
)
|
|
users = CSVModelMultipleChoiceField(
|
|
queryset=User.objects.all(),
|
|
required=False,
|
|
to_field_name='username',
|
|
)
|
|
|
|
class Meta:
|
|
model = Owner
|
|
fields = (
|
|
'group', 'name', 'description', 'user_groups', 'users',
|
|
)
|