diff --git a/plugins/oauth_client/lib/oauth_client_plugin.rb b/plugins/oauth_client/lib/oauth_client_plugin.rb index 35722d5..980cff2 100644 --- a/plugins/oauth_client/lib/oauth_client_plugin.rb +++ b/plugins/oauth_client/lib/oauth_client_plugin.rb @@ -105,7 +105,7 @@ class OauthClientPlugin < Noosfero::Plugin end def js_files - ["script.js"] + ["script.js", "provider.js"] end end diff --git a/plugins/oauth_client/models/oauth_client_plugin/provider.rb b/plugins/oauth_client/models/oauth_client_plugin/provider.rb index a678012..2b655eb 100644 --- a/plugins/oauth_client/models/oauth_client_plugin/provider.rb +++ b/plugins/oauth_client/models/oauth_client_plugin/provider.rb @@ -4,6 +4,8 @@ class OauthClientPlugin::Provider < ApplicationRecord validates_presence_of :name, :strategy + validate :noosfero_provider_must_have_a_site + acts_as_having_image acts_as_having_settings field: :options @@ -18,4 +20,10 @@ class OauthClientPlugin::Provider < ApplicationRecord acts_as_having_image + def noosfero_provider_must_have_a_site + if self.strategy == 'noosfero_oauth2' && (self.client_options.nil? || self.client_options[:site].blank?) + self.errors.add(:site, "A Noosfero provider must have a site") + end + end + end diff --git a/plugins/oauth_client/public/provider.js b/plugins/oauth_client/public/provider.js new file mode 100644 index 0000000..1a6cacd --- /dev/null +++ b/plugins/oauth_client/public/provider.js @@ -0,0 +1,12 @@ +function toggle_strategy(strategyName) { + if (strategyName == "noosfero_oauth2") { + $(".client-url").addClass("required-field"); + } else { + $(".client-url").removeClass("required-field"); + } +} + +$(document).on("change", "select#oauth_client_plugin_provider_strategy", function() { + var selectedOption = $(this).find(":selected").text(); + toggle_strategy(selectedOption); +}); diff --git a/plugins/oauth_client/test/unit/provider_test.rb b/plugins/oauth_client/test/unit/provider_test.rb new file mode 100644 index 0000000..4377916 --- /dev/null +++ b/plugins/oauth_client/test/unit/provider_test.rb @@ -0,0 +1,17 @@ +require 'test_helper' + +class ProviderTest < ActiveSupport::TestCase + + should "only create a noosfero provider without a site" do + provider = OauthClientPlugin::Provider.new(:name => 'noosfero', :strategy => 'noosfero_oauth2') + assert_not provider.valid? + + provider.client_options = { :site => "http://noosfero.org" } + assert provider.valid? + end + + should "create a regular provider without a site" do + provider = OauthClientPlugin::Provider.new(:name => 'github', :strategy => 'github') + assert provider.valid? + end +end diff --git a/plugins/oauth_client/views/oauth_client_plugin_admin/_noosfero_oauth2.html.erb b/plugins/oauth_client/views/oauth_client_plugin_admin/_noosfero_oauth2.html.erb deleted file mode 100644 index 6928d7a..0000000 --- a/plugins/oauth_client/views/oauth_client_plugin_admin/_noosfero_oauth2.html.erb +++ /dev/null @@ -1,5 +0,0 @@ -<%= f.fields_for :client_options, OpenStruct.new(provider.options[:client_options]) do |c| %> -