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 @@
<%= _('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} %> + | +