diff --git a/plugins/oauth_client/lib/ext/user.rb b/plugins/oauth_client/lib/ext/user.rb index 57cd6a7..9f44093 100644 --- a/plugins/oauth_client/lib/ext/user.rb +++ b/plugins/oauth_client/lib/ext/user.rb @@ -6,19 +6,32 @@ class User has_many :oauth_providers, through: :oauth_auths, source: :provider after_create :activate_oauth_user + after_create :store_oauth_providers + + def initialize_with_oauth_client(attributes = {}, options = {}) + @oauth_providers = attributes.delete(:oauth_providers) || [] + initialize_without_oauth_client(attributes, options) + end + alias_method_chain :initialize, :oauth_client + + def store_oauth_providers + @oauth_providers.each do |provider| + self.person.oauth_auths.create!(profile: self.person, provider: provider, enabled: true) + end + end def activate_oauth_user - self.activate if oauth_providers.present? + self.activate if oauth_providers.present? || @oauth_providers.present? end def password_required_with_oauth? - password_required_without_oauth? && oauth_providers.empty? + password_required_without_oauth? && oauth_providers.empty? && @oauth_providers.blank? end alias_method_chain :password_required?, :oauth def make_activation_code_with_oauth - oauth_providers.blank? ? make_activation_code_without_oauth : nil + @oauth_providers.blank? && oauth_providers.blank? ? make_activation_code_without_oauth : nil end alias_method_chain :make_activation_code, :oauth diff --git a/plugins/oauth_client/test/unit/user_test.rb b/plugins/oauth_client/test/unit/user_test.rb index 736064b..c201786 100644 --- a/plugins/oauth_client/test/unit/user_test.rb +++ b/plugins/oauth_client/test/unit/user_test.rb @@ -11,6 +11,11 @@ class UserTest < ActiveSupport::TestCase User.create!(:email => 'testoauth@example.com', :login => 'testoauth', :oauth_providers => [provider]) end + should 'associate the oauth provider with the created user' do + user = User.create!(:email => 'testoauth@example.com', :login => 'testoauth', :oauth_providers => [provider]) + assert_equal user.oauth_providers.reload, [provider] + end + should 'password is required if there is a oauth provider' do user = User.new(:email => 'testoauth@example.com', :login => 'testoauth') user.save -- libgit2 0.21.2