mirror of
https://github.com/eduardogsilva/routerfleet.git
synced 2025-08-05 10:44:32 +02:00
import user_manager and accounts from project wireguard_webadmin
This commit is contained in:
parent
757fa6a1e1
commit
c104a12df0
28 changed files with 618 additions and 4 deletions
0
accounts/__init__.py
Normal file
0
accounts/__init__.py
Normal file
3
accounts/admin.py
Normal file
3
accounts/admin.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
6
accounts/apps.py
Normal file
6
accounts/apps.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class AccountsConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'accounts'
|
41
accounts/forms.py
Normal file
41
accounts/forms.py
Normal file
|
@ -0,0 +1,41 @@
|
|||
from typing import Any
|
||||
from django import forms
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.contrib.auth import authenticate
|
||||
|
||||
|
||||
class CreateUserForm(forms.Form):
|
||||
username = forms.CharField(label='Username')
|
||||
password = forms.CharField(label='Password', widget=forms.PasswordInput)
|
||||
password2 = forms.CharField(label='Confirm Password', widget=forms.PasswordInput) # Adicione este campo para a confirmação da senha
|
||||
|
||||
def clean(self):
|
||||
cleaned_data = super().clean()
|
||||
|
||||
username = cleaned_data.get("username")
|
||||
if username and ' ' in username:
|
||||
self.add_error('username', ValidationError("Username cannot contain spaces."))
|
||||
cleaned_data['username'] = username.lower()
|
||||
password = cleaned_data.get("password")
|
||||
password2 = cleaned_data.get("password2")
|
||||
|
||||
if password and password2 and password != password2:
|
||||
self.add_error('password2', ValidationError("The two password fields didn't match."))
|
||||
return cleaned_data
|
||||
|
||||
|
||||
class LoginForm(forms.Form):
|
||||
username = forms.CharField(label='Username')
|
||||
password = forms.CharField(label='Password', widget=forms.PasswordInput)
|
||||
|
||||
def clean(self):
|
||||
cleaned_data = super().clean()
|
||||
username = cleaned_data.get("username")
|
||||
password = cleaned_data.get("password")
|
||||
if username and password:
|
||||
user = authenticate(username=username, password=password)
|
||||
if not user:
|
||||
self.add_error(None, ValidationError("Invalid username or password."))
|
||||
else:
|
||||
self.add_error(None, ValidationError("Both fields are required."))
|
||||
return cleaned_data
|
0
accounts/migrations/__init__.py
Normal file
0
accounts/migrations/__init__.py
Normal file
3
accounts/models.py
Normal file
3
accounts/models.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.db import models
|
||||
|
||||
# Create your models here.
|
3
accounts/tests.py
Normal file
3
accounts/tests.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
42
accounts/views.py
Normal file
42
accounts/views.py
Normal file
|
@ -0,0 +1,42 @@
|
|||
from django.shortcuts import render, Http404, redirect
|
||||
from django.contrib.auth.models import User
|
||||
from django.contrib import auth
|
||||
from .forms import CreateUserForm, LoginForm
|
||||
from django.http import HttpResponse
|
||||
from user_manager.models import UserAcl
|
||||
|
||||
|
||||
def view_create_first_user(request):
|
||||
if User.objects.filter().all():
|
||||
raise Http404('Superuser already exists')
|
||||
if request.method == 'POST':
|
||||
form = CreateUserForm(request.POST)
|
||||
if form.is_valid():
|
||||
username = form.cleaned_data['username']
|
||||
password = form.cleaned_data['password']
|
||||
new_user = User.objects.create_superuser(username=username, password=password)
|
||||
UserAcl.objects.create(user=new_user, user_level=50)
|
||||
return render(request, 'accounts/superuser_created.html')
|
||||
else:
|
||||
form = CreateUserForm()
|
||||
return render(request, 'accounts/create_first_user.html', {'form': form})
|
||||
|
||||
|
||||
def view_login(request):
|
||||
if not User.objects.filter().all():
|
||||
return redirect('/accounts/create_first_user/')
|
||||
if request.method == 'POST':
|
||||
form = LoginForm(request.POST)
|
||||
if form.is_valid():
|
||||
username = form.cleaned_data['username']
|
||||
user = User.objects.get(username=username)
|
||||
auth.login(request, user)
|
||||
return redirect('/')
|
||||
else:
|
||||
form = LoginForm()
|
||||
return render(request, 'accounts/login.html', {'form': form})
|
||||
|
||||
|
||||
def view_logout(request):
|
||||
auth.logout(request)
|
||||
return render(request, 'accounts/logout.html')
|
Loading…
Add table
Add a link
Reference in a new issue