Commit 2533959664d54a205e8e5872846fdabaa5bee92b

Authored by Gabriel Silva
1 parent 7826b2f7

Gerenates user image acording to provider

Signed-off-by: Gabriel Silva <gabriel93.silva@gmail.com>
Signed-off-by: Sabryna Sousa  <sabryna.sousa1323@gmail.com>
plugins/oauth_client/controllers/public/oauth_client_plugin_public_controller.rb
@@ -30,18 +30,20 @@ class OauthClientPluginPublicController &lt; PublicController @@ -30,18 +30,20 @@ class OauthClientPluginPublicController &lt; PublicController
30 provider = OauthClientPlugin::Provider.find(session[:provider_id]) 30 provider = OauthClientPlugin::Provider.find(session[:provider_id])
31 31
32 if provider.enabled? 32 if provider.enabled?
33 - user = User.new  
34 - user.email = auth.info.email  
35 - user.login = auth.info.name.to_slug  
36 - 33 + user = User.new(email: auth.info.email, login: auth.info.name.to_slug)
37 webfinger = OpenStruct.new( 34 webfinger = OpenStruct.new(
38 identifier: user.login, 35 identifier: user.login,
39 name: auth.info.name, 36 name: auth.info.name,
40 created_at: Time.now, 37 created_at: Time.now,
41 domain: auth.provider, 38 domain: auth.provider,
42 email: user.email) 39 email: user.email)
43 - user.external_person_id = ExternalPerson.get_or_create(webfinger).id  
44 - user.store_oauth_providers 40 + person = ExternalPerson.get_or_create(webfinger)
  41 + user.external_person_id = person.id
  42 +
  43 + if person.oauth_auth.nil?
  44 + OauthClientPlugin::Auth.create_for_strategy(provider.strategy, external_person: person,
  45 + provider: provider, uid: auth.uid, enabled: true)
  46 + end
45 self.current_user = user 47 self.current_user = user
46 else 48 else
47 session[:notice] = _("Can't login with %s") % provider.name 49 session[:notice] = _("Can't login with %s") % provider.name
plugins/oauth_client/db/migrate/20160720165808_add_external_profile_to_oauth_auth.rb 0 → 100644
@@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
  1 +class AddExternalProfileToOauthAuth < ActiveRecord::Migration
  2 + def up
  3 + add_column :oauth_client_plugin_auths, :external_person_id, :integer
  4 + add_column :oauth_client_plugin_auths, :uid, :string
  5 + add_index :oauth_client_plugin_auths, :external_person_id
  6 + end
  7 +
  8 + def down
  9 + remove_index :oauth_client_plugin_auths, :external_person_id
  10 + remove_column :oauth_client_plugin_auths, :external_person_id
  11 + remove_column :oauth_client_plugin_auths, :uid
  12 + end
  13 +end
plugins/oauth_client/lib/ext/external_person.rb 0 → 100644
@@ -0,0 +1,26 @@ @@ -0,0 +1,26 @@
  1 +require_dependency 'external_person'
  2 +
  3 +class ExternalPerson
  4 +
  5 + has_one :oauth_auth, foreign_key: :external_person_id, 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(oauth_auth)
  14 + end
  15 +
  16 + class ExternalPerson::Image
  17 + def initialize(oauth_auth)
  18 + @oauth_auth = oauth_auth
  19 + end
  20 +
  21 + def public_filename(size = nil)
  22 + URI(@oauth_auth.image_url(size))
  23 + end
  24 + end
  25 +
  26 +end
plugins/oauth_client/models/oauth_client_plugin/auth.rb
1 class OauthClientPlugin::Auth < ApplicationRecord 1 class OauthClientPlugin::Auth < ApplicationRecord
2 2
3 - attr_accessible :profile, :provider, :enabled,  
4 - :access_token, :expires_in 3 + attr_accessible :profile, :external_person, :provider,
  4 + :enabled, :access_token, :expires_in, :type, :uid
5 5
6 belongs_to :profile, class_name: 'Profile' 6 belongs_to :profile, class_name: 'Profile'
  7 + belongs_to :external_person, class_name: 'ExternalPerson'
7 belongs_to :provider, class_name: 'OauthClientPlugin::Provider' 8 belongs_to :provider, class_name: 'OauthClientPlugin::Provider'
8 9
9 - validates_presence_of :profile  
10 validates_presence_of :provider 10 validates_presence_of :provider
11 validates_uniqueness_of :profile_id, scope: :provider_id 11 validates_uniqueness_of :profile_id, scope: :provider_id
  12 + validates_uniqueness_of :external_person_id, scope: :provider_id
  13 +
  14 + validate :must_be_related_to_profile
12 15
13 acts_as_having_settings field: :data 16 acts_as_having_settings field: :data
14 17
@@ -26,4 +29,21 @@ class OauthClientPlugin::Auth &lt; ApplicationRecord @@ -26,4 +29,21 @@ class OauthClientPlugin::Auth &lt; ApplicationRecord
26 not self.expired? 29 not self.expired?
27 end 30 end
28 31
  32 + def self.create_for_strategy(strategy, args = {})
  33 + namespace = self.name.split("::")[0]
  34 + class_name = "#{namespace}::#{strategy.camelize}Auth"
  35 + OauthClientPlugin::Auth.create!(args.merge(type: class_name))
  36 + end
  37 +
  38 + def must_be_related_to_profile
  39 + if self.profile.nil? && self.external_person.nil?
  40 + self.errors.add(:base, "Must ")
  41 + end
  42 + end
  43 +
  44 + # Should be implemented by the Provider specific Auth classes
  45 + def image_url(size = nil)
  46 + nil
  47 + end
  48 +
29 end 49 end
plugins/oauth_client/models/oauth_client_plugin/facebook_auth.rb 0 → 100644
@@ -0,0 +1,16 @@ @@ -0,0 +1,16 @@
  1 +class OauthClientPlugin::FacebookAuth < OauthClientPlugin::Auth
  2 +
  3 + IMAGE_SIZES = {
  4 + :big => "150",
  5 + :thumb => "100",
  6 + :portrait => "64",
  7 + :minor => "50",
  8 + :icon => "20"
  9 + }
  10 +
  11 + def image_url(size = nil)
  12 + size = ""
  13 + "http://graph.facebook.com/#{self.uid}/picture"
  14 + end
  15 +
  16 +end
plugins/oauth_client/models/oauth_client_plugin/github_auth.rb 0 → 100644
@@ -0,0 +1,15 @@ @@ -0,0 +1,15 @@
  1 +class OauthClientPlugin::GithubAuth < OauthClientPlugin::Auth
  2 +
  3 + IMAGE_SIZE = {
  4 + :big => "150",
  5 + :thumb => "100",
  6 + :portrait => "64",
  7 + :minor => "50",
  8 + :icon => "18"
  9 + }
  10 +
  11 + def image_url(size = nil)
  12 + size = IMAGE_SIZES[size] || IMAGE_SIZES[:icon]
  13 + "https://avatars.githubusercontent.com/u/#{self.uid}?v=3&size=#{size}"
  14 + end
  15 +end