Commit 23837332e9b20780a87e40f67d967ba5889c8c26

Authored by Victor Costa
1 parent b5019f2e

oauth_client: refactoring to allow login with multiple noosfero providers

plugins/oauth_client/controllers/oauth_client_plugin_admin_controller.rb
1 1 class OauthClientPluginAdminController < AdminController
2 2  
3 3 def index
4   - settings = params[:settings] || {}
  4 + end
  5 +
  6 + def new
  7 + @provider = environment.oauth_providers.new
  8 + render :file => 'oauth_client_plugin_admin/edit'
  9 + end
  10 +
  11 + def remove
  12 + environment.oauth_providers.find(params[:id]).destroy
  13 + redirect_to :action => 'index'
  14 + end
5 15  
6   - @settings = Noosfero::Plugin::Settings.new(environment, OauthClientPlugin, settings)
7   - @providers = @settings.get_setting(:providers) || {}
  16 + def edit
  17 + @provider = params[:id] ? environment.oauth_providers.find(params[:id]) : environment.oauth_providers.new
8 18 if request.post?
9   - @settings.save!
10   - session[:notice] = 'Settings succefully saved.'
11   - redirect_to :action => 'index'
  19 + if @provider.update_attributes(params['oauth_client_plugin_provider'])
  20 + session[:notice] = _('Saved!')
  21 + else
  22 + session[:notice] = _('Error!')
  23 + end
12 24 end
13 25 end
14 26  
... ...
plugins/oauth_client/db/migrate/20141010135314_create_oauth_client_plugin_provider.rb 0 → 100644
... ... @@ -0,0 +1,20 @@
  1 +class CreateOauthClientPluginProvider < ActiveRecord::Migration
  2 +
  3 + def self.up
  4 + create_table :oauth_client_plugin_providers do |t|
  5 + t.integer :environment_id
  6 + t.string :strategy
  7 + t.string :identifier
  8 + t.string :name
  9 + t.text :options
  10 + t.boolean :enabled
  11 + t.integer :image_id
  12 +
  13 + t.timestamps
  14 + end
  15 + end
  16 +
  17 + def self.down
  18 + drop_table :oauth_client_plugin_providers
  19 + end
  20 +end
... ...
plugins/oauth_client/lib/ext/environment.rb 0 → 100644
... ... @@ -0,0 +1,7 @@
  1 +require_dependency 'environment'
  2 +
  3 +class Environment
  4 +
  5 + has_many :oauth_providers, :class_name => 'OauthClientPlugin::Provider'
  6 +
  7 +end
... ...
plugins/oauth_client/lib/ext/user.rb
... ... @@ -8,7 +8,7 @@ class User
8 8  
9 9 def self.find_with_omniauth(auth)
10 10 user = self.find_by_email(auth.info.email)
11   - if user && !user.oauth_providers.empty? #FIXME save new oauth providers
  11 + if user# && !user.oauth_providers.empty? #FIXME save new oauth providers
12 12 user
13 13 else
14 14 nil
... ...
plugins/oauth_client/lib/oauth_client_plugin.rb
... ... @@ -13,7 +13,7 @@ class OauthClientPlugin &lt; Noosfero::Plugin
13 13 def login_extra_contents
14 14 plugin = self
15 15 proc do
16   - render :partial => 'auth/oauth_login', :locals => {:providers => plugin.enabled_providers}
  16 + render :partial => 'auth/oauth_login', :locals => {:providers => environment.oauth_providers.enabled}
17 17 end
18 18 end
19 19  
... ... @@ -29,12 +29,6 @@ class OauthClientPlugin &lt; Noosfero::Plugin
29 29 end
30 30 end
31 31  
32   - def enabled_providers
33   - settings = Noosfero::Plugin::Settings.new(context.environment, OauthClientPlugin)
34   - providers = settings.get_setting(:providers)
35   - providers.select {|provider, options| options[:enabled]}
36   - end
37   -
38 32 PROVIDERS = {
39 33 :facebook => {
40 34 :name => 'Facebook'
... ... @@ -58,12 +52,14 @@ class OauthClientPlugin &lt; Noosfero::Plugin
58 52 setup = lambda { |env|
59 53 request = Rack::Request.new env
60 54 strategy = env['omniauth.strategy']
  55 + identifier = request.path.split('/').last
61 56  
62 57 domain = Domain.find_by_name(request.host)
63 58 environment = domain.environment rescue Environment.default
64   - settings = Noosfero::Plugin::Settings.new(environment, OauthClientPlugin)
65   - providers = settings.get_setting(:providers)
66   - strategy.options.merge!(providers[provider][:options].symbolize_keys)
  59 +
  60 + provider_id = request.session['omniauth.params'] ? request.session['omniauth.params']['id'] : request.params['id']
  61 + provider = environment.oauth_providers.find(provider_id)
  62 + strategy.options.merge!(provider.options.symbolize_keys)
67 63 }
68 64  
69 65 provider provider, :setup => setup,
... ...
plugins/oauth_client/lib/oauth_client_plugin/provider.rb 0 → 100644
... ... @@ -0,0 +1,19 @@
  1 +class OauthClientPlugin::Provider < Noosfero::Plugin::ActiveRecord
  2 +
  3 + belongs_to :environment
  4 +
  5 + validates_presence_of :identifier, :name, :strategy
  6 + validates_uniqueness_of :identifier, :scope => :environment_id
  7 +
  8 + acts_as_having_image
  9 + acts_as_having_settings :field => :options
  10 +
  11 + settings_items :client_id, :type => :string
  12 + settings_items :client_secret, :type => :string
  13 + settings_items :client_options, :type => Hash
  14 +
  15 + attr_accessible :identifier, :name, :environment, :strategy, :client_id, :client_secret, :enabled, :client_options
  16 +
  17 + scope :enabled, :conditions => {:enabled => true}
  18 +
  19 +end
... ...
plugins/oauth_client/views/auth/_facebook.html.erb
... ... @@ -1 +0,0 @@
1   -<a class="facebook" href="/plugin/oauth_client/facebook"><%= _('Login with Facebook') %></a>
plugins/oauth_client/views/auth/_google_oauth2.html.erb
... ... @@ -1 +0,0 @@
1   -<a class="google_oauth2" href="/plugin/oauth_client/google_oauth2"><%= _('Login with Google') %></a>
plugins/oauth_client/views/auth/_noosfero_oauth2.html.erb
... ... @@ -1 +0,0 @@
1   -<a class="noosfero_oauth2" href="/plugin/oauth_client/noosfero_oauth2"><%= _('Login with Noosfero') %></a>
plugins/oauth_client/views/auth/_oauth_login.html.erb
1 1 <div class="oauth-login">
2   - <% providers.each do |provider, options| %>
  2 + <% providers.each do |provider| %>
3 3 <span class="provider">
4   - <%= render :partial => "auth/#{provider}", :locals => {:app_id => options['client_id'] } %>
  4 + <%= link_to _('Login with %s' % provider.name), "/plugin/oauth_client/#{provider.strategy}?id=#{provider.id}", :class => provider.strategy %>
5 5 </span>
6 6 <% end %>
7 7  
... ...
plugins/oauth_client/views/oauth_client_plugin_admin/_noosfero_oauth2.html.erb
1   -<%= options.fields_for :client_options, OpenStruct.new(provider.options[:client_options]) do |c| %>
  1 +<%= f.fields_for :client_options, OpenStruct.new(provider.options[:client_options]) do |c| %>
2 2 <div class="client-url">
3 3 <span class="label"><%= _('Client Url') %></span>
4 4 <span class="value"><%= c.text_field :site %></span>
... ...
plugins/oauth_client/views/oauth_client_plugin_admin/edit.html.erb 0 → 100644
... ... @@ -0,0 +1,63 @@
  1 +<h1><%= _('Oauth Client Settings') %></h1>
  2 +<h3><%= _('Edit Provider') %></h3>
  3 +
  4 +<%= form_for @provider, :url => {:action => 'edit'}, :method => 'post' do |f| %>
  5 +
  6 + <div class="enabled">
  7 + <%= f.check_box :enabled %>
  8 + <%= _('Enabled') %>
  9 + </div>
  10 +
  11 + <div class="name">
  12 + <span class="label">
  13 + <%= _('Name') %>
  14 + </span>
  15 + <span class="value">
  16 + <%= f.text_field :name %>
  17 + </span>
  18 + </div>
  19 +
  20 + <div class="identifier">
  21 + <span class="label">
  22 + <%= _('Identifier') %>
  23 + </span>
  24 + <span class="value">
  25 + <%= f.text_field :identifier %>
  26 + </span>
  27 + </div>
  28 +
  29 + <div class="strategy">
  30 + <span class="label">
  31 + <%= _('Strategy') %>
  32 + </span>
  33 + <span class="value">
  34 + <%= f.select :strategy, OauthClientPlugin::PROVIDERS %>
  35 + </span>
  36 + </div>
  37 +
  38 + <div class="client-id">
  39 + <span class="label">
  40 + <%= _('Client Id') %>
  41 + </span>
  42 + <span class="value">
  43 + <%= f.text_field :client_id %>
  44 + </span>
  45 + </div>
  46 +
  47 + <div class="client-secret">
  48 + <span class="label">
  49 + <%= _('Client Secret') %>
  50 + </span>
  51 + <span class="value">
  52 + <%= f.text_field :client_secret %>
  53 + </span>
  54 + </div>
  55 +
  56 + <% if File.exists?(File.join(File.dirname(__FILE__), "_#{@provider.strategy}.html.erb")) %>
  57 + <%= render :partial => "#{@provider.strategy}", :locals => {:f => f, :provider => @provider} %>
  58 + <% end %>
  59 +
  60 + <% button_bar do %>
  61 + <%= submit_button(:save, _('Save'), :cancel => {:action => 'index'}) %>
  62 + <% end %>
  63 +<% end %>
... ...
plugins/oauth_client/views/oauth_client_plugin_admin/index.html.erb
1 1 <h1><%= _('Oauth Client Settings') %></h1>
  2 +<h3><%= _('Providers') %></h3>
  3 +<%= link_to _('New'), {:action => 'new'} %>
  4 +<table>
  5 + <tr>
  6 + <th><%= _('Name') %></th>
  7 + <th><%= _('Identifier') %></th>
  8 + <th><%= _('Strategy') %></th>
  9 + <th><%= _('Actions') %></th>
  10 + </tr>
2 11  
3   -<%= form_for(:settings) do |f| %>
4   - <div class="providers">
5   - <h3><%= _('Providers') %></h3>
6   - <%= f.fields_for :providers, OpenStruct.new(@providers) do |p| %>
7   -
8   - <% OauthClientPlugin::PROVIDERS.each do |available_provider, options| %>
9   - <% provider = OpenStruct.new(@providers[available_provider]) %>
10   - <% provider.options ||= {} %>
11   -
12   - <%= p.fields_for available_provider, provider do |a| %>
13   - <div class="provider">
14   - <div class="name">
15   - <h4><%= a.check_box :enabled, {:class => 'enable', :checked => provider.enabled=='true'}, true, false %>
16   - <%= options[:name] %></h4>
17   - </div>
18   - <%= a.fields_for :options, OpenStruct.new(provider.options) do |o| %>
19   - <div class="options" style="<%= provider.enabled=='true' ? '':'display:none' %>">
20   - <div class="client-id">
21   - <span class="label"><%= _('Client ID') %></span>
22   - <span class="value"><%= o.text_field :client_id %></span>
23   - </div>
24   - <div class="client-secret">
25   - <span class="label"><%= _('Client Secret') %></span>
26   - <span class="value"><%= o.text_field :client_secret %></span>
27   - </div>
28   - <% if File.exists?(File.join(File.dirname(__FILE__), "_#{available_provider}.html.erb")) %>
29   - <%= render :partial => "#{available_provider}", :locals => {:options => o, :provider => provider} %>
30   - <% end %>
31   - </div>
32   - <% end %>
33   - </div>
34   - <% end %>
35   - <% end %>
  12 + <% environment.oauth_providers.each do |provider| %>
  13 + <tr>
  14 + <td><%= provider.name %></td>
  15 + <td><%= provider.identifier %></td>
  16 + <td><%= provider.strategy %></td>
  17 + <td>
  18 + <%= link_to _('Edit'), {:action => 'edit', :id => provider.id} %>
  19 + <%= link_to _('Remove'), {:action => 'remove', :id => provider.id} %>
  20 + </td>
  21 + </tr>
36 22 <% end %>
37   -
38   - <% button_bar do %>
39   - <%= submit_button(:save, _('Save'), :cancel => {:controller => 'plugins', :action => 'index'}) %>
40   - <% end %>
41   - </div>
42   -<% end %>
43   -
44   -<script>
45   - jQuery(document).ready(function($) {
46   - $('.providers .provider .enable').on('click', function() {
47   - $(this).parents('.provider').find('.options').toggle('fast');
48   - });
49   - });
50   -</script>
  23 +</table>
... ...