Commit 36eb73564fe969b84fd16829bfd2f94fb54b50ed

Authored by Gabriel Silva
1 parent 0125b04e

Adds site validation for noosfero_oauth providers

Signed-off-by: Gabriel Silva <gabriel93.silva@gmail.com>
plugins/oauth_client/lib/oauth_client_plugin.rb
@@ -105,7 +105,7 @@ class OauthClientPlugin &lt; Noosfero::Plugin @@ -105,7 +105,7 @@ class OauthClientPlugin &lt; Noosfero::Plugin
105 end 105 end
106 106
107 def js_files 107 def js_files
108 - ["script.js"] 108 + ["script.js", "provider.js"]
109 end 109 end
110 110
111 end 111 end
plugins/oauth_client/models/oauth_client_plugin/provider.rb
@@ -4,6 +4,8 @@ class OauthClientPlugin::Provider &lt; ApplicationRecord @@ -4,6 +4,8 @@ class OauthClientPlugin::Provider &lt; ApplicationRecord
4 4
5 validates_presence_of :name, :strategy 5 validates_presence_of :name, :strategy
6 6
  7 + validate :noosfero_provider_must_have_a_site
  8 +
7 acts_as_having_image 9 acts_as_having_image
8 acts_as_having_settings field: :options 10 acts_as_having_settings field: :options
9 11
@@ -18,4 +20,10 @@ class OauthClientPlugin::Provider &lt; ApplicationRecord @@ -18,4 +20,10 @@ class OauthClientPlugin::Provider &lt; ApplicationRecord
18 20
19 acts_as_having_image 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 end 29 end
plugins/oauth_client/public/provider.js 0 → 100644
@@ -0,0 +1,12 @@ @@ -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 +});
plugins/oauth_client/test/unit/provider_test.rb 0 → 100644
@@ -0,0 +1,17 @@ @@ -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/_noosfero_oauth2.html.erb
@@ -1,5 +0,0 @@ @@ -1,5 +0,0 @@
1 -<%= f.fields_for :client_options, OpenStruct.new(provider.options[:client_options]) do |c| %>  
2 - <div class="client-url">  
3 - <%= labelled_form_field _('Client Url'), c.text_field(:site) %>  
4 - </div>  
5 -<% end %>  
plugins/oauth_client/views/oauth_client_plugin_admin/edit.html.erb
@@ -8,7 +8,7 @@ @@ -8,7 +8,7 @@
8 </div> 8 </div>
9 9
10 <div class="name"> 10 <div class="name">
11 - <%= labelled_form_field _('Name'), f.text_field(:name) %> 11 + <%= required labelled_form_field _('Name'), f.text_field(:name) %>
12 </div> 12 </div>
13 13
14 <div class="strategy"> 14 <div class="strategy">
@@ -27,8 +27,10 @@ @@ -27,8 +27,10 @@
27 <%= labelled_form_field _('Client Secret'), f.text_field(:client_secret) %> 27 <%= labelled_form_field _('Client Secret'), f.text_field(:client_secret) %>
28 </div> 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 <% end %> 34 <% end %>
33 35
34 <div class="image-icon"> 36 <div class="image-icon">