Replaces the OMNIAUTH_REGEX logic with a boolean

This commit is contained in:
kikito
2016-01-18 17:30:21 +01:00
parent 981e82fb4d
commit f74028e3b5
2 changed files with 14 additions and 16 deletions

View File

@@ -13,10 +13,10 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
end end
def after_sign_in_path_for(resource) def after_sign_in_path_for(resource)
if resource.email_provided? if resource.pending_finish_signup?
super(resource)
else
finish_signup_path finish_signup_path
else
super(resource)
end end
end end

View File

@@ -1,6 +1,4 @@
class User < ActiveRecord::Base class User < ActiveRecord::Base
OMNIAUTH_EMAIL_PREFIX = 'omniauth@participacion'
OMNIAUTH_EMAIL_REGEX = /\A#{OMNIAUTH_EMAIL_PREFIX}/
include Verification include Verification
@@ -31,7 +29,6 @@ class User < ActiveRecord::Base
validate :validate_username_length validate :validate_username_length
validates :official_level, inclusion: {in: 0..5} validates :official_level, inclusion: {in: 0..5}
validates_format_of :email, without: OMNIAUTH_EMAIL_REGEX, on: :update
validates :terms_of_service, acceptance: { allow_nil: false }, on: :create validates :terms_of_service, acceptance: { allow_nil: false }, on: :create
validates :locale, inclusion: {in: I18n.available_locales.map(&:to_s), validates :locale, inclusion: {in: I18n.available_locales.map(&:to_s),
@@ -42,6 +39,7 @@ class User < ActiveRecord::Base
accepts_nested_attributes_for :organization, update_only: true accepts_nested_attributes_for :organization, update_only: true
attr_accessor :skip_password_validation attr_accessor :skip_password_validation
attr_accessor :registering_with_oauth
scope :administrators, -> { joins(:administrators) } scope :administrators, -> { joins(:administrators) }
scope :moderators, -> { joins(:moderator) } scope :moderators, -> { joins(:moderator) }
@@ -58,14 +56,15 @@ class User < ActiveRecord::Base
email, user = nil, nil email, user = nil, nil
if auth.info.verified || auth.info.verified_email if auth.info.verified || auth.info.verified_email
email = auth.info.email email = auth.info.email
user = User.where(email: email).first if email user = User.where(email: email).first
end end
user || User.new( user || User.new(
registering_with_oauth: true,
username: auth.info.nickname || auth.extra.raw_info.name.parameterize('-') || auth.uid, username: auth.info.nickname || auth.extra.raw_info.name.parameterize('-') || auth.uid,
email: email || "#{OMNIAUTH_EMAIL_PREFIX}-#{auth.uid}-#{auth.provider}.com", email: email,
password: Devise.friendly_token[0,20], password: Devise.friendly_token[0,20],
terms_of_service: '1', terms_of_service: '1',
confirmed_at: Time.now confirmed_at: DateTime.now
) )
end end
@@ -149,11 +148,6 @@ class User < ActiveRecord::Base
erased_at.present? erased_at.present?
end end
def email_provided?
!!(email && email !~ OMNIAUTH_EMAIL_REGEX) ||
!!(unconfirmed_email && unconfirmed_email !~ OMNIAUTH_EMAIL_REGEX)
end
def locked? def locked?
Lock.find_or_create_by(user: self).locked? Lock.find_or_create_by(user: self).locked?
end end
@@ -176,11 +170,11 @@ class User < ActiveRecord::Base
end end
def username_required? def username_required?
!organization? && !erased? !organization? && !erased? && !registering_with_oauth
end end
def email_required? def email_required?
!erased? !erased? && !registering_with_oauth
end end
def has_official_email? def has_official_email?
@@ -192,6 +186,10 @@ class User < ActiveRecord::Base
self[:locale] ||= I18n.default_locale.to_s self[:locale] ||= I18n.default_locale.to_s
end end
def pending_finish_signup?
email.blank? && unconfirmed_email.blank?
end
private private
def clean_document_number def clean_document_number
self.document_number = self.document_number.gsub(/[^a-z0-9]+/i, "").upcase unless self.document_number.blank? self.document_number = self.document_number.gsub(/[^a-z0-9]+/i, "").upcase unless self.document_number.blank?