authentic/src/authentic2/custom_user/managers.py

170 lines
6.7 KiB
Python
Raw Normal View History

# authentic2 - versatile identity manager
# Copyright (C) 2010-2019 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import datetime
import logging
import unicodedata
from django.contrib.contenttypes.models import ContentType
from django.contrib.postgres.search import TrigramDistance
from django.db import models, transaction, connection
from django.db.models import F, Value, FloatField, Subquery, OuterRef
from django.db.models.functions import Lower, Coalesce
from django.utils import six
Create a custom user model (fixes #6919) - Add new model User through a custom user application, Django needs custom user models to be introduced in the first migration of an application, it features: - username length is 256 chars - username is nullable and optional - email is 254 chars - email default validator is our own - User has an automatic uuid field using uuid.uuid4 - Module fix_user_model.py can be removed - Old user models are automatically converted to new custom user model - Registration form does not have to generate an uuid username anymore - Add migrations to fix foreign key in our own applications and in third party applications out of our control like django-admin-tools and django-admin (the AlterField migration operation was overrided for theses cases) django.contrib.auth.models.User to authentic2.models.User - Set AUTH_USER_MODEL to authentic2.User - Register custome_user.User in admin site - Remove all monkey patching to Django default user model - username field is now forced in the model backend, as the USERNAME_FIELD attribute is now `uuid`. - Create a local modelform_factory implementation, it specializes User model by always using authentic2.forms.BaseUserForm as a base class and add a new parameter `required` which allow to set the required flag on fields without setting it on the model. - Add a BaseUserForm base model-form class supporting custom fields created using the Attribute model. - Refactor profile editing form, user admin form, manage user edit form and registrationform using using the new modelform_factory based on the BaseUserForm class. - Added new settings: - A2_USERNAME_IS_UNIQUE: it adds checking that the username attribute is unique before saving a user object - A2_REGISTRATION_USERNAME_IS_UNIQUE: it adds checking that the username attribute is unique before saving a user object; same as above but only for self-registered users. - A2_EMAIL_IS_UNIQUE: same as above but for email - Remove all unused symbols in authentic2.compat: use_attribute_aggregator, get_required_fields and get_registration_fields -
2015-04-15 10:51:00 +02:00
from django.utils import timezone
from django.contrib.auth.models import BaseUserManager
from authentic2 import app_settings
from authentic2.models import Attribute, AttributeValue
from authentic2.utils.lookups import Unaccent, ImmutableConcat
class UserQuerySet(models.QuerySet):
def free_text_search(self, search):
terms = search.split()
if not terms:
return self
searchable_attributes = Attribute.objects.filter(searchable=True)
queries = []
for term in terms:
q = None
specific_queries = []
for a in searchable_attributes:
kind = a.get_kind()
free_text_search_function = kind.get('free_text_search')
if free_text_search_function:
q = free_text_search_function(term)
if q is not None:
specific_queries.append(q & models.query.Q(attribute_values__attribute=a))
# if the term is recognized by some specific attribute type, like a
# date, does not use the later generic matcher
if specific_queries:
queries.append(six.moves.reduce(models.query.Q.__or__, specific_queries))
continue
q = (
models.query.Q(username__icontains=term)
| models.query.Q(first_name__icontains=term)
| models.query.Q(last_name__icontains=term)
| models.query.Q(email__icontains=term)
)
for a in searchable_attributes:
if a.name in ('first_name', 'last_name'):
continue
q = q | models.query.Q(
attribute_values__content__icontains=term, attribute_values__attribute=a)
queries.append(q)
self = self.filter(six.moves.reduce(models.query.Q.__and__, queries))
# search by attributes can match multiple times
if searchable_attributes:
self = self.distinct()
return self
def find_duplicates(self, first_name, last_name, birthdate=None):
with connection.cursor() as cursor:
cursor.execute(
"SET pg_trgm.similarity_threshold = %f" % app_settings.A2_DUPLICATES_THRESHOLD
)
name = '%s %s' % (first_name, last_name)
name = unicodedata.normalize('NFKD', name).encode('ascii', 'ignore').decode('ascii').lower()
qs = self.filter(deleted__isnull=True)
qs = qs.annotate(name=Lower(Unaccent(ImmutableConcat('first_name', Value(' '), 'last_name'))))
qs = qs.filter(name__trigram_similar=name)
qs = qs.annotate(dist=TrigramDistance('name', name))
qs = qs.order_by('dist')
qs = qs[:5]
# alter distance according to additionnal parameters
if birthdate:
bonus = app_settings.A2_DUPLICATES_BIRTHDATE_BONUS
content_type = ContentType.objects.get_for_model(self.model)
same_birthdate = AttributeValue.objects.filter(
object_id=OuterRef('pk'),
content_type=content_type,
attribute__kind='birthdate',
content=birthdate
).annotate(bonus=Value(1 - bonus, output_field=FloatField()))
qs = qs.annotate(dist=Coalesce(
Subquery(same_birthdate.values('bonus'), output_field=FloatField()) * F('dist'),
F('dist')
))
return qs
@transaction.atomic
def cleanup(self, threshold=600, timestamp=None):
'''Delete all deleted users for more than 10 minutes.'''
from .models import DeletedUser
not_after = (timestamp or timezone.now()) - datetime.timedelta(seconds=threshold)
qs = self.filter(deleted__lt=not_after)
loaded = list(qs)
def log():
logger = logging.getLogger('authentic2')
for user in loaded:
logger.info(u'deleted account %s', user)
transaction.on_commit(log)
deleted_users = []
for user in qs:
deleted_user = DeletedUser(deleted=user.deleted, old_user_id=user.id)
if 'email' in app_settings.A2_USER_DELETED_KEEP_DATA:
deleted_user.old_email = user.email.rsplit('#', 1)[0]
if 'uuid' in app_settings.A2_USER_DELETED_KEEP_DATA:
deleted_user.old_uuid = user.uuid
deleted_users.append(deleted_user)
DeletedUser.objects.bulk_create(deleted_users)
qs.delete()
Create a custom user model (fixes #6919) - Add new model User through a custom user application, Django needs custom user models to be introduced in the first migration of an application, it features: - username length is 256 chars - username is nullable and optional - email is 254 chars - email default validator is our own - User has an automatic uuid field using uuid.uuid4 - Module fix_user_model.py can be removed - Old user models are automatically converted to new custom user model - Registration form does not have to generate an uuid username anymore - Add migrations to fix foreign key in our own applications and in third party applications out of our control like django-admin-tools and django-admin (the AlterField migration operation was overrided for theses cases) django.contrib.auth.models.User to authentic2.models.User - Set AUTH_USER_MODEL to authentic2.User - Register custome_user.User in admin site - Remove all monkey patching to Django default user model - username field is now forced in the model backend, as the USERNAME_FIELD attribute is now `uuid`. - Create a local modelform_factory implementation, it specializes User model by always using authentic2.forms.BaseUserForm as a base class and add a new parameter `required` which allow to set the required flag on fields without setting it on the model. - Add a BaseUserForm base model-form class supporting custom fields created using the Attribute model. - Refactor profile editing form, user admin form, manage user edit form and registrationform using using the new modelform_factory based on the BaseUserForm class. - Added new settings: - A2_USERNAME_IS_UNIQUE: it adds checking that the username attribute is unique before saving a user object - A2_REGISTRATION_USERNAME_IS_UNIQUE: it adds checking that the username attribute is unique before saving a user object; same as above but only for self-registered users. - A2_EMAIL_IS_UNIQUE: same as above but for email - Remove all unused symbols in authentic2.compat: use_attribute_aggregator, get_required_fields and get_registration_fields -
2015-04-15 10:51:00 +02:00
class UserManager(BaseUserManager):
def _create_user(self, username, email, password,
is_staff, is_superuser, **extra_fields):
"""
Creates and saves a User with the given username, email and password.
"""
now = timezone.now()
if not username:
raise ValueError('The given username must be set')
email = self.normalize_email(email)
user = self.model(username=username, email=email,
is_staff=is_staff, is_active=True,
is_superuser=is_superuser, last_login=now,
date_joined=now, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, username, email=None, password=None, **extra_fields):
return self._create_user(username, email, password, False, False,
**extra_fields)
def create_superuser(self, username, email, password, **extra_fields):
return self._create_user(username, email, password, True, True,
**extra_fields)
def get_by_natural_key(self, uuid):
return self.get(uuid=uuid)