diff --git a/src/auth_and_perms/api/serializers.py b/src/auth_and_perms/api/serializers.py index 36d55c240..22c52447b 100644 --- a/src/auth_and_perms/api/serializers.py +++ b/src/auth_and_perms/api/serializers.py @@ -18,7 +18,7 @@ from django.conf import settings -from laboratory.utils import check_user_access_kwargs_org_lab +from laboratory.utils import check_user_access_kwargs_org_lab, get_actions_by_perms logger = logging.getLogger('organilab') @@ -359,3 +359,40 @@ class ValidateLabOrgObjectSerializer(serializers.Serializer): allow_null=False,allow_empty=False) +class UserSerializer(serializers.ModelSerializer): + delete_msg = serializers.SerializerMethodField() + actions = serializers.SerializerMethodField() + + def get_delete_msg(self, obj): + return "%s %s %s" % (_("The user"), obj.get_full_name(), + _("could have relations with multiple elements.")) + + def get_actions(self, obj): + user = self.context["request"].user + action_list = { + "destroy": ["auth.delete_user", "auth.view_user"] + } + return get_actions_by_perms(user, action_list) + + class Meta: + model = User + fields = ['id', 'username', 'first_name', 'last_name', 'email', 'delete_msg', + 'actions'] + + +class UserDataTableSerializer(serializers.Serializer): + data = serializers.ListField(child=UserSerializer(), required=True) + draw = serializers.IntegerField(required=True) + recordsFiltered = serializers.IntegerField(required=True) + recordsTotal = serializers.IntegerField(required=True) + + +class UserFilter(FilterSet): + class Meta: + model = User + fields = {'id': ['exact'], + 'username': ['icontains'], + 'first_name': ['icontains'], + 'last_name': ['icontains'], + 'email': ['icontains'], + } diff --git a/src/auth_and_perms/api/viewsets.py b/src/auth_and_perms/api/viewsets.py index fd3250d6b..fcfc41d40 100644 --- a/src/auth_and_perms/api/viewsets.py +++ b/src/auth_and_perms/api/viewsets.py @@ -6,6 +6,7 @@ from django.shortcuts import get_object_or_404 from django.template.loader import render_to_string from django_filters.rest_framework import DjangoFilterBackend +from djgentelella.objectmanagement import AuthAllPermBaseObjectManagement from rest_framework import mixins, viewsets, status from rest_framework.authentication import SessionAuthentication from rest_framework.filters import SearchFilter, OrderingFilter @@ -23,7 +24,8 @@ ProfileAssociateOrganizationSerializer, ValidateGroupsByProfileSerializer, \ ShelfObjectSerializer, ValidateSearchShelfObjectSerializer, \ ShelfObjectDataTableSerializer, ValidateOrganizationSerializer, \ - ExternalUserSerializer, AddExternalUserSerializer + ExternalUserSerializer, AddExternalUserSerializer, UserDataTableSerializer, \ + UserSerializer, UserFilter from auth_and_perms.forms import LaboratoryAndOrganizationForm, \ OrganizationForViewsetForm, SearchShelfObjectViewsetForm from auth_and_perms.models import Rol, ProfilePermission, Profile @@ -458,3 +460,21 @@ def get(self, request): else: return JsonResponse({"errors": serializer.errors}, status=status.HTTP_400_BAD_REQUEST) + +class UserManagementViewset(AuthAllPermBaseObjectManagement): + serializer_class = { + 'list': UserDataTableSerializer, + 'destroy': UserSerializer, + } + perms = { + 'list': ["auth.view_user"], + 'destroy': ["auth.delete_user"] + } + + queryset = User.objects.all() + pagination_class = LimitOffsetPagination + filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter) + search_fields = ['id', 'username', 'first_name', 'last_name', 'email'] # for the global search + filterset_class = UserFilter + ordering_fields = ['username', 'first_name'] + ordering = ('id',) # default order diff --git a/src/auth_and_perms/management/commands/urlname_permissions.py b/src/auth_and_perms/management/commands/urlname_permissions.py index 64b1335cf..d25f76af9 100644 --- a/src/auth_and_perms/management/commands/urlname_permissions.py +++ b/src/auth_and_perms/management/commands/urlname_permissions.py @@ -2652,5 +2652,23 @@ 'category': 'Equipment Type', 'permission': 'laboratory.delete_equipmenttype' } + ], + 'manage_users': [ + { + 'name': 'Change User', + 'category': 'User', + 'permission': 'auth.change_user' + }, + { + 'name': 'Delete User', + 'category': 'User', + 'permission': 'auth.delete_user' + }, + { + 'name': 'View User', + 'category': 'User', + 'permission': 'auth.view_user' + } ] + } diff --git a/src/auth_and_perms/templates/auth_and_perms/users_list.html b/src/auth_and_perms/templates/auth_and_perms/users_list.html new file mode 100644 index 000000000..a619e50d7 --- /dev/null +++ b/src/auth_and_perms/templates/auth_and_perms/users_list.html @@ -0,0 +1,76 @@ +{% extends 'base.html' %} +{% load i18n %} +{% load static %} +{% load urlname_tags %} +{% load gtsettings %} +{% block pre_head %} +{% define_urlname_action 'manage_users' %} +{% endblock %} + +{% block title %}{% trans 'Users List' %}{% endblock %} + +{% block content %} +