2011-07-31 10:46:29 +02:00
|
|
|
from django.contrib import messages
|
2015-02-12 22:42:54 +01:00
|
|
|
from django.contrib.auth import login as auth_login
|
|
|
|
from django.contrib.auth import logout as auth_logout
|
|
|
|
from django.contrib.auth.forms import AuthenticationForm, PasswordChangeForm
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from django.utils.translation import activate, ugettext_lazy
|
2015-02-17 00:45:53 +01:00
|
|
|
from rest_framework import status
|
2012-04-20 23:23:50 +02:00
|
|
|
|
2015-02-17 00:45:53 +01:00
|
|
|
from openslides.utils.rest_api import ModelViewSet, Response
|
2014-10-11 14:34:49 +02:00
|
|
|
from openslides.utils.views import (
|
2015-02-12 22:42:54 +01:00
|
|
|
CSVImportView,
|
|
|
|
FormView,
|
|
|
|
LoginMixin,
|
|
|
|
PDFView,
|
|
|
|
UpdateView,
|
|
|
|
APIView
|
|
|
|
)
|
2014-10-11 14:34:49 +02:00
|
|
|
|
2014-03-27 20:30:15 +01:00
|
|
|
from .csv_import import import_users
|
2015-02-12 22:42:54 +01:00
|
|
|
from .forms import UsersettingsForm
|
2014-10-11 14:34:49 +02:00
|
|
|
from .models import Group, User
|
2015-02-12 22:42:54 +01:00
|
|
|
from .pdf import users_passwords_to_pdf, users_to_pdf
|
|
|
|
from .serializers import (
|
|
|
|
GroupSerializer,
|
|
|
|
UserCreateUpdateSerializer,
|
|
|
|
UserFullSerializer,
|
|
|
|
UserShortSerializer
|
|
|
|
)
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2012-07-07 15:26:00 +02:00
|
|
|
|
2014-10-11 14:34:49 +02:00
|
|
|
class UsersListPDF(PDFView):
|
2012-07-07 15:26:00 +02:00
|
|
|
"""
|
2012-08-10 19:19:41 +02:00
|
|
|
Generate the userliste as PDF.
|
2012-07-07 15:26:00 +02:00
|
|
|
"""
|
2015-01-17 14:25:05 +01:00
|
|
|
required_permission = 'users.can_see_extra_data'
|
2014-10-11 14:34:49 +02:00
|
|
|
filename = ugettext_lazy("user-list")
|
|
|
|
document_title = ugettext_lazy('List of Users')
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2013-11-15 20:15:21 +01:00
|
|
|
def append_to_pdf(self, pdf):
|
|
|
|
"""
|
|
|
|
Append PDF objects.
|
|
|
|
"""
|
2014-10-11 14:34:49 +02:00
|
|
|
users_to_pdf(pdf)
|
2012-07-07 15:26:00 +02:00
|
|
|
|
2012-08-10 19:19:41 +02:00
|
|
|
|
2014-10-11 14:34:49 +02:00
|
|
|
class UsersPasswordsPDF(PDFView):
|
2012-07-07 15:26:00 +02:00
|
|
|
"""
|
2014-10-11 14:34:49 +02:00
|
|
|
Generate the access data welcome paper for all users as PDF.
|
2012-07-07 15:26:00 +02:00
|
|
|
"""
|
2014-10-11 14:34:49 +02:00
|
|
|
required_permission = 'users.can_manage'
|
|
|
|
filename = ugettext_lazy("User-access-data")
|
2012-08-10 19:19:41 +02:00
|
|
|
top_space = 0
|
2011-11-14 16:37:12 +01:00
|
|
|
|
2012-08-10 19:19:41 +02:00
|
|
|
def build_document(self, pdf_document, story):
|
|
|
|
pdf_document.build(story)
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2013-11-15 20:15:21 +01:00
|
|
|
def append_to_pdf(self, pdf):
|
|
|
|
"""
|
|
|
|
Append PDF objects.
|
|
|
|
"""
|
2014-10-11 14:34:49 +02:00
|
|
|
users_passwords_to_pdf(pdf)
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2012-07-07 15:26:00 +02:00
|
|
|
|
2014-03-27 20:30:15 +01:00
|
|
|
class UserCSVImportView(CSVImportView):
|
2012-08-10 19:49:46 +02:00
|
|
|
"""
|
2014-03-27 20:30:15 +01:00
|
|
|
Import users via CSV.
|
2012-08-10 19:49:46 +02:00
|
|
|
"""
|
2014-10-11 14:34:49 +02:00
|
|
|
required_permission = 'users.can_manage'
|
|
|
|
success_url_name = 'user_list'
|
|
|
|
template_name = 'users/user_form_csv_import.html'
|
2015-02-12 22:42:54 +01:00
|
|
|
import_function = staticmethod(import_users)
|
2012-08-10 19:49:46 +02:00
|
|
|
|
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
class UserViewSet(ModelViewSet):
|
2015-01-06 00:11:22 +01:00
|
|
|
"""
|
2015-02-04 00:08:38 +01:00
|
|
|
API endpoint to list, retrieve, create, update and delete users.
|
2015-01-06 00:11:22 +01:00
|
|
|
"""
|
|
|
|
queryset = User.objects.all()
|
|
|
|
|
|
|
|
def check_permissions(self, request):
|
|
|
|
"""
|
2015-01-24 16:35:50 +01:00
|
|
|
Calls self.permission_denied() if the requesting user has not the
|
|
|
|
permission to see users and in case of create, update or destroy
|
|
|
|
requests the permission to see extra user data and to manage users.
|
2015-01-06 00:11:22 +01:00
|
|
|
"""
|
2015-01-17 14:25:05 +01:00
|
|
|
if (not request.user.has_perm('users.can_see_name') or
|
|
|
|
(self.action in ('create', 'update', 'destroy') and not
|
|
|
|
(request.user.has_perm('users.can_manage') and
|
|
|
|
request.user.has_perm('users.can_see_extra_data')))):
|
2015-01-06 00:11:22 +01:00
|
|
|
self.permission_denied(request)
|
|
|
|
|
2015-01-17 14:25:05 +01:00
|
|
|
def get_serializer_class(self):
|
|
|
|
"""
|
2015-02-12 20:57:05 +01:00
|
|
|
Returns different serializer classes with respect to action and user's
|
|
|
|
permissions.
|
2015-01-17 14:25:05 +01:00
|
|
|
"""
|
2015-02-12 20:57:05 +01:00
|
|
|
if self.action in ('create', 'update'):
|
|
|
|
serializer_class = UserCreateUpdateSerializer
|
|
|
|
elif self.request.user.has_perm('users.can_see_extra_data'):
|
2015-01-17 14:25:05 +01:00
|
|
|
serializer_class = UserFullSerializer
|
|
|
|
else:
|
|
|
|
serializer_class = UserShortSerializer
|
|
|
|
return serializer_class
|
|
|
|
|
2015-01-06 00:11:22 +01:00
|
|
|
|
2015-02-12 18:48:14 +01:00
|
|
|
class GroupViewSet(ModelViewSet):
|
2015-02-04 00:08:38 +01:00
|
|
|
"""
|
|
|
|
API endpoint to list, retrieve, create, update and delete groups.
|
|
|
|
"""
|
|
|
|
queryset = Group.objects.all()
|
|
|
|
serializer_class = GroupSerializer
|
|
|
|
|
|
|
|
def check_permissions(self, request):
|
|
|
|
"""
|
|
|
|
Calls self.permission_denied() if the requesting user has not the
|
|
|
|
permission to see users and in case of create, update or destroy
|
|
|
|
requests the permission to see extra user data and to manage users.
|
|
|
|
"""
|
|
|
|
if (not request.user.has_perm('users.can_see_name') or
|
|
|
|
(self.action in ('create', 'update', 'destroy') and not
|
|
|
|
(request.user.has_perm('users.can_manage') and
|
|
|
|
request.user.has_perm('users.can_see_extra_data')))):
|
|
|
|
self.permission_denied(request)
|
|
|
|
|
2015-02-17 00:45:53 +01:00
|
|
|
def destroy(self, request, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Protects builtin groups 'Anonymous' (pk=1) and 'Registered' (pk=2)
|
|
|
|
from being deleted.
|
|
|
|
"""
|
|
|
|
instance = self.get_object()
|
|
|
|
if instance.pk in (1, 2,):
|
|
|
|
self.permission_denied(request)
|
|
|
|
else:
|
|
|
|
self.perform_destroy(instance)
|
|
|
|
response = Response(status=status.HTTP_204_NO_CONTENT)
|
|
|
|
return response
|
|
|
|
|
2015-02-04 00:08:38 +01:00
|
|
|
|
2014-10-11 14:34:49 +02:00
|
|
|
class UserSettingsView(LoginMixin, UpdateView):
|
2015-02-12 22:42:54 +01:00
|
|
|
required_permission = None
|
|
|
|
template_name = 'users/settings.html'
|
|
|
|
success_url_name = 'user_settings'
|
2014-10-11 14:34:49 +02:00
|
|
|
model = User
|
|
|
|
form_class = UsersettingsForm
|
|
|
|
url_name_args = []
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2014-10-11 14:34:49 +02:00
|
|
|
def get_initial(self):
|
|
|
|
initial = super().get_initial()
|
|
|
|
initial['language'] = self.request.session.get('django_language', self.request.LANGUAGE_CODE)
|
|
|
|
return initial
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2014-10-11 14:34:49 +02:00
|
|
|
def form_valid(self, form):
|
|
|
|
self.request.LANGUAGE_CODE = self.request.session['django_language'] = form.cleaned_data['language']
|
|
|
|
activate(self.request.LANGUAGE_CODE)
|
|
|
|
return super().form_valid(form)
|
2012-07-07 15:26:00 +02:00
|
|
|
|
2014-10-11 14:34:49 +02:00
|
|
|
def get_object(self):
|
|
|
|
return self.request.user
|
2012-07-11 09:46:15 +02:00
|
|
|
|
2014-10-11 14:34:49 +02:00
|
|
|
|
|
|
|
class UserPasswordSettingsView(LoginMixin, FormView):
|
2015-02-12 22:42:54 +01:00
|
|
|
required_permission = None
|
2014-10-11 14:34:49 +02:00
|
|
|
template_name = 'users/password_change.html'
|
2015-02-12 22:42:54 +01:00
|
|
|
success_url_name = 'core_dashboard'
|
|
|
|
form_class = PasswordChangeForm
|
2014-10-11 14:34:49 +02:00
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
form.save()
|
|
|
|
messages.success(self.request, _('Password successfully changed.'))
|
|
|
|
return super().form_valid(form)
|
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
|
|
|
kwargs = super().get_form_kwargs()
|
|
|
|
kwargs['user'] = self.request.user
|
|
|
|
return kwargs
|
2015-02-12 22:42:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
class UserLoginView(APIView):
|
|
|
|
"""
|
|
|
|
Login the user via ajax.
|
|
|
|
"""
|
|
|
|
http_method_names = ['post']
|
|
|
|
|
|
|
|
def post(self, *args, **kwargs):
|
|
|
|
form = AuthenticationForm(self.request, data=self.request.data)
|
|
|
|
if form.is_valid():
|
|
|
|
self.user = form.get_user()
|
|
|
|
auth_login(self.request, self.user)
|
|
|
|
self.success = True
|
|
|
|
else:
|
|
|
|
self.success = False
|
|
|
|
return super().post(*args, **kwargs)
|
|
|
|
|
|
|
|
def get_context_data(self, **context):
|
|
|
|
context['success'] = self.success
|
|
|
|
if self.success:
|
|
|
|
context['user_id'] = self.user.pk
|
|
|
|
return super().get_context_data(**context)
|
|
|
|
|
|
|
|
|
|
|
|
class UserLogoutView(APIView):
|
|
|
|
"""
|
|
|
|
Logout the user via ajax.
|
|
|
|
"""
|
|
|
|
http_method_names = ['post']
|
|
|
|
|
|
|
|
def post(self, *args, **kwargs):
|
|
|
|
auth_logout(self.request)
|
|
|
|
return super().post(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
class WhoAmIView(APIView):
|
|
|
|
"""
|
|
|
|
Returns the user id in the session.
|
|
|
|
"""
|
|
|
|
http_method_names = ['get']
|
|
|
|
|
|
|
|
def get_context_data(self, **context):
|
|
|
|
"""
|
|
|
|
Appends the user id into the context.
|
|
|
|
|
|
|
|
Uses None for the anonymous user.
|
|
|
|
"""
|
|
|
|
return super().get_context_data(
|
|
|
|
user_id=self.request.user.pk,
|
|
|
|
**context)
|