Reworking account handling

* allowing role management
* allowing user change without the need to update the password
This commit is contained in:
Michael Bergbauer 2025-07-19 09:08:40 +02:00
parent 69941166a1
commit 1e720228be
4 changed files with 45 additions and 4 deletions

View File

@ -13,12 +13,19 @@ class PersonForm(forms.ModelForm):
class AccountForm(forms.ModelForm):
password=forms.CharField(widget=forms.PasswordInput())
confirm_password=forms.CharField(widget=forms.PasswordInput())
password=forms.CharField(widget=forms.PasswordInput(), required=False)
confirm_password=forms.CharField(widget=forms.PasswordInput(), required=False)
class Meta:
model = UserAccount
fields = ['username', 'password']
fields = ['username', 'password', 'role']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.instance.pk is None:
self.fields['password'].required=True
self.fields['confirm_password'].required=True
def clean(self):
cleaned_data = super(AccountForm, self).clean()
@ -27,4 +34,14 @@ class AccountForm(forms.ModelForm):
if passwordStr != confirm_passwordStr:
raise forms.ValidateError("password and confirm_password does not match")
def save(self, commit=True):
user = super().save(commit=False)
pw = self.cleaned_data.get("password")
if pw:
user.set_password(pw)
if commit:
user.save()
return user

View File

@ -0,0 +1,18 @@
# Generated by Django 5.2.1 on 2025-07-18 15:12
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('member', '0002_alter_useraccount_options_alter_useraccount_managers_and_more'),
]
operations = [
migrations.AlterField(
model_name='useraccount',
name='role',
field=models.CharField(choices=[('mitglied', 'Mitglied'), ('geraetewart', 'Gerätewart'), ('kommandant', 'Kommandant'), ('admin', 'Administrator'), ('superadmin', 'Superadministrator')], default='mitglied', max_length=50),
),
]

View File

@ -11,7 +11,9 @@ class UserAccount(AbstractUser):
('geraetewart', 'Gerätewart'),
('kommandant', 'Kommandant'),
('admin', 'Administrator'),
])
('superadmin', 'Superadministrator'),
], default='mitglied')
def __str__(self):
return f"{self.benutzername} ({self.rolle})"

View File

@ -16,6 +16,10 @@
<label for="{{form.password.id_for_label}}">Password (confirmation)</label>
{{form.confirm_password}}
</div>
<div class="form-group">
<label for="{{form.role.id_for_label}}">Rolle</label>
{{form.role}}
</div>
<button class="button" type="submit">Speichern</button>
<a href="{% url 'details' id %}" class="button" style="background:#444;">Abbrechen</a>
</form>