Fr3deric
6 years ago
6 changed files with 95 additions and 3 deletions
@ -1,3 +1,34 @@ |
|||||||
from django.contrib import admin |
from django.contrib import admin |
||||||
|
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin |
||||||
|
from django.contrib.auth.models import User |
||||||
|
from django.contrib.auth.hashers import make_password |
||||||
|
from django import forms |
||||||
|
from .models import Account |
||||||
|
|
||||||
# Register your models here. |
|
||||||
|
class AccountInlineForm(forms.ModelForm): |
||||||
|
hashed_email_set = forms.EmailField(label='Set hashed email address') |
||||||
|
|
||||||
|
class Meta: |
||||||
|
model = Account |
||||||
|
fields = ['hashed_email'] |
||||||
|
|
||||||
|
def save(self, commit): |
||||||
|
self.instance.set_hashed_email(self.cleaned_data['hashed_email_set']) |
||||||
|
return super().save(commit) |
||||||
|
|
||||||
|
|
||||||
|
class AccountInline(admin.StackedInline): |
||||||
|
model = Account |
||||||
|
can_delete = False |
||||||
|
form = AccountInlineForm |
||||||
|
readonly_fields = ['hashed_email'] |
||||||
|
|
||||||
|
|
||||||
|
class UserAdmin(BaseUserAdmin): |
||||||
|
inlines = (AccountInline,) |
||||||
|
|
||||||
|
|
||||||
|
admin.site.unregister(User) |
||||||
|
admin.site.register(User, UserAdmin) |
||||||
|
admin.site.register(Account) |
||||||
|
@ -0,0 +1,18 @@ |
|||||||
|
import hashlib |
||||||
|
from django.contrib.auth.forms import PasswordResetForm |
||||||
|
from django.contrib.auth.hashers import check_password |
||||||
|
from django import forms |
||||||
|
from django.utils.translation import gettext, gettext_lazy as _ |
||||||
|
from .models import Account |
||||||
|
|
||||||
|
|
||||||
|
class HashedEmailPasswordResetForm(PasswordResetForm): |
||||||
|
username = forms.CharField(label=_('Username'), max_length=254) |
||||||
|
|
||||||
|
def get_users(self, email): |
||||||
|
accounts = Account.objects.filter( |
||||||
|
user__username=self.cleaned_data['username'] |
||||||
|
) |
||||||
|
return (a.user for a in accounts if a.user.has_usable_password() and |
||||||
|
(check_password(email, a.hashed_email) |
||||||
|
or a.user.email == email)) |
@ -0,0 +1,25 @@ |
|||||||
|
# Generated by Django 2.2 on 2019-04-28 10:12 |
||||||
|
|
||||||
|
from django.conf import settings |
||||||
|
from django.db import migrations, models |
||||||
|
import django.db.models.deletion |
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration): |
||||||
|
|
||||||
|
initial = True |
||||||
|
|
||||||
|
dependencies = [ |
||||||
|
migrations.swappable_dependency(settings.AUTH_USER_MODEL), |
||||||
|
] |
||||||
|
|
||||||
|
operations = [ |
||||||
|
migrations.CreateModel( |
||||||
|
name='Account', |
||||||
|
fields=[ |
||||||
|
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), |
||||||
|
('hashed_email', models.CharField(max_length=128)), |
||||||
|
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), |
||||||
|
], |
||||||
|
), |
||||||
|
] |
@ -1,3 +1,14 @@ |
|||||||
from django.db import models |
from django.db import models |
||||||
|
from django.contrib.auth.models import User |
||||||
|
from django.contrib.auth.hashers import make_password |
||||||
|
|
||||||
# Create your models here. |
|
||||||
|
class Account(models.Model): |
||||||
|
user = models.OneToOneField(User, on_delete=models.CASCADE) |
||||||
|
hashed_email = models.CharField(max_length=128) |
||||||
|
|
||||||
|
def __str__(self): |
||||||
|
return '%s' % (self.user.username) |
||||||
|
|
||||||
|
def set_hashed_email(self, email): |
||||||
|
self.hashed_email = make_password(email) |
||||||
|
@ -1,5 +1,6 @@ |
|||||||
from django.views.generic.base import TemplateView |
from django.views.generic.base import TemplateView |
||||||
from django.contrib.auth.mixins import LoginRequiredMixin |
from django.contrib.auth.mixins import LoginRequiredMixin |
||||||
|
|
||||||
|
|
||||||
class ProfileView(LoginRequiredMixin, TemplateView): |
class ProfileView(LoginRequiredMixin, TemplateView): |
||||||
template_name = 'bam/profile.html' |
template_name = 'bam/profile.html' |
||||||
|
Loading…
Reference in new issue