OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Assign autologin session link, and use it to show and delete sessions
Oliver Günther8fee175faf66
diff --git a/app/controllers/my/auto_login_tokens_controller.rb b/app/controllers/my/auto_login_tokens_controller.rbindex c75274c2753..176b4e6bf6a 100644--- a/app/controllers/my/auto_login_tokens_controller.rb+++ b/app/controllers/my/auto_login_tokens_controller.rb@@ -41,6 +41,11 @@ module Mydef destroy@token.destroy+ # Destroy all linked sessions to it+ linked_sessions = Sessions::AutologinSessionLink.where(token_id: @token.id)+ Sessions::UserSession.where(id: linked_sessions.select(:session_id)).delete_all+ linked_sessions.delete_all+flash[:notice] = I18n.t(:notice_successful_delete)redirect_to my_sessions_pathenddiff --git a/app/controllers/my/sessions_controller.rb b/app/controllers/my/sessions_controller.rbindex aa8fe3a1b91..19b6eefd21a 100644--- a/app/controllers/my/sessions_controller.rb+++ b/app/controllers/my/sessions_controller.rb@@ -44,14 +44,15 @@ module Mymenu_item :sessionsdef index- @sessions = ::Sessions::UserSession- .for_user(current_user)- .order(updated_at: :desc)-@autologin_tokens = ::Token::AutoLogin.for_user(current_user).order(expires_on: :asc)+ @unmapped_sessions = ::Sessions::UserSession+ .for_user(current_user)+ .order(updated_at: :desc)+ .where.not(id: ::Sessions::AutologinSessionLink.where(token_id: @autologin_tokens.select(:id)).select(:session_id))+token = cookies[OpenProject::Configuration["autologin_cookie_name"]]if token@current_token = @autologin_tokens.find_by_plaintext_value(token) # rubocop:disable Rails/DynamicFindBydiff --git a/app/services/users/login_service.rb b/app/services/users/login_service.rbindex 3eaae27309d..427f26e30e4 100644--- a/app/services/users/login_service.rb+++ b/app/services/users/login_service.rb@@ -44,12 +44,14 @@ module Usersdef call!autologin_requested = session.delete(:autologin_requested)+ autologin_token_id = session.delete(:autologin_token_id)retain_session_values doreset_session!User.current = userset_autologin_cookie if autologin_requested+ assign_autologin_session(autologin_token_id) if autologin_token_id.present?endsuccessful_login@@ -61,18 +63,25 @@ module Usersreturn unless Setting::Autologin.enabled?# generate a key and set cookie if autologin- expires_on = Setting.autologin.days.from_now.beginning_of_day- token = Token::AutoLogin.create(user:, data: session_identification, expires_on:)+ token = Token::AutoLogin.create(user:, data: token_session_information)cookie_options = {value: token.plain_value,# The autologin expiry is checked on validating the token# but still expire the cookie to avoid unnecessary retries- expires: expires_on,+ expires: token.expires_on,path: OpenProject::Configuration["autologin_cookie_path"],secure: OpenProject::Configuration.https?,httponly: true}cookies[OpenProject::Configuration["autologin_cookie_name"]] = cookie_options++ # Tie the current session to this autologin token already+ assign_autologin_session(token.id)+ end++ def assign_autologin_session(token_id)+ session_id = ::Sessions::UserSession.where(session_id: session.id.private_id).pick(:id)+ ::Sessions::AutologinSessionLink.create(token_id:, session_id:)enddef successful_login@@ -122,6 +131,10 @@ module Users}end+ def token_session_information+ session_identification.merge(session_id: session.id)+ end+def retained_session_valuescontroller.session.to_h.slice *(default_retained_keys + omniauth_provider_keys)end@@ -137,7 +150,7 @@ module Usersenddef default_retained_keys- %w[omniauth_provider user_from_auth_header]+ %w[omniauth_provider user_from_auth_header autologin_token_id]end##diff --git a/app/views/my/sessions/index.html.erb b/app/views/my/sessions/index.html.erbindex 54249157fde..07701af1509 100644--- a/app/views/my/sessions/index.html.erb+++ b/app/views/my/sessions/index.html.erb@@ -39,16 +39,8 @@ See COPYRIGHT and LICENSE files for more details.end%>-<%# Collect a “fingerprint” of each token: user_id + platform + browser %>-<% token_fingerprints = @autologin_tokens.map { |t| [t.user_id, t.data["platform"], t.data["browser"]] } %>--<%# Reject sessions that match any token fingerprint %>-<% rows = @sessions.reject do |s|- token_fingerprints.include?([s.user_id, s.data["platform"], s.data["browser"]])- end + @autologin_tokens %>-<%= render ::Users::Sessions::TableComponent.new(- rows: rows,+ rows: @autologin_tokens + @unmapped_sessions,current_session: session,current_token: @current_token) %>diff --git a/spec/services/users/login_service_spec.rb b/spec/services/users/login_service_spec.rbindex ecfe924490b..fb99349e2ea 100644--- a/spec/services/users/login_service_spec.rb+++ b/spec/services/users/login_service_spec.rb@@ -33,7 +33,8 @@ require "spec_helper"RSpec.describe Users::LoginService, type: :model doshared_let(:input_user) { create(:user) }let(:request) { {} }- let(:session) { {} }+ let(:session) { ActionController::TestSession.new }+ let!(:user_session) { create(:user_session, session_id: session.id.private_id) }let(:browser) doinstance_double(Browser::Safari,name: "Safari",@@ -175,11 +176,12 @@ RSpec.describe Users::LoginService, type: :model dotokens = Token::AutoLogin.where(user: input_user)expect(tokens.count).to eq 1expect(tokens.first.user_id).to eq input_user.id+ expect(tokens.first.expires_on).to be_within(1.minute).of(1.day.from_now)autologin_cookie = cookies[OpenProject::Configuration["autologin_cookie_name"]]expect(autologin_cookie).to be_presentexpect(autologin_cookie[:value]).to be_present- expect(autologin_cookie[:expires]).to eq (Time.zone.today + 1.day).beginning_of_day+ expect(autologin_cookie[:expires]).to be_within(1.minute).of(1.day.from_now)expect(autologin_cookie[:path]).to eq "/"expect(autologin_cookie[:httponly]).to be trueexpect(autologin_cookie[:secure]).to eq Setting.https?