Move automatic official_level assignment to the controller.
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
|
||||
|
||||
@@ -47,8 +47,6 @@ class User < ActiveRecord::Base
|
||||
scope :by_document, -> (document_type, document_number) { where(document_type: document_type, document_number: document_number) }
|
||||
|
||||
before_validation :clean_document_number
|
||||
|
||||
before_save :check_if_confirmation
|
||||
|
||||
def self.find_for_oauth(auth, signed_in_resource = nil)
|
||||
# Get the identity and user if they exist
|
||||
@@ -204,24 +202,7 @@ class User < ActiveRecord::Base
|
||||
|
||||
def has_official_email?
|
||||
domain = Setting.value_for 'email_domain_for_officials'
|
||||
return false if !email or !domain or domain.length == 0
|
||||
(email.end_with? "@#{domain}") or (email.end_with? ".#{domain}")
|
||||
end
|
||||
|
||||
# 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_official_email
|
||||
if confirmed_at and !official? and has_official_email?
|
||||
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_official_email
|
||||
end
|
||||
!email.blank? && ( (email.end_with? "@#{domain}") || (email.end_with? ".#{domain}") )
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
Reference in New Issue
Block a user