diff --git a/apps/jumpserver/urls.py b/apps/jumpserver/urls.py index ba7611e2f..e9bf765de 100644 --- a/apps/jumpserver/urls.py +++ b/apps/jumpserver/urls.py @@ -24,7 +24,7 @@ urlpatterns = [ url(r'^$', TemplateView.as_view(template_name='base.html'), name='index'), url(r'^(api/)?users/', include('users.urls')), url(r'^assets/', include('assets.urls')), - url(r'^perms/', include('perms.urls')), + url(r'^(api/)?perms/', include('perms.urls')), url(r'^(api/)?audits/', include('audits.urls')), url(r'^(api/)?terminal/', include('terminal.urls')), ] diff --git a/apps/perms/api.py b/apps/perms/api.py index ecbf4289f..4d3537ec9 100644 --- a/apps/perms/api.py +++ b/apps/perms/api.py @@ -1,3 +1,30 @@ # ~*~ coding: utf-8 ~*~ # +from rest_framework.views import APIView, Response +from users.backends import IsValidUser +from .utils import get_user_granted_assets, get_user_granted_asset_groups + + +class UserAssetsGrantedApi(APIView): + permission_classes = (IsValidUser,) + + def get(self, request, *args, **kwargs): + assets_json = [] + user = request.user + + if user: + assets = get_user_granted_assets(user) + + for asset, system_users in assets.items(): + assets_json.append({ + 'id': asset.id, + 'hostname': asset.hostname, + 'ip': asset.ip, + 'port': asset.port, + 'system_users': [system_user.name for system_user in system_users], + 'comment': asset.comment + }) + + return Response(assets_json, status=200) + diff --git a/apps/perms/hands.py b/apps/perms/hands.py index 2a0f5b43f..ebf002686 100644 --- a/apps/perms/hands.py +++ b/apps/perms/hands.py @@ -1,12 +1,8 @@ # ~*~ coding: utf-8 ~*~ # -from django.db import models -from django.utils.translation import ugettext_lazy as _ - - from users.utils import AdminUserRequiredMixin +# from users.backends import IsValdiUser from users.models import User, UserGroup from assets.models import Asset, AssetGroup, SystemUser - diff --git a/apps/perms/serializers.py b/apps/perms/serializers.py new file mode 100644 index 000000000..f630d5f0d --- /dev/null +++ b/apps/perms/serializers.py @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -*- +# + +from rest_framework import serializers + + +class TerminalHeatbeatSerializer(serializers.ModelSerializer): + class Meta: + model = TerminalHeatbeat + fields = ['terminal'] + + +if __name__ == '__main__': + pass diff --git a/apps/perms/urls.py b/apps/perms/urls.py index 9b6b446f5..33681da6d 100644 --- a/apps/perms/urls.py +++ b/apps/perms/urls.py @@ -2,6 +2,7 @@ from django.conf.urls import url import views +import api app_name = 'perms' @@ -20,3 +21,8 @@ urlpatterns = [ name='asset-permission-asset-list'), ] +urlpatterns += [ + url(r'^v1/user/assets/granted/$', api.UserAssetsGrantedApi.as_view(), + name='user-assets-granted'), +] + diff --git a/apps/users/backends.py b/apps/users/backends.py index 07c5c2f70..970a35dda 100644 --- a/apps/users/backends.py +++ b/apps/users/backends.py @@ -6,6 +6,7 @@ from rest_framework.compat import is_authenticated from django.utils.translation import ugettext as _ from common.utils import unsign, get_object_or_none + from .hands import Terminal diff --git a/apps/users/hands.py b/apps/users/hands.py index f7159bf26..ad44762a0 100644 --- a/apps/users/hands.py +++ b/apps/users/hands.py @@ -10,6 +10,6 @@ :license: GPL v2, see LICENSE for more details. """ +from terminal.models import Terminal from perms.models import AssetPermission from perms.utils import get_user_granted_assets, get_user_granted_asset_groups -from terminal.models import Terminal \ No newline at end of file