From 1311558cacc5575d3ec4ab6adf662ad8d297afff Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 24 Aug 2021 19:12:43 +0200 Subject: [PATCH 01/72] add in SSO for #1421 --- InvenTree/InvenTree/settings.py | 10 +++++++++- InvenTree/InvenTree/urls.py | 1 + requirements.txt | 1 + 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index f3c166df88..091840a8bf 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -249,6 +249,7 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'django.contrib.sites', # InvenTree apps 'build.apps.BuildConfig', @@ -279,6 +280,10 @@ INSTALLED_APPS = [ 'error_report', # Error reporting in the admin interface 'django_q', 'formtools', # Form wizard tools + + 'allauth', # Base app for SSO + 'allauth.account', # Extend user with accounts + 'allauth.socialaccount', # Use 'social' providers ] MIDDLEWARE = CONFIG.get('middleware', [ @@ -298,7 +303,8 @@ MIDDLEWARE = CONFIG.get('middleware', [ MIDDLEWARE.append('error_report.middleware.ExceptionProcessor') AUTHENTICATION_BACKENDS = CONFIG.get('authentication_backends', [ - 'django.contrib.auth.backends.ModelBackend' + 'django.contrib.auth.backends.ModelBackend', + 'allauth.account.auth_backends.AuthenticationBackend', # SSO login via external providers ]) # If the debug toolbar is enabled, add the modules @@ -646,3 +652,5 @@ MESSAGE_TAGS = { messages.ERROR: 'alert alert-block alert-danger', messages.INFO: 'alert alert-block alert-info', } + +SITE_ID = 1 diff --git a/InvenTree/InvenTree/urls.py b/InvenTree/InvenTree/urls.py index 71f6388c68..0ae6919669 100644 --- a/InvenTree/InvenTree/urls.py +++ b/InvenTree/InvenTree/urls.py @@ -165,6 +165,7 @@ urlpatterns = [ url(r'^api-doc/', include_docs_urls(title='InvenTree API')), url(r'^markdownx/', include('markdownx.urls')), + url(r'^accounts/', include('allauth.urls')), # SSO ] # Server running in "DEBUG" mode? diff --git a/requirements.txt b/requirements.txt index 049bedcbeb..ff0d84703a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -34,5 +34,6 @@ python-barcode[images]==0.13.1 # Barcode generator qrcode[pil]==6.1 # QR code generator django-q==1.3.4 # Background task scheduling django-formtools==2.3 # Form wizard tools +django-allauth==0.45.0 # SSO for external providers via OpenID inventree # Install the latest version of the InvenTree API python library From d569d70f5bab7b7fd64e9394237938c5b37a77bc Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 24 Aug 2021 19:35:41 +0200 Subject: [PATCH 02/72] use config.yaml for SSO providers --- InvenTree/InvenTree/settings.py | 5 +++++ InvenTree/config_template.yaml | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 091840a8bf..7fcda29dd4 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -286,6 +286,11 @@ INSTALLED_APPS = [ 'allauth.socialaccount', # Use 'social' providers ] +# Load the allauth social backends +SOCIAL_BACKENDS = CONFIG.get('social_backends', []) +for app in SOCIAL_BACKENDS: + INSTALLED_APPS.append(app) + MIDDLEWARE = CONFIG.get('middleware', [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', diff --git a/InvenTree/config_template.yaml b/InvenTree/config_template.yaml index 0e6232d270..177b1eaf0a 100644 --- a/InvenTree/config_template.yaml +++ b/InvenTree/config_template.yaml @@ -157,3 +157,7 @@ static_root: '/home/inventree/data/static' # - 'django.contrib.messages.middleware.MessageMiddleware' # - 'django.middleware.clickjacking.XFrameOptionsMiddleware' # - 'InvenTree.middleware.AuthRequiredMiddleware' + +# Add SSO login-backends +# social_backends: +# - 'allauth.socialaccount.providers.github' From aebea337187b2808546df67351e1c80f3dc261e2 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 24 Aug 2021 23:53:20 +0200 Subject: [PATCH 03/72] enrich login template with login buttons --- InvenTree/templates/registration/login.html | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/InvenTree/templates/registration/login.html b/InvenTree/templates/registration/login.html index 708a69531a..1d3f261394 100644 --- a/InvenTree/templates/registration/login.html +++ b/InvenTree/templates/registration/login.html @@ -1,6 +1,7 @@ {% load static %} {% load i18n %} {% load inventree_extras %} +{% load socialaccount %} @@ -84,9 +85,21 @@ {% endif %} + +
- + {% get_providers as socialaccount_providers %} + + {% if socialaccount_providers %} +

{% trans 'Login with provider' %}

+ {% for provider in socialaccount_providers %} + {{ provider.name }} + + {% blocktrans with name=provider.name %}Login with {{name}}{% endblocktrans%} + + {% endfor %} + {% endif %} @@ -101,5 +114,7 @@ +{% providers_media_js %} + \ No newline at end of file From 118bac0591eceb86f3db4851a1488b78bbee91b1 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 25 Aug 2021 00:08:50 +0200 Subject: [PATCH 04/72] fix layout --- InvenTree/templates/registration/login.html | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/InvenTree/templates/registration/login.html b/InvenTree/templates/registration/login.html index 1d3f261394..7e0f76b00b 100644 --- a/InvenTree/templates/registration/login.html +++ b/InvenTree/templates/registration/login.html @@ -85,17 +85,16 @@ {% endif %} - -
+ + {% get_providers as socialaccount_providers %} {% if socialaccount_providers %} -

{% trans 'Login with provider' %}

+

{% for provider in socialaccount_providers %} - {{ provider.name }} - + {% blocktrans with name=provider.name %}Login with {{name}}{% endblocktrans%} {% endfor %} From c34a5967b84f7713150eac397a6d61ec7b95e01b Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 26 Aug 2021 23:17:00 +0200 Subject: [PATCH 05/72] user-settings template for social accounts --- .../templates/InvenTree/settings/user.html | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/InvenTree/templates/InvenTree/settings/user.html b/InvenTree/templates/InvenTree/settings/user.html index 922e9ebc79..f57221c9ac 100644 --- a/InvenTree/templates/InvenTree/settings/user.html +++ b/InvenTree/templates/InvenTree/settings/user.html @@ -2,6 +2,7 @@ {% load i18n %} {% load inventree_extras %} +{% load socialaccount %} {% block label %}account{% endblock %} @@ -38,6 +39,54 @@ +
+

{% trans "Social Accounts" %}

+
+ +
+ {% if form.accounts %} +

{% blocktrans %}You can sign in to your account using any of the following third party accounts:{% endblocktrans %}

+ + +
+ {% csrf_token %} + +
+ {% if form.non_field_errors %} +
{{ form.non_field_errors }}
+ {% endif %} + + {% for base_account in form.accounts %} + {% with base_account.get_provider_account as account %} +
+ +
+ {% endwith %} + {% endfor %} + +
+ +
+ +
+ +
+ + {% else %} +

{% trans 'You currently have no social network accounts connected to this account.' %}

+ {% endif %} + +

{% trans 'Add a 3rd Party Account' %}

+ {% include "socialaccount/snippets/provider_list.html" with process="connect" %} + {% include "socialaccount/snippets/login_extra.html" %} + +
+ +

{% trans "Theme Settings" %}

From 28f0e99b84e60923023ec94730805e1657ea2d27 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 30 Aug 2021 22:43:50 +0200 Subject: [PATCH 06/72] rulesets for all_auth --- InvenTree/users/models.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/InvenTree/users/models.py b/InvenTree/users/models.py index 179a70ed74..8a417a050d 100644 --- a/InvenTree/users/models.py +++ b/InvenTree/users/models.py @@ -67,7 +67,12 @@ class RuleSet(models.Model): 'report_billofmaterialsreport', 'report_purchaseorderreport', 'report_salesorderreport', - + 'account_emailaddress', + 'account_emailconfirmation', + 'sites_site', + 'socialaccount_socialaccount', + 'socialaccount_socialapp', + 'socialaccount_socialtoken', ], 'part_category': [ 'part_partcategory', From 8afb8ac4d1f614c55a2accbceb7e759fec363b51 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 31 Aug 2021 01:12:35 +0200 Subject: [PATCH 07/72] move to use allauth for login etc --- InvenTree/InvenTree/middleware.py | 10 +++++----- InvenTree/InvenTree/urls.py | 5 ----- InvenTree/templates/navbar.html | 4 ++-- 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/InvenTree/InvenTree/middleware.py b/InvenTree/InvenTree/middleware.py index 3cd5aa74f7..2df90bc5b7 100644 --- a/InvenTree/InvenTree/middleware.py +++ b/InvenTree/InvenTree/middleware.py @@ -64,15 +64,15 @@ class AuthRequiredMiddleware(object): # No authorization was found for the request if not authorized: # A logout request will redirect the user to the login screen - if request.path_info == reverse_lazy('logout'): - return HttpResponseRedirect(reverse_lazy('login')) + if request.path_info == reverse_lazy('account_logout'): + return HttpResponseRedirect(reverse_lazy('account_login')) path = request.path_info # List of URL endpoints we *do not* want to redirect to urls = [ - reverse_lazy('login'), - reverse_lazy('logout'), + reverse_lazy('account_login'), + reverse_lazy('account_logout'), reverse_lazy('admin:login'), reverse_lazy('admin:logout'), ] @@ -80,7 +80,7 @@ class AuthRequiredMiddleware(object): if path not in urls and not path.startswith('/api/'): # Save the 'next' parameter to pass through to the login view - return redirect('%s?next=%s' % (reverse_lazy('login'), request.path)) + return redirect('%s?next=%s' % (reverse_lazy('account_login'), request.path)) response = self.get_response(request) diff --git a/InvenTree/InvenTree/urls.py b/InvenTree/InvenTree/urls.py index 0ae6919669..b700743c98 100644 --- a/InvenTree/InvenTree/urls.py +++ b/InvenTree/InvenTree/urls.py @@ -8,7 +8,6 @@ Passes URL lookup downstream to each app as required. from django.conf.urls import url, include from django.urls import path from django.contrib import admin -from django.contrib.auth import views as auth_views from company.urls import company_urls from company.urls import manufacturer_part_urls @@ -142,9 +141,6 @@ urlpatterns = [ url(r'^auth/', include('rest_framework.urls', namespace='rest_framework')), - url(r'^login/?', auth_views.LoginView.as_view(), name='login'), - url(r'^logout/', auth_views.LogoutView.as_view(template_name='registration/logged_out.html'), name='logout'), - url(r'^settings/', include(settings_urls)), url(r'^edit-user/', EditUserView.as_view(), name='edit-user'), @@ -153,7 +149,6 @@ urlpatterns = [ url(r'^admin/error_log/', include('error_report.urls')), url(r'^admin/shell/', include('django_admin_shell.urls')), url(r'^admin/', admin.site.urls, name='inventree-admin'), - url(r'accounts/', include('django.contrib.auth.urls')), url(r'^index/', IndexView.as_view(), name='index'), url(r'^search/', SearchView.as_view(), name='search'), diff --git a/InvenTree/templates/navbar.html b/InvenTree/templates/navbar.html index 39719edba0..2210746d44 100644 --- a/InvenTree/templates/navbar.html +++ b/InvenTree/templates/navbar.html @@ -86,9 +86,9 @@ {% if user.is_staff %}
  • {% trans "Admin" %}
  • {% endif %} -
  • {% trans "Logout" %}
  • +
  • {% trans "Logout" %}
  • {% else %} -
  • {% trans "Login" %}
  • +
  • {% trans "Login" %}
  • {% endif %}
  • {% trans "Settings" %}
  • From 592fd09ad4b3a596d07f7dd2af609759e5c7e257 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 31 Aug 2021 01:38:46 +0200 Subject: [PATCH 08/72] remove old templates --- .../templates/registration/logged_out.html | 59 --------- InvenTree/templates/registration/login.html | 119 ------------------ .../registration/password_reset_complete.html | 59 --------- .../registration/password_reset_confirm.html | 69 ---------- .../registration/password_reset_done.html | 65 ---------- .../registration/password_reset_form.html | 68 ---------- 6 files changed, 439 deletions(-) delete mode 100644 InvenTree/templates/registration/logged_out.html delete mode 100644 InvenTree/templates/registration/login.html delete mode 100644 InvenTree/templates/registration/password_reset_complete.html delete mode 100644 InvenTree/templates/registration/password_reset_confirm.html delete mode 100644 InvenTree/templates/registration/password_reset_done.html delete mode 100644 InvenTree/templates/registration/password_reset_form.html diff --git a/InvenTree/templates/registration/logged_out.html b/InvenTree/templates/registration/logged_out.html deleted file mode 100644 index 703e077f35..0000000000 --- a/InvenTree/templates/registration/logged_out.html +++ /dev/null @@ -1,59 +0,0 @@ -{% load static %} -{% load i18n %} -{% load crispy_forms_tags %} -{% load inventree_extras %} - - - - - - - - - - - - - - - - - - - - - - - - {% inventree_title %} - - - - - - - - \ No newline at end of file diff --git a/InvenTree/templates/registration/login.html b/InvenTree/templates/registration/login.html deleted file mode 100644 index 7e0f76b00b..0000000000 --- a/InvenTree/templates/registration/login.html +++ /dev/null @@ -1,119 +0,0 @@ -{% load static %} -{% load i18n %} -{% load inventree_extras %} -{% load socialaccount %} - - - - - - - - - - - - - - - - - - - - - - - - {% inventree_title %} - - - - - - - - -{% providers_media_js %} - - - \ No newline at end of file diff --git a/InvenTree/templates/registration/password_reset_complete.html b/InvenTree/templates/registration/password_reset_complete.html deleted file mode 100644 index f332e23d0e..0000000000 --- a/InvenTree/templates/registration/password_reset_complete.html +++ /dev/null @@ -1,59 +0,0 @@ -{% load static %} -{% load i18n %} -{% load crispy_forms_tags %} -{% load inventree_extras %} - - - - - - - - - - - - - - - - - - - - - - - - {% inventree_title %} - - - - - - - - \ No newline at end of file diff --git a/InvenTree/templates/registration/password_reset_confirm.html b/InvenTree/templates/registration/password_reset_confirm.html deleted file mode 100644 index cede63f770..0000000000 --- a/InvenTree/templates/registration/password_reset_confirm.html +++ /dev/null @@ -1,69 +0,0 @@ -{% load static %} -{% load i18n %} -{% load crispy_forms_tags %} -{% load inventree_extras %} - - - - - - - - - - - - - - - - - - - - - - - - {% inventree_title %} - - - - - - - - \ No newline at end of file diff --git a/InvenTree/templates/registration/password_reset_done.html b/InvenTree/templates/registration/password_reset_done.html deleted file mode 100644 index a097e518dd..0000000000 --- a/InvenTree/templates/registration/password_reset_done.html +++ /dev/null @@ -1,65 +0,0 @@ -{% load static %} -{% load i18n %} -{% load crispy_forms_tags %} -{% load inventree_extras %} - - - - - - - - - - - - - - - - - - - - - - - - {% inventree_title %} - - - - - - - - \ No newline at end of file diff --git a/InvenTree/templates/registration/password_reset_form.html b/InvenTree/templates/registration/password_reset_form.html deleted file mode 100644 index 865a74ca1a..0000000000 --- a/InvenTree/templates/registration/password_reset_form.html +++ /dev/null @@ -1,68 +0,0 @@ -{% load static %} -{% load i18n %} -{% load crispy_forms_tags %} -{% load inventree_extras %} - - - - - - - - - - - - - - - - - - - - - - - - {% inventree_title %} - - - - - - - - \ No newline at end of file From 9a72ddf823e74581b888dbbc8c368dc2516ece58 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 31 Aug 2021 01:39:44 +0200 Subject: [PATCH 09/72] base for login / signup layout --- InvenTree/templates/account/base.html | 88 +++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 InvenTree/templates/account/base.html diff --git a/InvenTree/templates/account/base.html b/InvenTree/templates/account/base.html new file mode 100644 index 0000000000..077bfa55bc --- /dev/null +++ b/InvenTree/templates/account/base.html @@ -0,0 +1,88 @@ +{% load static %} +{% load i18n %} +{% load crispy_forms_tags %} +{% load inventree_extras %} + + + + + + + + + + + + + + + + + + + + + + + + {% inventree_title %} | {% block head_title %}{% endblock %} + + +{% block extra_head %} +{% endblock %} + + + + + + {% if messages %} +
    + {% for message in messages %} +
    +
    + {{message}} +
    +
    + {% endfor %} +
    + {% endif %} + +
    + {% if part.variant_of %} +
    + {% object_link 'part-detail' part.variant_of.id part.variant_of.full_name as link %} + {% blocktrans %}This part is a variant of {{link}}{% endblocktrans %} +
    + {% endif %} +
    + + + + + + {% block extra_body %} + {% endblock %} + + \ No newline at end of file From 1772348d36acec61fb137800febff8ec0329a1d7 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 31 Aug 2021 07:49:38 +0200 Subject: [PATCH 10/72] layout fixes --- InvenTree/templates/InvenTree/settings/user.html | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/InvenTree/templates/InvenTree/settings/user.html b/InvenTree/templates/InvenTree/settings/user.html index f57221c9ac..1acad0bb87 100644 --- a/InvenTree/templates/InvenTree/settings/user.html +++ b/InvenTree/templates/InvenTree/settings/user.html @@ -43,7 +43,7 @@

    {% trans "Social Accounts" %}

    -
    +
    {% if form.accounts %}

    {% blocktrans %}You can sign in to your account using any of the following third party accounts:{% endblocktrans %}

    @@ -81,7 +81,9 @@ {% endif %}

    {% trans 'Add a 3rd Party Account' %}

    - {% include "socialaccount/snippets/provider_list.html" with process="connect" %} +
      + {% include "socialaccount/snippets/provider_list.html" with process="connect" %} +
    {% include "socialaccount/snippets/login_extra.html" %}
    From 566257881d99430bd523534cbc56825a63718320 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 31 Aug 2021 23:49:31 +0200 Subject: [PATCH 11/72] patch email-endpoint to use settings view --- InvenTree/InvenTree/urls.py | 8 ++++++-- InvenTree/InvenTree/views.py | 10 ++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/InvenTree/InvenTree/urls.py b/InvenTree/InvenTree/urls.py index b700743c98..d0c0bc076a 100644 --- a/InvenTree/InvenTree/urls.py +++ b/InvenTree/InvenTree/urls.py @@ -37,7 +37,7 @@ from rest_framework.documentation import include_docs_urls from .views import auth_request from .views import IndexView, SearchView, DatabaseStatsView -from .views import SettingsView, EditUserView, SetPasswordView +from .views import SettingsView, EditUserView, SetPasswordView, CustomEmailView from .views import CurrencyRefreshView from .views import AppearanceSelectView, SettingCategorySelectView from .views import DynamicJsView @@ -160,7 +160,11 @@ urlpatterns = [ url(r'^api-doc/', include_docs_urls(title='InvenTree API')), url(r'^markdownx/', include('markdownx.urls')), - url(r'^accounts/', include('allauth.urls')), # SSO + + # Single Sign On / allauth + # overrides of urlpatterns + url(r'^accounts/email/', CustomEmailView.as_view(), name='account_email'), + url(r'^accounts/', include('allauth.urls')), # included urlpatterns ] # Server running in "DEBUG" mode? diff --git a/InvenTree/InvenTree/views.py b/InvenTree/InvenTree/views.py index 0528c6c694..a8b9b792fc 100644 --- a/InvenTree/InvenTree/views.py +++ b/InvenTree/InvenTree/views.py @@ -812,6 +812,16 @@ class SettingsView(TemplateView): return ctx +class CustomEmailView(LoginRequiredMixin, EmailView): + """ + Override of allauths EmailView to always show the settings but leave the functions allow + """ + success_url = reverse_lazy("settings") + + def get(self, request, *args, **kwargs): + # always redirect to settings + return HttpResponseRedirect(self.success_url) + class CurrencyRefreshView(RedirectView): """ From 5aca27f2852a6583022f566580ce47e6e0b93d53 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 31 Aug 2021 23:51:33 +0200 Subject: [PATCH 12/72] integrate email-actions into settings --- InvenTree/InvenTree/views.py | 10 ++- .../templates/InvenTree/settings/user.html | 71 +++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/views.py b/InvenTree/InvenTree/views.py index a8b9b792fc..6d99e12ef8 100644 --- a/InvenTree/InvenTree/views.py +++ b/InvenTree/InvenTree/views.py @@ -17,13 +17,17 @@ from django.urls import reverse_lazy from django.shortcuts import redirect from django.conf import settings -from django.contrib.auth.mixins import PermissionRequiredMixin +from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin from django.views import View from django.views.generic import ListView, DetailView, CreateView, FormView, DeleteView, UpdateView from django.views.generic.base import RedirectView, TemplateView from djmoney.contrib.exchange.models import ExchangeBackend, Rate +from allauth.account.forms import AddEmailForm +from allauth.account.models import EmailAddress +from allauth.account.views import EmailView + from common.settings import currency_code_default, currency_codes from part.models import Part, PartCategory @@ -810,6 +814,10 @@ class SettingsView(TemplateView): except: ctx["locale_stats"] = {} + # Forms and context for allauth + ctx['add_email_form'] = AddEmailForm + ctx["can_add_email"] = EmailAddress.objects.can_add_email(self.request.user) + return ctx class CustomEmailView(LoginRequiredMixin, EmailView): diff --git a/InvenTree/templates/InvenTree/settings/user.html b/InvenTree/templates/InvenTree/settings/user.html index 1acad0bb87..39309eab73 100644 --- a/InvenTree/templates/InvenTree/settings/user.html +++ b/InvenTree/templates/InvenTree/settings/user.html @@ -39,6 +39,63 @@ +
    +

    {% trans "E-Mail" %}

    +
    + +
    + {% if user.emailaddress_set.all %} +

    {% trans 'The following e-mail addresses are associated with your account:' %}

    + + + + {% else %} +

    {% trans 'Warning:'%} + {% trans "You currently do not have any e-mail address set up. You should really add an e-mail address so you can receive notifications, reset your password, etc." %} +

    + + {% endif %} + + {% if can_add_email %} +

    {% trans "Add E-mail Address" %}

    + +
    + {% csrf_token %} + {{ add_email_form.as_p }} + +
    + {% endif %} +
    +
    +

    {% trans "Social Accounts" %}

    @@ -156,4 +213,18 @@
    +{% endblock %} + +{% block js_ready %} +(function() { + var message = "{% trans 'Do you really want to remove the selected e-mail address?' %}"; + var actions = document.getElementsByName('action_remove'); + if (actions.length) { + actions[0].addEventListener("click", function(e) { + if (! confirm(message)) { + e.preventDefault(); + } + }); + } +})(); {% endblock %} \ No newline at end of file From 501802e0f9f95abdf025c91750b619b71d692708 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 1 Sep 2021 00:04:34 +0200 Subject: [PATCH 13/72] PEP fix --- InvenTree/InvenTree/views.py | 1 + 1 file changed, 1 insertion(+) diff --git a/InvenTree/InvenTree/views.py b/InvenTree/InvenTree/views.py index 6d99e12ef8..53ced1e6dc 100644 --- a/InvenTree/InvenTree/views.py +++ b/InvenTree/InvenTree/views.py @@ -820,6 +820,7 @@ class SettingsView(TemplateView): return ctx + class CustomEmailView(LoginRequiredMixin, EmailView): """ Override of allauths EmailView to always show the settings but leave the functions allow From 3eeb6e161df6dfc2cb9fe9ab62caf78d41b211fa Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 1 Sep 2021 00:08:13 +0200 Subject: [PATCH 14/72] fix layout --- InvenTree/templates/InvenTree/settings/user.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/InvenTree/templates/InvenTree/settings/user.html b/InvenTree/templates/InvenTree/settings/user.html index 39309eab73..b3b03cf238 100644 --- a/InvenTree/templates/InvenTree/settings/user.html +++ b/InvenTree/templates/InvenTree/settings/user.html @@ -85,7 +85,8 @@ {% endif %} {% if can_add_email %} -

    {% trans "Add E-mail Address" %}

    +
    +

    {% trans "Add E-mail Address" %}

    {% csrf_token %} From f997e67ea2a3ded84a1a37a73745b91ee7decb6b Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 1 Sep 2021 01:09:23 +0200 Subject: [PATCH 15/72] patch social-connections into settings --- InvenTree/InvenTree/urls.py | 3 ++- InvenTree/InvenTree/views.py | 17 +++++++++++++++++ .../templates/InvenTree/settings/user.html | 8 ++++---- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/InvenTree/InvenTree/urls.py b/InvenTree/InvenTree/urls.py index d0c0bc076a..de874193b1 100644 --- a/InvenTree/InvenTree/urls.py +++ b/InvenTree/InvenTree/urls.py @@ -37,7 +37,7 @@ from rest_framework.documentation import include_docs_urls from .views import auth_request from .views import IndexView, SearchView, DatabaseStatsView -from .views import SettingsView, EditUserView, SetPasswordView, CustomEmailView +from .views import SettingsView, EditUserView, SetPasswordView, CustomEmailView, CustomConnectionsView from .views import CurrencyRefreshView from .views import AppearanceSelectView, SettingCategorySelectView from .views import DynamicJsView @@ -164,6 +164,7 @@ urlpatterns = [ # Single Sign On / allauth # overrides of urlpatterns url(r'^accounts/email/', CustomEmailView.as_view(), name='account_email'), + url(r'^accounts/social/connections/', CustomConnectionsView.as_view(), name='socialaccount_connections'), url(r'^accounts/', include('allauth.urls')), # included urlpatterns ] diff --git a/InvenTree/InvenTree/views.py b/InvenTree/InvenTree/views.py index 53ced1e6dc..9951bc992f 100644 --- a/InvenTree/InvenTree/views.py +++ b/InvenTree/InvenTree/views.py @@ -25,8 +25,10 @@ from django.views.generic.base import RedirectView, TemplateView from djmoney.contrib.exchange.models import ExchangeBackend, Rate from allauth.account.forms import AddEmailForm +from allauth.socialaccount.forms import DisconnectForm from allauth.account.models import EmailAddress from allauth.account.views import EmailView +from allauth.socialaccount.views import ConnectionsView from common.settings import currency_code_default, currency_codes @@ -818,6 +820,10 @@ class SettingsView(TemplateView): ctx['add_email_form'] = AddEmailForm ctx["can_add_email"] = EmailAddress.objects.can_add_email(self.request.user) + # Form and context for allauth social-accounts + ctx["request"] = self.request + ctx['social_form'] = DisconnectForm(request=self.request) + return ctx @@ -832,6 +838,17 @@ class CustomEmailView(LoginRequiredMixin, EmailView): return HttpResponseRedirect(self.success_url) +class CustomConnectionsView(LoginRequiredMixin, ConnectionsView): + """ + Override of allauths ConnectionsView to always show the settings but leave the functions allow + """ + success_url = reverse_lazy("settings") + + def get(self, request, *args, **kwargs): + # always redirect to settings + return HttpResponseRedirect(self.success_url) + + class CurrencyRefreshView(RedirectView): """ POST endpoint to refresh / update exchange rates diff --git a/InvenTree/templates/InvenTree/settings/user.html b/InvenTree/templates/InvenTree/settings/user.html index b3b03cf238..7ac64880e0 100644 --- a/InvenTree/templates/InvenTree/settings/user.html +++ b/InvenTree/templates/InvenTree/settings/user.html @@ -102,7 +102,7 @@
    - {% if form.accounts %} + {% if social_form.accounts %}

    {% blocktrans %}You can sign in to your account using any of the following third party accounts:{% endblocktrans %}

    @@ -110,11 +110,11 @@ {% csrf_token %}
    - {% if form.non_field_errors %} -
    {{ form.non_field_errors }}
    + {% if social_form.non_field_errors %} +
    {{ social_form.non_field_errors }}
    {% endif %} - {% for base_account in form.accounts %} + {% for base_account in social_form.accounts %} {% with base_account.get_provider_account as account %}
    diff --git a/InvenTree/templates/socialaccount/snippets/provider_list.html b/InvenTree/templates/socialaccount/snippets/provider_list.html new file mode 100644 index 0000000000..268134b35f --- /dev/null +++ b/InvenTree/templates/socialaccount/snippets/provider_list.html @@ -0,0 +1,17 @@ +{% load socialaccount %} + +{% get_providers as socialaccount_providers %} + +{% for provider in socialaccount_providers %} +{% if provider.id == "openid" %} +{% for brand in provider.get_brands %} + {{brand.name}} +{% endfor %} +{% endif %} + {{provider.name}} +{% endfor %} From 0b8a88e2160b47699547f7ab1aac83cfced49c37 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 1 Sep 2021 02:18:41 +0200 Subject: [PATCH 19/72] js function to ingest icons --- InvenTree/templates/js/dynamic/inventree.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/InvenTree/templates/js/dynamic/inventree.js b/InvenTree/templates/js/dynamic/inventree.js index acfd57762c..c7792c1deb 100644 --- a/InvenTree/templates/js/dynamic/inventree.js +++ b/InvenTree/templates/js/dynamic/inventree.js @@ -130,6 +130,11 @@ function inventreeDocReady() { minLength: 2, classes: {'ui-autocomplete': 'dropdown-menu search-menu'}, }); + + // Generate brand-icons + $('.brand-icon').each(function(i, obj){ + loadBrandIcon($(this), $(this).attr('brand_name')); + }); } function isFileTransfer(transfer) { @@ -381,4 +386,14 @@ function inventreeDel(name) { var key = 'inventree-' + name; localStorage.removeItem(key); -} \ No newline at end of file +} + +function loadBrandIcon(elem, name) { + // check if icon exists + var icon = window.FontAwesome.icon({prefix: 'fab', iconName: name}); + + if (icon){ + // add icon to button + elem.addClass('fab fa-' + name); + } +} From 146f856f539e95360fe8e8161db1b3c86bdbb56f Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 1 Sep 2021 02:18:58 +0200 Subject: [PATCH 20/72] use icon also in connections list --- InvenTree/templates/InvenTree/settings/user.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/InvenTree/templates/InvenTree/settings/user.html b/InvenTree/templates/InvenTree/settings/user.html index 2002069c3b..2e28166609 100644 --- a/InvenTree/templates/InvenTree/settings/user.html +++ b/InvenTree/templates/InvenTree/settings/user.html @@ -115,7 +115,8 @@
    From 22401d4229bca61d4a884b77bdf6748b7c08bcdd Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 1 Sep 2021 07:34:00 +0200 Subject: [PATCH 21/72] use normal button style --- InvenTree/templates/InvenTree/settings/user.html | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/InvenTree/templates/InvenTree/settings/user.html b/InvenTree/templates/InvenTree/settings/user.html index 2e28166609..bf1b26a725 100644 --- a/InvenTree/templates/InvenTree/settings/user.html +++ b/InvenTree/templates/InvenTree/settings/user.html @@ -65,9 +65,9 @@ {% endfor %}
    - - - + + +
    @@ -87,7 +87,7 @@ {% csrf_token %} {{ add_email_form.as_p }} - + {% endif %}
    @@ -124,7 +124,7 @@ {% endfor %}
    - +
    From 0ff6de62e9cfda43a5701bbea1b8eb6c7ff147c3 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 1 Sep 2021 07:34:37 +0200 Subject: [PATCH 22/72] crispy forms! --- InvenTree/templates/InvenTree/settings/user.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/InvenTree/templates/InvenTree/settings/user.html b/InvenTree/templates/InvenTree/settings/user.html index bf1b26a725..27a7fd3506 100644 --- a/InvenTree/templates/InvenTree/settings/user.html +++ b/InvenTree/templates/InvenTree/settings/user.html @@ -3,6 +3,7 @@ {% load i18n %} {% load inventree_extras %} {% load socialaccount %} +{% load crispy_forms_tags %} {% block label %}account{% endblock %} @@ -86,7 +87,7 @@
    {% csrf_token %} - {{ add_email_form.as_p }} + {{ add_email_form|crispy }}
    {% endif %} From 276eab19b5652feb878dc945529dd282974ff09d Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 1 Sep 2021 22:02:43 +0200 Subject: [PATCH 23/72] reordering the base template makes comparring to the original template much easier --- InvenTree/templates/account/base.html | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/InvenTree/templates/account/base.html b/InvenTree/templates/account/base.html index 077bfa55bc..bdf30eedd8 100644 --- a/InvenTree/templates/account/base.html +++ b/InvenTree/templates/account/base.html @@ -12,15 +12,12 @@ + + - - - - - {% inventree_title %} | {% block head_title %}{% endblock %} From f428ade8773fd3b495e3957781b2184d497b4442 Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Sun, 5 Sep 2021 18:34:59 +0200 Subject: [PATCH 37/72] doc for account base --- InvenTree/templates/account/base.html | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/InvenTree/templates/account/base.html b/InvenTree/templates/account/base.html index c7b484f82f..ff4613ead1 100644 --- a/InvenTree/templates/account/base.html +++ b/InvenTree/templates/account/base.html @@ -77,8 +77,12 @@ {% block extra_body %} {% endblock %} +<!-- Scripts --> <script type="text/javascript" src="{% static 'script/jquery_3.3.1_jquery.min.js' %}"></script> + +<!-- dynamic javascript templates --> <script type='text/javascript' src="{% url 'inventree.js' %}"></script> + <script type='text/javascript' src="{% static 'fontawesome/js/solid.js' %}"></script> <script type='text/javascript' src="{% static 'fontawesome/js/brands.js' %}"></script> <script type='text/javascript' src="{% static 'fontawesome/js/fontawesome.js' %}"></script> From d2a68125ff2818950f1b202e72afd44ecfc962da Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Sun, 5 Sep 2021 18:35:58 +0200 Subject: [PATCH 38/72] cleanup --- InvenTree/templates/account/base.html | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/InvenTree/templates/account/base.html b/InvenTree/templates/account/base.html index ff4613ead1..9e9272c478 100644 --- a/InvenTree/templates/account/base.html +++ b/InvenTree/templates/account/base.html @@ -45,17 +45,6 @@ </div> {% endif %} - <div class='info-messages'> - {% if part.variant_of %} - <div class='alert alert-info alert-block' style='padding: 10px;'> - {% object_link 'part-detail' part.variant_of.id part.variant_of.full_name as link %} - {% blocktrans %}This part is a variant of {{link}}{% endblocktrans %} - </div> - {% endif %} - </div> - - - <div class='main body-wrapper login-screen'> <div class='login-container'> From 1146e2abd98bfd8f3c71c30d0f0aac9515889a70 Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Sun, 5 Sep 2021 19:01:21 +0200 Subject: [PATCH 39/72] adding login settings in admin --- .../templates/InvenTree/settings/login.html | 21 +++++++++++++++++++ .../templates/InvenTree/settings/navbar.html | 6 ++++++ .../InvenTree/settings/settings.html | 1 + 3 files changed, 28 insertions(+) create mode 100644 InvenTree/templates/InvenTree/settings/login.html diff --git a/InvenTree/templates/InvenTree/settings/login.html b/InvenTree/templates/InvenTree/settings/login.html new file mode 100644 index 0000000000..675787bfe9 --- /dev/null +++ b/InvenTree/templates/InvenTree/settings/login.html @@ -0,0 +1,21 @@ +{% extends "panel.html" %} +{% load i18n %} +{% load inventree_extras %} + +{% block label %}login{% endblock %} + + +{% block heading %} +{% trans "Login Settings" %} +{% endblock %} + +{% block content %} + +<table class='table table-striped table-condensed'> + {% include "InvenTree/settings/header.html" %} + <tbody> + + </tbody> +</table> + +{% endblock %} diff --git a/InvenTree/templates/InvenTree/settings/navbar.html b/InvenTree/templates/InvenTree/settings/navbar.html index ebf24bffb1..aa4db644a6 100644 --- a/InvenTree/templates/InvenTree/settings/navbar.html +++ b/InvenTree/templates/InvenTree/settings/navbar.html @@ -62,6 +62,12 @@ </a> </li> + <li class='list-group-item' title='{% trans "Login" %}'> + <a href='#' class='nav-toggle' id='select-login'> + <span class='fas fa-fingerprint'></span> {% trans "Login" %} + </a> + </li> + <li class='list-group-item' title='{% trans "Barcodes" %}'> <a href='#' class='nav-toggle' id='select-barcodes'> <span class='fas fa-qrcode'></span> {% trans "Barcodes" %} diff --git a/InvenTree/templates/InvenTree/settings/settings.html b/InvenTree/templates/InvenTree/settings/settings.html index beb7f5eb04..ce88c197a5 100644 --- a/InvenTree/templates/InvenTree/settings/settings.html +++ b/InvenTree/templates/InvenTree/settings/settings.html @@ -24,6 +24,7 @@ {% if user.is_staff %} {% include "InvenTree/settings/global.html" %} +{% include "InvenTree/settings/login.html" %} {% include "InvenTree/settings/barcode.html" %} {% include "InvenTree/settings/currencies.html" %} {% include "InvenTree/settings/report.html" %} From fa1ea2f7017b97396e108d708e86c3df0a732797 Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Sun, 5 Sep 2021 19:56:48 +0200 Subject: [PATCH 40/72] setting for pwd forgot --- InvenTree/common/models.py | 8 ++++++++ InvenTree/templates/InvenTree/settings/login.html | 4 +++- InvenTree/templates/account/login.html | 6 +++++- InvenTree/templates/account/password_change.html | 7 ++++++- InvenTree/templates/account/password_reset.html | 10 +++++++++- 5 files changed, 31 insertions(+), 4 deletions(-) diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index aed6f2bf14..88377e0201 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -826,6 +826,14 @@ class InvenTreeSetting(BaseInvenTreeSetting): 'default': True, 'validator': bool, }, + + # login / SSO + 'LOGIN_ENABLE_PWD_FORGOT': { + 'name': _('Enable password forgot'), + 'description': _('Enable password forgot function on the login-pages'), + 'default': True, + 'validator': bool, + }, } class Meta: diff --git a/InvenTree/templates/InvenTree/settings/login.html b/InvenTree/templates/InvenTree/settings/login.html index 675787bfe9..b3758aadf6 100644 --- a/InvenTree/templates/InvenTree/settings/login.html +++ b/InvenTree/templates/InvenTree/settings/login.html @@ -14,7 +14,9 @@ <table class='table table-striped table-condensed'> {% include "InvenTree/settings/header.html" %} <tbody> - + {% include "InvenTree/settings/setting.html" with key="LOGIN_ENABLE_REG" icon="fa-info-circle" %} + {% include "InvenTree/settings/setting.html" with key="LOGIN_ENABLE_SSO" icon="fa-info-circle" %} + {% include "InvenTree/settings/setting.html" with key="LOGIN_ENABLE_PWD_FORGOT" icon="fa-info-circle" %} </tbody> </table> diff --git a/InvenTree/templates/account/login.html b/InvenTree/templates/account/login.html index 340098d7a1..f5cc64717f 100644 --- a/InvenTree/templates/account/login.html +++ b/InvenTree/templates/account/login.html @@ -1,11 +1,13 @@ {% extends "account/base.html" %} -{% load i18n account socialaccount crispy_forms_tags %} +{% load i18n account socialaccount crispy_forms_tags inventree_extras %} {% block head_title %}{% trans "Sign In" %}{% endblock %} {% block content %} +{% settings_value 'LOGIN_ENABLE_PWD_FORGOT' as enable_pwd_forgot %} + <h1>{% trans "Sign In" %}</h1> {% get_providers as socialaccount_providers %} @@ -27,7 +29,9 @@ for a account and sign in below:{% endblocktrans %}</p> <div class="btn-toolbar"> <button class="btn btn-primary col-md-8" type="submit">{% trans "Sign In" %}</button> + {% if enable_pwd_forgot %} <a class="btn btn-primary" href="{% url 'account_reset_password' %}">{% trans "Forgot Password?" %}</a> + {% endif %} </div> </form> diff --git a/InvenTree/templates/account/password_change.html b/InvenTree/templates/account/password_change.html index 22f34ab824..702094fcba 100644 --- a/InvenTree/templates/account/password_change.html +++ b/InvenTree/templates/account/password_change.html @@ -1,16 +1,21 @@ {% extends "account/base.html" %} -{% load i18n crispy_forms_tags %} +{% load i18n crispy_forms_tags inventree_extras %} {% block head_title %}{% trans "Change Password" %}{% endblock %} {% block content %} + +{% settings_value 'LOGIN_ENABLE_PWD_FORGOT' as enable_pwd_forgot %} + <h1>{% trans "Change Password" %}</h1> <form method="POST" action="{% url 'account_change_password' %}" class="password_change"> {% csrf_token %} {{ form|crispy }} <button type="submit" class="btn btn-primary" name="action">{% trans "Change Password" %}</button> + {% if enable_pwd_forgot %} <a href="{% url 'account_reset_password' %}">{% trans "Forgot Password?" %}</a> + {% endif %} </form> {% endblock %} diff --git a/InvenTree/templates/account/password_reset.html b/InvenTree/templates/account/password_reset.html index 7ed3a5c772..d28110b81c 100644 --- a/InvenTree/templates/account/password_reset.html +++ b/InvenTree/templates/account/password_reset.html @@ -1,16 +1,19 @@ {% extends "account/base.html" %} -{% load i18n account crispy_forms_tags %} +{% load i18n account crispy_forms_tags inventree_extras %} {% block head_title %}{% trans "Password Reset" %}{% endblock %} {% block content %} +{% settings_value 'LOGIN_ENABLE_PWD_FORGOT' as enable_pwd_forgot %} + <h1>{% trans "Password Reset" %}</h1> {% if user.is_authenticated %} {% include "account/snippets/already_logged_in.html" %} {% endif %} + {% if enable_pwd_forgot %} <p>{% trans "Forgotten your password? Enter your e-mail address below, and we'll send you an e-mail allowing you to reset it." %}</p> <form method="POST" action="{% url 'account_reset_password' %}" class="password_reset"> @@ -18,4 +21,9 @@ {{ form|crispy }} <input type="submit" class="btn btn-primary btn-block" value="{% trans 'Reset My Password' %}" /> </form> + {% else %} + <div class='alert alert-block alert-danger'> + <p>{% trans "This function is currently disabled. Please contact an administrator." %}</p> + </div> + {% endif %} {% endblock %} From 7fa87edda7c03e539318addbe632cbf15b5d4a76 Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Sun, 5 Sep 2021 19:58:21 +0200 Subject: [PATCH 41/72] refactor --- InvenTree/InvenTree/settings.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 7fcda29dd4..0aa70d88f1 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -286,11 +286,6 @@ INSTALLED_APPS = [ 'allauth.socialaccount', # Use 'social' providers ] -# Load the allauth social backends -SOCIAL_BACKENDS = CONFIG.get('social_backends', []) -for app in SOCIAL_BACKENDS: - INSTALLED_APPS.append(app) - MIDDLEWARE = CONFIG.get('middleware', [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', @@ -659,3 +654,10 @@ MESSAGE_TAGS = { } SITE_ID = 1 + + +# Load the allauth social backends +SOCIAL_BACKENDS = CONFIG.get('social_backends', []) +for app in SOCIAL_BACKENDS: + INSTALLED_APPS.append(app) + From 34a15ff51f0074012a7b52ceb544b430d952575f Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Sun, 5 Sep 2021 20:17:49 +0200 Subject: [PATCH 42/72] setting to enable signup --- InvenTree/common/models.py | 6 ++++++ InvenTree/templates/account/login.html | 3 +++ InvenTree/templates/account/signup.html | 11 ++++++++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index 88377e0201..6d75186c95 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -834,6 +834,12 @@ class InvenTreeSetting(BaseInvenTreeSetting): 'default': True, 'validator': bool, }, + 'LOGIN_ENABLE_REG': { + 'name': _('Enable registration'), + 'description': _('Enable self-registration for users on the login-pages'), + 'default': False, + 'validator': bool, + }, } class Meta: diff --git a/InvenTree/templates/account/login.html b/InvenTree/templates/account/login.html index f5cc64717f..33bc15b526 100644 --- a/InvenTree/templates/account/login.html +++ b/InvenTree/templates/account/login.html @@ -6,10 +6,12 @@ {% block content %} +{% settings_value 'LOGIN_ENABLE_REG' as enable_reg %} {% settings_value 'LOGIN_ENABLE_PWD_FORGOT' as enable_pwd_forgot %} <h1>{% trans "Sign In" %}</h1> +{% if enable_reg %} {% get_providers as socialaccount_providers %} {% if socialaccount_providers %} <p>{% blocktrans with site.name as site_name %}Please sign in with one @@ -19,6 +21,7 @@ for a account and sign in below:{% endblocktrans %}</p> <p>{% blocktrans %}If you have not created an account yet, then please <a href="{{ signup_url }}">sign up</a> first.{% endblocktrans %}</p> {% endif %} +{% endif %} <form class="login" method="POST" action="{% url 'account_login' %}"> {% csrf_token %} diff --git a/InvenTree/templates/account/signup.html b/InvenTree/templates/account/signup.html index 6b68637374..c72a09502a 100644 --- a/InvenTree/templates/account/signup.html +++ b/InvenTree/templates/account/signup.html @@ -1,14 +1,17 @@ {% extends "account/base.html" %} -{% load i18n crispy_forms_tags %} +{% load i18n crispy_forms_tags inventree_extras %} {% block head_title %}{% trans "Signup" %}{% endblock %} {% block content %} +{% settings_value 'LOGIN_ENABLE_REG' as enable_reg %} + <h1>{% trans "Sign Up" %}</h1> <p>{% blocktrans %}Already have an account? Then please <a href="{{ login_url }}">sign in</a>.{% endblocktrans %}</p> +{% if enable_reg %} <form class="signup" id="signup_form" method="post" action="{% url 'account_signup' %}"> {% csrf_token %} {{ form|crispy }} @@ -25,4 +28,10 @@ </div> {% include "socialaccount/snippets/login_extra.html" %} +{% else %} +<div class='alert alert-block alert-danger'> + <p>{% trans "This function is currently disabled. Please contact an administrator." %}</p> +</div> +{% endif %} + {% endblock %} From e255f2bf42c3edd046f566f5478fb1c2dae4662a Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Sun, 5 Sep 2021 20:20:57 +0200 Subject: [PATCH 43/72] setting for sso login --- InvenTree/common/models.py | 6 ++++++ InvenTree/templates/account/login.html | 4 +++- InvenTree/templates/account/signup.html | 3 +++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index 6d75186c95..74d6b02f2f 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -840,6 +840,12 @@ class InvenTreeSetting(BaseInvenTreeSetting): 'default': False, 'validator': bool, }, + 'LOGIN_ENABLE_SSO': { + 'name': _('Enable SSO'), + 'description': _('Enable SSO on the login-pages'), + 'default': False, + 'validator': bool, + }, } class Meta: diff --git a/InvenTree/templates/account/login.html b/InvenTree/templates/account/login.html index 33bc15b526..574cfa01f3 100644 --- a/InvenTree/templates/account/login.html +++ b/InvenTree/templates/account/login.html @@ -8,6 +8,7 @@ {% settings_value 'LOGIN_ENABLE_REG' as enable_reg %} {% settings_value 'LOGIN_ENABLE_PWD_FORGOT' as enable_pwd_forgot %} +{% settings_value 'LOGIN_ENABLE_SSO' as enable_sso %} <h1>{% trans "Sign In" %}</h1> @@ -38,12 +39,13 @@ for a account and sign in below:{% endblocktrans %}</p> </div> </form> - +{% if enable_sso %} <br> <h4 class="text-center">{% trans 'or use SSO' %}</h4> <div> {% include "socialaccount/snippets/provider_list.html" with process="login" %} </div> {% include "socialaccount/snippets/login_extra.html" %} +{% endif %} {% endblock %} diff --git a/InvenTree/templates/account/signup.html b/InvenTree/templates/account/signup.html index c72a09502a..f2972ba30b 100644 --- a/InvenTree/templates/account/signup.html +++ b/InvenTree/templates/account/signup.html @@ -6,6 +6,7 @@ {% block content %} {% settings_value 'LOGIN_ENABLE_REG' as enable_reg %} +{% settings_value 'LOGIN_ENABLE_SSO' as enable_sso %} <h1>{% trans "Sign Up" %}</h1> @@ -21,12 +22,14 @@ <button type="submit" class="btn btn-primary btn-block">{% trans "Sign Up" %}</button> </form> +{% if enable_sso %} <br> <h4>{% trans 'Or use a SSO-provider for signup' %}</h4> <div> {% include "socialaccount/snippets/provider_list.html" with process="login" %} </div> {% include "socialaccount/snippets/login_extra.html" %} +{% endif %} {% else %} <div class='alert alert-block alert-danger'> From ae8e2696b6557fe882c1e314145a24e9b5a64888 Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Mon, 6 Sep 2021 07:10:32 +0200 Subject: [PATCH 44/72] PEP fix --- InvenTree/InvenTree/settings.py | 1 - 1 file changed, 1 deletion(-) diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 0aa70d88f1..f5d6ce9689 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -660,4 +660,3 @@ SITE_ID = 1 SOCIAL_BACKENDS = CONFIG.get('social_backends', []) for app in SOCIAL_BACKENDS: INSTALLED_APPS.append(app) - From 3963ece6d2e348d6a5a8683ea30d33aacd67437c Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Mon, 6 Sep 2021 07:29:40 +0200 Subject: [PATCH 45/72] make notifications go away --- InvenTree/templates/account/base.html | 30 +++++++++++++-------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/InvenTree/templates/account/base.html b/InvenTree/templates/account/base.html index 9e9272c478..97ccec4bc5 100644 --- a/InvenTree/templates/account/base.html +++ b/InvenTree/templates/account/base.html @@ -33,18 +33,6 @@ Background Image Attribution: https://unsplash.com/photos/Ixvv3YZkd7w --> - {% if messages %} - <div> - {% for message in messages %} - <div class='info-messages'> - <div class='alert alert-info alert-block' style='padding: 10px;'> - {{message}} - </div> - </div> - {% endfor %} - </div> - {% endif %} - <div class='main body-wrapper login-screen'> <div class='login-container'> @@ -61,13 +49,16 @@ </div> </div> - </div> + {% block extra_body %} + {% endblock %} - {% block extra_body %} - {% endblock %} + {% include 'notification.html' %} + </div> <!-- Scripts --> <script type="text/javascript" src="{% static 'script/jquery_3.3.1_jquery.min.js' %}"></script> +<!-- general InvenTree --> +<script type='text/javascript' src="{% static 'script/inventree/notification.js' %}"></script> <!-- dynamic javascript templates --> <script type='text/javascript' src="{% url 'inventree.js' %}"></script> @@ -80,6 +71,15 @@ <script type='text/javascript'> $(document).ready(function () { + // notifications + {% if messages %} + {% for message in messages %} + showAlertOrCache('alert-info', '{{message}}', true); + {% endfor %} + {% endif %} + + showCachedAlerts(); + inventreeDocReady(); }); From a4209d38ccf1c51291f408493d8221638d1718c0 Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Mon, 6 Sep 2021 22:07:50 +0200 Subject: [PATCH 46/72] setting to make mail required on signup --- InvenTree/InvenTree/forms.py | 14 +++++++++++++- InvenTree/InvenTree/settings.py | 12 ++++++++++++ InvenTree/common/models.py | 6 ++++++ InvenTree/templates/InvenTree/settings/login.html | 1 + 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/forms.py b/InvenTree/InvenTree/forms.py index 228eda7a3c..5cdba32014 100644 --- a/InvenTree/InvenTree/forms.py +++ b/InvenTree/InvenTree/forms.py @@ -13,8 +13,10 @@ from crispy_forms.helper import FormHelper from crispy_forms.layout import Layout, Field from crispy_forms.bootstrap import PrependedText, AppendedText, PrependedAppendedText, StrictButton, Div -from part.models import PartCategory +from allauth.account.forms import SignupForm +from part.models import PartCategory +from common.models import InvenTreeSetting class HelperForm(forms.ModelForm): """ Provides simple integration of crispy_forms extension. """ @@ -203,3 +205,13 @@ class SettingCategorySelectForm(forms.ModelForm): css_class='row', ), ) + + +# override allauth forms +class CustomSignupForm(SignupForm): + """ + Override to use dynamic settings + """ + def __init__(self, *args, **kwargs): + kwargs['email_required'] = InvenTreeSetting.get_setting('LOGIN_MAIL_REQUIRED') + super().__init__(*args, **kwargs) diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index f5d6ce9689..5f2323d986 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -660,3 +660,15 @@ SITE_ID = 1 SOCIAL_BACKENDS = CONFIG.get('social_backends', []) for app in SOCIAL_BACKENDS: INSTALLED_APPS.append(app) + +# settings for allauth +ACCOUNT_FORMS = { + 'login': 'allauth.account.forms.LoginForm', + 'signup': 'InvenTree.forms.CustomSignupForm', + 'add_email': 'allauth.account.forms.AddEmailForm', + 'change_password': 'allauth.account.forms.ChangePasswordForm', + 'set_password': 'allauth.account.forms.SetPasswordForm', + 'reset_password': 'allauth.account.forms.ResetPasswordForm', + 'reset_password_from_key': 'allauth.account.forms.ResetPasswordKeyForm', + 'disconnect': 'allauth.socialaccount.forms.DisconnectForm', +} diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index 74d6b02f2f..25f360c0a0 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -846,6 +846,12 @@ class InvenTreeSetting(BaseInvenTreeSetting): 'default': False, 'validator': bool, }, + 'LOGIN_MAIL_REQUIRED': { + 'name': _('E-Mail required'), + 'description': _('Require user to supply mail on signup'), + 'default': False, + 'validator': bool, + }, } class Meta: diff --git a/InvenTree/templates/InvenTree/settings/login.html b/InvenTree/templates/InvenTree/settings/login.html index b3758aadf6..c13155a5ae 100644 --- a/InvenTree/templates/InvenTree/settings/login.html +++ b/InvenTree/templates/InvenTree/settings/login.html @@ -17,6 +17,7 @@ {% include "InvenTree/settings/setting.html" with key="LOGIN_ENABLE_REG" icon="fa-info-circle" %} {% include "InvenTree/settings/setting.html" with key="LOGIN_ENABLE_SSO" icon="fa-info-circle" %} {% include "InvenTree/settings/setting.html" with key="LOGIN_ENABLE_PWD_FORGOT" icon="fa-info-circle" %} + {% include "InvenTree/settings/setting.html" with key="LOGIN_MAIL_REQUIRED" icon="fa-info-circle" %} </tbody> </table> From 32e216599b88ac0afdfbd3b2eb214c5259954a8f Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Tue, 7 Sep 2021 00:03:55 +0200 Subject: [PATCH 47/72] PEP fix --- InvenTree/InvenTree/forms.py | 1 + 1 file changed, 1 insertion(+) diff --git a/InvenTree/InvenTree/forms.py b/InvenTree/InvenTree/forms.py index 5cdba32014..68e7f90290 100644 --- a/InvenTree/InvenTree/forms.py +++ b/InvenTree/InvenTree/forms.py @@ -18,6 +18,7 @@ from allauth.account.forms import SignupForm from part.models import PartCategory from common.models import InvenTreeSetting + class HelperForm(forms.ModelForm): """ Provides simple integration of crispy_forms extension. """ From e380f94e01e69fa0f18070718f592c40eb13b30e Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Tue, 7 Sep 2021 00:33:26 +0200 Subject: [PATCH 48/72] allauth always logout --- InvenTree/InvenTree/settings.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 5f2323d986..692a116f35 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -662,6 +662,8 @@ for app in SOCIAL_BACKENDS: INSTALLED_APPS.append(app) # settings for allauth +ACCOUNT_LOGOUT_ON_PASSWORD_CHANGE = True + ACCOUNT_FORMS = { 'login': 'allauth.account.forms.LoginForm', 'signup': 'InvenTree.forms.CustomSignupForm', From 1f03d43b927b44ee7f73d7216e3b35fa1117dcd1 Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Tue, 7 Sep 2021 01:19:44 +0200 Subject: [PATCH 49/72] settings for auto-fill on sso --- InvenTree/InvenTree/forms.py | 13 ++++++++++++- InvenTree/InvenTree/settings.py | 4 ++++ InvenTree/common/models.py | 6 ++++++ InvenTree/templates/InvenTree/settings/login.html | 5 +++++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/forms.py b/InvenTree/InvenTree/forms.py index 68e7f90290..dedb078623 100644 --- a/InvenTree/InvenTree/forms.py +++ b/InvenTree/InvenTree/forms.py @@ -14,6 +14,7 @@ from crispy_forms.layout import Layout, Field from crispy_forms.bootstrap import PrependedText, AppendedText, PrependedAppendedText, StrictButton, Div from allauth.account.forms import SignupForm +from allauth.socialaccount.adapter import DefaultSocialAccountAdapter from part.models import PartCategory from common.models import InvenTreeSetting @@ -208,7 +209,7 @@ class SettingCategorySelectForm(forms.ModelForm): ) -# override allauth forms +# override allauth class CustomSignupForm(SignupForm): """ Override to use dynamic settings @@ -216,3 +217,13 @@ class CustomSignupForm(SignupForm): def __init__(self, *args, **kwargs): kwargs['email_required'] = InvenTreeSetting.get_setting('LOGIN_MAIL_REQUIRED') super().__init__(*args, **kwargs) + + +class CustomSocialAccountAdapter(DefaultSocialAccountAdapter): + """ + Override of adapter to use dynamic settings + """ + def is_auto_signup_allowed(self, request, sociallogin): + if InvenTreeSetting.get_setting('LOGIN_SIGNUP_SSO_AUTO', True): + return super().is_auto_signup_allowed(request, sociallogin) + return False diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 692a116f35..c04b550e34 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -664,6 +664,8 @@ for app in SOCIAL_BACKENDS: # settings for allauth ACCOUNT_LOGOUT_ON_PASSWORD_CHANGE = True + +# override forms / adapters ACCOUNT_FORMS = { 'login': 'allauth.account.forms.LoginForm', 'signup': 'InvenTree.forms.CustomSignupForm', @@ -674,3 +676,5 @@ ACCOUNT_FORMS = { 'reset_password_from_key': 'allauth.account.forms.ResetPasswordKeyForm', 'disconnect': 'allauth.socialaccount.forms.DisconnectForm', } + +SOCIALACCOUNT_ADAPTER = 'InvenTree.forms.CustomSocialAccountAdapter' diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index 25f360c0a0..a209c5d5ed 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -852,6 +852,12 @@ class InvenTreeSetting(BaseInvenTreeSetting): 'default': False, 'validator': bool, }, + 'LOGIN_SIGNUP_SSO_AUTO': { + 'name': _('Auto-fill SSO users'), + 'description': _('Automatically fill out user-details from SSO account-data'), + 'default': True, + 'validator': bool, + }, } class Meta: diff --git a/InvenTree/templates/InvenTree/settings/login.html b/InvenTree/templates/InvenTree/settings/login.html index c13155a5ae..471903554b 100644 --- a/InvenTree/templates/InvenTree/settings/login.html +++ b/InvenTree/templates/InvenTree/settings/login.html @@ -18,6 +18,11 @@ {% include "InvenTree/settings/setting.html" with key="LOGIN_ENABLE_SSO" icon="fa-info-circle" %} {% include "InvenTree/settings/setting.html" with key="LOGIN_ENABLE_PWD_FORGOT" icon="fa-info-circle" %} {% include "InvenTree/settings/setting.html" with key="LOGIN_MAIL_REQUIRED" icon="fa-info-circle" %} + <tr> + <td>{% trans 'Signup' %}</td> + <td colspan='4'></td> + </tr> + {% include "InvenTree/settings/setting.html" with key="LOGIN_SIGNUP_SSO_AUTO" icon="fa-info-circle" %} </tbody> </table> From af286766ea112c5d3510dd817ce082f1e6e42440 Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Tue, 7 Sep 2021 07:17:00 +0200 Subject: [PATCH 50/72] setting for two mail fields on signup --- InvenTree/InvenTree/forms.py | 30 ++++++++++++++++++- InvenTree/common/models.py | 6 ++++ .../templates/InvenTree/settings/login.html | 1 + 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/forms.py b/InvenTree/InvenTree/forms.py index dedb078623..beec4cc758 100644 --- a/InvenTree/InvenTree/forms.py +++ b/InvenTree/InvenTree/forms.py @@ -13,7 +13,7 @@ from crispy_forms.helper import FormHelper from crispy_forms.layout import Layout, Field from crispy_forms.bootstrap import PrependedText, AppendedText, PrependedAppendedText, StrictButton, Div -from allauth.account.forms import SignupForm +from allauth.account.forms import SignupForm, set_form_field_order from allauth.socialaccount.adapter import DefaultSocialAccountAdapter from part.models import PartCategory @@ -216,8 +216,36 @@ class CustomSignupForm(SignupForm): """ def __init__(self, *args, **kwargs): kwargs['email_required'] = InvenTreeSetting.get_setting('LOGIN_MAIL_REQUIRED') + super().__init__(*args, **kwargs) + # check for two mail fields + if InvenTreeSetting.get_setting('LOGIN_SIGNUP_MAIL_TWICE'): + self.fields["email2"] = forms.EmailField( + label=_("E-mail (again)"), + widget=forms.TextInput( + attrs={ + "type": "email", + "placeholder": _("E-mail address confirmation"), + } + ), + ) + + # reorder fields + set_form_field_order(self, ["username", "email", "email2", "password1", "password2", ]) + + def clean(self): + cleaned_data = super().clean() + + # check for two mail fields + if InvenTreeSetting.get_setting('LOGIN_SIGNUP_MAIL_TWICE'): + email = cleaned_data.get("email") + email2 = cleaned_data.get("email2") + if (email and email2) and email != email2: + self.add_error("email2", _("You must type the same email each time.")) + + return cleaned_data + class CustomSocialAccountAdapter(DefaultSocialAccountAdapter): """ diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index a209c5d5ed..21e2d37c7f 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -858,6 +858,12 @@ class InvenTreeSetting(BaseInvenTreeSetting): 'default': True, 'validator': bool, }, + 'LOGIN_SIGNUP_MAIL_TWICE': { + 'name': _('Mail twice'), + 'description': _('On signup ask users twice for their mail'), + 'default': False, + 'validator': bool, + }, } class Meta: diff --git a/InvenTree/templates/InvenTree/settings/login.html b/InvenTree/templates/InvenTree/settings/login.html index 471903554b..bcc2f16767 100644 --- a/InvenTree/templates/InvenTree/settings/login.html +++ b/InvenTree/templates/InvenTree/settings/login.html @@ -22,6 +22,7 @@ <td>{% trans 'Signup' %}</td> <td colspan='4'></td> </tr> + {% include "InvenTree/settings/setting.html" with key="LOGIN_SIGNUP_MAIL_TWICE" icon="fa-info-circle" %} {% include "InvenTree/settings/setting.html" with key="LOGIN_SIGNUP_SSO_AUTO" icon="fa-info-circle" %} </tbody> </table> From 53de59f84b7a9727ffa1b4c8edfaa2cd49368fc5 Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Tue, 7 Sep 2021 07:37:28 +0200 Subject: [PATCH 51/72] setting for two password fields on signup --- InvenTree/InvenTree/forms.py | 8 ++++++-- InvenTree/InvenTree/settings.py | 1 - InvenTree/common/models.py | 6 ++++++ InvenTree/templates/InvenTree/settings/login.html | 1 + 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/InvenTree/InvenTree/forms.py b/InvenTree/InvenTree/forms.py index beec4cc758..b6905fd85a 100644 --- a/InvenTree/InvenTree/forms.py +++ b/InvenTree/InvenTree/forms.py @@ -231,8 +231,12 @@ class CustomSignupForm(SignupForm): ), ) - # reorder fields - set_form_field_order(self, ["username", "email", "email2", "password1", "password2", ]) + # check for two password fields + if not InvenTreeSetting.get_setting('LOGIN_SIGNUP_PWD_TWICE'): + self.fields.pop("password2") + + # reorder fields + set_form_field_order(self, ["username", "email", "email2", "password1", "password2", ]) def clean(self): cleaned_data = super().clean() diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index c04b550e34..20f1bfbf19 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -664,7 +664,6 @@ for app in SOCIAL_BACKENDS: # settings for allauth ACCOUNT_LOGOUT_ON_PASSWORD_CHANGE = True - # override forms / adapters ACCOUNT_FORMS = { 'login': 'allauth.account.forms.LoginForm', diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index 21e2d37c7f..8e36624ad7 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -864,6 +864,12 @@ class InvenTreeSetting(BaseInvenTreeSetting): 'default': False, 'validator': bool, }, + 'LOGIN_SIGNUP_PWD_TWICE': { + 'name': _('Password twice'), + 'description': _('On signup ask users twice for their password'), + 'default': True, + 'validator': bool, + }, } class Meta: diff --git a/InvenTree/templates/InvenTree/settings/login.html b/InvenTree/templates/InvenTree/settings/login.html index bcc2f16767..289f87a3c9 100644 --- a/InvenTree/templates/InvenTree/settings/login.html +++ b/InvenTree/templates/InvenTree/settings/login.html @@ -23,6 +23,7 @@ <td colspan='4'></td> </tr> {% 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" %} </tbody> </table> From dbc58b299c50da9722e592d920de8224e8fda2bf Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Tue, 7 Sep 2021 22:35:19 +0200 Subject: [PATCH 52/72] fix layout for password reset --- InvenTree/InvenTree/urls.py | 3 ++- InvenTree/InvenTree/views.py | 9 +++++++- .../account/password_reset_from_key.html | 23 +++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 InvenTree/templates/account/password_reset_from_key.html diff --git a/InvenTree/InvenTree/urls.py b/InvenTree/InvenTree/urls.py index fa6261ed80..1abb7b50ac 100644 --- a/InvenTree/InvenTree/urls.py +++ b/InvenTree/InvenTree/urls.py @@ -37,7 +37,7 @@ from rest_framework.documentation import include_docs_urls from .views import auth_request from .views import IndexView, SearchView, DatabaseStatsView -from .views import SettingsView, EditUserView, SetPasswordView, CustomEmailView, CustomConnectionsView, CustomPasswordChangeView +from .views import SettingsView, EditUserView, SetPasswordView, CustomEmailView, CustomConnectionsView, CustomPasswordChangeView, CustomPasswordResetFromKeyView from .views import CurrencyRefreshView from .views import AppearanceSelectView, SettingCategorySelectView from .views import DynamicJsView @@ -167,6 +167,7 @@ urlpatterns = [ url(r'^accounts/email/', CustomEmailView.as_view(), name='account_email'), url(r'^accounts/social/connections/', CustomConnectionsView.as_view(), name='socialaccount_connections'), url(r'^accounts/password/change/', CustomPasswordChangeView.as_view(), name='account_change_password'), + url(r"^accounts/password/reset/key/(?P<uidb36>[0-9A-Za-z]+)-(?P<key>.+)/$", CustomPasswordResetFromKeyView.as_view(), name="account_reset_password_from_key"), url(r'^accounts/', include('allauth.urls')), # included urlpatterns ] diff --git a/InvenTree/InvenTree/views.py b/InvenTree/InvenTree/views.py index adcf923ed2..14de376d25 100644 --- a/InvenTree/InvenTree/views.py +++ b/InvenTree/InvenTree/views.py @@ -27,7 +27,7 @@ from djmoney.contrib.exchange.models import ExchangeBackend, Rate from allauth.account.forms import AddEmailForm from allauth.socialaccount.forms import DisconnectForm from allauth.account.models import EmailAddress -from allauth.account.views import EmailView, PasswordChangeView +from allauth.account.views import EmailView, PasswordChangeView, PasswordResetFromKeyView from allauth.socialaccount.views import ConnectionsView from common.settings import currency_code_default, currency_codes @@ -857,6 +857,13 @@ class CustomPasswordChangeView(LoginRequiredMixin, PasswordChangeView): success_url = reverse_lazy("login") +class CustomPasswordResetFromKeyView(PasswordResetFromKeyView): + """ + Override of allauths PasswordResetFromKeyView to always show the settings but leave the functions allow + """ + success_url = reverse_lazy("account_login") + + class CurrencyRefreshView(RedirectView): """ POST endpoint to refresh / update exchange rates diff --git a/InvenTree/templates/account/password_reset_from_key.html b/InvenTree/templates/account/password_reset_from_key.html new file mode 100644 index 0000000000..f9fa339983 --- /dev/null +++ b/InvenTree/templates/account/password_reset_from_key.html @@ -0,0 +1,23 @@ +{% extends "account/base.html" %} + +{% load i18n crispy_forms_tags %} +{% block head_title %}{% trans "Change Password" %}{% endblock %} + +{% block content %} + <h1>{% if token_fail %}{% trans "Bad Token" %}{% else %}{% trans "Change Password" %}{% endif %}</h1> + + {% if token_fail %} + {% url 'account_reset_password' as passwd_reset_url %} + <p>{% blocktrans %}The password reset link was invalid, possibly because it has already been used. Please request a <a href="{{ passwd_reset_url }}">new password reset</a>.{% endblocktrans %}</p> + {% else %} + {% if form %} + <form method="POST" action="{{ action_url }}"> + {% csrf_token %} + {{ form|crispy }} + <input type="submit" name="action" class="btn btn-primary btn-block" value="{% trans 'change password' %}"/> + </form> + {% else %} + <p>{% trans 'Your password is now changed.' %}</p> + {% endif %} + {% endif %} +{% endblock %} From b281241607b8df5378f293e6fcb876cba8fb3267 Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Tue, 7 Sep 2021 22:38:01 +0200 Subject: [PATCH 53/72] remove unneeded override --- InvenTree/InvenTree/urls.py | 3 +-- InvenTree/InvenTree/views.py | 9 +------- .../templates/account/password_change.html | 21 ------------------- 3 files changed, 2 insertions(+), 31 deletions(-) delete mode 100644 InvenTree/templates/account/password_change.html diff --git a/InvenTree/InvenTree/urls.py b/InvenTree/InvenTree/urls.py index 1abb7b50ac..77a0e06a0c 100644 --- a/InvenTree/InvenTree/urls.py +++ b/InvenTree/InvenTree/urls.py @@ -37,7 +37,7 @@ from rest_framework.documentation import include_docs_urls from .views import auth_request from .views import IndexView, SearchView, DatabaseStatsView -from .views import SettingsView, EditUserView, SetPasswordView, CustomEmailView, CustomConnectionsView, CustomPasswordChangeView, CustomPasswordResetFromKeyView +from .views import SettingsView, EditUserView, SetPasswordView, CustomEmailView, CustomConnectionsView, CustomPasswordResetFromKeyView from .views import CurrencyRefreshView from .views import AppearanceSelectView, SettingCategorySelectView from .views import DynamicJsView @@ -166,7 +166,6 @@ urlpatterns = [ # overrides of urlpatterns url(r'^accounts/email/', CustomEmailView.as_view(), name='account_email'), url(r'^accounts/social/connections/', CustomConnectionsView.as_view(), name='socialaccount_connections'), - url(r'^accounts/password/change/', CustomPasswordChangeView.as_view(), name='account_change_password'), url(r"^accounts/password/reset/key/(?P<uidb36>[0-9A-Za-z]+)-(?P<key>.+)/$", CustomPasswordResetFromKeyView.as_view(), name="account_reset_password_from_key"), url(r'^accounts/', include('allauth.urls')), # included urlpatterns ] diff --git a/InvenTree/InvenTree/views.py b/InvenTree/InvenTree/views.py index 14de376d25..af97877933 100644 --- a/InvenTree/InvenTree/views.py +++ b/InvenTree/InvenTree/views.py @@ -27,7 +27,7 @@ from djmoney.contrib.exchange.models import ExchangeBackend, Rate from allauth.account.forms import AddEmailForm from allauth.socialaccount.forms import DisconnectForm from allauth.account.models import EmailAddress -from allauth.account.views import EmailView, PasswordChangeView, PasswordResetFromKeyView +from allauth.account.views import EmailView, PasswordResetFromKeyView from allauth.socialaccount.views import ConnectionsView from common.settings import currency_code_default, currency_codes @@ -850,13 +850,6 @@ class CustomConnectionsView(AllauthOverrides, ConnectionsView): success_url = reverse_lazy("settings") -class CustomPasswordChangeView(LoginRequiredMixin, PasswordChangeView): - """ - Override of allauths PasswordChangeView to always show the settings but leave the functions allow - """ - success_url = reverse_lazy("login") - - class CustomPasswordResetFromKeyView(PasswordResetFromKeyView): """ Override of allauths PasswordResetFromKeyView to always show the settings but leave the functions allow diff --git a/InvenTree/templates/account/password_change.html b/InvenTree/templates/account/password_change.html deleted file mode 100644 index 702094fcba..0000000000 --- a/InvenTree/templates/account/password_change.html +++ /dev/null @@ -1,21 +0,0 @@ -{% extends "account/base.html" %} - -{% load i18n crispy_forms_tags inventree_extras %} - -{% block head_title %}{% trans "Change Password" %}{% endblock %} - -{% block content %} - -{% settings_value 'LOGIN_ENABLE_PWD_FORGOT' as enable_pwd_forgot %} - - <h1>{% trans "Change Password" %}</h1> - - <form method="POST" action="{% url 'account_change_password' %}" class="password_change"> - {% csrf_token %} - {{ form|crispy }} - <button type="submit" class="btn btn-primary" name="action">{% trans "Change Password" %}</button> - {% if enable_pwd_forgot %} - <a href="{% url 'account_reset_password' %}">{% trans "Forgot Password?" %}</a> - {% endif %} - </form> -{% endblock %} From 0a8401da1d034b13789cb86455ebe231b31c9e19 Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Tue, 7 Sep 2021 23:07:23 +0200 Subject: [PATCH 54/72] some settings in config --- InvenTree/InvenTree/settings.py | 4 ++++ InvenTree/config_template.yaml | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 20f1bfbf19..28893c926f 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -662,6 +662,10 @@ for app in SOCIAL_BACKENDS: INSTALLED_APPS.append(app) # settings for allauth +ACCOUNT_EMAIL_CONFIRMATION_EXPIRE_DAYS = get_setting('INVENTREE_LOGIN_CONFIRM_DAYS', CONFIG.get('login_confirm_days', 3)) + +ACCOUNT_LOGIN_ATTEMPTS_LIMIT = get_setting('INVENTREE_LOGIN_ATTEMPTS', CONFIG.get('login_attempts', 5)) + ACCOUNT_LOGOUT_ON_PASSWORD_CHANGE = True # override forms / adapters diff --git a/InvenTree/config_template.yaml b/InvenTree/config_template.yaml index 177b1eaf0a..4ee52d54ad 100644 --- a/InvenTree/config_template.yaml +++ b/InvenTree/config_template.yaml @@ -141,6 +141,14 @@ static_root: '/home/inventree/data/static' # - git # - ssh +# Login configuration +# How long do confirmation mail last? +# Use environment variable INVENTREE_LOGIN_CONFIRM_DAYS +#login_confirm_days: 3 +# How many wrong login attempts are permitted? +# Use environment variable INVENTREE_LOGIN_ATTEMPTS +#login_attempts: 5 + # Permit custom authentication backends #authentication_backends: # - 'django.contrib.auth.backends.ModelBackend' From 0eb9d3ad5bda7b3eca965927187b10e11a69c189 Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Tue, 7 Sep 2021 23:08:04 +0200 Subject: [PATCH 55/72] cleanup --- InvenTree/InvenTree/settings.py | 1 - 1 file changed, 1 deletion(-) diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 28893c926f..0b1c60a094 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -655,7 +655,6 @@ MESSAGE_TAGS = { SITE_ID = 1 - # Load the allauth social backends SOCIAL_BACKENDS = CONFIG.get('social_backends', []) for app in SOCIAL_BACKENDS: From f730702a7d46aa6727b9616de459b33610651e23 Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Tue, 7 Sep 2021 23:55:11 +0200 Subject: [PATCH 56/72] add aditional provider settings --- InvenTree/InvenTree/settings.py | 2 ++ InvenTree/config_template.yaml | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 0b1c60a094..77ba684edf 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -660,6 +660,8 @@ SOCIAL_BACKENDS = CONFIG.get('social_backends', []) for app in SOCIAL_BACKENDS: INSTALLED_APPS.append(app) +SOCIALACCOUNT_PROVIDERS = CONFIG.get('social_providers', []) + # settings for allauth ACCOUNT_EMAIL_CONFIRMATION_EXPIRE_DAYS = get_setting('INVENTREE_LOGIN_CONFIRM_DAYS', CONFIG.get('login_confirm_days', 3)) diff --git a/InvenTree/config_template.yaml b/InvenTree/config_template.yaml index 4ee52d54ad..3472a37d8e 100644 --- a/InvenTree/config_template.yaml +++ b/InvenTree/config_template.yaml @@ -168,4 +168,11 @@ static_root: '/home/inventree/data/static' # Add SSO login-backends # social_backends: -# - 'allauth.socialaccount.providers.github' +# - 'allauth.socialaccount.providers.keycloak' + +# Add specific settings +# social_providers: +# keycloak: +# KEYCLOAK_URL: 'https://keycloak.custom/auth' +# KEYCLOAK_REALM: 'master' + From 5b7941ab17dcd1132361b8b1bb641a5a3a7b7465 Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Tue, 7 Sep 2021 23:55:32 +0200 Subject: [PATCH 57/72] crispyfy confirmation --- .../templates/account/email_confirm.html | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 InvenTree/templates/account/email_confirm.html diff --git a/InvenTree/templates/account/email_confirm.html b/InvenTree/templates/account/email_confirm.html new file mode 100644 index 0000000000..12b041f710 --- /dev/null +++ b/InvenTree/templates/account/email_confirm.html @@ -0,0 +1,31 @@ +{% extends "account/base.html" %} + +{% load i18n %} +{% load account %} + +{% block head_title %}{% trans "Confirm E-mail Address" %}{% endblock %} + + +{% block content %} +<h1>{% trans "Confirm E-mail Address" %}</h1> + +{% if confirmation %} + +{% user_display confirmation.email_address.user as user_display %} + +<p>{% blocktrans with confirmation.email_address.email as email %}Please confirm that <a href="mailto:{{ email }}">{{ email }}</a> is an e-mail address for user {{ user_display }}.{% endblocktrans %}</p> + +<form method="post" action="{% url 'account_confirm_email' confirmation.key %}"> +{% csrf_token %} + <button type="submit" class="btn btn-primary btn-block">{% trans 'Confirm' %}</button> +</form> + +{% else %} + +{% url 'account_email' as email_url %} + +<p>{% blocktrans %}This e-mail confirmation link expired or is invalid. Please <a href="{{ email_url }}">issue a new e-mail confirmation request</a>.{% endblocktrans %}</p> + +{% endif %} + +{% endblock %} From 48a4715b580cc26e4ab6202d18c62071cec8440e Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Tue, 7 Sep 2021 23:59:18 +0200 Subject: [PATCH 58/72] PEP fix --- InvenTree/InvenTree/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 77ba684edf..f9b9887036 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -660,7 +660,7 @@ SOCIAL_BACKENDS = CONFIG.get('social_backends', []) for app in SOCIAL_BACKENDS: INSTALLED_APPS.append(app) -SOCIALACCOUNT_PROVIDERS = CONFIG.get('social_providers', []) +SOCIALACCOUNT_PROVIDERS = CONFIG.get('social_providers', []) # settings for allauth ACCOUNT_EMAIL_CONFIRMATION_EXPIRE_DAYS = get_setting('INVENTREE_LOGIN_CONFIRM_DAYS', CONFIG.get('login_confirm_days', 3)) From 30c98b828e2432791fe4d33219db61484deb7453 Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Tue, 7 Sep 2021 23:59:28 +0200 Subject: [PATCH 59/72] fix checks --- InvenTree/InvenTree/test_urls.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/InvenTree/InvenTree/test_urls.py b/InvenTree/InvenTree/test_urls.py index 0723332d7d..c74db43b17 100644 --- a/InvenTree/InvenTree/test_urls.py +++ b/InvenTree/InvenTree/test_urls.py @@ -111,6 +111,9 @@ class URLTest(TestCase): if url.startswith("admin:"): return + if url.startswith("account:"): + return + if pk: # We will assume that there is at least one item in the database reverse(url, kwargs={"pk": 1}) From ff98c87311867e53cd72787ca032bada79c07f09 Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Wed, 8 Sep 2021 00:16:51 +0200 Subject: [PATCH 60/72] muting unneeded check --- InvenTree/InvenTree/test_urls.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/test_urls.py b/InvenTree/InvenTree/test_urls.py index c74db43b17..229950b7be 100644 --- a/InvenTree/InvenTree/test_urls.py +++ b/InvenTree/InvenTree/test_urls.py @@ -111,7 +111,8 @@ class URLTest(TestCase): if url.startswith("admin:"): return - if url.startswith("account:"): + # TODO can this be more elegant? + if url == 'account_reset_password': return if pk: From 6ff4b78b23d8b4ce72d7934dba8d348571b479d1 Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Wed, 8 Sep 2021 00:55:37 +0200 Subject: [PATCH 61/72] maybe now this is fixd? --- InvenTree/InvenTree/test_urls.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/test_urls.py b/InvenTree/InvenTree/test_urls.py index 229950b7be..042f43b6eb 100644 --- a/InvenTree/InvenTree/test_urls.py +++ b/InvenTree/InvenTree/test_urls.py @@ -112,7 +112,7 @@ class URLTest(TestCase): return # TODO can this be more elegant? - if url == 'account_reset_password': + if url.startswith("account_"): return if pk: From c872412d04c2f266b03686ca53d9ce5d8ed84b2c Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Thu, 7 Oct 2021 00:26:37 +0200 Subject: [PATCH 62/72] rust for SSO needed --- docker/Dockerfile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index e4ebbc1b4b..f2aa590ad1 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -68,7 +68,9 @@ RUN apk add --no-cache git make bash \ # PostgreSQL support postgresql postgresql-contrib postgresql-dev libpq \ # MySQL/MariaDB support - mariadb-connector-c mariadb-dev mariadb-client + mariadb-connector-c mariadb-dev mariadb-client \ + # Required for python cryptography support + rust cargo # Install required base-level python packages COPY requirements.txt requirements.txt From a6c6b5c2488bda690b9bb0d19f6c8c27afc149c5 Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Thu, 7 Oct 2021 14:08:19 +0200 Subject: [PATCH 63/72] check if registration should be open in python --- InvenTree/InvenTree/forms.py | 19 ++++++++++++++++++- InvenTree/InvenTree/settings.py | 1 + 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/forms.py b/InvenTree/InvenTree/forms.py index b6905fd85a..809f126069 100644 --- a/InvenTree/InvenTree/forms.py +++ b/InvenTree/InvenTree/forms.py @@ -14,6 +14,7 @@ from crispy_forms.layout import Layout, Field from crispy_forms.bootstrap import PrependedText, AppendedText, PrependedAppendedText, StrictButton, Div from allauth.account.forms import SignupForm, set_form_field_order +from allauth.account.adapter import DefaultAccountAdapter from allauth.socialaccount.adapter import DefaultSocialAccountAdapter from part.models import PartCategory @@ -251,7 +252,23 @@ class CustomSignupForm(SignupForm): return cleaned_data -class CustomSocialAccountAdapter(DefaultSocialAccountAdapter): +class RegistratonMixin: + """ + Mixin to check if registration should be enabled + """ + def is_open_for_signup(self, request): + if InvenTreeSetting.get_setting('LOGIN_ENABLE_REG', True): + return super().is_open_for_signup(request) + return False + + +class CustomAccountAdapter(RegistratonMixin, DefaultAccountAdapter): + """ + Override of adapter to use dynamic settings + """ + + +class CustomSocialAccountAdapter(RegistratonMixin, DefaultSocialAccountAdapter): """ Override of adapter to use dynamic settings """ diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index f9b9887036..a07324ec84 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -682,3 +682,4 @@ ACCOUNT_FORMS = { } SOCIALACCOUNT_ADAPTER = 'InvenTree.forms.CustomSocialAccountAdapter' +ACCOUNT_ADAPTER = 'InvenTree.forms.CustomAccountAdapter' From f89dd84daff70761986b251cb6daf40da1647c1b Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Thu, 7 Oct 2021 14:24:20 +0200 Subject: [PATCH 64/72] check if mail is enabled --- InvenTree/InvenTree/forms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/forms.py b/InvenTree/InvenTree/forms.py index 809f126069..47d33e621d 100644 --- a/InvenTree/InvenTree/forms.py +++ b/InvenTree/InvenTree/forms.py @@ -257,7 +257,7 @@ class RegistratonMixin: Mixin to check if registration should be enabled """ def is_open_for_signup(self, request): - if InvenTreeSetting.get_setting('LOGIN_ENABLE_REG', True): + if InvenTreeSetting.get_setting('EMAIL_HOST', None) and InvenTreeSetting.get_setting('LOGIN_ENABLE_REG', True): return super().is_open_for_signup(request) return False From 8ef07dcf611ee94bcd2b8231d9e0ebe0c9ee3a6a Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Thu, 7 Oct 2021 15:08:08 +0200 Subject: [PATCH 65/72] custom reset form --- InvenTree/InvenTree/forms.py | 11 ++++++++++- InvenTree/InvenTree/settings.py | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/InvenTree/InvenTree/forms.py b/InvenTree/InvenTree/forms.py index 47d33e621d..9cfd542d81 100644 --- a/InvenTree/InvenTree/forms.py +++ b/InvenTree/InvenTree/forms.py @@ -13,7 +13,7 @@ from crispy_forms.helper import FormHelper from crispy_forms.layout import Layout, Field from crispy_forms.bootstrap import PrependedText, AppendedText, PrependedAppendedText, StrictButton, Div -from allauth.account.forms import SignupForm, set_form_field_order +from allauth.account.forms import SignupForm, ResetPasswordForm, set_form_field_order from allauth.account.adapter import DefaultAccountAdapter from allauth.socialaccount.adapter import DefaultSocialAccountAdapter @@ -252,6 +252,15 @@ class CustomSignupForm(SignupForm): return cleaned_data +class CustomResetPasswordForm(ResetPasswordForm): + """ + Override to use dynamic settings + """ + def save(self): + email_address = super().save() + return email_address + + class RegistratonMixin: """ Mixin to check if registration should be enabled diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index a07324ec84..ed018f1f3d 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -676,7 +676,7 @@ ACCOUNT_FORMS = { 'add_email': 'allauth.account.forms.AddEmailForm', 'change_password': 'allauth.account.forms.ChangePasswordForm', 'set_password': 'allauth.account.forms.SetPasswordForm', - 'reset_password': 'allauth.account.forms.ResetPasswordForm', + 'reset_password': 'InvenTree.forms.CustomResetPasswordForm', 'reset_password_from_key': 'allauth.account.forms.ResetPasswordKeyForm', 'disconnect': 'allauth.socialaccount.forms.DisconnectForm', } From b4d9f0ff7e3458feb2168ab4c03ea63d8a200e9e Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Thu, 7 Oct 2021 17:11:47 +0200 Subject: [PATCH 66/72] fix save arguments --- InvenTree/InvenTree/forms.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/InvenTree/InvenTree/forms.py b/InvenTree/InvenTree/forms.py index 9cfd542d81..028fba433b 100644 --- a/InvenTree/InvenTree/forms.py +++ b/InvenTree/InvenTree/forms.py @@ -256,8 +256,8 @@ class CustomResetPasswordForm(ResetPasswordForm): """ Override to use dynamic settings """ - def save(self): - email_address = super().save() + def save(self, request): + email_address = super().save(request) return email_address From c83d2446639fca93360beacb9d6b841ed684816c Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Thu, 7 Oct 2021 17:30:34 +0200 Subject: [PATCH 67/72] change of strategies --- InvenTree/InvenTree/forms.py | 9 --------- InvenTree/InvenTree/settings.py | 2 +- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/InvenTree/InvenTree/forms.py b/InvenTree/InvenTree/forms.py index 028fba433b..22dd34a9aa 100644 --- a/InvenTree/InvenTree/forms.py +++ b/InvenTree/InvenTree/forms.py @@ -252,15 +252,6 @@ class CustomSignupForm(SignupForm): return cleaned_data -class CustomResetPasswordForm(ResetPasswordForm): - """ - Override to use dynamic settings - """ - def save(self, request): - email_address = super().save(request) - return email_address - - class RegistratonMixin: """ Mixin to check if registration should be enabled diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index ed018f1f3d..a07324ec84 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -676,7 +676,7 @@ ACCOUNT_FORMS = { 'add_email': 'allauth.account.forms.AddEmailForm', 'change_password': 'allauth.account.forms.ChangePasswordForm', 'set_password': 'allauth.account.forms.SetPasswordForm', - 'reset_password': 'InvenTree.forms.CustomResetPasswordForm', + 'reset_password': 'allauth.account.forms.ResetPasswordForm', 'reset_password_from_key': 'allauth.account.forms.ResetPasswordKeyForm', 'disconnect': 'allauth.socialaccount.forms.DisconnectForm', } From c37e40d1445cde04dfd81a858611df70de74d5ec Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Thu, 7 Oct 2021 17:31:18 +0200 Subject: [PATCH 68/72] use tag for testing --- InvenTree/part/templatetags/inventree_extras.py | 6 ++++++ InvenTree/templates/account/password_reset.html | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/InvenTree/part/templatetags/inventree_extras.py b/InvenTree/part/templatetags/inventree_extras.py index ce4ead853a..dab7be6eb9 100644 --- a/InvenTree/part/templatetags/inventree_extras.py +++ b/InvenTree/part/templatetags/inventree_extras.py @@ -351,6 +351,12 @@ def object_link(url_name, pk, ref): return mark_safe('<b><a href="{}">{}</a></b>'.format(ref_url, ref)) +@register.simple_tag() +def mail_configured(): + """ Return if mail is configured """ + return bool(settings.EMAIL_HOST) + + class I18nStaticNode(StaticNode): """ custom StaticNode diff --git a/InvenTree/templates/account/password_reset.html b/InvenTree/templates/account/password_reset.html index d28110b81c..2cfb45a716 100644 --- a/InvenTree/templates/account/password_reset.html +++ b/InvenTree/templates/account/password_reset.html @@ -7,13 +7,14 @@ {% block content %} {% settings_value 'LOGIN_ENABLE_PWD_FORGOT' as enable_pwd_forgot %} +{% mail_configured as mail_conf %} <h1>{% trans "Password Reset" %}</h1> {% if user.is_authenticated %} {% include "account/snippets/already_logged_in.html" %} {% endif %} - {% if enable_pwd_forgot %} + {% if mail_conf and enable_pwd_forgot %} <p>{% trans "Forgotten your password? Enter your e-mail address below, and we'll send you an e-mail allowing you to reset it." %}</p> <form method="POST" action="{% url 'account_reset_password' %}" class="password_reset"> From 6c0c8bf1b618f55a47d86840a1dd185a0d6a06f4 Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Thu, 7 Oct 2021 17:32:58 +0200 Subject: [PATCH 69/72] PEP fix --- InvenTree/InvenTree/forms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/forms.py b/InvenTree/InvenTree/forms.py index 22dd34a9aa..47d33e621d 100644 --- a/InvenTree/InvenTree/forms.py +++ b/InvenTree/InvenTree/forms.py @@ -13,7 +13,7 @@ from crispy_forms.helper import FormHelper from crispy_forms.layout import Layout, Field from crispy_forms.bootstrap import PrependedText, AppendedText, PrependedAppendedText, StrictButton, Div -from allauth.account.forms import SignupForm, ResetPasswordForm, set_form_field_order +from allauth.account.forms import SignupForm, set_form_field_order from allauth.account.adapter import DefaultAccountAdapter from allauth.socialaccount.adapter import DefaultSocialAccountAdapter From 9a310bdd5ccc91dcab80328eb49efe3abf977b56 Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Thu, 7 Oct 2021 17:42:20 +0200 Subject: [PATCH 70/72] do not send if not configured --- InvenTree/InvenTree/forms.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/InvenTree/InvenTree/forms.py b/InvenTree/InvenTree/forms.py index 47d33e621d..8b4b87637c 100644 --- a/InvenTree/InvenTree/forms.py +++ b/InvenTree/InvenTree/forms.py @@ -266,6 +266,11 @@ class CustomAccountAdapter(RegistratonMixin, DefaultAccountAdapter): """ Override of adapter to use dynamic settings """ + def send_mail(self, template_prefix, email, context): + """only send mail if backend configured""" + if InvenTreeSetting.get_setting('EMAIL_HOST', None): + return super().send_mail(template_prefix, email, context) + return False class CustomSocialAccountAdapter(RegistratonMixin, DefaultSocialAccountAdapter): From a45b8dcf0e72f82236e9b18b82ce5e815e379633 Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Thu, 7 Oct 2021 17:50:36 +0200 Subject: [PATCH 71/72] check also here if mail is enabled --- InvenTree/templates/account/login.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/InvenTree/templates/account/login.html b/InvenTree/templates/account/login.html index 574cfa01f3..d925867eb7 100644 --- a/InvenTree/templates/account/login.html +++ b/InvenTree/templates/account/login.html @@ -9,6 +9,7 @@ {% settings_value 'LOGIN_ENABLE_REG' as enable_reg %} {% settings_value 'LOGIN_ENABLE_PWD_FORGOT' as enable_pwd_forgot %} {% settings_value 'LOGIN_ENABLE_SSO' as enable_sso %} +{% mail_configured as mail_conf %} <h1>{% trans "Sign In" %}</h1> @@ -33,7 +34,7 @@ for a account and sign in below:{% endblocktrans %}</p> <div class="btn-toolbar"> <button class="btn btn-primary col-md-8" type="submit">{% trans "Sign In" %}</button> - {% if enable_pwd_forgot %} + {% if mail_conf and enable_pwd_forgot %} <a class="btn btn-primary" href="{% url 'account_reset_password' %}">{% trans "Forgot Password?" %}</a> {% endif %} </div> From 68f5af26b6cad45473ce7cd4f961899eb0ab9eac Mon Sep 17 00:00:00 2001 From: Matthias <matthias.mair@oewf.org> Date: Thu, 7 Oct 2021 18:08:55 +0200 Subject: [PATCH 72/72] dont show mail functions if not enabled --- InvenTree/templates/InvenTree/settings/user.html | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/InvenTree/templates/InvenTree/settings/user.html b/InvenTree/templates/InvenTree/settings/user.html index 27a7fd3506..569b218b43 100644 --- a/InvenTree/templates/InvenTree/settings/user.html +++ b/InvenTree/templates/InvenTree/settings/user.html @@ -12,6 +12,8 @@ {% endblock %} {% block content %} +{% mail_configured as mail_conf %} + <div class='btn-group' style='float: right;'> <div class='btn btn-primary' type='button' id='edit-user' title='{% trans "Edit User Information" %}'> <span class='fas fa-user-cog'></span> {% trans "Edit" %} @@ -67,7 +69,7 @@ <div class="buttonHolder"> <button class="btn btn-primary secondaryAction" type="submit" name="action_primary" >{% trans 'Make Primary' %}</button> - <button class="btn btn-primary secondaryAction" type="submit" name="action_send" >{% trans 'Re-send Verification' %}</button> + <button class="btn btn-primary secondaryAction" type="submit" name="action_send" {% if not mail_conf %}disabled{% endif %}>{% trans 'Re-send Verification' %}</button> <button class="btn btn-primary primaryAction" type="submit" name="action_remove" >{% trans 'Remove' %}</button> </div>