about summary refs log tree commit diff
path: root/app/validators/unreserved_username_validator.rb
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2017-06-09 19:46:01 +0200
committerGitHub <noreply@github.com>2017-06-09 19:46:01 +0200
commitcdff1da901c5e649f75f9fe89e5cf17b591f049e (patch)
tree50472779a91e2adef53fbe006e6224f51a1baf53 /app/validators/unreserved_username_validator.rb
parent1a065fb146752367ed9f9b75973ed9331879e437 (diff)
Correct validators so that existing error messages would look correct (#3668)
Diffstat (limited to 'app/validators/unreserved_username_validator.rb')
-rw-r--r--app/validators/unreserved_username_validator.rb15
1 files changed, 15 insertions, 0 deletions
diff --git a/app/validators/unreserved_username_validator.rb b/app/validators/unreserved_username_validator.rb
new file mode 100644
index 000000000..44ea4359b
--- /dev/null
+++ b/app/validators/unreserved_username_validator.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+class UnreservedUsernameValidator < ActiveModel::Validator
+  def validate(account)
+    return if account.username.nil?
+    account.errors.add(:username, I18n.t('accounts.reserved_username')) if reserved_username?(account.username)
+  end
+
+  private
+
+  def reserved_username?(value)
+    return false unless Setting.reserved_usernames
+    Setting.reserved_usernames.include?(value.downcase)
+  end
+end