Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
J
jumpserver
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ops
jumpserver
Commits
cf719f49
Commit
cf719f49
authored
Oct 24, 2019
by
ibuler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Update] 修改user group relations api
parent
9bb7c1ad
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
126 additions
and
61 deletions
+126
-61
__init__.py
apps/users/api/__init__.py
+1
-0
relation.py
apps/users/api/relation.py
+30
-0
__init__.py
apps/users/serializers/__init__.py
+3
-2
group.py
apps/users/serializers/group.py
+68
-0
realtion.py
apps/users/serializers/realtion.py
+16
-0
user.py
apps/users/serializers/user.py
+7
-59
api_urls.py
apps/users/urls/api_urls.py
+1
-0
No files found.
apps/users/api/__init__.py
View file @
cf719f49
...
...
@@ -3,3 +3,4 @@
from
.user
import
*
from
.group
import
*
from
.relation
import
*
apps/users/api/relation.py
0 → 100644
View file @
cf719f49
# -*- coding: utf-8 -*-
#
from
rest_framework_bulk
import
BulkModelViewSet
from
django.db.models
import
F
from
common.permissions
import
IsOrgAdmin
from
..
import
serializers
from
..models
import
User
__all__
=
[
'UserUserGroupRelationViewSet'
]
class
UserUserGroupRelationViewSet
(
BulkModelViewSet
):
filter_fields
=
(
'user'
,
'usergroup'
)
search_fields
=
filter_fields
serializer_class
=
serializers
.
UserUserGroupRelationSerializer
permission_classes
=
(
IsOrgAdmin
,)
def
get_queryset
(
self
):
queryset
=
User
.
groups
.
through
.
objects
.
all
()
\
.
annotate
(
user_name
=
F
(
'user__name'
))
\
.
annotate
(
usergroup_name
=
F
(
'usergroup__name'
))
return
queryset
def
allow_bulk_destroy
(
self
,
qs
,
filtered
):
if
filtered
.
count
()
!=
1
:
return
False
else
:
return
True
apps/users/serializers/__init__.py
View file @
cf719f49
# -*- coding: utf-8 -*-
#
from
.v1
import
*
\ No newline at end of file
from
.user
import
*
from
.group
import
*
from
.realtion
import
*
apps/users/serializers/group.py
0 → 100644
View file @
cf719f49
# -*- coding: utf-8 -*-
#
from
django.utils.translation
import
ugettext_lazy
as
_
from
rest_framework
import
serializers
from
common.fields
import
StringManyToManyField
from
common.serializers
import
AdaptedBulkListSerializer
from
orgs.mixins.serializers
import
BulkOrgResourceModelSerializer
from
..models
import
User
,
UserGroup
from
..
import
utils
__all__
=
[
'UserGroupSerializer'
,
'UserGroupListSerializer'
,
'UserGroupUpdateMemberSerializer'
,
]
class
UserGroupSerializer
(
BulkOrgResourceModelSerializer
):
users
=
serializers
.
PrimaryKeyRelatedField
(
required
=
False
,
many
=
True
,
queryset
=
User
.
objects
,
label
=
_
(
'User'
)
)
class
Meta
:
model
=
UserGroup
list_serializer_class
=
AdaptedBulkListSerializer
fields
=
[
'id'
,
'name'
,
'users'
,
'comment'
,
'date_created'
,
'created_by'
,
]
extra_kwargs
=
{
'created_by'
:
{
'label'
:
_
(
'Created by'
),
'read_only'
:
True
}
}
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
()
.
__init__
(
*
args
,
**
kwargs
)
self
.
set_fields_queryset
()
def
set_fields_queryset
(
self
):
users_field
=
self
.
fields
[
'users'
]
users_field
.
child_relation
.
queryset
=
utils
.
get_current_org_members
()
def
validate_users
(
self
,
users
):
for
user
in
users
:
if
user
.
is_super_auditor
:
msg
=
_
(
'Auditors cannot be join in the user group'
)
raise
serializers
.
ValidationError
(
msg
)
return
users
class
UserGroupListSerializer
(
UserGroupSerializer
):
users
=
StringManyToManyField
(
many
=
True
,
read_only
=
True
)
class
UserGroupUpdateMemberSerializer
(
serializers
.
ModelSerializer
):
users
=
serializers
.
PrimaryKeyRelatedField
(
many
=
True
,
queryset
=
User
.
objects
)
class
Meta
:
model
=
UserGroup
fields
=
[
'id'
,
'users'
]
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
()
.
__init__
(
*
args
,
**
kwargs
)
self
.
set_fields_queryset
()
def
set_fields_queryset
(
self
):
users_field
=
self
.
fields
[
'users'
]
users_field
.
child_relation
.
queryset
=
utils
.
get_current_org_members
()
apps/users/serializers/realtion.py
0 → 100644
View file @
cf719f49
# -*- coding: utf-8 -*-
#
from
rest_framework
import
serializers
from
..models
import
User
__all__
=
[
'UserUserGroupRelationSerializer'
]
class
UserUserGroupRelationSerializer
(
serializers
.
ModelSerializer
):
user_name
=
serializers
.
CharField
(
read_only
=
True
)
usergroup_name
=
serializers
.
CharField
(
read_only
=
True
)
class
Meta
:
model
=
User
.
groups
.
through
fields
=
[
'id'
,
'user'
,
'user_name'
,
'usergroup'
,
'usergroup_name'
]
apps/users/serializers/
v1
.py
→
apps/users/serializers/
user
.py
View file @
cf719f49
# -*- coding: utf-8 -*-
#
from
django.utils.translation
import
ugettext_lazy
as
_
from
rest_framework
import
serializers
from
common.utils
import
validate_ssh_public_key
from
common.mixins
import
BulkSerializerMixin
from
common.fields
import
StringManyToManyField
from
common.serializers
import
AdaptedBulkListSerializer
from
common.permissions
import
CanUpdateDeleteUser
from
orgs.mixins.serializers
import
BulkOrgResourceModelSerializer
from
..models
import
User
,
UserGroup
from
..
import
utils
__all__
=
[
'UserSerializer'
,
'UserPKUpdateSerializer'
,
'UserUpdateGroupSerializer'
,
'UserGroupSerializer'
,
'UserGroupListSerializer'
,
'UserGroupUpdateMemberSerializer'
,
'ChangeUserPasswordSerializer'
,
'ResetOTPSerializer'
,
'ChangeUserPasswordSerializer'
,
'ResetOTPSerializer'
,
]
...
...
@@ -127,58 +121,6 @@ class UserUpdateGroupSerializer(serializers.ModelSerializer):
fields
=
[
'id'
,
'groups'
]
class
UserGroupSerializer
(
BulkOrgResourceModelSerializer
):
users
=
serializers
.
PrimaryKeyRelatedField
(
required
=
False
,
many
=
True
,
queryset
=
User
.
objects
,
label
=
_
(
'User'
)
)
class
Meta
:
model
=
UserGroup
list_serializer_class
=
AdaptedBulkListSerializer
fields
=
[
'id'
,
'name'
,
'users'
,
'comment'
,
'date_created'
,
'created_by'
,
]
extra_kwargs
=
{
'created_by'
:
{
'label'
:
_
(
'Created by'
),
'read_only'
:
True
}
}
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
()
.
__init__
(
*
args
,
**
kwargs
)
self
.
set_fields_queryset
()
def
set_fields_queryset
(
self
):
users_field
=
self
.
fields
[
'users'
]
users_field
.
child_relation
.
queryset
=
utils
.
get_current_org_members
()
def
validate_users
(
self
,
users
):
for
user
in
users
:
if
user
.
is_super_auditor
:
msg
=
_
(
'Auditors cannot be join in the user group'
)
raise
serializers
.
ValidationError
(
msg
)
return
users
class
UserGroupListSerializer
(
UserGroupSerializer
):
users
=
StringManyToManyField
(
many
=
True
,
read_only
=
True
)
class
UserGroupUpdateMemberSerializer
(
serializers
.
ModelSerializer
):
users
=
serializers
.
PrimaryKeyRelatedField
(
many
=
True
,
queryset
=
User
.
objects
)
class
Meta
:
model
=
UserGroup
fields
=
[
'id'
,
'users'
]
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
()
.
__init__
(
*
args
,
**
kwargs
)
self
.
set_fields_queryset
()
def
set_fields_queryset
(
self
):
users_field
=
self
.
fields
[
'users'
]
users_field
.
child_relation
.
queryset
=
utils
.
get_current_org_members
()
class
ChangeUserPasswordSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
...
...
@@ -188,3 +130,9 @@ class ChangeUserPasswordSerializer(serializers.ModelSerializer):
class
ResetOTPSerializer
(
serializers
.
Serializer
):
msg
=
serializers
.
CharField
(
read_only
=
True
)
def
create
(
self
,
validated_data
):
pass
def
update
(
self
,
instance
,
validated_data
):
pass
apps/users/urls/api_urls.py
View file @
cf719f49
...
...
@@ -14,6 +14,7 @@ app_name = 'users'
router
=
BulkRouter
()
router
.
register
(
r'users'
,
api
.
UserViewSet
,
'user'
)
router
.
register
(
r'groups'
,
api
.
UserGroupViewSet
,
'user-group'
)
router
.
register
(
r'users-groups-relations'
,
api
.
UserUserGroupRelationViewSet
,
'user-group-relation'
)
urlpatterns
=
[
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment