Commit 2abb9efe authored by ibuler's avatar ibuler

[Update] Org功能修改

parent f17727de
......@@ -43,7 +43,7 @@ class AssetGrantedSerializer(serializers.ModelSerializer):
fields = (
"id", "hostname", "ip", "port", "system_users_granted",
"is_active", "system_users_join", "os", 'domain',
"platform", "comment", "protocol",
"platform", "comment", "protocol", "org_id",
)
@staticmethod
......
......@@ -68,10 +68,8 @@ class Signer(metaclass=Singleton):
self.secret_key = secret_key
def sign(self, value):
if isinstance(value, bytes):
value = value.decode("utf-8")
s = JSONWebSignatureSerializer(self.secret_key)
return s.dumps(value)
return s.dumps(value).decode()
def unsign(self, value):
if value is None:
......
......@@ -3,6 +3,7 @@
from threading import local
from django.db import models
from django.db.models import Q
from django.shortcuts import redirect
import warnings
from django.forms import ModelForm
......@@ -34,7 +35,7 @@ class OrgManager(models.Manager):
elif current_org.is_real():
kwargs['org_id'] = current_org.id
elif current_org.is_default():
queryset = queryset.filter(org_id="").filter(org_id__isnull=True)
queryset = queryset.filter(Q(org_id="") | Q(org_id__isnull=True))
queryset = queryset.filter(**kwargs)
tl.times += 1
return queryset
......@@ -61,7 +62,7 @@ class OrgModelMixin(models.Model):
def save(self, *args, **kwargs):
if current_org and current_org.is_real():
self.org_id = current_org.id
return super(OrgModelMixin, self).save(*args, **kwargs)
return super().save(*args, **kwargs)
class Meta:
abstract = True
......
......@@ -24,8 +24,7 @@ from common.utils import get_object_or_none
from .models import Terminal, Status, Session, Task
from .serializers import TerminalSerializer, StatusSerializer, \
SessionSerializer, TaskSerializer, ReplaySerializer
from common.permissions import IsOrgAdmin, IsAppUser, IsOrgAdminOrAppUser, \
IsOrgAdminOrAppUserOrUserReadonly
from common.permissions import IsAppUser, IsOrgAdminOrAppUser
from .backends import get_command_storage, get_multi_command_storage, \
SessionCommandSerializer
......@@ -35,7 +34,7 @@ logger = logging.getLogger(__file__)
class TerminalViewSet(viewsets.ModelViewSet):
queryset = Terminal.objects.filter(is_deleted=False)
serializer_class = TerminalSerializer
permission_classes = (IsOrgAdminOrAppUserOrUserReadonly,)
permission_classes = (AllowAny,)
def create(self, request, *args, **kwargs):
name = request.data.get('name')
......@@ -238,13 +237,14 @@ class CommandViewSet(viewsets.ViewSet):
def create(self, request, *args, **kwargs):
serializer = self.serializer_class(data=request.data, many=True)
if serializer.is_valid():
print(serializer.validated_data)
ok = self.command_store.bulk_save(serializer.validated_data)
if ok:
return Response("ok", status=201)
else:
return Response("Save error", status=500)
else:
msg = "Not valid: {}".format(serializer.errors)
msg = "Command not valid: {}".format(serializer.errors)
logger.error(msg)
return Response({"msg": msg}, status=401)
......
......@@ -12,6 +12,6 @@ class SessionCommandSerializer(serializers.Serializer):
input = serializers.CharField(max_length=128)
output = serializers.CharField(max_length=1024, allow_blank=True)
session = serializers.CharField(max_length=36)
org_id = serializers.CharField(max_length=36)
org_id = serializers.CharField(max_length=36, required=False, default='')
timestamp = serializers.IntegerField()
......@@ -10,9 +10,9 @@ from .. import api
app_name = 'terminal'
router = routers.DefaultRouter()
router.register(r'terminal', api.TerminalViewSet, 'terminal2')
router.register(r'terminal/(?P<terminal>[a-zA-Z0-9\-]{36})?/?status', api.StatusViewSet, 'terminal-status')
router.register(r'terminal/(?P<terminal>[a-zA-Z0-9\-]{36})?/?sessions', api.SessionViewSet, 'terminal-sessions')
router.register(r'terminal', api.TerminalViewSet, 'terminal')
router.register(r'tasks', api.TaskViewSet, 'tasks')
router.register(r'command', api.CommandViewSet, 'command')
router.register(r'sessions', api.SessionViewSet, 'session')
......@@ -20,16 +20,16 @@ router.register(r'status', api.StatusViewSet, 'session')
urlpatterns = [
url(r'^sessions/(?P<pk>[0-9a-zA-Z\-]{36})/replay/$',
api.SessionReplayViewSet.as_view({'get': 'retrieve', 'post': 'create'}),
api.SessionReplayV2ViewSet.as_view({'get': 'retrieve', 'post': 'create'}),
name='session-replay'),
url(r'^tasks/kill-session/', api.KillSessionAPI.as_view(), name='kill-session'),
url(r'^terminal/(?P<terminal>[a-zA-Z0-9\-]{36})/access-key', api.TerminalTokenApi.as_view(),
name='terminal-access-key'),
url(r'^terminal/config', api.TerminalConfig.as_view(), name='terminal-config'),
# v2: get session's replay
url(r'^v2/sessions/(?P<pk>[0-9a-zA-Z\-]{36})/replay/$',
api.SessionReplayV2ViewSet.as_view({'get': 'retrieve'}),
name='session-replay-v2'),
# url(r'^v2/sessions/(?P<pk>[0-9a-zA-Z\-]{36})/replay/$',
# api.SessionReplayV2ViewSet.as_view({'get': 'retrieve'}),
# name='session-replay-v2'),
]
urlpatterns += router.urls
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment