diff --git a/plugins/remote_user/lib/remote_user_plugin.rb b/plugins/remote_user/lib/remote_user_plugin.rb index c8a0cf2..79536de 100644 --- a/plugins/remote_user/lib/remote_user_plugin.rb +++ b/plugins/remote_user/lib/remote_user_plugin.rb @@ -13,6 +13,7 @@ class RemoteUserPlugin < Noosfero::Plugin begin remote_user = request.headers["HTTP_REMOTE_USER"] + user_data = request.env['HTTP_REMOTE_USER_DATA'] if remote_user.blank? if logged_in? @@ -20,10 +21,19 @@ class RemoteUserPlugin < Noosfero::Plugin reset_session end else + if user_data.blank? + remote_user_email = remote_user + '@remote.user' + remote_user_name = remote_user + else + user_data = JSON.parse(user_data) + remote_user_email = user_data['email'] + remote_user_name = user_data['name'] + end + 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)) + self.current_user = User.create!(:login => remote_user, :email => remote_user_email, :name => remote_user_name, :password => ('pw4'+remote_user), :password_confirmation => ('pw4'+remote_user)) self.current_user.activate end self.current_user.save! @@ -34,7 +44,7 @@ class RemoteUserPlugin < Noosfero::Plugin 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)) + self.current_user = User.create!(:login => remote_user, :email => remote_user_email, :name => remote_user_name, :password => ('pw4'+remote_user), :password_confirmation => ('pw4'+remote_user)) self.current_user.activate end self.current_user.save! diff --git a/plugins/remote_user/test/functional/remote_user_plugin_test.rb b/plugins/remote_user/test/functional/remote_user_plugin_test.rb index 13f25ef..e054cc2 100644 --- a/plugins/remote_user/test/functional/remote_user_plugin_test.rb +++ b/plugins/remote_user/test/functional/remote_user_plugin_test.rb @@ -42,26 +42,64 @@ class AccountControllerTest < ActionController::TestCase assert_equal user2.id, session[:user] end - should 'create a new user if the remote user does not exist' do + should 'create a new user with remote_user_data if the remote user does not exist' do User.destroy_all assert_equal 0, User.count @request.env["HTTP_REMOTE_USER"] = "testuser" + @request.env["CONTENT_TYPE"] = "application/json" + @request.env["HTTP_REMOTE_USER_DATA"] = '{"email":"testuser@domain.com", "name":"Test User"}' get :index assert_equal 1, User.count assert_equal "testuser", User.last.login assert_equal true, User.last.activated? assert_equal User.last.id, session[:user] + assert_equal "Test User", User.last.name + assert_equal "testuser@domain.com", User.last.email end - should 'create a new user even if there is a logged user but the remote user is different' do + should 'create a new user with remote_user_data 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' + @request.env["CONTENT_TYPE"] = "application/json" + @request.env["HTTP_REMOTE_USER_DATA"] = '{"email":"another_user@domain.com", "name":"Another User"}' + get :index + + assert_equal 2, User.count + assert_equal "another_user", User.last.login + assert_equal true, User.last.activated? + assert_equal User.last.id, session[:user] + assert_equal "Another User", User.last.name + assert_equal "another_user@domain.com", User.last.email + end + + should 'create a new user without remote_user_data 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 true, User.last.activated? + assert_equal User.last.id, session[:user] + assert_equal "testuser", User.last.name + assert_equal "testuser@remote.user", User.last.email + end + + should 'create a new user without remote_user_data 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 @@ -70,6 +108,8 @@ class AccountControllerTest < ActionController::TestCase assert_equal "another_user", User.last.login assert_equal true, User.last.activated? assert_equal User.last.id, session[:user] + assert_equal "another_user", User.last.name + assert_equal "another_user@remote.user", User.last.email end should 'logout if there is a current logged user but not a remote user' do -- libgit2 0.21.2