diff --git a/apps/users/api/group.py b/apps/users/api/group.py index 1107a7eb8..a87f964e9 100644 --- a/apps/users/api/group.py +++ b/apps/users/api/group.py @@ -1,8 +1,11 @@ # -*- coding: utf-8 -*- # +from rest_framework import status +from rest_framework.decorators import action +from rest_framework.response import Response from orgs.mixins.api import OrgBulkModelViewSet -from ..models import UserGroup +from ..models import UserGroup, User from ..serializers import UserGroupSerializer __all__ = ['UserGroupViewSet'] @@ -14,3 +17,13 @@ class UserGroupViewSet(OrgBulkModelViewSet): search_fields = filterset_fields serializer_class = UserGroupSerializer ordering = ('name',) + rbac_perms = ( + ("add_all_users", "users.change_usergroup"), + ) + + @action(methods=['post'], detail=True, url_path='add-all-users') + def add_all_users(self, request, *args, **kwargs): + instance = self.get_object() + users = User.get_org_users().exclude(groups__id=instance.id) + instance.users.add(*users) + return Response(status=status.HTTP_200_OK) diff --git a/apps/users/filters.py b/apps/users/filters.py index 1bcc1c683..9715a14af 100644 --- a/apps/users/filters.py +++ b/apps/users/filters.py @@ -11,12 +11,17 @@ class UserFilter(BaseFilterSet): system_roles = filters.CharFilter(method='filter_system_roles') org_roles = filters.CharFilter(method='filter_org_roles') groups = filters.CharFilter(field_name="groups__name", lookup_expr='exact') + group_id = filters.CharFilter(field_name="groups__id", lookup_expr='exact') + exclude_group_id = filters.CharFilter( + field_name="groups__id", lookup_expr='exact', exclude=True + ) class Meta: model = User fields = ( - 'id', 'username', 'email', 'name', 'groups', 'source', - 'org_roles', 'system_roles', 'is_active', + 'id', 'username', 'email', 'name', + 'groups', 'group_id', 'exclude_group_id', + 'source', 'org_roles', 'system_roles', 'is_active', ) @staticmethod