From 36eb73564fe969b84fd16829bfd2f94fb54b50ed Mon Sep 17 00:00:00 2001 From: Gabriel Silva Date: Mon, 1 Aug 2016 14:02:35 +0000 Subject: [PATCH] Adds site validation for noosfero_oauth providers --- plugins/oauth_client/lib/oauth_client_plugin.rb | 2 +- plugins/oauth_client/models/oauth_client_plugin/provider.rb | 8 ++++++++ plugins/oauth_client/public/provider.js | 12 ++++++++++++ plugins/oauth_client/test/unit/provider_test.rb | 17 +++++++++++++++++ plugins/oauth_client/views/oauth_client_plugin_admin/_noosfero_oauth2.html.erb | 5 ----- plugins/oauth_client/views/oauth_client_plugin_admin/edit.html.erb | 8 +++++--- 6 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 plugins/oauth_client/public/provider.js create mode 100644 plugins/oauth_client/test/unit/provider_test.rb delete mode 100644 plugins/oauth_client/views/oauth_client_plugin_admin/_noosfero_oauth2.html.erb 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| %> -
- <%= labelled_form_field _('Client Url'), c.text_field(:site) %> -
-<% end %> diff --git a/plugins/oauth_client/views/oauth_client_plugin_admin/edit.html.erb b/plugins/oauth_client/views/oauth_client_plugin_admin/edit.html.erb index a028185..a6cf1bb 100644 --- a/plugins/oauth_client/views/oauth_client_plugin_admin/edit.html.erb +++ b/plugins/oauth_client/views/oauth_client_plugin_admin/edit.html.erb @@ -8,7 +8,7 @@
- <%= labelled_form_field _('Name'), f.text_field(:name) %> + <%= required labelled_form_field _('Name'), f.text_field(:name) %>
@@ -27,8 +27,10 @@ <%= labelled_form_field _('Client Secret'), f.text_field(:client_secret) %>
- <% if File.exists?(File.join(File.dirname(__FILE__), "_#{@provider.strategy}.html.erb")) %> - <%= render :partial => "#{@provider.strategy}", :locals => {:f => f, :provider => @provider} %> + <%= f.fields_for :client_options, OpenStruct.new(@provider.options[:client_options]) do |c| %> +
"> + <%= labelled_form_field _('Client Url'), c.text_field(:site) %> +
<% end %>
-- libgit2 0.21.2