Compare View
Commits (5)
-
Signed-off-by: Gabriel Silva <gabriel93.silva@gmail.com> Signed-off-by: Sabryna Sousa <sabryna.sousa1323@gmail.com>
-
Signed-off-by: Gabriel Silva <gabriel93.silva@gmail.com>
-
Signed-off-by: Gabriel Silva <gabriel93.silva@gmail.com>
-
Signed-off-by: Gabriel Silva <gabriel93.silva@gmail.com>
-
Signed-off-by: Gabriel Silva <gabriel93.silva@gmail.com> Signed-off-by: Marcos Ronaldo <marcos.rpj2@gmail.com>
Showing
16 changed files
Show diff stats
app/models/external_person.rb
plugins/oauth_client/controllers/public/oauth_client_plugin_public_controller.rb
... | ... | @@ -32,17 +32,18 @@ class OauthClientPluginPublicController < PublicController |
32 | 32 | if provider.enabled? |
33 | 33 | user = User.new(email: auth.info.email, login: auth.info.name.to_slug) |
34 | 34 | webfinger = OpenStruct.new( |
35 | - identifier: user.login, | |
35 | + identifier: auth.info.nickname || user.login, | |
36 | 36 | name: auth.info.name, |
37 | 37 | created_at: Time.now, |
38 | - domain: auth.provider, | |
38 | + domain: provider.site || auth.provider, | |
39 | 39 | email: user.email) |
40 | 40 | person = ExternalPerson.get_or_create(webfinger) |
41 | 41 | user.external_person_id = person.id |
42 | 42 | |
43 | 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) | |
44 | + auth_data = { external_person: person, provider: provider, enabled: true, | |
45 | + external_person_uid: auth.uid, external_person_image_url: auth.info.image } | |
46 | + OauthClientPlugin::Auth.create_for_strategy(provider.strategy, auth_data) | |
46 | 47 | end |
47 | 48 | self.current_user = user |
48 | 49 | else | ... | ... |
plugins/oauth_client/db/migrate/20160720165808_add_external_profile_to_oauth_auth.rb
1 | 1 | class AddExternalProfileToOauthAuth < ActiveRecord::Migration |
2 | 2 | def up |
3 | 3 | add_column :oauth_client_plugin_auths, :external_person_id, :integer |
4 | - add_column :oauth_client_plugin_auths, :uid, :string | |
4 | + add_column :oauth_client_plugin_auths, :external_person_uid, :string | |
5 | + add_column :oauth_client_plugin_auths, :external_person_image_url, :string | |
5 | 6 | add_index :oauth_client_plugin_auths, :external_person_id |
6 | 7 | end |
7 | 8 | |
8 | 9 | def down |
9 | 10 | remove_index :oauth_client_plugin_auths, :external_person_id |
10 | 11 | remove_column :oauth_client_plugin_auths, :external_person_id |
11 | - remove_column :oauth_client_plugin_auths, :uid | |
12 | + remove_column :oauth_client_plugin_auths, :external_person_uid | |
13 | + remove_column :oauth_client_plugin_auths, :external_person_image_url | |
12 | 14 | end |
13 | 15 | end | ... | ... |
plugins/oauth_client/lib/ext/external_person.rb
... | ... | @@ -10,21 +10,19 @@ class ExternalPerson |
10 | 10 | end |
11 | 11 | |
12 | 12 | def image |
13 | - ExternalPerson::Image.new(oauth_auth) | |
13 | + ExternalPerson::Image.new(self.oauth_auth) | |
14 | 14 | end |
15 | 15 | |
16 | - # This method is un alias to 'url' method of ExternalPerson < ActiveRecord::Base | |
17 | 16 | def public_profile_url |
18 | - self.oauth_auth.perfil_url | |
17 | + self.oauth_auth.profile_url | |
19 | 18 | end |
20 | 19 | |
21 | - | |
22 | 20 | def url |
23 | - self.oauth_auth.perfil_url | |
21 | + self.oauth_auth.profile_url | |
24 | 22 | end |
25 | 23 | |
26 | 24 | def admin_url |
27 | - self.oauth_auth.setting_url | |
25 | + self.oauth_auth.settings_url | |
28 | 26 | end |
29 | 27 | |
30 | 28 | class ExternalPerson::Image | ... | ... |
plugins/oauth_client/lib/oauth_client_plugin.rb
plugins/oauth_client/models/oauth_client_plugin/auth.rb
1 | 1 | class OauthClientPlugin::Auth < ApplicationRecord |
2 | 2 | |
3 | 3 | attr_accessible :profile, :external_person, :provider, |
4 | - :enabled, :access_token, :expires_in, :type, :uid | |
4 | + :enabled, :access_token, :expires_in, :type, | |
5 | + :external_person_uid, :external_person_image_url | |
5 | 6 | |
6 | 7 | belongs_to :profile, class_name: 'Profile' |
7 | 8 | belongs_to :external_person, class_name: 'ExternalPerson' |
... | ... | @@ -41,6 +42,14 @@ class OauthClientPlugin::Auth < ApplicationRecord |
41 | 42 | end |
42 | 43 | end |
43 | 44 | |
45 | + IMAGE_SIZES = { | |
46 | + :big => "150", | |
47 | + :thumb => "100", | |
48 | + :portrait => "64", | |
49 | + :minor => "50", | |
50 | + :icon => "18" | |
51 | + } | |
52 | + | |
44 | 53 | # Should be implemented by the Provider specific Auth classes |
45 | 54 | def image_url(size = nil) |
46 | 55 | nil | ... | ... |
plugins/oauth_client/models/oauth_client_plugin/facebook_auth.rb
1 | 1 | class OauthClientPlugin::FacebookAuth < OauthClientPlugin::Auth |
2 | 2 | |
3 | - IMAGE_SIZES = { | |
4 | - :big => "150", | |
5 | - :thumb => "100", | |
6 | - :portrait => "64", | |
7 | - :minor => "50", | |
8 | - :icon => "20" | |
9 | - } | |
10 | - | |
11 | 3 | def image_url(size = nil) |
12 | - size = "" | |
13 | - "http://graph.facebook.com/#{self.uid}/picture" | |
4 | + size = IMAGE_SIZES[size] || IMAGE_SIZES[:icon] | |
5 | + "#{self.external_person_image_url}?width=#{size}" | |
14 | 6 | end |
15 | 7 | |
16 | - def perfil_url | |
17 | - "https://www.facebook.com/#{self.uid}" | |
8 | + def profile_url | |
9 | + "https://www.facebook.com/#{self.external_person_uid}" | |
18 | 10 | end |
19 | 11 | |
20 | - def setting_url | |
12 | + def settings_url | |
21 | 13 | "https://www.facebook.com/settings" |
22 | 14 | end |
23 | 15 | ... | ... |
plugins/oauth_client/models/oauth_client_plugin/github_auth.rb
1 | 1 | class OauthClientPlugin::GithubAuth < OauthClientPlugin::Auth |
2 | 2 | |
3 | - IMAGE_SIZE = { | |
4 | - :big => "150", | |
5 | - :thumb => "100", | |
6 | - :portrait => "64", | |
7 | - :minor => "50", | |
8 | - :icon => "18" | |
9 | - } | |
10 | - | |
11 | 3 | def image_url(size = nil) |
12 | 4 | size = IMAGE_SIZES[size] || IMAGE_SIZES[:icon] |
13 | - "https://avatars.githubusercontent.com/u/#{self.uid}?v=3&size=#{size}" | |
5 | + "#{self.external_person_image_url}&size=#{size}" | |
6 | + end | |
7 | + | |
8 | + def profile_url | |
9 | + "https://www.github.com/#{self.external_person.identifier}" | |
10 | + end | |
11 | + | |
12 | + def settings_url | |
13 | + "https://www.github.com/settings" | |
14 | 14 | end |
15 | 15 | end | ... | ... |
plugins/oauth_client/models/oauth_client_plugin/google_oauth2_auth.rb
0 → 100644
... | ... | @@ -0,0 +1,15 @@ |
1 | +class OauthClientPlugin::GoogleOauth2Auth < OauthClientPlugin::Auth | |
2 | + | |
3 | + def image_url(size = nil) | |
4 | + size = IMAGE_SIZES[size] || IMAGE_SIZES[:icon] | |
5 | + "#{self.external_person_image_url}?sz=#{size}" | |
6 | + end | |
7 | + | |
8 | + def profile_url | |
9 | + "https://plus.google.com/#{self.external_person_uid}" | |
10 | + end | |
11 | + | |
12 | + def settings_url | |
13 | + "https://plus.google.com/u/0/settings" | |
14 | + end | |
15 | +end | ... | ... |
plugins/oauth_client/models/oauth_client_plugin/noosfero_oauth2_auth.rb
0 → 100644
... | ... | @@ -0,0 +1,14 @@ |
1 | +class OauthClientPlugin::NoosferoOauth2Auth < OauthClientPlugin::Auth | |
2 | + | |
3 | + def image_url(size = nil) | |
4 | + URI.join("http://#{self.provider.client_options[:site]}/profile/#{self.external_person.identifier}/icon/", size) | |
5 | + end | |
6 | + | |
7 | + def profile_url | |
8 | + "http://#{self.external_person.source}/profile/#{self.external_person.identifier}" | |
9 | + end | |
10 | + | |
11 | + def settings_url | |
12 | + "http://#{self.external_person.source}/myprofile/#{self.external_person.identifier}" | |
13 | + end | |
14 | +end | ... | ... |
plugins/oauth_client/models/oauth_client_plugin/provider.rb
... | ... | @@ -4,6 +4,8 @@ class OauthClientPlugin::Provider < ApplicationRecord |
4 | 4 | |
5 | 5 | validates_presence_of :name, :strategy |
6 | 6 | |
7 | + validate :noosfero_provider_must_have_a_site | |
8 | + | |
7 | 9 | acts_as_having_image |
8 | 10 | acts_as_having_settings field: :options |
9 | 11 | |
... | ... | @@ -18,4 +20,10 @@ class OauthClientPlugin::Provider < ApplicationRecord |
18 | 20 | |
19 | 21 | acts_as_having_image |
20 | 22 | |
23 | + def noosfero_provider_must_have_a_site | |
24 | + if self.strategy == 'noosfero_oauth2' && (self.client_options.nil? || self.client_options[:site].blank?) | |
25 | + self.errors.add(:site, "A Noosfero provider must have a site") | |
26 | + end | |
27 | + end | |
28 | + | |
21 | 29 | end | ... | ... |
plugins/oauth_client/models/oauth_client_plugin/twitter_auth.rb
0 → 100644
... | ... | @@ -0,0 +1,23 @@ |
1 | +class OauthClientPlugin::TwitterAuth < OauthClientPlugin::Auth | |
2 | + | |
3 | + IMAGE_SIZES = { | |
4 | + :big => "", | |
5 | + :thumb => "_bigger", | |
6 | + :portrait => "_normal", | |
7 | + :minor => "_normal", | |
8 | + :icon => "_mini" | |
9 | + } | |
10 | + | |
11 | + def image_url(size = nil) | |
12 | + size = IMAGE_SIZES[size] || IMAGE_SIZES[:icon] | |
13 | + self.external_person_image_url.gsub("_normal", size) | |
14 | + end | |
15 | + | |
16 | + def profile_url | |
17 | + "https://twitter.com/#{self.external_person.identifier}" | |
18 | + end | |
19 | + | |
20 | + def settings_url | |
21 | + "https://twitter.com/settings" | |
22 | + end | |
23 | +end | ... | ... |
... | ... | @@ -0,0 +1,12 @@ |
1 | +function toggle_strategy(strategyName) { | |
2 | + if (strategyName == "noosfero_oauth2") { | |
3 | + $(".client-url").addClass("required-field"); | |
4 | + } else { | |
5 | + $(".client-url").removeClass("required-field"); | |
6 | + } | |
7 | +} | |
8 | + | |
9 | +$(document).on("change", "select#oauth_client_plugin_provider_strategy", function() { | |
10 | + var selectedOption = $(this).find(":selected").text(); | |
11 | + toggle_strategy(selectedOption); | |
12 | +}); | ... | ... |
... | ... | @@ -0,0 +1,17 @@ |
1 | +require 'test_helper' | |
2 | + | |
3 | +class ProviderTest < ActiveSupport::TestCase | |
4 | + | |
5 | + should "only create a noosfero provider without a site" do | |
6 | + provider = OauthClientPlugin::Provider.new(:name => 'noosfero', :strategy => 'noosfero_oauth2') | |
7 | + assert_not provider.valid? | |
8 | + | |
9 | + provider.client_options = { :site => "http://noosfero.org" } | |
10 | + assert provider.valid? | |
11 | + end | |
12 | + | |
13 | + should "create a regular provider without a site" do | |
14 | + provider = OauthClientPlugin::Provider.new(:name => 'github', :strategy => 'github') | |
15 | + assert provider.valid? | |
16 | + end | |
17 | +end | ... | ... |
plugins/oauth_client/views/oauth_client_plugin_admin/edit.html.erb
... | ... | @@ -8,7 +8,7 @@ |
8 | 8 | </div> |
9 | 9 | |
10 | 10 | <div class="name"> |
11 | - <%= labelled_form_field _('Name'), f.text_field(:name) %> | |
11 | + <%= required labelled_form_field _('Name'), f.text_field(:name) %> | |
12 | 12 | </div> |
13 | 13 | |
14 | 14 | <div class="strategy"> |
... | ... | @@ -27,8 +27,10 @@ |
27 | 27 | <%= labelled_form_field _('Client Secret'), f.text_field(:client_secret) %> |
28 | 28 | </div> |
29 | 29 | |
30 | - <% if File.exists?(File.join(File.dirname(__FILE__), "_#{@provider.strategy}.html.erb")) %> | |
31 | - <%= render :partial => "#{@provider.strategy}", :locals => {:f => f, :provider => @provider} %> | |
30 | + <%= f.fields_for :client_options, OpenStruct.new(@provider.options[:client_options]) do |c| %> | |
31 | + <div class="client-url <%= "required-field" if @provider.strategy == "noosfero_oauth2" %>"> | |
32 | + <%= labelled_form_field _('Client Url'), c.text_field(:site) %> | |
33 | + </div> | |
32 | 34 | <% end %> |
33 | 35 | |
34 | 36 | <div class="image-icon"> | ... | ... |