From 9ad23ae1aad9e2e491df879ac500561e107b3edd Mon Sep 17 00:00:00 2001 From: Arthur Del Esposte Date: Thu, 11 Sep 2014 13:20:46 +0000 Subject: [PATCH] Add remote_user plugin --- plugins/remote_user/README.md | 46 ++++++++++++++++++++++++++++++++++++++++++++++ plugins/remote_user/lib/remote_user_plugin.rb | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/remote_user/test/functional/remote_user_plugin_test.rb | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 173 insertions(+), 0 deletions(-) create mode 100644 plugins/remote_user/README.md create mode 100644 plugins/remote_user/lib/remote_user_plugin.rb create mode 100644 plugins/remote_user/test/functional/remote_user_plugin_test.rb diff --git a/plugins/remote_user/README.md b/plugins/remote_user/README.md new file mode 100644 index 0000000..0b74ec7 --- /dev/null +++ b/plugins/remote_user/README.md @@ -0,0 +1,46 @@ +README - Remote User (Remote User Plugin) +================================ + +Remote User is a plugin that allow an alternative authentication using the HTTP_REMOTE_USE + +It must be used with a reliable proxy witch is responsible to send for noosfero the HTTP_REMOTE_USER + +INSTALL +======= + +Enable Plugin +------------- + +Also, you need to enable Remote User Plugin on your Noosfero: + +cd +./script/noosfero-plugins enable remote_user + +Active Plugin +------------- + +As a Noosfero administrator user, go to administrator panel: + +- Click on "Enable/disable plugins" option +- Click on "Remote User Plugin" check-box + +LICENSE +======= + +Copyright (c) The Author developers. + +See Noosfero license. + + +AUTHORS +======= + +Arthur de Moura Del Esposte (arthurmde at gmail.com) +David Carlos (ddavidcarlos1392 at gmail.com) +Gustavo Jaruga (darksshades@gmail.com) +Parley Martins (parley@outlook.com) + +ACKNOWLEDGMENTS +=============== + +The author have been supported by UnB and SPB. diff --git a/plugins/remote_user/lib/remote_user_plugin.rb b/plugins/remote_user/lib/remote_user_plugin.rb new file mode 100644 index 0000000..dde31a2 --- /dev/null +++ b/plugins/remote_user/lib/remote_user_plugin.rb @@ -0,0 +1,55 @@ +class RemoteUserPlugin < Noosfero::Plugin + + def self.plugin_name + "Remote User Plugin" + end + + def self.plugin_description + _("A plugin that add remote user support.") + end + + def application_controller_filters + block = proc do + + begin + remote_user = request.headers["HTTP_REMOTE_USER"] + + if remote_user.nil? + if logged_in? + self.current_user.forget_me + reset_session + end + else + if !logged_in? + self.current_user = User.find_by_login(remote_user) + unless self.current_user + self.current_user = User.create!(:login => remote_user, :email => (remote_user + '@remote.user'), :password => ('pw4'+remote_user), :password_confirmation => ('pw4'+remote_user)) + end + self.current_user.save! + else + if remote_user != self.current_user.login + self.current_user.forget_me + reset_session + + self.current_user = User.find_by_login(remote_user) + unless self.current_user + self.current_user = User.create!(:login => remote_user, :email => (remote_user + '@remote.user'), :password => ('pw4'+remote_user), :password_confirmation => ('pw4'+remote_user)) + end + self.current_user.save! + end + end + end + rescue ActiveRecord::RecordInvalid => invalid + session[:notice] = _('Could not create the remote_user.') + render_404 + end + end + + [{ + :type => "before_filter", + :method_name => "remote_user_authentication", + :options => { }, + :block => block + }] + end +end diff --git a/plugins/remote_user/test/functional/remote_user_plugin_test.rb b/plugins/remote_user/test/functional/remote_user_plugin_test.rb new file mode 100644 index 0000000..07e1a95 --- /dev/null +++ b/plugins/remote_user/test/functional/remote_user_plugin_test.rb @@ -0,0 +1,72 @@ +require File.dirname(__FILE__) + '/../../../../test/test_helper' + +# Re-raise errors caught by the controller. +class AccountController; def rescue_action(e) raise e end; end + +class AccountControllerTest < ActionController::TestCase + def setup + @environment = Environment.default + @environment.enabled_plugins = ['RemoteUserPlugin'] + @environment.save + + @controller = AccountController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + end + + should 'not authenticate user if there is no remote user' do + get :index + assert_nil session[:user] + end + + should 'authenticate user if its a valid remote user' do + user = create_user('testuser', :email => 'testuser@example.com', :password => 'test', :password_confirmation => 'test') + user.activate + @request.env["HTTP_REMOTE_USER"] = user.login + get :index + assert session[:user] + end + + should 'authenticate another user if the remote user doesnt belong to the current user' do + user1 = create_user('testuser', :email => 'testuser@example.com', :password => 'test', :password_confirmation => 'test') + user1.activate + user2 = create_user('anotheruser', :email => 'anotheruser@example.com', :password => 'test', :password_confirmation => 'test') + user2.activate + + login_as user1.login + assert_equal user1.id, session[:user] + + @request.env["HTTP_REMOTE_USER"] = user2.login + get :index + + assert_equal user2.id, session[:user] + end + + should 'create a new user if the remote user does not exist' do + User.destroy_all + + assert_equal 0, User.count + + @request.env["HTTP_REMOTE_USER"] = "testuser" + get :index + + assert_equal 1, User.count + assert_equal "testuser", User.last.login + assert_equal User.last.id, session[:user] + end + + should 'create a new user even if there is a logged user but the remote user is different' do + user = create_user('testuser', :email => 'testuser@example.com', :password => 'test', :password_confirmation => 'test') + user.activate + + login_as user.login + + + @request.env["HTTP_REMOTE_USER"] = 'another_user' + get :index + + assert_equal 2, User.count + assert_equal "another_user", User.last.login + assert_equal User.last.id, session[:user] + end +end -- libgit2 0.21.2