diff --git a/plugins/oauth_client/controllers/oauth_client_plugin_admin_controller.rb b/plugins/oauth_client/controllers/oauth_client_plugin_admin_controller.rb index 50bde52..5b8f535 100644 --- a/plugins/oauth_client/controllers/oauth_client_plugin_admin_controller.rb +++ b/plugins/oauth_client/controllers/oauth_client_plugin_admin_controller.rb @@ -1,14 +1,26 @@ class OauthClientPluginAdminController < AdminController def index - settings = params[:settings] || {} + end + + def new + @provider = environment.oauth_providers.new + render :file => 'oauth_client_plugin_admin/edit' + end + + def remove + environment.oauth_providers.find(params[:id]).destroy + redirect_to :action => 'index' + end - @settings = Noosfero::Plugin::Settings.new(environment, OauthClientPlugin, settings) - @providers = @settings.get_setting(:providers) || {} + def edit + @provider = params[:id] ? environment.oauth_providers.find(params[:id]) : environment.oauth_providers.new if request.post? - @settings.save! - session[:notice] = 'Settings succefully saved.' - redirect_to :action => 'index' + if @provider.update_attributes(params['oauth_client_plugin_provider']) + session[:notice] = _('Saved!') + else + session[:notice] = _('Error!') + end end end diff --git a/plugins/oauth_client/db/migrate/20141010135314_create_oauth_client_plugin_provider.rb b/plugins/oauth_client/db/migrate/20141010135314_create_oauth_client_plugin_provider.rb new file mode 100644 index 0000000..1b73865 --- /dev/null +++ b/plugins/oauth_client/db/migrate/20141010135314_create_oauth_client_plugin_provider.rb @@ -0,0 +1,20 @@ +class CreateOauthClientPluginProvider < ActiveRecord::Migration + + def self.up + create_table :oauth_client_plugin_providers do |t| + t.integer :environment_id + t.string :strategy + t.string :identifier + t.string :name + t.text :options + t.boolean :enabled + t.integer :image_id + + t.timestamps + end + end + + def self.down + drop_table :oauth_client_plugin_providers + end +end diff --git a/plugins/oauth_client/lib/ext/environment.rb b/plugins/oauth_client/lib/ext/environment.rb new file mode 100644 index 0000000..725221e --- /dev/null +++ b/plugins/oauth_client/lib/ext/environment.rb @@ -0,0 +1,7 @@ +require_dependency 'environment' + +class Environment + + has_many :oauth_providers, :class_name => 'OauthClientPlugin::Provider' + +end diff --git a/plugins/oauth_client/lib/ext/user.rb b/plugins/oauth_client/lib/ext/user.rb index 3ca6649..f70e20b 100644 --- a/plugins/oauth_client/lib/ext/user.rb +++ b/plugins/oauth_client/lib/ext/user.rb @@ -8,7 +8,7 @@ class User def self.find_with_omniauth(auth) user = self.find_by_email(auth.info.email) - if user && !user.oauth_providers.empty? #FIXME save new oauth providers + if user# && !user.oauth_providers.empty? #FIXME save new oauth providers user else nil diff --git a/plugins/oauth_client/lib/oauth_client_plugin.rb b/plugins/oauth_client/lib/oauth_client_plugin.rb index a967a1d..53bcd10 100644 --- a/plugins/oauth_client/lib/oauth_client_plugin.rb +++ b/plugins/oauth_client/lib/oauth_client_plugin.rb @@ -13,7 +13,7 @@ class OauthClientPlugin < Noosfero::Plugin def login_extra_contents plugin = self proc do - render :partial => 'auth/oauth_login', :locals => {:providers => plugin.enabled_providers} + render :partial => 'auth/oauth_login', :locals => {:providers => environment.oauth_providers.enabled} end end @@ -29,12 +29,6 @@ class OauthClientPlugin < Noosfero::Plugin end end - def enabled_providers - settings = Noosfero::Plugin::Settings.new(context.environment, OauthClientPlugin) - providers = settings.get_setting(:providers) - providers.select {|provider, options| options[:enabled]} - end - PROVIDERS = { :facebook => { :name => 'Facebook' @@ -58,12 +52,14 @@ class OauthClientPlugin < Noosfero::Plugin setup = lambda { |env| request = Rack::Request.new env strategy = env['omniauth.strategy'] + identifier = request.path.split('/').last domain = Domain.find_by_name(request.host) environment = domain.environment rescue Environment.default - settings = Noosfero::Plugin::Settings.new(environment, OauthClientPlugin) - providers = settings.get_setting(:providers) - strategy.options.merge!(providers[provider][:options].symbolize_keys) + + provider_id = request.session['omniauth.params'] ? request.session['omniauth.params']['id'] : request.params['id'] + provider = environment.oauth_providers.find(provider_id) + strategy.options.merge!(provider.options.symbolize_keys) } provider provider, :setup => setup, diff --git a/plugins/oauth_client/lib/oauth_client_plugin/provider.rb b/plugins/oauth_client/lib/oauth_client_plugin/provider.rb new file mode 100644 index 0000000..a62fbfd --- /dev/null +++ b/plugins/oauth_client/lib/oauth_client_plugin/provider.rb @@ -0,0 +1,19 @@ +class OauthClientPlugin::Provider < Noosfero::Plugin::ActiveRecord + + belongs_to :environment + + validates_presence_of :identifier, :name, :strategy + validates_uniqueness_of :identifier, :scope => :environment_id + + acts_as_having_image + acts_as_having_settings :field => :options + + settings_items :client_id, :type => :string + settings_items :client_secret, :type => :string + settings_items :client_options, :type => Hash + + attr_accessible :identifier, :name, :environment, :strategy, :client_id, :client_secret, :enabled, :client_options + + scope :enabled, :conditions => {:enabled => true} + +end diff --git a/plugins/oauth_client/views/auth/_facebook.html.erb b/plugins/oauth_client/views/auth/_facebook.html.erb deleted file mode 100644 index 96a203c..0000000 --- a/plugins/oauth_client/views/auth/_facebook.html.erb +++ /dev/null @@ -1 +0,0 @@ -<%= _('Login with Facebook') %> diff --git a/plugins/oauth_client/views/auth/_google_oauth2.html.erb b/plugins/oauth_client/views/auth/_google_oauth2.html.erb deleted file mode 100644 index 5defae3..0000000 --- a/plugins/oauth_client/views/auth/_google_oauth2.html.erb +++ /dev/null @@ -1 +0,0 @@ -<%= _('Login with Google') %> diff --git a/plugins/oauth_client/views/auth/_noosfero_oauth2.html.erb b/plugins/oauth_client/views/auth/_noosfero_oauth2.html.erb deleted file mode 100644 index 4632c1d..0000000 --- a/plugins/oauth_client/views/auth/_noosfero_oauth2.html.erb +++ /dev/null @@ -1 +0,0 @@ -<%= _('Login with Noosfero') %> diff --git a/plugins/oauth_client/views/auth/_oauth_login.html.erb b/plugins/oauth_client/views/auth/_oauth_login.html.erb index bf33a02..cb523c1 100644 --- a/plugins/oauth_client/views/auth/_oauth_login.html.erb +++ b/plugins/oauth_client/views/auth/_oauth_login.html.erb @@ -1,7 +1,7 @@
- <% providers.each do |provider, options| %> + <% providers.each do |provider| %> - <%= render :partial => "auth/#{provider}", :locals => {:app_id => options['client_id'] } %> + <%= link_to _('Login with %s' % provider.name), "/plugin/oauth_client/#{provider.strategy}?id=#{provider.id}", :class => provider.strategy %> <% 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 index fec602c..758e89b 100644 --- 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 @@ -1,4 +1,4 @@ -<%= options.fields_for :client_options, OpenStruct.new(provider.options[:client_options]) do |c| %> +<%= f.fields_for :client_options, OpenStruct.new(provider.options[:client_options]) do |c| %>
<%= _('Client Url') %> <%= c.text_field :site %> 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 new file mode 100644 index 0000000..0e70a18 --- /dev/null +++ b/plugins/oauth_client/views/oauth_client_plugin_admin/edit.html.erb @@ -0,0 +1,63 @@ +

<%= _('Oauth Client Settings') %>

+

<%= _('Edit Provider') %>

+ +<%= form_for @provider, :url => {:action => 'edit'}, :method => 'post' do |f| %> + +
+ <%= f.check_box :enabled %> + <%= _('Enabled') %> +
+ +
+ + <%= _('Name') %> + + + <%= f.text_field :name %> + +
+ +
+ + <%= _('Identifier') %> + + + <%= f.text_field :identifier %> + +
+ +
+ + <%= _('Strategy') %> + + + <%= f.select :strategy, OauthClientPlugin::PROVIDERS %> + +
+ +
+ + <%= _('Client Id') %> + + + <%= f.text_field :client_id %> + +
+ +
+ + <%= _('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} %> + <% end %> + + <% button_bar do %> + <%= submit_button(:save, _('Save'), :cancel => {:action => 'index'}) %> + <% end %> +<% end %> diff --git a/plugins/oauth_client/views/oauth_client_plugin_admin/index.html.erb b/plugins/oauth_client/views/oauth_client_plugin_admin/index.html.erb index ee82288..ffefdf6 100644 --- a/plugins/oauth_client/views/oauth_client_plugin_admin/index.html.erb +++ b/plugins/oauth_client/views/oauth_client_plugin_admin/index.html.erb @@ -1,50 +1,23 @@

<%= _('Oauth Client Settings') %>

+

<%= _('Providers') %>

+<%= link_to _('New'), {:action => 'new'} %> + + + + + + + -<%= form_for(:settings) do |f| %> -
-

<%= _('Providers') %>

- <%= f.fields_for :providers, OpenStruct.new(@providers) do |p| %> - - <% OauthClientPlugin::PROVIDERS.each do |available_provider, options| %> - <% provider = OpenStruct.new(@providers[available_provider]) %> - <% provider.options ||= {} %> - - <%= p.fields_for available_provider, provider do |a| %> -
-
-

<%= a.check_box :enabled, {:class => 'enable', :checked => provider.enabled=='true'}, true, false %> - <%= options[:name] %>

-
- <%= a.fields_for :options, OpenStruct.new(provider.options) do |o| %> -
-
- <%= _('Client ID') %> - <%= o.text_field :client_id %> -
-
- <%= _('Client Secret') %> - <%= o.text_field :client_secret %> -
- <% if File.exists?(File.join(File.dirname(__FILE__), "_#{available_provider}.html.erb")) %> - <%= render :partial => "#{available_provider}", :locals => {:options => o, :provider => provider} %> - <% end %> -
- <% end %> -
- <% end %> - <% end %> + <% environment.oauth_providers.each do |provider| %> +
+ + + + + <% end %> - - <% button_bar do %> - <%= submit_button(:save, _('Save'), :cancel => {:controller => 'plugins', :action => 'index'}) %> - <% end %> - -<% end %> - - +
<%= _('Name') %><%= _('Identifier') %><%= _('Strategy') %><%= _('Actions') %>
<%= provider.name %><%= provider.identifier %><%= provider.strategy %> + <%= link_to _('Edit'), {:action => 'edit', :id => provider.id} %> + <%= link_to _('Remove'), {:action => 'remove', :id => provider.id} %> +
-- libgit2 0.21.2