From b24e60efb22bd3dbf57a79bed2cc9a97162e25ad Mon Sep 17 00:00:00 2001 From: Braulio Bhavamitra Date: Fri, 26 Jun 2015 11:06:13 -0300 Subject: [PATCH] Associate current session with the user model --- app/controllers/public/account_controller.rb | 5 +++-- app/models/user.rb | 2 ++ lib/authenticated_system.rb | 7 ++++++- test/functional/account_controller_test.rb | 5 +++++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/app/controllers/public/account_controller.rb b/app/controllers/public/account_controller.rb index 9b95673..ce5cad1 100644 --- a/app/controllers/public/account_controller.rb +++ b/app/controllers/public/account_controller.rb @@ -16,7 +16,7 @@ class AccountController < ApplicationController def activate @user = User.find_by_activation_code(params[:activation_code]) if params[:activation_code] if @user - unless @user.environment.enabled?('admin_must_approve_new_users') + unless @user.environment.enabled?('admin_must_approve_new_users') if @user.activate @message = _("Your account has been activated, now you can log in!") check_redirection @@ -30,7 +30,7 @@ class AccountController < ApplicationController @user.activation_code = nil @user.save! redirect_to :controller => :home - end + end end else session[:notice] = _("It looks like you're trying to activate an account. Perhaps have already activated this account?") @@ -94,6 +94,7 @@ class AccountController < ApplicationController @invitation_code = params[:invitation_code] begin @user = User.new(params[:user]) + @user.session = session @user.terms_of_use = environment.terms_of_use @user.environment = environment @terms_of_use = environment.terms_of_use diff --git a/app/models/user.rb b/app/models/user.rb index c205438..e613513 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -97,6 +97,8 @@ class User < ActiveRecord::Base belongs_to :environment has_many :sessions, dependent: :destroy + # holds the current session, see lib/authenticated_system.rb + attr_accessor :session attr_protected :activated_at diff --git a/lib/authenticated_system.rb b/lib/authenticated_system.rb index 3c5d091..71d21e2 100644 --- a/lib/authenticated_system.rb +++ b/lib/authenticated_system.rb @@ -24,7 +24,11 @@ module AuthenticatedSystem # Accesses the current user from the session. def current_user @current_user ||= begin - User.current = (session[:user] && User.find_by_id(session[:user])) || nil + id = session[:user] + user = User.where(id: id).first if id + user.session = session if user + User.current = user + user end end @@ -34,6 +38,7 @@ module AuthenticatedSystem session.delete(:user) else session[:user] = new_user.id + new_user.session = session new_user.register_login end @current_user = User.current = new_user diff --git a/test/functional/account_controller_test.rb b/test/functional/account_controller_test.rb index b07883c..ed92a77 100644 --- a/test/functional/account_controller_test.rb +++ b/test/functional/account_controller_test.rb @@ -623,6 +623,11 @@ class AccountControllerTest < ActionController::TestCase end end + should 'fill session for new users' do + post :signup, :user => { :login => 'testuser', :password => '123456', :password_confirmation => '123456', :email => 'testuser@example.com' }, :profile_data => { :organization => 'example.com' } + assert_equal assigns(:user).session, session + end + should 'signup filling in mandatory person fields' do Person.any_instance.stubs(:required_fields).returns(['organization']) assert_difference 'User.count' do -- libgit2 0.21.2