added forbidden user names (including case insensitivity!)

This commit is contained in:
nilsding 2015-01-09 14:12:52 +01:00
parent 081a9ee4a8
commit 6fcbee55c4
3 changed files with 17 additions and 5 deletions

View File

@ -29,11 +29,11 @@ class User < ActiveRecord::Base
SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]{1,16}\z/
WEBSITE_REGEX = /https?:\/\/([A-Za-z.\-]+)\/?(?:.*)/i
validates :screen_name, presence: true, format: { with: SCREEN_NAME_REGEX }, uniqueness: { case_sensitive: false }#,
#exclusion: { in: %w(justask_admin retrospring_admin admin justask retrospring support about public
# notifications inbox sign_in sign_up sidekiq moderation moderator mod administrator
# siteadmin site_admin),
# message: "%{value} is reserved." }
before_validation do
screen_name.strip!
end
validates :screen_name, presence: true, format: { with: SCREEN_NAME_REGEX }, uniqueness: { case_sensitive: false }, screen_name: true
validates :display_name, length: { maximum: 50 }
validates :bio, length: { maximum: 200 }

View File

@ -0,0 +1,11 @@
class ScreenNameValidator < ActiveModel::EachValidator
FORBIDDEN_SCREEN_NAMES = %w(justask_admin retrospring_admin admin justask retrospring support about public
notifications inbox sign_in sign_up sidekiq moderation moderator mod administrator
siteadmin site_admin help retro_spring retroospring retrosprlng niisding nllsding)
def validate_each(record, attribute, value)
if FORBIDDEN_SCREEN_NAMES.include? value.downcase
record.errors[attribute] << "Thou shalt not use this username! Please choose another one."
end
end
end

View File

@ -19,5 +19,6 @@ module Justask
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
config.autoload_paths += %W["#{config.root}/app/validators"]
end
end