From 6f44a0ee84a8dca7a94a38b45493a444390f13ec Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 27 Jul 2020 15:13:34 -0500 Subject: [PATCH] Add configurable registration_reason limit --- config/config.exs | 1 + lib/pleroma/user.ex | 2 ++ ...0712234852_add_approval_fields_to_users.exs | 2 +- test/user_test.exs | 18 +++++++++++++++++- 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/config/config.exs b/config/config.exs index d8bf921bb..1dc196a6b 100644 --- a/config/config.exs +++ b/config/config.exs @@ -238,6 +238,7 @@ config :pleroma, :instance, max_remote_account_fields: 20, account_field_name_length: 512, account_field_value_length: 2048, + registration_reason_length: 500, external_user_synchronization: true, extended_nickname_format: true, cleanup_attachments: false, diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index a78123fe4..913b6afd1 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -641,6 +641,7 @@ defmodule Pleroma.User do def register_changeset(struct, params \\ %{}, opts \\ []) do bio_limit = Config.get([:instance, :user_bio_length], 5000) name_limit = Config.get([:instance, :user_name_length], 100) + reason_limit = Config.get([:instance, :registration_reason_length], 500) params = Map.put_new(params, :accepts_chat_messages, true) need_confirmation? = @@ -681,6 +682,7 @@ defmodule Pleroma.User do |> validate_format(:email, @email_regex) |> validate_length(:bio, max: bio_limit) |> validate_length(:name, min: 1, max: name_limit) + |> validate_length(:registration_reason, max: reason_limit) |> maybe_validate_required_email(opts[:external]) |> put_password_hash |> put_ap_id() diff --git a/priv/repo/migrations/20200712234852_add_approval_fields_to_users.exs b/priv/repo/migrations/20200712234852_add_approval_fields_to_users.exs index 559640f01..43f741a5b 100644 --- a/priv/repo/migrations/20200712234852_add_approval_fields_to_users.exs +++ b/priv/repo/migrations/20200712234852_add_approval_fields_to_users.exs @@ -4,7 +4,7 @@ defmodule Pleroma.Repo.Migrations.AddApprovalFieldsToUsers do def change do alter table(:users) do add(:approval_pending, :boolean) - add(:registration_reason, :string) + add(:registration_reason, :text) end end end diff --git a/test/user_test.exs b/test/user_test.exs index 5da86bcec..5bf677666 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -550,7 +550,8 @@ defmodule Pleroma.UserTest do nickname: "nick", password: "test", password_confirmation: "test", - email: "email@example.com" + email: "email@example.com", + registration_reason: "I'm a cool guy :)" } setup do: clear_config([:instance, :account_approval_required], true) @@ -561,6 +562,21 @@ defmodule Pleroma.UserTest do {:ok, user} = Repo.insert(changeset) assert user.approval_pending + assert user.registration_reason == "I'm a cool guy :)" + end + + test "it restricts length of registration reason" do + reason_limit = Pleroma.Config.get([:instance, :registration_reason_length]) + + assert is_integer(reason_limit) + + params = + @full_user_data + |> Map.put(:registration_reason, "Quia et nesciunt dolores numquam ipsam nisi sapiente soluta. Ullam repudiandae nisi quam porro officiis officiis ad. Consequatur animi velit ex quia. Odit voluptatem perferendis quia ut nisi. Dignissimos sit soluta atque aliquid dolorem ut dolorum ut. Labore voluptates iste iusto amet voluptatum earum. Ad fugit illum nam eos ut nemo. Pariatur ea fuga non aspernatur. Dignissimos debitis officia corporis est nisi ab et. Atque itaque alias eius voluptas minus. Accusamus numquam tempore occaecati in.") + + changeset = User.register_changeset(%User{}, params) + + refute changeset.valid? end end -- 2.45.2