Officials level 1 is auto-assigned on email confirmation. Settings option is now optional. Also working on subdomains
This commit is contained in:
@@ -48,7 +48,7 @@ class User < ActiveRecord::Base
|
||||
|
||||
before_validation :clean_document_number
|
||||
|
||||
before_create :check_email_domain
|
||||
before_save :check_if_confirmation
|
||||
|
||||
def self.find_for_oauth(auth, signed_in_resource = nil)
|
||||
# Get the identity and user if they exist
|
||||
@@ -204,15 +204,25 @@ class User < ActiveRecord::Base
|
||||
|
||||
def has_officials_email_domain?
|
||||
domain = Setting.value_for 'email_domain_for_officials'
|
||||
email.end_with? "@#{domain}"
|
||||
return false if !email or !domain or domain.length == 0
|
||||
(email.end_with? "@#{domain}") or (email.end_with? ".#{domain}")
|
||||
end
|
||||
|
||||
def check_email_domain
|
||||
if !official? and has_officials_email_domain?
|
||||
# Check if the user is confirmed and has an official email address
|
||||
# In that case, we assign a level 1 official level
|
||||
def check_if_officials_email_domain
|
||||
if confirmed_at and !official? and has_officials_email_domain?
|
||||
self.official_level = 1
|
||||
self.official_position = Setting.value_for 'official_level_1_name'
|
||||
end
|
||||
end
|
||||
|
||||
def check_if_confirmation
|
||||
# If we are confirming the mail address, we check if the user is an official
|
||||
if confirmed_at and confirmed_at_changed?
|
||||
check_if_officials_email_domain
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def clean_document_number
|
||||
|
||||
@@ -28,4 +28,5 @@ Setting.create(key: 'proposal_code_prefix', value: 'MAD')
|
||||
Setting.create(key: 'votes_for_proposal_success', value: '53726')
|
||||
|
||||
# Users with this email domain will automatically be marked as level 1 officials
|
||||
Setting.create(key: 'email_domain_for_officials', value: 'madrid.es')
|
||||
# Emails under the domain's subdomains will also be included
|
||||
Setting.create(key: 'email_domain_for_officials', value: '')
|
||||
|
||||
@@ -6,10 +6,18 @@ namespace :users do
|
||||
end
|
||||
|
||||
desc "Assigns official level to users with the officials' email domain"
|
||||
task check_email_domains: :environment do
|
||||
User.find_each do |user|
|
||||
user.check_email_domain
|
||||
user.save
|
||||
task check_if_officials_email_domains: :environment do
|
||||
domain = Setting.value_for 'email_domain_for_officials'
|
||||
|
||||
# We end the task if there is no email domain configured
|
||||
if domain.length > 0
|
||||
# We filter the mail addresses with SQL to speed up the process
|
||||
# The real check will be done by check_if_officials_email_domains, however.
|
||||
User.where('official_level = 0 and email like ?', "%#{domain}").find_each do |user|
|
||||
user.check_if_officials_email_domain
|
||||
puts "#{user.username} (#{user.email}) is now a level-1 official." if user.official?
|
||||
user.save
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -240,12 +240,26 @@ describe User do
|
||||
end
|
||||
end
|
||||
|
||||
describe "check_email_domain" do
|
||||
it "assigns official level to users with the officials' email domain" do
|
||||
user1 = create(:user, email: "john@madrid.es")
|
||||
user2 = create(:user, email: "john@example.org")
|
||||
describe "check_if_officials_email_domain" do
|
||||
it "assigns official level to confirmed users with the officials' email domain" do
|
||||
# We will use empleados.madrid.es as the officials' domain
|
||||
# Subdomains are also accepted
|
||||
Setting.find_by(key: 'email_domain_for_officials').update(value: 'officials.madrid.es')
|
||||
|
||||
user1 = create(:user, email: "john@officials.madrid.es", confirmed_at: Time.now)
|
||||
user2 = create(:user, email: "john@yes.officials.madrid.es", confirmed_at: Time.now)
|
||||
user3 = create(:user, email: "john@unofficials.madrid.es", confirmed_at: Time.now)
|
||||
user4 = create(:user, email: "john@example.org", confirmed_at: Time.now)
|
||||
|
||||
expect(user1.official_level).to eq(1)
|
||||
expect(user2.official?).to_not eq(true)
|
||||
expect(user2.official_level).to eq(1)
|
||||
expect(user3.official?).to_not eq(true)
|
||||
expect(user4.official?).to_not eq(true)
|
||||
|
||||
[user1, user2, user3, user4].each { |user| user.destroy }
|
||||
|
||||
# We reset the officials' domain setting
|
||||
Setting.find_by(key: 'email_domain_for_officials').update(value: '')
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user