Commit a651debcf71483e4d8042e57608bd948048f489d

Authored by Gabriel Silva
1 parent ef5d596c

Fixes ExternalUser info override

Signed-off-by: Gabriel Silva <gabriel93.silva@gmail.com>
plugins/oauth_client/controllers/public/oauth_client_plugin_public_controller.rb
... ... @@ -30,13 +30,13 @@ class OauthClientPluginPublicController &lt; PublicController
30 30 provider = OauthClientPlugin::Provider.find(session[:provider_id])
31 31  
32 32 user = User.new(email: auth_data.info.email, login: auth_data.info.name.to_slug)
33   - person_data = OpenStruct.new(
  33 + person = OauthClientPlugin::OauthExternalPerson.find_or_create_by(
34 34 identifier: auth_data.info.nickname || user.login,
35 35 name: auth_data.info.name,
36 36 created_at: Time.now,
37   - domain: provider.site || auth_data.provider,
38   - email: user.email)
39   - person = ExternalPerson.get_or_create(person_data)
  37 + source: provider.site || auth_data.provider,
  38 + email: user.email
  39 + )
40 40 user.external_person_id = person.id
41 41  
42 42 oauth_auth = person.oauth_auth
... ...
plugins/oauth_client/db/migrate/20160809181708_adds_type_to_external_person.rb 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +class AddsTypeToExternalPerson < ActiveRecord::Migration
  2 + def up
  3 + add_column :external_people, :type, :string
  4 + end
  5 +
  6 + def down
  7 + remove_column :external_people, :type
  8 + end
  9 +end
... ...
plugins/oauth_client/lib/ext/external_person.rb
... ... @@ -1,38 +0,0 @@
1   -require_dependency 'external_person'
2   -
3   -class ExternalPerson
4   -
5   - has_one :oauth_auth, as: :profile, class_name: 'OauthClientPlugin::Auth', dependent: :destroy
6   - has_one :oauth_provider, through: :oauth_auth, source: :provider
7   -
8   - def avatar
9   - self.oauth_auth.image_url
10   - end
11   -
12   - def image
13   - ExternalPerson::Image.new(self.oauth_auth)
14   - end
15   -
16   - def public_profile_url
17   - self.oauth_auth.profile_url
18   - end
19   -
20   - def url
21   - self.oauth_auth.profile_url
22   - end
23   -
24   - def admin_url
25   - self.oauth_auth.settings_url
26   - end
27   -
28   - class ExternalPerson::Image
29   - def initialize(oauth_auth)
30   - @oauth_auth = oauth_auth
31   - end
32   -
33   - def public_filename(size = nil)
34   - URI(@oauth_auth.image_url(size))
35   - end
36   - end
37   -
38   -end
plugins/oauth_client/models/oauth_client_plugin/oauth_external_person.rb 0 → 100644
... ... @@ -0,0 +1,35 @@
  1 +class OauthClientPlugin::OauthExternalPerson < ExternalPerson
  2 +
  3 + has_one :oauth_auth, as: :profile, class_name: 'OauthClientPlugin::Auth', dependent: :destroy
  4 + has_one :oauth_provider, through: :oauth_auth, source: :provider
  5 +
  6 + def avatar
  7 + self.oauth_auth.image_url
  8 + end
  9 +
  10 + def image
  11 + OauthClientPlugin::OauthExternalPerson::Image.new(self.oauth_auth)
  12 + end
  13 +
  14 + def public_profile_url
  15 + self.oauth_auth.profile_url
  16 + end
  17 +
  18 + def url
  19 + self.oauth_auth.profile_url
  20 + end
  21 +
  22 + def admin_url
  23 + self.oauth_auth.settings_url
  24 + end
  25 +
  26 + class OauthClientPlugin::OauthExternalPerson::Image < ExternalPerson::Image
  27 + def initialize(oauth_auth)
  28 + @oauth_auth = oauth_auth
  29 + end
  30 +
  31 + def public_filename(size = nil)
  32 + URI(@oauth_auth.image_url(size))
  33 + end
  34 + end
  35 +end
... ...
plugins/oauth_client/test/functional/oauth_client_plugin_public_controller_test.rb
... ... @@ -95,7 +95,7 @@ class OauthClientPluginPublicControllerTest &lt; ActionController::TestCase
95 95 request.env["omniauth.params"] = {"action" => "external_login"}
96 96  
97 97 get :callback
98   - external_person = ExternalPerson.find_by(identifier: auth.info.nickname)
  98 + external_person = OauthClientPlugin::OauthExternalPerson.find_by(identifier: auth.info.nickname)
99 99 assert_equal provider, external_person.oauth_auth.provider
100 100 end
101 101 end
... ...
plugins/oauth_client/test/unit/oauth_external_person_test.rb 0 → 100644
... ... @@ -0,0 +1,31 @@
  1 +
  2 +require 'test_helper'
  3 +
  4 +class OauthExternalPersonTest < ActiveSupport::TestCase
  5 +
  6 + def setup
  7 + provider = fast_create(OauthClientPlugin::Provider, name: "GitHub")
  8 + @external_person = fast_create(ExternalPerson, name: "testuser", email: "test@email.com",
  9 + identifier: "testuser")
  10 + OauthClientPlugin::GithubAuth.create!(profile: @external_person, provider: provider)
  11 +
  12 + @oauth_external_person = fast_create(OauthClientPlugin::OauthExternalPerson,
  13 + name: "testuser", email: "test@email.com",
  14 + identifier: "testuser")
  15 + OauthClientPlugin::GithubAuth.create!(profile: @oauth_external_person,
  16 + provider: provider)
  17 + end
  18 +
  19 + should "not orverride info from a regular external person" do
  20 + assert_not_equal @external_person.avatar, @oauth_external_person.avatar
  21 + assert_not_equal @external_person.url, @oauth_external_person.url
  22 + assert_not_equal @external_person.admin_url, @oauth_external_person.admin_url
  23 + assert_not_equal @external_person.public_profile_url,
  24 + @oauth_external_person.public_profile_url
  25 + end
  26 +
  27 + should "not override the Image class from a regular external person" do
  28 + assert @external_person.image.is_a? ExternalPerson::Image
  29 + assert @oauth_external_person.image.is_a? OauthClientPlugin::OauthExternalPerson::Image
  30 + end
  31 +end
... ...
plugins/oauth_client/test/unit/provider_test.rb
... ... @@ -2,7 +2,7 @@ require &#39;test_helper&#39;
2 2  
3 3 class ProviderTest < ActiveSupport::TestCase
4 4  
5   - should "only create a noosfero provider without a site" do
  5 + should "only create a noosfero provider with a site" do
6 6 provider = OauthClientPlugin::Provider.new(:name => 'noosfero', :strategy => 'noosfero_oauth2')
7 7 assert_not provider.valid?
8 8  
... ...