Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AO3-4817 AdminBlacklistedEmail Strong Parameters #2719

Merged
merged 1 commit into from Jan 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions app/controllers/admin/blacklisted_emails_controller.rb
Expand Up @@ -3,7 +3,7 @@ class Admin::BlacklistedEmailsController < ApplicationController
before_filter :admin_only

def index
@admin_blacklisted_email = AdminBlacklistedEmail.new
@admin_blacklisted_email = AdminBlacklistedEmail.new
if params[:query]
@admin_blacklisted_emails = AdminBlacklistedEmail.where(["email LIKE ?", '%' + params[:query] + '%'])
@admin_blacklisted_emails = @admin_blacklisted_emails.paginate(page: params[:page], per_page: ArchiveConfig.ITEMS_PER_PAGE)
Expand All @@ -15,7 +15,7 @@ def new
end

def create
@admin_blacklisted_email = AdminBlacklistedEmail.new(params[:admin_blacklisted_email])
@admin_blacklisted_email = AdminBlacklistedEmail.new(admin_blacklisted_email_params)

if @admin_blacklisted_email.save
flash[:notice] = ts("Email address #{@admin_blacklisted_email.email} added to blacklist.")
Expand All @@ -28,8 +28,16 @@ def create
def destroy
@admin_blacklisted_email = AdminBlacklistedEmail.find(params[:id])
@admin_blacklisted_email.destroy

flash[:notice] = ts("Email address #{@admin_blacklisted_email.email} removed from blacklist.")
redirect_to admin_blacklisted_emails_url
end

private

def admin_blacklisted_email_params
params.require(:admin_blacklisted_email).permit(
:email
)
end
end
12 changes: 7 additions & 5 deletions app/models/admin_blacklisted_email.rb
@@ -1,14 +1,16 @@
class AdminBlacklistedEmail < ActiveRecord::Base
include ActiveModel::ForbiddenAttributesProtection

attr_accessible :email

before_validation :canonicalize_email

validates :email, presence: true, uniqueness: true, email_veracity: true

def canonicalize_email
self.email = AdminBlacklistedEmail.canonical_email(self.email) if self.email
end

# Produces a canonical version of a given email reduced to its simplest form
# This is what we store in the db, so that if we subsequently check a submitted email,
# we only need to clean the submitted email the same way and look for it.
Expand All @@ -24,13 +26,13 @@ def self.canonical_email(email_to_clean)

# strip out anything after a +
canonical_email.sub!(/(\+.*)(@.*$)/, '\2')

return canonical_email
end

# Check if an email is
def self.is_blacklisted?(email_to_check)
AdminBlacklistedEmail.where(email: AdminBlacklistedEmail.canonical_email(email_to_check)).exists?
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty line detected at class body end.

end