From b26bf780c32baf2f9e08dc6cb60a1fa4036ea424 Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 14 Oct 2021 21:27:09 +0200 Subject: [PATCH] setting to register group on signup --- InvenTree/InvenTree/forms.py | 18 ++++++++++++++++-- InvenTree/common/models.py | 11 ++++++++++- .../templates/InvenTree/settings/login.html | 1 + 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/InvenTree/InvenTree/forms.py b/InvenTree/InvenTree/forms.py index 8b4b87637c..eede6b8d32 100644 --- a/InvenTree/InvenTree/forms.py +++ b/InvenTree/InvenTree/forms.py @@ -4,10 +4,11 @@ Helper forms which subclass Django forms to provide additional functionality # -*- coding: utf-8 -*- from __future__ import unicode_literals +import logging from django.utils.translation import ugettext_lazy as _ from django import forms -from django.contrib.auth.models import User +from django.contrib.auth.models import User, Group from crispy_forms.helper import FormHelper from crispy_forms.layout import Layout, Field @@ -20,6 +21,7 @@ from allauth.socialaccount.adapter import DefaultSocialAccountAdapter from part.models import PartCategory from common.models import InvenTreeSetting +logger = logging.getLogger('inventree') class HelperForm(forms.ModelForm): """ Provides simple integration of crispy_forms extension. """ @@ -261,6 +263,18 @@ class RegistratonMixin: return super().is_open_for_signup(request) return False + def save_user(self, request, user, form, commit=True): + user = super().save_user(request, user, form, commit=commit) + start_group = InvenTreeSetting.get_setting('SIGNUP_GROUP') + if start_group: + try: + group = Group.objects.get(id=start_group) + user.groups.add(group) + except Group.DoesNotExist: + logger.error('The setting `SIGNUP_GROUP` contains an non existant group', start_group) + user.save() + return user + class CustomAccountAdapter(RegistratonMixin, DefaultAccountAdapter): """ @@ -268,7 +282,7 @@ class CustomAccountAdapter(RegistratonMixin, DefaultAccountAdapter): """ def send_mail(self, template_prefix, email, context): """only send mail if backend configured""" - if InvenTreeSetting.get_setting('EMAIL_HOST', None): + if settings.EMAIL_HOST: return super().send_mail(template_prefix, email, context) return False diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index d4f26af739..a1b48d74d3 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -11,7 +11,7 @@ import decimal import math from django.db import models, transaction -from django.contrib.auth.models import User +from django.contrib.auth.models import User, Group from django.db.utils import IntegrityError, OperationalError from django.conf import settings @@ -845,6 +845,15 @@ class InvenTreeSetting(BaseInvenTreeSetting): 'default': True, 'validator': bool, }, + 'SIGNUP_GROUP': { + 'name': _('Group on signup'), + 'description': _('Group new user are asigned on registration'), + 'default': '', + 'choices': [ + ('', _('No group')), + *[(str(a.id), str(a)) for a in Group.objects.all()] + ], + }, } class Meta: diff --git a/InvenTree/templates/InvenTree/settings/login.html b/InvenTree/templates/InvenTree/settings/login.html index 289f87a3c9..57e0adb45e 100644 --- a/InvenTree/templates/InvenTree/settings/login.html +++ b/InvenTree/templates/InvenTree/settings/login.html @@ -25,6 +25,7 @@ {% include "InvenTree/settings/setting.html" with key="LOGIN_SIGNUP_MAIL_TWICE" icon="fa-info-circle" %} {% include "InvenTree/settings/setting.html" with key="LOGIN_SIGNUP_PWD_TWICE" icon="fa-info-circle" %} {% include "InvenTree/settings/setting.html" with key="LOGIN_SIGNUP_SSO_AUTO" icon="fa-info-circle" %} + {% include "InvenTree/settings/setting.html" with key="SIGNUP_GROUP" %}