From ae3411b7a26fb712a86ec1efbace353a66c395b3 Mon Sep 17 00:00:00 2001 From: Luna D Date: Fri, 17 Sep 2021 21:14:23 +0200 Subject: [PATCH] add user flag to bypass rate limits --- lib/philomena/users/user.ex | 3 ++- lib/philomena_web/plugs/limit_plug.ex | 3 +++ .../templates/admin/user/_form.html.slime | 3 +++ ...0917190346_add_bypass_rate_limits_to_users.exs | 15 +++++++++++++++ priv/repo/structure.sql | 8 +++++--- 5 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 priv/repo/migrations/20210917190346_add_bypass_rate_limits_to_users.exs diff --git a/lib/philomena/users/user.ex b/lib/philomena/users/user.ex index dc41404d..66a9ba0a 100644 --- a/lib/philomena/users/user.ex +++ b/lib/philomena/users/user.ex @@ -118,6 +118,7 @@ defmodule Philomena.Users.User do field :secondary_role, :string field :hide_default_role, :boolean, default: false field :senior_staff, :boolean, default: false + field :bypass_rate_limits, :boolean, default: false # For avatar validation/persistence field :avatar_width, :integer, virtual: true @@ -271,7 +272,7 @@ defmodule Philomena.Users.User do def update_changeset(user, attrs, roles) do user - |> cast(attrs, [:name, :email, :role, :secondary_role, :hide_default_role, :senior_staff]) + |> cast(attrs, [:name, :email, :role, :secondary_role, :hide_default_role, :senior_staff, :bypass_rate_limits]) |> validate_required([:name, :email, :role]) |> validate_inclusion(:role, ["user", "assistant", "moderator", "admin"]) |> put_assoc(:roles, roles) diff --git a/lib/philomena_web/plugs/limit_plug.ex b/lib/philomena_web/plugs/limit_plug.ex index fbb92094..3e0c37e8 100644 --- a/lib/philomena_web/plugs/limit_plug.ex +++ b/lib/philomena_web/plugs/limit_plug.ex @@ -43,6 +43,9 @@ defmodule PhilomenaWeb.LimitPlug do is_staff(conn.assigns.current_user) and skip_staff -> conn + conn.assigns.current_user.bypass_rate_limits -> + conn + conn.assigns.ajax? -> conn |> Controller.put_flash(:error, error) diff --git a/lib/philomena_web/templates/admin/user/_form.html.slime b/lib/philomena_web/templates/admin/user/_form.html.slime index 9234d6d1..bb9e4f8c 100644 --- a/lib/philomena_web/templates/admin/user/_form.html.slime +++ b/lib/philomena_web/templates/admin/user/_form.html.slime @@ -24,6 +24,9 @@ label.table-list__label .table-list__label__text Senior staff: .table-list__label__input = checkbox f, :senior_staff, class: "checkbox" + label.table-list__label + .table-list__label__text Bypass rate limits: + .table-list__label__input = checkbox f, :bypass_rate_limits, class: "checkbox" .table-list__label .table-list__label__text Avatar .table-list__label__input diff --git a/priv/repo/migrations/20210917190346_add_bypass_rate_limits_to_users.exs b/priv/repo/migrations/20210917190346_add_bypass_rate_limits_to_users.exs new file mode 100644 index 00000000..93512e5e --- /dev/null +++ b/priv/repo/migrations/20210917190346_add_bypass_rate_limits_to_users.exs @@ -0,0 +1,15 @@ +defmodule Philomena.Repo.Migrations.AddBypassRateLimitsToUsers do + use Ecto.Migration + + def up do + alter table(:users) do + add :bypass_rate_limits, :boolean, default: false + end + end + + def down do + alter table(:users) do + remove :bypass_rate_limits + end + end +end diff --git a/priv/repo/structure.sql b/priv/repo/structure.sql index 3337ab7d..a40a8cea 100644 --- a/priv/repo/structure.sql +++ b/priv/repo/structure.sql @@ -2,8 +2,8 @@ -- PostgreSQL database dump -- --- Dumped from database version 13.2 --- Dumped by pg_dump version 13.2 +-- Dumped from database version 13.4 +-- Dumped by pg_dump version 13.4 SET statement_timeout = 0; SET lock_timeout = 0; @@ -1988,7 +1988,8 @@ CREATE TABLE public.users ( last_renamed_at timestamp without time zone DEFAULT '1970-01-01 00:00:00'::timestamp without time zone NOT NULL, forced_filter_id bigint, confirmed_at timestamp(0) without time zone, - senior_staff boolean DEFAULT false + senior_staff boolean DEFAULT false, + bypass_rate_limits boolean DEFAULT false ); @@ -4844,3 +4845,4 @@ INSERT INTO public."schema_migrations" (version) VALUES (20201124224116); INSERT INTO public."schema_migrations" (version) VALUES (20210121200815); INSERT INTO public."schema_migrations" (version) VALUES (20210301012137); INSERT INTO public."schema_migrations" (version) VALUES (20210427022351); +INSERT INTO public."schema_migrations" (version) VALUES (20210917190346);