From f74028e3b58c009bf92e4e9c51caa76d7d6c78ed Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 18 Jan 2016 17:30:21 +0100 Subject: [PATCH] Replaces the OMNIAUTH_REGEX logic with a boolean --- .../users/omniauth_callbacks_controller.rb | 6 ++--- app/models/user.rb | 24 +++++++++---------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb index 16385a5a7..eaecb7efc 100644 --- a/app/controllers/users/omniauth_callbacks_controller.rb +++ b/app/controllers/users/omniauth_callbacks_controller.rb @@ -13,10 +13,10 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController end def after_sign_in_path_for(resource) - if resource.email_provided? - super(resource) - else + if resource.pending_finish_signup? finish_signup_path + else + super(resource) end end diff --git a/app/models/user.rb b/app/models/user.rb index ffdcc1b35..035c60913 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,6 +1,4 @@ class User < ActiveRecord::Base - OMNIAUTH_EMAIL_PREFIX = 'omniauth@participacion' - OMNIAUTH_EMAIL_REGEX = /\A#{OMNIAUTH_EMAIL_PREFIX}/ include Verification @@ -31,7 +29,6 @@ class User < ActiveRecord::Base validate :validate_username_length 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 :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 attr_accessor :skip_password_validation + attr_accessor :registering_with_oauth scope :administrators, -> { joins(:administrators) } scope :moderators, -> { joins(:moderator) } @@ -58,14 +56,15 @@ class User < ActiveRecord::Base email, user = nil, nil if auth.info.verified || auth.info.verified_email email = auth.info.email - user = User.where(email: email).first if email + user = User.where(email: email).first end user || User.new( + registering_with_oauth: true, 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], terms_of_service: '1', - confirmed_at: Time.now + confirmed_at: DateTime.now ) end @@ -149,11 +148,6 @@ class User < ActiveRecord::Base erased_at.present? end - def email_provided? - !!(email && email !~ OMNIAUTH_EMAIL_REGEX) || - !!(unconfirmed_email && unconfirmed_email !~ OMNIAUTH_EMAIL_REGEX) - end - def locked? Lock.find_or_create_by(user: self).locked? end @@ -176,11 +170,11 @@ class User < ActiveRecord::Base end def username_required? - !organization? && !erased? + !organization? && !erased? && !registering_with_oauth end def email_required? - !erased? + !erased? && !registering_with_oauth end def has_official_email? @@ -192,6 +186,10 @@ class User < ActiveRecord::Base self[:locale] ||= I18n.default_locale.to_s end + def pending_finish_signup? + email.blank? && unconfirmed_email.blank? + end + private def clean_document_number self.document_number = self.document_number.gsub(/[^a-z0-9]+/i, "").upcase unless self.document_number.blank?