From 229355f6e2ab17d65824fcfdd7a517f039ccda0f Mon Sep 17 00:00:00 2001 From: davide Date: Tue, 22 Feb 2022 15:37:44 +0000 Subject: [PATCH] Resolve #593: Allow for group registrations while public registration is closed --- .../src/baserow/api/settings/serializers.py | 3 ++- backend/src/baserow/core/handler.py | 4 +++- ...gs_allow_signups_with_group_invitations.py | 22 +++++++++++++++++++ backend/src/baserow/core/models.py | 5 +++++ backend/src/baserow/core/user/handler.py | 12 +++++++--- .../baserow/core/user/test_user_handler.py | 14 ++++++++++++ changelog.md | 2 ++ web-frontend/modules/core/locales/en.json | 2 ++ .../modules/core/pages/admin/settings.vue | 22 +++++++++++++++++++ web-frontend/modules/core/pages/signup.vue | 9 +++++++- 10 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 backend/src/baserow/core/migrations/0014_settings_allow_signups_with_group_invitations.py diff --git a/backend/src/baserow/api/settings/serializers.py b/backend/src/baserow/api/settings/serializers.py index 62eb85542d..4e2fdefd3b 100644 --- a/backend/src/baserow/api/settings/serializers.py +++ b/backend/src/baserow/api/settings/serializers.py @@ -6,9 +6,10 @@ from baserow.core.models import Settings class SettingsSerializer(serializers.ModelSerializer): class Meta: model = Settings - fields = ("allow_new_signups",) + fields = ("allow_new_signups", "allow_signups_via_group_invitations") extra_kwargs = { "allow_new_signups": {"required": False}, + "allow_signups_via_group_invitations": {"required": False}, } diff --git a/backend/src/baserow/core/handler.py b/backend/src/baserow/core/handler.py index 7bb809f7d7..3f8b41a519 100644 --- a/backend/src/baserow/core/handler.py +++ b/backend/src/baserow/core/handler.py @@ -98,7 +98,9 @@ class CoreHandler: settings_instance = self.get_settings() settings_instance = set_allowed_attrs( - kwargs, ["allow_new_signups"], settings_instance + kwargs, + ["allow_new_signups", "allow_signups_via_group_invitations"], + settings_instance, ) settings_instance.save() diff --git a/backend/src/baserow/core/migrations/0014_settings_allow_signups_with_group_invitations.py b/backend/src/baserow/core/migrations/0014_settings_allow_signups_with_group_invitations.py new file mode 100644 index 0000000000..358b4c1461 --- /dev/null +++ b/backend/src/baserow/core/migrations/0014_settings_allow_signups_with_group_invitations.py @@ -0,0 +1,22 @@ +# Generated by Django 3.2.6 on 2022-02-19 14:10 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("core", "0013_alter_application_name_squashed_0014_alter_group_name"), + ] + + operations = [ + migrations.AddField( + model_name="settings", + name="allow_signups_via_group_invitations", + field=models.BooleanField( + default=True, + help_text="Indicates whether invited users can create an account " + "when signing up, even if allow_new_signups is disabled.", + ), + ), + ] diff --git a/backend/src/baserow/core/models.py b/backend/src/baserow/core/models.py index 66c8b5980b..cf8e173867 100644 --- a/backend/src/baserow/core/models.py +++ b/backend/src/baserow/core/models.py @@ -63,6 +63,11 @@ class Settings(models.Model): help_text="Indicates whether new users can create a new account when signing " "up.", ) + allow_signups_via_group_invitations = models.BooleanField( + default=True, + help_text="Indicates whether invited users can create an account when signing " + "up, even if allow_new_signups is disabled.", + ) class UserProfile(models.Model): diff --git a/backend/src/baserow/core/user/handler.py b/backend/src/baserow/core/user/handler.py index b93af422b0..a9e9a07a43 100644 --- a/backend/src/baserow/core/user/handler.py +++ b/backend/src/baserow/core/user/handler.py @@ -103,9 +103,6 @@ class UserHandler: core_handler = CoreHandler() - if not core_handler.get_settings().allow_new_signups: - raise DisabledSignupError("Sign up is disabled.") - email = normalize_email_address(email) if User.objects.filter(Q(email=email) | Q(username=email)).exists(): @@ -125,6 +122,15 @@ class UserHandler: "user." ) + settings = core_handler.get_settings() + allow_new_signups = settings.allow_new_signups + allow_signup_for_invited_user = ( + settings.allow_signups_via_group_invitations + and group_invitation is not None + ) + if not (allow_new_signups or allow_signup_for_invited_user): + raise DisabledSignupError("Sign up is disabled.") + user = User(first_name=name, email=email, username=email) try: diff --git a/backend/tests/baserow/core/user/test_user_handler.py b/backend/tests/baserow/core/user/test_user_handler.py index 3b0f9a1f3e..e264e994e8 100644 --- a/backend/tests/baserow/core/user/test_user_handler.py +++ b/backend/tests/baserow/core/user/test_user_handler.py @@ -220,6 +220,20 @@ def test_create_user_with_invitation(data_fixture): group_invitation_token=signer.dumps(invitation.id), ) + data_fixture.update_settings( + allow_new_signups=False, allow_signups_via_group_invitations=False + ) + with pytest.raises(DisabledSignupError): + user_handler.create_user( + "Test1", + "test0@test.nl", + valid_password, + group_invitation_token=signer.dumps(invitation.id), + ) + + data_fixture.update_settings( + allow_new_signups=False, allow_signups_via_group_invitations=True + ) user = user_handler.create_user( "Test1", "test0@test.nl", diff --git a/changelog.md b/changelog.md index f70aafa408..3ff398f550 100644 --- a/changelog.md +++ b/changelog.md @@ -16,6 +16,8 @@ tools, e.g. to various REST clients. * Added search to gallery views. * Views supporting search are properly updated when a column with a matching default value is added. +* Allow for group registrations while public registration is closed +* Allow for signup via group invitation while public registration is closed. ## Released (2022-01-13 1.8.2) diff --git a/web-frontend/modules/core/locales/en.json b/web-frontend/modules/core/locales/en.json index 2df7cf7a56..f817b6745f 100644 --- a/web-frontend/modules/core/locales/en.json +++ b/web-frontend/modules/core/locales/en.json @@ -240,6 +240,8 @@ "groupSignupRestrictions": "Signup restrictions", "settingAllowNewAccountsName": "Allow creating new accounts", "settingAllowNewAccountsDescription": "By default, any user visiting your Baserow domain can sign up for a new account.", + "settingAllowSignupsViaGroupInvitationsName": "Allow signups via group invitations", + "settingAllowSignupsViaGroupInvitationDescription": "Even if the creation of new accounts is disabled, this option permits directly invited users to still create an account.", "enabled": "enabled" } } diff --git a/web-frontend/modules/core/pages/admin/settings.vue b/web-frontend/modules/core/pages/admin/settings.vue index bbd0c462da..25bdfab538 100644 --- a/web-frontend/modules/core/pages/admin/settings.vue +++ b/web-frontend/modules/core/pages/admin/settings.vue @@ -48,6 +48,28 @@ > +
+
+
+ {{ $t('settings.settingAllowSignupsViaGroupInvitationsName') }} +
+
+ {{ + $t('settings.settingAllowSignupsViaGroupInvitationDescription') + }} +
+
+
+ {{ $t('settings.enabled') }} +
+
diff --git a/web-frontend/modules/core/pages/signup.vue b/web-frontend/modules/core/pages/signup.vue index 0f97a676c7..217dfbac11 100644 --- a/web-frontend/modules/core/pages/signup.vue +++ b/web-frontend/modules/core/pages/signup.vue @@ -6,7 +6,7 @@ -