Merge pull request #740 from xJakub/master
Auto-assign official level to users with official emails
This commit is contained in:
@@ -10,6 +10,7 @@ class Users::ConfirmationsController < Devise::ConfirmationsController
|
||||
|
||||
if resource.valid? # password is set correctly
|
||||
resource.save
|
||||
set_official_position if resource.has_official_email?
|
||||
resource.confirm
|
||||
set_flash_message(:notice, :confirmed) if is_flashing_format?
|
||||
sign_in_and_redirect(resource_name, resource)
|
||||
@@ -34,6 +35,7 @@ class Users::ConfirmationsController < Devise::ConfirmationsController
|
||||
if resource.encrypted_password.blank?
|
||||
respond_with_navigational(resource){ render :show }
|
||||
elsif resource.errors.empty?
|
||||
set_official_position if resource.has_official_email?
|
||||
resource.confirm # Last change: confirm happens here for people with passwords instead of af the top of the show action
|
||||
set_flash_message(:notice, :confirmed) if is_flashing_format?
|
||||
respond_with_navigational(resource){ redirect_to after_confirmation_path_for(resource_name, resource) }
|
||||
@@ -48,4 +50,10 @@ class Users::ConfirmationsController < Devise::ConfirmationsController
|
||||
params.require(resource_name).permit(:password, :password_confirmation, :email)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_official_position
|
||||
resource.add_official_position! (Setting.value_for 'official_level_1_name'), 1
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -199,6 +199,11 @@ class User < ActiveRecord::Base
|
||||
def email_required?
|
||||
!erased?
|
||||
end
|
||||
|
||||
def has_official_email?
|
||||
domain = Setting.value_for 'email_domain_for_officials'
|
||||
!email.blank? && ( (email.end_with? "@#{domain}") || (email.end_with? ".#{domain}") )
|
||||
end
|
||||
|
||||
private
|
||||
def clean_document_number
|
||||
|
||||
@@ -10,3 +10,4 @@ en:
|
||||
max_votes_for_debate_edit: "Number of votes from which a Debate can no longer be edited"
|
||||
proposal_code_prefix: "Prefix for Proposal codes"
|
||||
votes_for_proposal_success: "Number of votes necessary for approval of a Proposal"
|
||||
email_domain_for_officials: "Email domain for public officials"
|
||||
|
||||
@@ -10,3 +10,4 @@ es:
|
||||
max_votes_for_debate_edit: "Número de votos en que un Debate deja de poderse editar"
|
||||
proposal_code_prefix: "Prefijo para los códigos de Propuestas"
|
||||
votes_for_proposal_success: "Número de votos necesarios para aprobar una Propuesta"
|
||||
email_domain_for_officials: "Dominio de email para cargos públicos"
|
||||
|
||||
@@ -26,3 +26,7 @@ Setting.create(key: 'proposal_code_prefix', value: 'MAD')
|
||||
|
||||
# Number of votes needed for proposal success
|
||||
Setting.create(key: 'votes_for_proposal_success', value: '53726')
|
||||
|
||||
# Users with this email domain will automatically be marked as level 1 officials
|
||||
# Emails under the domain's subdomains will also be included
|
||||
Setting.create(key: 'email_domain_for_officials', value: '')
|
||||
|
||||
@@ -4,5 +4,22 @@ namespace :users do
|
||||
task count_failed_census_calls: :environment do
|
||||
User.find_each{ |user| User.reset_counters(user.id, :failed_census_calls)}
|
||||
end
|
||||
|
||||
desc "Assigns official level to users with the officials' email domain"
|
||||
task check_for_official_emails: :environment do
|
||||
domain = Setting.value_for 'email_domain_for_officials'
|
||||
|
||||
# We end the task if there is no email domain configured
|
||||
if !domain.blank?
|
||||
# We filter the mail addresses with SQL to speed up the process
|
||||
# The real check will be done by check_if_official_email, however.
|
||||
User.where('official_level = 0 and email like ?', "%#{domain}").find_each do |user|
|
||||
if user.has_official_email?
|
||||
user.add_official_position! (Setting.value_for 'official_level_1_name'), 1
|
||||
puts "#{user.username} (#{user.email}) is now a level-1 official."
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -239,6 +239,27 @@ describe User do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "has_official_email" do
|
||||
it "checks if the mail address has the officials 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.has_official_email?).to eq(true)
|
||||
expect(user2.has_official_email?).to eq(true)
|
||||
expect(user3.has_official_email?).to eq(false)
|
||||
expect(user4.has_official_email?).to eq(false)
|
||||
|
||||
# We reset the officials' domain setting
|
||||
Setting.find_by(key: 'email_domain_for_officials').update(value: '')
|
||||
end
|
||||
end
|
||||
|
||||
describe "self.search" do
|
||||
it "find users by email" do
|
||||
|
||||
Reference in New Issue
Block a user