diff --git a/app/controllers/users/confirmations_controller.rb b/app/controllers/users/confirmations_controller.rb index 78574cfd9..e4ababb7d 100644 --- a/app/controllers/users/confirmations_controller.rb +++ b/app/controllers/users/confirmations_controller.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index b6ee626d6..dd2bc5cf0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/config/locales/settings.en.yml b/config/locales/settings.en.yml index 5527736b8..b322d5e6a 100755 --- a/config/locales/settings.en.yml +++ b/config/locales/settings.en.yml @@ -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" diff --git a/config/locales/settings.es.yml b/config/locales/settings.es.yml index 1a0bf67f2..681d45c41 100644 --- a/config/locales/settings.es.yml +++ b/config/locales/settings.es.yml @@ -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" diff --git a/db/seeds.rb b/db/seeds.rb index 8e65d2925..6870497d2 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -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: '') diff --git a/lib/tasks/users.rake b/lib/tasks/users.rake index 0e51d1c19..7f82979f0 100644 --- a/lib/tasks/users.rake +++ b/lib/tasks/users.rake @@ -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 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 9b045a7ed..9b654ddb2 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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