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 11:01:49 +00:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2019-12-31 11:51:16 +00:00
|
|
|
from django.contrib.auth.mixins import (
|
|
|
|
PermissionRequiredMixin as DjangoPermissionRequiredMixin,
|
|
|
|
)
|
2018-12-14 13:24:04 +00:00
|
|
|
from django.contrib.messages.views import SuccessMessageMixin
|
2020-07-01 10:10:12 +00:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2019-10-10 11:01:49 +00:00
|
|
|
from django.shortcuts import redirect
|
2019-02-25 19:46:23 +00:00
|
|
|
from django.urls import reverse, reverse_lazy
|
2020-07-01 10:10:12 +00:00
|
|
|
from django.utils.http import urlencode
|
2020-09-11 21:21:11 +00:00
|
|
|
from django.utils.translation import gettext as _
|
2020-07-01 22:13:33 +00:00
|
|
|
from django.views.generic import DetailView, ListView, UpdateView
|
2020-02-23 14:27:28 +00:00
|
|
|
from guardian.mixins import (
|
|
|
|
PermissionListMixin,
|
|
|
|
PermissionRequiredMixin,
|
|
|
|
get_anonymous_user,
|
|
|
|
)
|
2018-12-14 13:24:04 +00:00
|
|
|
|
2019-03-02 21:41:14 +00:00
|
|
|
from passbook.admin.forms.users import UserForm
|
2020-09-25 23:56:28 +00:00
|
|
|
from passbook.admin.views.utils import (
|
|
|
|
BackSuccessUrlMixin,
|
|
|
|
DeleteMessageView,
|
2020-10-03 17:32:01 +00:00
|
|
|
SearchListMixin,
|
2020-09-25 23:56:28 +00:00
|
|
|
UserPaginateListMixin,
|
|
|
|
)
|
2020-05-16 14:11:53 +00:00
|
|
|
from passbook.core.models import Token, User
|
2019-10-10 11:01:49 +00:00
|
|
|
from passbook.lib.views import CreateAssignPermView
|
2018-12-14 13:24:04 +00:00
|
|
|
|
|
|
|
|
2020-09-25 23:56:28 +00:00
|
|
|
class UserListView(
|
2020-10-03 17:32:01 +00:00
|
|
|
LoginRequiredMixin,
|
|
|
|
PermissionListMixin,
|
|
|
|
UserPaginateListMixin,
|
|
|
|
SearchListMixin,
|
|
|
|
ListView,
|
2020-09-25 23:56:28 +00:00
|
|
|
):
|
2018-12-14 13:24:04 +00:00
|
|
|
"""Show list of all users"""
|
|
|
|
|
|
|
|
model = User
|
2019-12-31 11:51:16 +00:00
|
|
|
permission_required = "passbook_core.view_user"
|
|
|
|
ordering = "username"
|
|
|
|
template_name = "administration/user/list.html"
|
2020-10-03 17:32:01 +00:00
|
|
|
search_fields = ["username", "name", "attributes"]
|
2018-12-14 13:24:04 +00:00
|
|
|
|
2020-02-23 14:27:28 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
return super().get_queryset().exclude(pk=get_anonymous_user().pk)
|
|
|
|
|
2018-12-14 13:24:04 +00:00
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
class UserCreateView(
|
|
|
|
SuccessMessageMixin,
|
2020-09-25 23:56:28 +00:00
|
|
|
BackSuccessUrlMixin,
|
2019-12-31 11:51:16 +00:00
|
|
|
LoginRequiredMixin,
|
|
|
|
DjangoPermissionRequiredMixin,
|
|
|
|
CreateAssignPermView,
|
|
|
|
):
|
2019-10-08 09:27:19 +00:00
|
|
|
"""Create user"""
|
|
|
|
|
|
|
|
model = User
|
|
|
|
form_class = UserForm
|
2019-12-31 11:51:16 +00:00
|
|
|
permission_required = "passbook_core.add_user"
|
2019-10-08 09:27:19 +00:00
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
template_name = "generic/create.html"
|
|
|
|
success_url = reverse_lazy("passbook_admin:users")
|
|
|
|
success_message = _("Successfully created User")
|
2019-10-08 09:27:19 +00:00
|
|
|
|
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
class UserUpdateView(
|
2020-09-25 23:56:28 +00:00
|
|
|
SuccessMessageMixin,
|
|
|
|
BackSuccessUrlMixin,
|
|
|
|
LoginRequiredMixin,
|
|
|
|
PermissionRequiredMixin,
|
|
|
|
UpdateView,
|
2019-12-31 11:51:16 +00:00
|
|
|
):
|
2018-12-14 13:24:04 +00:00
|
|
|
"""Update user"""
|
|
|
|
|
|
|
|
model = User
|
2019-03-02 21:41:14 +00:00
|
|
|
form_class = UserForm
|
2019-12-31 11:51:16 +00: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
|
2019-12-31 11:51:16 +00:00
|
|
|
context_object_name = "object"
|
|
|
|
template_name = "generic/update.html"
|
|
|
|
success_url = reverse_lazy("passbook_admin:users")
|
|
|
|
success_message = _("Successfully updated User")
|
2018-12-14 13:24:04 +00:00
|
|
|
|
|
|
|
|
2020-07-01 22:13:33 +00:00
|
|
|
class UserDeleteView(LoginRequiredMixin, PermissionRequiredMixin, DeleteMessageView):
|
2018-12-14 13:24:04 +00:00
|
|
|
"""Delete user"""
|
|
|
|
|
|
|
|
model = User
|
2019-12-31 11:51:16 +00:00
|
|
|
permission_required = "passbook_core.delete_user"
|
2019-10-10 11:01:49 +00:00
|
|
|
|
2020-02-18 20:35:06 +00:00
|
|
|
# By default the object's name is user which is used by other checks
|
|
|
|
context_object_name = "object"
|
2019-12-31 11:51:16 +00:00
|
|
|
template_name = "generic/delete.html"
|
|
|
|
success_url = reverse_lazy("passbook_admin:users")
|
|
|
|
success_message = _("Successfully deleted User")
|
2019-02-26 08:46:44 +00:00
|
|
|
|
2019-02-25 19:46:23 +00:00
|
|
|
|
2019-10-10 11:01:49 +00:00
|
|
|
class UserPasswordResetView(LoginRequiredMixin, PermissionRequiredMixin, DetailView):
|
2019-02-25 19:46:23 +00:00
|
|
|
"""Get Password reset link for user"""
|
|
|
|
|
2019-10-10 11:01:49 +00:00
|
|
|
model = User
|
2019-12-31 11:51:16 +00:00
|
|
|
permission_required = "passbook_core.reset_user_password"
|
2019-10-10 11:01:49 +00:00
|
|
|
|
2020-07-01 10:10:12 +00:00
|
|
|
def get(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
|
2020-05-16 14:11:53 +00:00
|
|
|
"""Create token for user and return link"""
|
2019-10-10 11:01:49 +00:00
|
|
|
super().get(request, *args, **kwargs)
|
2020-05-16 14:11:53 +00:00
|
|
|
token = Token.objects.create(user=self.object)
|
2020-07-01 10:10:12 +00:00
|
|
|
querystring = urlencode({"token": token.token_uuid})
|
2019-12-31 11:51:16 +00:00
|
|
|
link = request.build_absolute_uri(
|
2020-07-01 10:10:12 +00:00
|
|
|
reverse("passbook_flows:default-recovery") + f"?{querystring}"
|
2019-12-31 11:51:16 +00:00
|
|
|
)
|
|
|
|
messages.success(
|
|
|
|
request, _("Password reset link: <pre>%(link)s</pre>" % {"link": link})
|
|
|
|
)
|
|
|
|
return redirect("passbook_admin:users")
|