setting to register group on signup

This commit is contained in:
Matthias 2021-10-14 21:27:09 +02:00
parent 0e589533e5
commit b26bf780c3
No known key found for this signature in database
GPG Key ID: F50EF5741D33E076
3 changed files with 27 additions and 3 deletions

View File

@ -4,10 +4,11 @@ Helper forms which subclass Django forms to provide additional functionality
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals
import logging
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django import forms 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.helper import FormHelper
from crispy_forms.layout import Layout, Field from crispy_forms.layout import Layout, Field
@ -20,6 +21,7 @@ from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
from part.models import PartCategory from part.models import PartCategory
from common.models import InvenTreeSetting from common.models import InvenTreeSetting
logger = logging.getLogger('inventree')
class HelperForm(forms.ModelForm): class HelperForm(forms.ModelForm):
""" Provides simple integration of crispy_forms extension. """ """ Provides simple integration of crispy_forms extension. """
@ -261,6 +263,18 @@ class RegistratonMixin:
return super().is_open_for_signup(request) return super().is_open_for_signup(request)
return False 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): class CustomAccountAdapter(RegistratonMixin, DefaultAccountAdapter):
""" """
@ -268,7 +282,7 @@ class CustomAccountAdapter(RegistratonMixin, DefaultAccountAdapter):
""" """
def send_mail(self, template_prefix, email, context): def send_mail(self, template_prefix, email, context):
"""only send mail if backend configured""" """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 super().send_mail(template_prefix, email, context)
return False return False

View File

@ -11,7 +11,7 @@ import decimal
import math import math
from django.db import models, transaction 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.db.utils import IntegrityError, OperationalError
from django.conf import settings from django.conf import settings
@ -845,6 +845,15 @@ class InvenTreeSetting(BaseInvenTreeSetting):
'default': True, 'default': True,
'validator': bool, '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: class Meta:

View File

@ -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_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_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="LOGIN_SIGNUP_SSO_AUTO" icon="fa-info-circle" %}
{% include "InvenTree/settings/setting.html" with key="SIGNUP_GROUP" %}
</tbody> </tbody>
</table> </table>