[noosfero/noosfero][next] 5 commits: Create support for plugins' hotspots

Rodrigo Souto gitlab at gitlab.com
Thu Apr 23 23:29:51 BRT 2015


Rodrigo Souto pushed to branch next at Noosfero / noosfero


Commits:
57a47d2f by Arthur Del Esposte at 2015-04-24T01:45:04Z
Create support for plugins' hotspots

- - - - -
44266031 by Arthur Del Esposte at 2015-04-24T01:45:05Z
Allow person creation with a identifier different from his user login

- - - - -
be877df5 by Arthur Del Esposte at 2015-04-24T01:45:05Z
Add a LDAP plugin's hotspots

- A Hotspot to change person_data before user creation
- A Hotspot to update the created person by LDAP plugin

- - - - -
a0a33c64 by Arthur Del Esposte at 2015-04-24T01:46:31Z
Change LDAP plugin to return all user attributes from a LDAP connection

- - - - -
b3756e14 by Rodrigo Souto at 2015-04-24T02:29:45Z
Merge branch 'plugins_hotspots_support' into 'next'

Plugins hotspots support

This MR introduce the required code to support plugins extention through hotspots. This is a generic way that must be used to add specific plugins hotspots, as can been seen in FooPlugin.

Also, it is not necessary to declare the hotspot in Noosfero::Plugin class. All you need is implement the method plugin_hotspots returning an array with your plugin's hotspot name and document their use in the main plugin class.

This MR also introduce a new hotspot in LDAP plugin to manipulate the person data before user creation.

This MR substitutes the old MR !507

See merge request !534

- - - - -


7 changed files:

- app/models/user.rb
- lib/noosfero/plugin.rb
- plugins/foo/lib/foo_plugin.rb
- plugins/foo/test/unit/foo_plugin_test.rb
- plugins/ldap/lib/ldap_authentication.rb
- plugins/ldap/lib/ldap_plugin.rb
- test/unit/user_test.rb


Changes:

=====================================
app/models/user.rb
=====================================
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -45,7 +45,7 @@ class User < ActiveRecord::Base
       p = Person.new
 
       p.attributes = user.person_data
-      p.identifier = user.login
+      p.identifier = user.login if p.identifier.blank?
       p.user = user
       p.environment = user.environment
       p.name ||= user.name || user.login


=====================================
lib/noosfero/plugin.rb
=====================================
--- a/lib/noosfero/plugin.rb
+++ b/lib/noosfero/plugin.rb
@@ -8,6 +8,10 @@ class Noosfero::Plugin
     self.context = context
   end
 
+  def environment
+    context.environment if self.context
+  end
+
   class << self
 
     attr_writer :should_load
@@ -35,6 +39,7 @@ class Noosfero::Plugin
       # filters must be loaded after all extensions
       klasses.each do |plugin|
         load_plugin_filters plugin
+        load_plugin_hotspots plugin
       end
     end
 
@@ -108,6 +113,23 @@ class Noosfero::Plugin
       end
     end
 
+    # This is a generic method to extend the hotspots list with plugins
+    # hotspots. This allows plugins to extend other plugins.
+    # To use this, the plugin must define its hotspots inside a module Hotspots.
+    # Its also needed to include Noosfero::Plugin::HotSpot module
+    # in order to dispatch plugins methods.
+    #
+    # Checkout FooPlugin for usage example.
+    def load_plugin_hotspots(plugin)
+      ActionDispatch::Reloader.to_prepare do
+        begin
+          module_name = "#{plugin.name}::Hotspots"
+          Noosfero::Plugin.send(:include, module_name.constantize)
+        rescue NameError
+        end
+      end
+    end
+
     def add_controller_filters(controller_class, plugin, filters)
       unless filters.is_a?(Array)
         filters = [filters]


=====================================
plugins/foo/lib/foo_plugin.rb
=====================================
--- a/plugins/foo/lib/foo_plugin.rb
+++ b/plugins/foo/lib/foo_plugin.rb
@@ -1,4 +1,5 @@
 class FooPlugin < Noosfero::Plugin
+  include Noosfero::Plugin::HotSpot
 
   def self.plugin_name
     "Foo"
@@ -8,12 +9,29 @@ class FooPlugin < Noosfero::Plugin
     _("A sample plugin to test autoload craziness.")
   end
 
+  module Hotspots
+    # -> Custom foo plugin hotspot
+    # do something to extend the FooPlugin behaviour
+    # receive params a, b and c
+    # returns = boolean or something else
+    def foo_plugin_my_hotspot(a, b, c)
+    end
+
+    # -> Custom title for foo profiles tab
+    # returns = a string with a custom title
+    def foo_plugin_tab_title
+    end
+  end
+
   def control_panel_buttons
     {:title => 'Foo plugin button', :icon => '', :url => ''}
   end
 
   def profile_tabs
-    {:title => 'Foo plugin tab', :id => 'foo_plugin', :content => lambda {'Foo plugin random content'} }
+    title = plugins.dispatch_first(:foo_plugin_tab_title)
+    title = 'Foo plugin tab' unless title
+
+    {:title => title, :id => 'foo_plugin', :content => lambda {'Foo plugin random content'} }
   end
 
 end


=====================================
plugins/foo/test/unit/foo_plugin_test.rb
=====================================
--- a/plugins/foo/test/unit/foo_plugin_test.rb
+++ b/plugins/foo/test/unit/foo_plugin_test.rb
@@ -4,7 +4,25 @@ class FooPluginTest < ActiveSupport::TestCase
   def test_foo
     FooPlugin::Bar.create!
   end
+
   def test_monkey_patch
     Profile.new.bar
   end
+
+  should "respond to new hotspots" do
+    plugin = FooPlugin.new
+
+    assert plugin.respond_to?(:foo_plugin_my_hotspot)
+    assert plugin.respond_to?(:foo_plugin_tab_title)
+  end
+
+  should "other plugin respond to new hotspots" do
+    class TestPlugin < Noosfero::Plugin
+    end
+
+    plugin = TestPlugin.new
+
+    assert plugin.respond_to?(:foo_plugin_my_hotspot)
+    assert plugin.respond_to?(:foo_plugin_tab_title)
+  end
 end


=====================================
plugins/ldap/lib/ldap_authentication.rb
=====================================
--- a/plugins/ldap/lib/ldap_authentication.rb
+++ b/plugins/ldap/lib/ldap_authentication.rb
@@ -77,18 +77,20 @@ class LdapAuthentication
   end
 
   def get_user_attributes_from_ldap_entry(entry)
-    {
-     :dn => entry.dn,
-     :fullname => LdapAuthentication.get_attr(entry, self.attr_fullname),
-     :mail => LdapAuthentication.get_attr(entry, self.attr_mail),
-    }
+    attributes = entry.instance_values["myhash"]
+
+    attributes[:dn] = entry.dn
+    attributes[:fullname] = LdapAuthentication.get_attr(entry, self.attr_fullname)
+    attributes[:mail] = LdapAuthentication.get_attr(entry, self.attr_mail)
+
+    attributes
   end
 
   # Return the attributes needed for the LDAP search.  It will only
   # include the user attributes if on-the-fly registration is enabled
   def search_attributes
     if onthefly_register?
-      ['dn', self.attr_fullname, self.attr_mail]
+      nil
     else
       ['dn']
     end
@@ -111,6 +113,7 @@ class LdapAuthentication
     end
     login_filter = Net::LDAP::Filter.eq( self.attr_login, login )
     object_filter = Net::LDAP::Filter.eq( "objectClass", "*" )
+
     attrs = {}
 
     search_filter = object_filter & login_filter


=====================================
plugins/ldap/lib/ldap_plugin.rb
=====================================
--- a/plugins/ldap/lib/ldap_plugin.rb
+++ b/plugins/ldap/lib/ldap_plugin.rb
@@ -1,6 +1,7 @@
 require File.dirname(__FILE__) + '/ldap_authentication.rb'
 
 class LdapPlugin < Noosfero::Plugin
+  include Noosfero::Plugin::HotSpot
 
   def self.plugin_name
     "LdapPlugin"
@@ -10,6 +11,25 @@ class LdapPlugin < Noosfero::Plugin
     _("A plugin that add ldap support.")
   end
 
+  module Hotspots
+    # -> Custom ldap plugin hotspot to set profile data before user creation
+    # receive the followings params:
+    # - attrs with ldap received data
+    # - login received by ldap
+    # - params from current context
+    # returns = updated person_data hash
+    def ldap_plugin_set_profile_data(attrs, login, params)
+    end
+
+    # -> Custom ldap plugin hotspot to update user object
+    # receive the followings params:
+    # - user: user object
+    # - attrs with ldap received data
+    # returns = none
+    def ldap_plugin_update_user(user, attrs)
+    end
+  end
+
   def allow_user_registration
     false
   end
@@ -39,13 +59,15 @@ class LdapPlugin < Noosfero::Plugin
         user.name =  attrs[:fullname]
         user.password = password
         user.password_confirmation = password
-        user.person_data = context.params[:profile_data]
+        person_data = plugins.dispatch(:ldap_plugin_set_profile_data, attrs, login, context.params)
+        user.person_data = person_data.blank? ? context.params[:profile_data] : person_data
         user.activated_at = Time.now.utc
         user.activation_code = nil
 
         ldap = LdapAuthentication.new(context.environment.ldap_plugin_attributes)
         begin
-          user = nil unless user.save
+          user = nil unless user.save!
+          plugins.dispatch(:ldap_plugin_update_user, user, attrs)
         rescue
           #User not saved
         end
@@ -54,7 +76,6 @@ class LdapPlugin < Noosfero::Plugin
       end
 
     else
-
       return nil if !user.activated?
 
       begin


=====================================
test/unit/user_test.rb
=====================================
--- a/test/unit/user_test.rb
+++ b/test/unit/user_test.rb
@@ -87,6 +87,14 @@ class UserTest < ActiveSupport::TestCase
     assert_equal person_count + 1, Person.count
   end
 
+  def test_should_create_person_with_identifier_different_from_login
+    user = User.create!(:login => 'new_user', :email => 'new_user at example.com', :password => 'test', :password_confirmation => 'test', :person_data => {:identifier => "new_test"})
+
+    assert Person.exists?(['user_id = ?', user.id])
+
+    assert user.login != user.person.identifier
+  end
+
   def test_login_validation
     u = User.new
     u.valid?
@@ -355,7 +363,7 @@ class UserTest < ActiveSupport::TestCase
     Person.any_instance.stubs(:created_at).returns(DateTime.parse('16-08-2010'))
     expected_hash = {
       'login' => 'x_and_y', 'is_admin' => true, 'since_month' => 8,
-      'chat_enabled' => false, 'since_year' => 2010, 'email_domain' => nil, 
+      'chat_enabled' => false, 'since_year' => 2010, 'email_domain' => nil,
       'amount_of_friends' => 0, 'friends_list' => {}, 'enterprises' => [],
     }
 



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/43a702b4d7caec7a65cbc889f296768b8f1e13a0...b3756e146cf4594195d3f4b73dd2abddb2c7648e
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20150424/47a92b1c/attachment-0001.html>


More information about the Noosfero-dev mailing list