From da490a46616fa7c3f22d119d26aafd44d1b30485 Mon Sep 17 00:00:00 2001 From: Jakub Date: Mon, 30 Nov 2015 20:48:12 +0100 Subject: [PATCH] Officials level 1 is auto-assigned on email confirmation. Settings option is now optional. Also working on subdomains --- app/models/user.rb | 18 ++++++++++++++---- db/seeds.rb | 3 ++- lib/tasks/users.rake | 16 ++++++++++++---- spec/models/user_spec.rb | 24 +++++++++++++++++++----- 4 files changed, 47 insertions(+), 14 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index d936d044e..8d470f2e5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/db/seeds.rb b/db/seeds.rb index 939f484f2..6870497d2 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -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: '') diff --git a/lib/tasks/users.rake b/lib/tasks/users.rake index 278cd0481..f93d6c5be 100644 --- a/lib/tasks/users.rake +++ b/lib/tasks/users.rake @@ -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 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 6ffe3901f..82f9f9a58 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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