|
48 | 48 | from openwisp_radius.api.serializers import RadiusUserSerializer |
49 | 49 | from openwisp_users.api.authentication import BearerAuthentication, SesameAuthentication |
50 | 50 | from openwisp_users.api.filters import OrganizationManagedFilter |
51 | | -from openwisp_users.api.mixins import FilterByOrganizationManaged, ProtectedAPIMixin |
| 51 | +from openwisp_users.api.mixins import ( |
| 52 | + FilterByOrganizationManaged, |
| 53 | + FilterByParentManaged, |
| 54 | + ProtectedAPIMixin, |
| 55 | +) |
52 | 56 | from openwisp_users.api.permissions import IsOrganizationManager |
53 | 57 | from openwisp_users.api.views import ChangePasswordView as BasePasswordChangeView |
54 | 58 | from openwisp_users.backends import UsersAuthenticationBackend |
|
69 | 73 | RadiusAccountingSerializer, |
70 | 74 | RadiusBatchSerializer, |
71 | 75 | RadiusGroupSerializer, |
| 76 | + RadiusUserGroupSerializer, |
72 | 77 | UserRadiusUsageSerializer, |
73 | 78 | ValidatePhoneTokenSerializer, |
74 | 79 | ) |
@@ -942,3 +947,99 @@ class RadiusGroupDetailView( |
942 | 947 |
|
943 | 948 |
|
944 | 949 | radius_group_detail = RadiusGroupDetailView.as_view() |
| 950 | + |
| 951 | + |
| 952 | +class BaseRadiusUserGroupView(ProtectedAPIMixin, FilterByParentManaged): |
| 953 | + """ |
| 954 | + Base view for RadiusUserGroup management. |
| 955 | + Provides user parent filtering and queryset logic. |
| 956 | + """ |
| 957 | + |
| 958 | + serializer_class = RadiusUserGroupSerializer |
| 959 | + queryset = RadiusUserGroup.objects.select_related("group", "user").order_by( |
| 960 | + "-created" |
| 961 | + ) |
| 962 | + |
| 963 | + def get_queryset(self): |
| 964 | + qs = super().get_queryset() |
| 965 | + if getattr(self, "swagger_fake_view", False): |
| 966 | + return super().get_queryset() |
| 967 | + return qs.filter(user_id=self.kwargs["user_pk"]) |
| 968 | + |
| 969 | + def get_parent_queryset(self): |
| 970 | + """Get the parent user from the URL.""" |
| 971 | + return User.objects.filter(pk=self.kwargs["user_pk"]) |
| 972 | + |
| 973 | + def get_organization_queryset(self, qs): |
| 974 | + """Filter users by organizations the request user manages.""" |
| 975 | + orgs = self.request.user.organizations_managed |
| 976 | + app_label = User._meta.app_config.label |
| 977 | + filter_kwargs = { |
| 978 | + # exclude superusers |
| 979 | + "is_superuser": False, |
| 980 | + # ensure user is member of the org |
| 981 | + f"{app_label}_organizationuser__organization_id__in": orgs, |
| 982 | + } |
| 983 | + return qs.filter(**filter_kwargs).distinct() |
| 984 | + |
| 985 | + |
| 986 | +@method_decorator( |
| 987 | + name="get", |
| 988 | + decorator=swagger_auto_schema( |
| 989 | + operation_description=""" |
| 990 | + Returns the list of RADIUS user groups for a specific user. |
| 991 | + """, |
| 992 | + ), |
| 993 | +) |
| 994 | +@method_decorator( |
| 995 | + name="post", |
| 996 | + decorator=swagger_auto_schema( |
| 997 | + operation_description=""" |
| 998 | + Creates a new RADIUS user group assignment for the user. |
| 999 | + """, |
| 1000 | + ), |
| 1001 | +) |
| 1002 | +class RadiusUserGroupListCreateView(BaseRadiusUserGroupView, ListCreateAPIView): |
| 1003 | + pagination_class = RadiusGroupPaginator |
| 1004 | + |
| 1005 | + |
| 1006 | +radius_user_group_list = RadiusUserGroupListCreateView.as_view() |
| 1007 | + |
| 1008 | + |
| 1009 | +@method_decorator( |
| 1010 | + name="get", |
| 1011 | + decorator=swagger_auto_schema( |
| 1012 | + operation_description=""" |
| 1013 | + Returns a single RADIUS user group by its UUID. |
| 1014 | + """, |
| 1015 | + ), |
| 1016 | +) |
| 1017 | +@method_decorator( |
| 1018 | + name="put", |
| 1019 | + decorator=swagger_auto_schema( |
| 1020 | + operation_description=""" |
| 1021 | + Updates a RADIUS user group identified by its UUID. |
| 1022 | + """, |
| 1023 | + ), |
| 1024 | +) |
| 1025 | +@method_decorator( |
| 1026 | + name="patch", |
| 1027 | + decorator=swagger_auto_schema( |
| 1028 | + operation_description=""" |
| 1029 | + Partially updates a RADIUS user group identified by its UUID. |
| 1030 | + """, |
| 1031 | + ), |
| 1032 | +) |
| 1033 | +@method_decorator( |
| 1034 | + name="delete", |
| 1035 | + decorator=swagger_auto_schema( |
| 1036 | + operation_description=""" |
| 1037 | + Deletes a RADIUS user group identified by its UUID. |
| 1038 | + """, |
| 1039 | + ), |
| 1040 | +) |
| 1041 | +class RadiusUserGroupDetailView(BaseRadiusUserGroupView, RetrieveUpdateDestroyAPIView): |
| 1042 | + organization_field = "group__organization" |
| 1043 | + |
| 1044 | + |
| 1045 | +radius_user_group_detail = RadiusUserGroupDetailView.as_view() |
0 commit comments