mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-12-27 02:20:31 +00:00
Register admin user
This commit is contained in:
parent
ed536e6b41
commit
23d0d3e2b7
4 changed files with 63 additions and 21 deletions
|
@ -54,6 +54,15 @@ class RegisterForm(CustomForm):
|
|||
help_texts = {f: None for f in fields}
|
||||
widgets = {"password": PasswordInput()}
|
||||
|
||||
def clean(self):
|
||||
"""Check if the username is taken"""
|
||||
cleaned_data = super().clean()
|
||||
localname = cleaned_data.get("localname").strip()
|
||||
if models.User.objects.filter(localname=localname).first():
|
||||
self.add_error(
|
||||
"localname", _("User with this username already exists")
|
||||
)
|
||||
|
||||
|
||||
class RatingForm(CustomForm):
|
||||
class Meta:
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
<div class="column is-half">
|
||||
<div class="box has-background-primary-light">
|
||||
<h2 class="title is-4">{% trans "Create your account" %}</h2>
|
||||
<form name="register" method="post" action="/register">
|
||||
<form name="register" method="post" action="{% url 'setup' %}">
|
||||
<div class="field">
|
||||
<label class="label" for="id_admin_key">
|
||||
{% trans "Admin key:" %}
|
||||
|
|
|
@ -42,9 +42,16 @@ class Register(View):
|
|||
invite = None
|
||||
|
||||
form = forms.RegisterForm(request.POST)
|
||||
errors = False
|
||||
if not form.is_valid():
|
||||
errors = True
|
||||
data = {
|
||||
"login_form": forms.LoginForm(),
|
||||
"register_form": form,
|
||||
"invite": invite,
|
||||
"valid": invite.valid() if invite else True,
|
||||
}
|
||||
if invite:
|
||||
return TemplateResponse(request, "landing/invite.html", data)
|
||||
return TemplateResponse(request, "landing/login.html", data)
|
||||
|
||||
localname = form.data["localname"].strip()
|
||||
email = form.data["email"]
|
||||
|
@ -56,22 +63,6 @@ class Register(View):
|
|||
# treat this like a successful registration, but don't do anything
|
||||
return redirect("confirm-email")
|
||||
|
||||
# check localname and email uniqueness
|
||||
if models.User.objects.filter(localname=localname).first():
|
||||
form.errors["localname"] = ["User with this username already exists"]
|
||||
errors = True
|
||||
|
||||
if errors:
|
||||
data = {
|
||||
"login_form": forms.LoginForm(),
|
||||
"register_form": form,
|
||||
"invite": invite,
|
||||
"valid": invite.valid() if invite else True,
|
||||
}
|
||||
if invite:
|
||||
return TemplateResponse(request, "landing/invite.html", data)
|
||||
return TemplateResponse(request, "landing/login.html", data)
|
||||
|
||||
username = f"{localname}@{DOMAIN}"
|
||||
user = models.User.objects.create_user(
|
||||
username,
|
||||
|
|
|
@ -1,9 +1,14 @@
|
|||
""" Installation wizard 🧙 """
|
||||
from django.contrib.auth import login
|
||||
from django.contrib.auth.models import Group
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.db import transaction
|
||||
from django.shortcuts import redirect
|
||||
from django.template.response import TemplateResponse
|
||||
from django.views import View
|
||||
|
||||
from bookwyrm import forms, models
|
||||
from bookwyrm.settings import DOMAIN
|
||||
|
||||
|
||||
# pylint: disable= no-self-use
|
||||
|
@ -11,8 +16,8 @@ class CreateAdmin(View):
|
|||
"""manage things like the instance name"""
|
||||
|
||||
def get(self, request):
|
||||
"""Create admin user"""
|
||||
# only allow this view when an instance is being installed
|
||||
"""Create admin user form"""
|
||||
# only allow this view when an instance is being configured
|
||||
site = models.SiteSettings.objects.get()
|
||||
if not site.install_mode:
|
||||
raise PermissionDenied()
|
||||
|
@ -21,3 +26,40 @@ class CreateAdmin(View):
|
|||
"register_form": forms.RegisterForm()
|
||||
}
|
||||
return TemplateResponse(request, "setup/admin.html", data)
|
||||
|
||||
@transaction.atomic
|
||||
def post(self, request):
|
||||
"""Create that user"""
|
||||
site = models.SiteSettings.get()
|
||||
# you can't create an admin user if you're in config mode
|
||||
if not site.install_mode:
|
||||
raise PermissionDenied()
|
||||
|
||||
form = forms.RegisterForm(request.POST)
|
||||
if not form.is_valid():
|
||||
data = {"register_form": form}
|
||||
return TemplateResponse(request, "setup/admin.html", data)
|
||||
|
||||
localname = form.data["localname"].strip()
|
||||
username = f"{localname}@{DOMAIN}"
|
||||
|
||||
user = models.User.objects.create_superuser(
|
||||
username,
|
||||
form.data["email"],
|
||||
form.data["password"],
|
||||
localname=localname,
|
||||
local=True,
|
||||
deactivation_reason=None,
|
||||
is_active=True,
|
||||
)
|
||||
# Set "admin" role
|
||||
try:
|
||||
user.groups.set(Group.objects.filter(name__in=["admin", "moderator"]))
|
||||
except Group.DoesNotExist:
|
||||
# this should only happen in tests
|
||||
pass
|
||||
|
||||
login(request, user)
|
||||
site.install_mode = False
|
||||
site.save()
|
||||
return redirect("settings-site")
|
||||
|
|
Loading…
Reference in a new issue