From 2533959664d54a205e8e5872846fdabaa5bee92b Mon Sep 17 00:00:00 2001 From: Gabriel Silva Date: Thu, 21 Jul 2016 13:13:25 +0000 Subject: [PATCH] Gerenates user image acording to provider --- plugins/oauth_client/controllers/public/oauth_client_plugin_public_controller.rb | 14 ++++++++------ plugins/oauth_client/db/migrate/20160720165808_add_external_profile_to_oauth_auth.rb | 13 +++++++++++++ plugins/oauth_client/lib/ext/external_person.rb | 26 ++++++++++++++++++++++++++ plugins/oauth_client/models/oauth_client_plugin/auth.rb | 26 +++++++++++++++++++++++--- plugins/oauth_client/models/oauth_client_plugin/facebook_auth.rb | 16 ++++++++++++++++ plugins/oauth_client/models/oauth_client_plugin/github_auth.rb | 15 +++++++++++++++ 6 files changed, 101 insertions(+), 9 deletions(-) create mode 100644 plugins/oauth_client/db/migrate/20160720165808_add_external_profile_to_oauth_auth.rb create mode 100644 plugins/oauth_client/lib/ext/external_person.rb create mode 100644 plugins/oauth_client/models/oauth_client_plugin/facebook_auth.rb create mode 100644 plugins/oauth_client/models/oauth_client_plugin/github_auth.rb diff --git a/plugins/oauth_client/controllers/public/oauth_client_plugin_public_controller.rb b/plugins/oauth_client/controllers/public/oauth_client_plugin_public_controller.rb index 9fbe799..66c8b48 100644 --- a/plugins/oauth_client/controllers/public/oauth_client_plugin_public_controller.rb +++ b/plugins/oauth_client/controllers/public/oauth_client_plugin_public_controller.rb @@ -30,18 +30,20 @@ class OauthClientPluginPublicController < PublicController provider = OauthClientPlugin::Provider.find(session[:provider_id]) if provider.enabled? - user = User.new - user.email = auth.info.email - user.login = auth.info.name.to_slug - + user = User.new(email: auth.info.email, login: auth.info.name.to_slug) webfinger = OpenStruct.new( identifier: user.login, name: auth.info.name, created_at: Time.now, domain: auth.provider, email: user.email) - user.external_person_id = ExternalPerson.get_or_create(webfinger).id - user.store_oauth_providers + person = ExternalPerson.get_or_create(webfinger) + user.external_person_id = person.id + + if person.oauth_auth.nil? + OauthClientPlugin::Auth.create_for_strategy(provider.strategy, external_person: person, + provider: provider, uid: auth.uid, enabled: true) + end self.current_user = user else session[:notice] = _("Can't login with %s") % provider.name diff --git a/plugins/oauth_client/db/migrate/20160720165808_add_external_profile_to_oauth_auth.rb b/plugins/oauth_client/db/migrate/20160720165808_add_external_profile_to_oauth_auth.rb new file mode 100644 index 0000000..acdd427 --- /dev/null +++ b/plugins/oauth_client/db/migrate/20160720165808_add_external_profile_to_oauth_auth.rb @@ -0,0 +1,13 @@ +class AddExternalProfileToOauthAuth < ActiveRecord::Migration + def up + add_column :oauth_client_plugin_auths, :external_person_id, :integer + add_column :oauth_client_plugin_auths, :uid, :string + add_index :oauth_client_plugin_auths, :external_person_id + end + + def down + remove_index :oauth_client_plugin_auths, :external_person_id + remove_column :oauth_client_plugin_auths, :external_person_id + remove_column :oauth_client_plugin_auths, :uid + end +end diff --git a/plugins/oauth_client/lib/ext/external_person.rb b/plugins/oauth_client/lib/ext/external_person.rb new file mode 100644 index 0000000..a8db11d --- /dev/null +++ b/plugins/oauth_client/lib/ext/external_person.rb @@ -0,0 +1,26 @@ +require_dependency 'external_person' + +class ExternalPerson + + has_one :oauth_auth, foreign_key: :external_person_id, class_name: 'OauthClientPlugin::Auth', dependent: :destroy + has_one :oauth_provider, through: :oauth_auth, source: :provider + + def avatar + self.oauth_auth.image_url + end + + def image + ExternalPerson::Image.new(oauth_auth) + end + + class ExternalPerson::Image + def initialize(oauth_auth) + @oauth_auth = oauth_auth + end + + def public_filename(size = nil) + URI(@oauth_auth.image_url(size)) + end + end + +end diff --git a/plugins/oauth_client/models/oauth_client_plugin/auth.rb b/plugins/oauth_client/models/oauth_client_plugin/auth.rb index 6fdb53c..7e52714 100644 --- a/plugins/oauth_client/models/oauth_client_plugin/auth.rb +++ b/plugins/oauth_client/models/oauth_client_plugin/auth.rb @@ -1,14 +1,17 @@ class OauthClientPlugin::Auth < ApplicationRecord - attr_accessible :profile, :provider, :enabled, - :access_token, :expires_in + attr_accessible :profile, :external_person, :provider, + :enabled, :access_token, :expires_in, :type, :uid belongs_to :profile, class_name: 'Profile' + belongs_to :external_person, class_name: 'ExternalPerson' belongs_to :provider, class_name: 'OauthClientPlugin::Provider' - validates_presence_of :profile validates_presence_of :provider validates_uniqueness_of :profile_id, scope: :provider_id + validates_uniqueness_of :external_person_id, scope: :provider_id + + validate :must_be_related_to_profile acts_as_having_settings field: :data @@ -26,4 +29,21 @@ class OauthClientPlugin::Auth < ApplicationRecord not self.expired? end + def self.create_for_strategy(strategy, args = {}) + namespace = self.name.split("::")[0] + class_name = "#{namespace}::#{strategy.camelize}Auth" + OauthClientPlugin::Auth.create!(args.merge(type: class_name)) + end + + def must_be_related_to_profile + if self.profile.nil? && self.external_person.nil? + self.errors.add(:base, "Must ") + end + end + + # Should be implemented by the Provider specific Auth classes + def image_url(size = nil) + nil + end + end diff --git a/plugins/oauth_client/models/oauth_client_plugin/facebook_auth.rb b/plugins/oauth_client/models/oauth_client_plugin/facebook_auth.rb new file mode 100644 index 0000000..e07bd28 --- /dev/null +++ b/plugins/oauth_client/models/oauth_client_plugin/facebook_auth.rb @@ -0,0 +1,16 @@ +class OauthClientPlugin::FacebookAuth < OauthClientPlugin::Auth + + IMAGE_SIZES = { + :big => "150", + :thumb => "100", + :portrait => "64", + :minor => "50", + :icon => "20" + } + + def image_url(size = nil) + size = "" + "http://graph.facebook.com/#{self.uid}/picture" + end + +end diff --git a/plugins/oauth_client/models/oauth_client_plugin/github_auth.rb b/plugins/oauth_client/models/oauth_client_plugin/github_auth.rb new file mode 100644 index 0000000..0e5ca8c --- /dev/null +++ b/plugins/oauth_client/models/oauth_client_plugin/github_auth.rb @@ -0,0 +1,15 @@ +class OauthClientPlugin::GithubAuth < OauthClientPlugin::Auth + + IMAGE_SIZE = { + :big => "150", + :thumb => "100", + :portrait => "64", + :minor => "50", + :icon => "18" + } + + def image_url(size = nil) + size = IMAGE_SIZES[size] || IMAGE_SIZES[:icon] + "https://avatars.githubusercontent.com/u/#{self.uid}?v=3&size=#{size}" + end +end -- libgit2 0.21.2