[Git][noosfero/noosfero][master] 3 commits: oauth_client: properly save the oauth provider when signup

Bráulio Bhavamitra gitlab at mg.gitlab.com
Mon Jan 25 18:12:02 BRST 2016


Bráulio Bhavamitra pushed to branch master at Noosfero / noosfero


Commits:
571c237b by Victor Costa at 2016-01-25T16:25:42-03:00
oauth_client: properly save the oauth provider when signup

- - - - -
09fe0be7 by Victor Costa at 2016-01-25T16:59:28-03:00
oauth_client: fix omniauth-oauth2 version

See https://github.com/intridea/omniauth-oauth2/issues/81

- - - - -
8297b72b by Bráulio Bhavamitra at 2016-01-25T20:11:36+00:00
Merge branch 'fix_oauth_client' into 'master'

Fix oauth client plugin

Fix broken tests: https://travis-ci.org/vfcosta/noosfero/jobs/104698347#L1254
Downgrade to a working version of omniauth-oauth2: https://github.com/intridea/omniauth-oauth2/issues/81

See merge request !775
- - - - -


3 changed files:

- plugins/oauth_client/Gemfile
- plugins/oauth_client/lib/ext/user.rb
- plugins/oauth_client/test/unit/user_test.rb


Changes:

=====================================
plugins/oauth_client/Gemfile
=====================================
--- a/plugins/oauth_client/Gemfile
+++ b/plugins/oauth_client/Gemfile
@@ -1,3 +1,4 @@
 gem 'omniauth', '~> 1.2.2'
 gem 'omniauth-facebook', '~> 2.0.0'
 gem "omniauth-google-oauth2", '~> 0.2.6'
+gem "omniauth-oauth2", '~> 1.3.1'


=====================================
plugins/oauth_client/lib/ext/user.rb
=====================================
--- 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


=====================================
plugins/oauth_client/test/unit/user_test.rb
=====================================
--- 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 at example.com', :login => 'testoauth', :oauth_providers => [provider])
   end
 
+  should 'associate the oauth provider with the created user' do
+    user = User.create!(:email => 'testoauth at 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 at example.com', :login => 'testoauth')
     user.save



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/d7c346f742b6383c9e84a7088131630833a03c5a...8297b72b45c20ce6c91ed64cabf68bc84c048816
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20160125/4ba139d9/attachment.html>


More information about the Noosfero-dev mailing list