2018-12-14 13:24:04 +00:00
|
|
|
"""passbook User administration"""
|
2019-02-25 19:46:23 +00:00
|
|
|
from django.contrib import messages
|
2019-10-10 12:01:49 +01:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
|
|
from django.contrib.auth.mixins import \
|
|
|
|
PermissionRequiredMixin as DjangoPermissionRequiredMixin
|
2018-12-14 13:24:04 +00:00
|
|
|
from django.contrib.messages.views import SuccessMessageMixin
|
2019-10-10 12:01:49 +01:00
|
|
|
from django.shortcuts import redirect
|
2019-02-25 19:46:23 +00:00
|
|
|
from django.urls import reverse, reverse_lazy
|
2018-12-14 13:24:04 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
2019-10-10 12:01:49 +01:00
|
|
|
from django.views.generic import DeleteView, DetailView, ListView, UpdateView
|
|
|
|
from guardian.mixins import PermissionListMixin, PermissionRequiredMixin
|
2018-12-14 13:24:04 +00:00
|
|
|
|
2019-03-02 21:41:14 +00:00
|
|
|
from passbook.admin.forms.users import UserForm
|
2019-02-25 19:46:23 +00:00
|
|
|
from passbook.core.models import Nonce, User
|
2019-10-10 12:01:49 +01:00
|
|
|
from passbook.lib.views import CreateAssignPermView
|
2018-12-14 13:24:04 +00:00
|
|
|
|
|
|
|
|
2019-10-10 12:01:49 +01:00
|
|
|
class UserListView(LoginRequiredMixin, PermissionListMixin, ListView):
|
2018-12-14 13:24:04 +00:00
|
|
|
"""Show list of all users"""
|
|
|
|
|
|
|
|
model = User
|
2019-10-10 12:01:49 +01:00
|
|
|
permission_required = 'passbook_core.view_user'
|
2019-10-14 12:15:42 +01:00
|
|
|
ordering = 'username'
|
|
|
|
paginate_by = 40
|
2018-12-14 13:24:04 +00:00
|
|
|
template_name = 'administration/user/list.html'
|
|
|
|
|
|
|
|
|
2019-10-10 12:01:49 +01:00
|
|
|
class UserCreateView(SuccessMessageMixin, LoginRequiredMixin,
|
|
|
|
DjangoPermissionRequiredMixin, CreateAssignPermView):
|
2019-10-08 10:27:19 +01:00
|
|
|
"""Create user"""
|
|
|
|
|
|
|
|
model = User
|
|
|
|
form_class = UserForm
|
2019-10-10 12:01:49 +01:00
|
|
|
permission_required = 'passbook_core.add_user'
|
2019-10-08 10:27:19 +01:00
|
|
|
|
|
|
|
template_name = 'generic/create.html'
|
|
|
|
success_url = reverse_lazy('passbook_admin:users')
|
|
|
|
success_message = _('Successfully created User')
|
|
|
|
|
|
|
|
|
2019-10-10 12:01:49 +01:00
|
|
|
class UserUpdateView(SuccessMessageMixin, LoginRequiredMixin,
|
|
|
|
PermissionRequiredMixin, UpdateView):
|
2018-12-14 13:24:04 +00:00
|
|
|
"""Update user"""
|
|
|
|
|
|
|
|
model = User
|
2019-03-02 21:41:14 +00:00
|
|
|
form_class = UserForm
|
2019-10-10 12:01:49 +01:00
|
|
|
permission_required = 'passbook_core.change_user'
|
2018-12-14 13:24:04 +00:00
|
|
|
|
2019-12-05 13:31:44 +00:00
|
|
|
# By default the object's name is user which is used by other checks
|
|
|
|
context_object_name = 'object'
|
2018-12-14 13:24:04 +00:00
|
|
|
template_name = 'generic/update.html'
|
|
|
|
success_url = reverse_lazy('passbook_admin:users')
|
|
|
|
success_message = _('Successfully updated User')
|
|
|
|
|
|
|
|
|
2019-10-10 12:01:49 +01:00
|
|
|
class UserDeleteView(SuccessMessageMixin, LoginRequiredMixin,
|
|
|
|
PermissionRequiredMixin, DeleteView):
|
2018-12-14 13:24:04 +00:00
|
|
|
"""Delete user"""
|
|
|
|
|
|
|
|
model = User
|
2019-10-10 12:01:49 +01:00
|
|
|
permission_required = 'passbook_core.delete_user'
|
|
|
|
|
2019-02-26 08:46:44 +00:00
|
|
|
template_name = 'generic/delete.html'
|
2018-12-14 13:24:04 +00:00
|
|
|
success_url = reverse_lazy('passbook_admin:users')
|
2019-02-26 08:46:44 +00:00
|
|
|
success_message = _('Successfully deleted User')
|
|
|
|
|
|
|
|
def delete(self, request, *args, **kwargs):
|
|
|
|
messages.success(self.request, self.success_message)
|
|
|
|
return super().delete(request, *args, **kwargs)
|
2019-02-25 19:46:23 +00:00
|
|
|
|
|
|
|
|
2019-10-10 12:01:49 +01:00
|
|
|
class UserPasswordResetView(LoginRequiredMixin, PermissionRequiredMixin, DetailView):
|
2019-02-25 19:46:23 +00:00
|
|
|
"""Get Password reset link for user"""
|
|
|
|
|
2019-10-10 12:01:49 +01:00
|
|
|
model = User
|
|
|
|
permission_required = 'passbook_core.reset_user_password'
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
2019-02-25 19:46:23 +00:00
|
|
|
"""Create nonce for user and return link"""
|
2019-10-10 12:01:49 +01:00
|
|
|
super().get(request, *args, **kwargs)
|
|
|
|
nonce = Nonce.objects.create(user=self.object)
|
2019-02-25 19:46:23 +00:00
|
|
|
link = request.build_absolute_uri(reverse(
|
|
|
|
'passbook_core:auth-password-reset', kwargs={'nonce': nonce.uuid}))
|
|
|
|
messages.success(request, _('Password reset link: <pre>%(link)s</pre>' % {'link': link}))
|
|
|
|
return redirect('passbook_admin:users')
|