国产chinesehdxxxx野外,国产av无码专区亚洲av琪琪,播放男人添女人下边视频,成人国产精品一区二区免费看,chinese丰满人妻videos

Phoenix username 禁止使用 admin 等

2023-12-18 14:21 更新

為避免用戶(hù)混淆,網(wǎng)站通常都會(huì)保留一系列用戶(hù)名,不開(kāi)放給普通用戶(hù)使用,比如 admin、administrator,TvRecipe 項(xiàng)目里,我們將保留這兩個(gè)用戶(hù)名,禁止用戶(hù)注冊(cè),如果有人嘗試使用它們注冊(cè),則提示系統(tǒng)保留,無(wú)法注冊(cè),請(qǐng)更換

我們從測(cè)試寫(xiě)起:

diff --git a/test/tv_recipe/users_test.exs b/test/tv_recipe/users_test.exs
index 26a7735..f70d4a1 100644
--- a/test/tv_recipe/users_test.exs
+++ b/test/tv_recipe/users_test.exs
@@ -65,4 +65,9 @@ defmodule TvRecipe.UserTest do
     changeset = User.changeset(%User{}, attrs)
     assert %{username: ["用戶(hù)名最長(zhǎng) 15 位"]} = errors_on(changeset)
   end
+
+  test "username should not be admin or administrator" do
+    assert %{username: ["系統(tǒng)保留,無(wú)法注冊(cè),請(qǐng)更換"]} = errors_on(%User{}, %{@valid_attrs | username: "admin"})
+    assert %{username: ["系統(tǒng)保留,無(wú)法注冊(cè),請(qǐng)更換"]} = errors_on(%User{}, %{@valid_attrs | username: "administrator"})
+  end
 end

然后是添加規(guī)則,照例還是在 user.ex 文件中:

diff --git a/lib/tv_recipe/users/user.ex b/lib/tv_recipe/users/user.ex
index 8c68e6d..35e4d0b 100644
--- a/lib/tv_recipe/users/user.ex
+++ b/lib/tv_recipe/users/user.ex
@@ -19,6 +19,7 @@ defmodule TvRecipe.User do
     |> validate_format(:username, ~r/^[a-zA-Z0-9_]+$/, message: "用戶(hù)名只允許使用英文字母、數(shù)字及下劃線")
     |> validate_length(:username, min: 3, message: "用戶(hù)名最短 3 位")
     |> validate_length(:username, max: 15, message: "用戶(hù)名最長(zhǎng) 15 位")
+    |> validate_exclusion(:username, ~w(admin administrator), message: "系統(tǒng)保留,無(wú)法注冊(cè),請(qǐng)更換")
     |> unique_constraint(:username, name: :users_lower_username_index, message: "用戶(hù)名已被人占用")
     |> unique_constraint(:email)
   end

這里,我們用 validate_exclusion 來(lái)排除 ~w(admin administrator) 數(shù)組中的兩個(gè)用戶(hù)名。

再運(yùn)行測(cè)試,悉數(shù)通過(guò)。

這樣,我們就完成了所有 username 有關(guān)的規(guī)則.

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)