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/ SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]{1,16}\z/
WEBSITE_REGEX = /https?:\/\/([A-Za-z.\-]+)\/?(?:.*)/i WEBSITE_REGEX = /https?:\/\/([A-Za-z.\-]+)\/?(?:.*)/i
validates :screen_name, presence: true, format: { with: SCREEN_NAME_REGEX }, uniqueness: { case_sensitive: false }#, before_validation do
#exclusion: { in: %w(justask_admin retrospring_admin admin justask retrospring support about public screen_name.strip!
# notifications inbox sign_in sign_up sidekiq moderation moderator mod administrator end
# siteadmin site_admin),
# message: "%{value} is reserved." } validates :screen_name, presence: true, format: { with: SCREEN_NAME_REGEX }, uniqueness: { case_sensitive: false }, screen_name: true
validates :display_name, length: { maximum: 50 } validates :display_name, length: { maximum: 50 }
validates :bio, length: { maximum: 200 } 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. # 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.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de # config.i18n.default_locale = :de
config.autoload_paths += %W["#{config.root}/app/validators"]
end end
end end