mirror of https://github.com/jumpserver/jumpserver
parent
10e3100d3c
commit
a18f544cf8
@ -0,0 +1,21 @@
|
||||
# Generated by Django 3.2.14 on 2022-12-02 02:48
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
def migrate_login_type(apps, schema_editor):
|
||||
login_asset_model = apps.get_model('acls', 'LoginAssetACL')
|
||||
login_asset_model.objects.filter(action='login_confirm').update(action='review')
|
||||
|
||||
login_system_model = apps.get_model('acls', 'LoginACL')
|
||||
login_system_model.objects.filter(action='confirm').update(action='review')
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
('acls', '0006_commandfilteracl_commandgroup'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(migrate_login_type),
|
||||
]
|
@ -0,0 +1,94 @@
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from rest_framework import serializers
|
||||
|
||||
from acls.models.base import ActionChoices
|
||||
from common.drf.fields import LabeledChoiceField, ObjectRelatedField
|
||||
from orgs.models import Organization
|
||||
from users.models import User
|
||||
|
||||
common_help_text = _(
|
||||
"Format for comma-delimited string, with * indicating a match all. "
|
||||
)
|
||||
|
||||
|
||||
class ACLUsersSerializer(serializers.Serializer):
|
||||
username_group = serializers.ListField(
|
||||
default=["*"],
|
||||
child=serializers.CharField(max_length=128),
|
||||
label=_("Username"),
|
||||
help_text=common_help_text,
|
||||
)
|
||||
|
||||
|
||||
class ACLAssestsSerializer(serializers.Serializer):
|
||||
address_group_help_text = _(
|
||||
"Format for comma-delimited string, with * indicating a match all. "
|
||||
"Such as: "
|
||||
"192.168.10.1, 192.168.1.0/24, 10.1.1.1-10.1.1.20, 2001:db8:2de::e13, 2001:db8:1a:1110::/64"
|
||||
" (Domain name support)"
|
||||
)
|
||||
|
||||
name_group = serializers.ListField(
|
||||
default=["*"],
|
||||
child=serializers.CharField(max_length=128),
|
||||
label=_("Name"),
|
||||
help_text=common_help_text,
|
||||
)
|
||||
address_group = serializers.ListField(
|
||||
default=["*"],
|
||||
child=serializers.CharField(max_length=1024),
|
||||
label=_("IP/Host"),
|
||||
help_text=address_group_help_text,
|
||||
)
|
||||
|
||||
|
||||
class ACLAccountsSerializer(serializers.Serializer):
|
||||
username_group = serializers.ListField(
|
||||
default=["*"],
|
||||
child=serializers.CharField(max_length=128),
|
||||
label=_("Username"),
|
||||
help_text=common_help_text,
|
||||
)
|
||||
|
||||
|
||||
class BaseUserAssetAccountACLSerializerMixin(serializers.Serializer):
|
||||
users = ACLUsersSerializer()
|
||||
assets = ACLAssestsSerializer()
|
||||
accounts = ACLAccountsSerializer()
|
||||
reviewers = ObjectRelatedField(
|
||||
queryset=User.objects, many=True, required=False, label=_('Reviewers')
|
||||
)
|
||||
reviewers_amount = serializers.IntegerField(read_only=True, source="reviewers.count")
|
||||
action = LabeledChoiceField(
|
||||
choices=ActionChoices.choices, label=_("Action")
|
||||
)
|
||||
|
||||
class Meta:
|
||||
fields_mini = ["id", "name"]
|
||||
fields_small = fields_mini + [
|
||||
"users", "accounts", "assets", "is_active",
|
||||
"date_created", "date_updated", "priority",
|
||||
"action", "comment", "created_by", "org_id",
|
||||
]
|
||||
fields_m2m = ["reviewers", "reviewers_amount"]
|
||||
fields = fields_small + fields_m2m
|
||||
extra_kwargs = {
|
||||
"reviewers": {"allow_null": False, "required": True},
|
||||
"priority": {"default": 50},
|
||||
"is_active": {"default": True},
|
||||
}
|
||||
|
||||
def validate_reviewers(self, reviewers):
|
||||
org_id = self.fields["org_id"].default()
|
||||
org = Organization.get_instance(org_id)
|
||||
if not org:
|
||||
error = _("The organization `{}` does not exist".format(org_id))
|
||||
raise serializers.ValidationError(error)
|
||||
users = org.get_members()
|
||||
valid_reviewers = list(set(reviewers) & set(users))
|
||||
if not valid_reviewers:
|
||||
error = _(
|
||||
"None of the reviewers belong to Organization `{}`".format(org.name)
|
||||
)
|
||||
raise serializers.ValidationError(error)
|
||||
return valid_reviewers
|
@ -0,0 +1,16 @@
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from acls.models import CommandGroup, CommandFilterACL
|
||||
from common.drf.fields import ObjectRelatedField
|
||||
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
||||
from .base import BaseUserAssetAccountACLSerializerMixin
|
||||
|
||||
__all__ = ["CommandFilterACLSerializer"]
|
||||
|
||||
|
||||
class CommandFilterACLSerializer(BaseUserAssetAccountACLSerializerMixin, BulkOrgResourceModelSerializer):
|
||||
commands = ObjectRelatedField(queryset=CommandGroup.objects, many=True, required=False, label=_('Commands'))
|
||||
|
||||
class Meta(BaseUserAssetAccountACLSerializerMixin.Meta):
|
||||
model = CommandFilterACL
|
||||
fields = BaseUserAssetAccountACLSerializerMixin.Meta.fields + ['commands']
|
@ -1,109 +1,11 @@
|
||||
from rest_framework import serializers
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from common.drf.fields import LabeledChoiceField
|
||||
from common.drf.fields import ObjectRelatedField
|
||||
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
||||
from orgs.models import Organization
|
||||
from users.models import User
|
||||
from acls import models
|
||||
|
||||
from .base import BaseUserAssetAccountACLSerializerMixin
|
||||
from ..models import LoginAssetACL
|
||||
|
||||
__all__ = ["LoginAssetACLSerializer"]
|
||||
|
||||
|
||||
common_help_text = _(
|
||||
"Format for comma-delimited string, with * indicating a match all. "
|
||||
)
|
||||
|
||||
|
||||
class LoginAssetACLUsersSerializer(serializers.Serializer):
|
||||
username_group = serializers.ListField(
|
||||
default=["*"],
|
||||
child=serializers.CharField(max_length=128),
|
||||
label=_("Username"),
|
||||
help_text=common_help_text,
|
||||
)
|
||||
|
||||
|
||||
class LoginAssetACLAssestsSerializer(serializers.Serializer):
|
||||
address_group_help_text = _(
|
||||
"Format for comma-delimited string, with * indicating a match all. "
|
||||
"Such as: "
|
||||
"192.168.10.1, 192.168.1.0/24, 10.1.1.1-10.1.1.20, 2001:db8:2de::e13, 2001:db8:1a:1110::/64"
|
||||
" (Domain name support)"
|
||||
)
|
||||
|
||||
name_group = serializers.ListField(
|
||||
default=["*"],
|
||||
child=serializers.CharField(max_length=128),
|
||||
label=_("Name"),
|
||||
help_text=common_help_text,
|
||||
)
|
||||
address_group = serializers.ListField(
|
||||
default=["*"],
|
||||
child=serializers.CharField(max_length=1024),
|
||||
label=_("IP/Host"),
|
||||
help_text=address_group_help_text,
|
||||
)
|
||||
|
||||
|
||||
class LoginAssetACLAccountsSerializer(serializers.Serializer):
|
||||
username_group = serializers.ListField(
|
||||
default=["*"],
|
||||
child=serializers.CharField(max_length=128),
|
||||
label=_("Username"),
|
||||
help_text=common_help_text,
|
||||
)
|
||||
|
||||
|
||||
class LoginAssetACLSerializer(BulkOrgResourceModelSerializer):
|
||||
users = LoginAssetACLUsersSerializer()
|
||||
assets = LoginAssetACLAssestsSerializer()
|
||||
accounts = LoginAssetACLAccountsSerializer()
|
||||
reviewers = ObjectRelatedField(
|
||||
queryset=User.objects, many=True, required=False, label=_('Reviewers')
|
||||
)
|
||||
reviewers_amount = serializers.IntegerField(read_only=True, source="reviewers.count")
|
||||
action = LabeledChoiceField(
|
||||
choices=models.LoginAssetACL.ActionChoices.choices, label=_("Action")
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = models.LoginAssetACL
|
||||
fields_mini = ["id", "name"]
|
||||
fields_small = fields_mini + [
|
||||
"users",
|
||||
"accounts",
|
||||
"assets",
|
||||
"is_active",
|
||||
"date_created",
|
||||
"date_updated",
|
||||
"priority",
|
||||
"action",
|
||||
"comment",
|
||||
"created_by",
|
||||
"org_id",
|
||||
]
|
||||
fields_m2m = ["reviewers", "reviewers_amount"]
|
||||
fields = fields_small + fields_m2m
|
||||
extra_kwargs = {
|
||||
"reviewers": {"allow_null": False, "required": True},
|
||||
"priority": {"default": 50},
|
||||
"is_active": {"default": True},
|
||||
}
|
||||
|
||||
def validate_reviewers(self, reviewers):
|
||||
org_id = self.fields["org_id"].default()
|
||||
org = Organization.get_instance(org_id)
|
||||
if not org:
|
||||
error = _("The organization `{}` does not exist".format(org_id))
|
||||
raise serializers.ValidationError(error)
|
||||
users = org.get_members()
|
||||
valid_reviewers = list(set(reviewers) & set(users))
|
||||
if not valid_reviewers:
|
||||
error = _(
|
||||
"None of the reviewers belong to Organization `{}`".format(org.name)
|
||||
)
|
||||
raise serializers.ValidationError(error)
|
||||
return valid_reviewers
|
||||
class LoginAssetACLSerializer(BaseUserAssetAccountACLSerializerMixin, BulkOrgResourceModelSerializer):
|
||||
class Meta(BaseUserAssetAccountACLSerializerMixin.Meta):
|
||||
model = LoginAssetACL
|
||||
|
Loading…
Reference in new issue