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
Show autologin tokens in the my/sessions view
Oliver Günther7bfec434a437
diff --git a/app/components/table_component.html.erb b/app/components/table_component.html.erbindex ebaf4f905a2..26821fd827f 100644--- a/app/components/table_component.html.erb+++ b/app/components/table_component.html.erb@@ -27,7 +27,7 @@ See COPYRIGHT and LICENSE files for more details.++#%>-<div class="generic-table--container">+<div class="generic-table--container" data-test-selector="<%= test_selector %>"><div class="generic-table--results-container"><table class="generic-table"><colgroup>diff --git a/app/components/table_component.rb b/app/components/table_component.rbindex 09174529be9..c20972668d9 100644--- a/app/components/table_component.rb+++ b/app/components/table_component.rb@@ -145,6 +145,10 @@ class TableComponent < ApplicationComponent.per_page(helpers.per_page_param)end+ def test_selector+ self.class.name.dasherize+ end+def rowsmodelenddiff --git a/app/components/users/auto_login_tokens/row_component.rb b/app/components/users/auto_login_tokens/row_component.rbnew file mode 100644index 00000000000..fad98299ed7--- /dev/null+++ b/app/components/users/auto_login_tokens/row_component.rb@@ -0,0 +1,92 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++module Users+ module AutoLoginTokens+ class RowComponent < ::RowComponent+ delegate :current_token, to: :table++ def token+ model+ end++ def token_data+ token.data+ end++ def current?+ token == current_token+ end++ def is_current # rubocop:disable Naming/PredicateName+ if current?+ helpers.op_icon 'icon-yes'+ end+ end++ def device+ token_data[:platform] || I18n.t('users.sessions.unknown_os')+ end++ def browser+ name = token_data[:browser] || 'unknown browser'+ version = token_data[:browser_version]+ "#{name} #{version ? "(Version #{version})" : ''}"+ end++ def platform+ token_data[:platform] || 'unknown platform'+ end++ def expires_on+ expires = token.expires_on || (token.created_at + Setting.autologin.days)+ helpers.format_date(expires)+ end++ def button_links+ [delete_link].compact+ end++ def delete_link+ return if current?++ link_to(+ helpers.op_icon('icon icon-delete'),+ { controller: '/my/auto_login_tokens', action: 'destroy', id: token.id },+ class: 'button--link',+ role: :button,+ method: :delete,+ data: { confirm: I18n.t(:text_are_you_sure), disable_with: I18n.t(:label_loading) },+ title: I18n.t(:button_delete)+ )+ end+ end+ end+enddiff --git a/app/components/users/auto_login_tokens/table_component.rb b/app/components/users/auto_login_tokens/table_component.rbnew file mode 100644index 00000000000..9492bae3b9d--- /dev/null+++ b/app/components/users/auto_login_tokens/table_component.rb@@ -0,0 +1,52 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++module Users+ module AutoLoginTokens+ class TableComponent < ::TableComponent+ columns :is_current, :browser, :device, :expires_on+ sortable_columns :updated_at+ options :current_token++ def sortable?+ false+ end++ def headers+ [+ [:is_current, { caption: I18n.t('users.sessions.current') }],+ [:browser, { caption: I18n.t('users.sessions.browser') }],+ [:device, { caption: I18n.t('users.sessions.device') }],+ [:expires_on, { caption: I18n.t('attributes.expires_at') }]+ ]+ end+ end+ end+enddiff --git a/app/controllers/my/auto_login_tokens_controller.rb b/app/controllers/my/auto_login_tokens_controller.rbnew file mode 100644index 00000000000..05801209019--- /dev/null+++ b/app/controllers/my/auto_login_tokens_controller.rb@@ -0,0 +1,23 @@+module My+ class AutoLoginTokensController < ::ApplicationController+ before_action :find_token, only: %i(destroy)++ layout 'my'+ menu_item :sessions++ def destroy+ @token.destroy++ flash[:notice] = I18n.t(:notice_successful_delete)+ redirect_to my_sessions_path+ end++ private++ def find_token+ @token = Token::AutoLogin+ .for_user(current_user)+ .find(params[:id])+ end+ end+enddiff --git a/app/controllers/my/sessions_controller.rb b/app/controllers/my/sessions_controller.rbindex a76b07b0ba5..7d244f9549b 100644--- a/app/controllers/my/sessions_controller.rb+++ b/app/controllers/my/sessions_controller.rb@@ -13,6 +13,15 @@ module My@sessions = ::Sessions::UserSession.for_user(current_user).order(updated_at: :desc)++ @autologin_tokens = ::Token::AutoLogin+ .for_user(current_user)+ .order(expires_on: :asc)++ token = cookies[OpenProject::Configuration['autologin_cookie_name']]+ if token+ @current_token = @autologin_tokens.find_by_plaintext_value(token) # rubocop:disable Rails/DynamicFindBy+ endenddef show; enddiff --git a/app/models/token/base.rb b/app/models/token/base.rbindex 345ddf853e7..43347a466d7 100644--- a/app/models/token/base.rb+++ b/app/models/token/base.rb@@ -19,6 +19,7 @@module Tokenclass Base < ApplicationRecordself.table_name = 'tokens'+ serialize :data, ::Serializers::IndifferentHashSerializer# Hashed tokens belong to a user and are unique per typebelongs_to :user@@ -39,6 +40,12 @@ module Tokenfind_by(value: input)end+ ##+ # Find tokens for the given user+ def self.for_user(user)+ where(user:)+ end+### Generate a random hex token valuedef self.generate_token_valuediff --git a/app/services/users/login_service.rb b/app/services/users/login_service.rbindex 94bf19c2f66..17c753bf119 100644--- a/app/services/users/login_service.rb+++ b/app/services/users/login_service.rb@@ -59,12 +59,13 @@ module Usersreturn unless Setting::Autologin.enabled?# generate a key and set cookie if autologin- token = Token::AutoLogin.create(user:, data: session_identification)+ expires_on = Setting.autologin.days.from_now.beginning_of_day+ token = Token::AutoLogin.create(user:, data: session_identification, expires_on:)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: Setting.autologin.days.from_now.beginning_of_day,+ expires: expires_on,path: OpenProject::Configuration['autologin_cookie_path'],secure: OpenProject::Configuration.https?,httponly: truediff --git a/app/views/my/sessions/index.html.erb b/app/views/my/sessions/index.html.erbindex 87e6d462081..daed8cd7034 100644--- a/app/views/my/sessions/index.html.erb+++ b/app/views/my/sessions/index.html.erb@@ -34,9 +34,9 @@ See COPYRIGHT and LICENSE files for more details.<%= render ::Users::Sessions::TableComponent.new(rows: @sessions, current_session: session) %>-<h2></h2>+<% if Setting::Autologin.enabled? && @autologin_tokens.any? %>+ <h3><%= t('users.sessions.remembered_devices') %></h3>+ <p><%= t('users.sessions.remembered_devices_caption') %></p>-<% ::Token::AutoLogin.where(user: User.current).find_each do |token| %>- <%= token.data.inspect %>- <br/>+ <%= render ::Users::AutoLoginTokens::TableComponent.new(rows: @autologin_tokens, current_token: @current_token) %><% end %>diff --git a/config/locales/en.yml b/config/locales/en.ymlindex 5ec2ee16e12..059329d8c4b 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -323,8 +323,9 @@ en:users:autologins:prompt: "Stay logged in for %{num_days}"- title: "Remembered devices"sessions:+ remembered_devices: "Remembered devices"+ remembered_devices_caption: "A list of all devices that logged into this account using the 'Stay logged in' option."session_name: "%{browser_name} %{browser_version} on %{os_name}"browser: "Browser"device: "Device / OS"@@ -555,7 +556,6 @@ en:value: "Value"enterprise_token:starts_at: "Valid since"- expires_at: "Expires at"subscriber: "Subscriber"encoded_token: "Enterprise support token"active_user_count_restriction: "Maximum active users"@@ -1099,6 +1099,7 @@ en:due_date: "Finish date"estimated_hours: "Estimated time"estimated_time: "Estimated time"+ expires_at: "Expires at"firstname: "First name"group: "Group"groups: "Groups"diff --git a/config/routes.rb b/config/routes.rbindex a84dc7af56f..cc3dc78bbc2 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -572,6 +572,7 @@ OpenProject::Application.routes.draw dodelete '/storage_token/:id' => 'my#delete_storage_token', as: 'storage_token_delete'resources :sessions, controller: 'my/sessions', as: 'my_sessions', only: %i[index show destroy]+ resources :auto_login_tokens, controller: 'my/auto_login_tokens', as: 'my_auto_login_tokens', only: %i[destroy]endscope controller: 'my' dodiff --git a/spec/controllers/concerns/auth_source_sso_spec.rb b/spec/controllers/concerns/auth_source_sso_spec.rbindex ee768323f3f..37306f900de 100644--- a/spec/controllers/concerns/auth_source_sso_spec.rb+++ b/spec/controllers/concerns/auth_source_sso_spec.rb@@ -209,11 +209,11 @@ RSpec.describe MyController,it 'logs out the user and logs it in again' doallow(Users::LogoutService).to receive(:new).and_return(service)- allow(service).to receive(:call).with(other_user).and_call_original+ allow(service).to receive(:call!).with(other_user).and_call_originalget :account- expect(service).to have_received(:call).with(other_user)+ expect(service).to have_received(:call!).with(other_user)expect(response).to redirect_to my_account_pathexpect(user.reload.last_login_on).to be_within(10.seconds).of(Time.current)expect(session[:user_id]).to eq user.iddiff --git a/spec/features/my/autologin_tokens_management_spec.rb b/spec/features/my/autologin_tokens_management_spec.rbnew file mode 100644index 00000000000..21eabdb6aa2--- /dev/null+++ b/spec/features/my/autologin_tokens_management_spec.rb@@ -0,0 +1,83 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++require 'spec_helper'++RSpec.describe 'My account autologin tokens management', :js, :with_cuprite do+ include Redmine::I18n+ shared_let(:user) { create(:user) }+ shared_let(:old_token) { create(:autologin_token, user:, created_at: 1.year.ago) }+ shared_let(:new_token) do+ create(:autologin_token,+ user:,+ data: { browser: 'Mozilla Firefox', browser_version: '12.3', platform: 'Linux' })+ end++ before do+ login_as user+ visit my_sessions_path+ end++ context 'with autologin disabled', with_settings: { autologin: 0 } do+ it 'does not show tokens' do+ expect(page).not_to have_text "Remembered devices"+ expect(page).not_to have_test_selector('Users::AutoLoginTokens::TableComponent')+ end+ end++ context 'with autologin enabled', with_settings: { autologin: 1 } do+ it 'can list and terminate sessions' do+ page.within_test_selector('Users::AutoLoginTokens::TableComponent') do+ expect(page).to have_css('.generic-table tbody tr', count: 2)+ trs = page.all('.generic-table tbody tr')+ expect(trs[0]).to have_text('unknown browser')+ expect(trs[0]).to have_text('unknown operating system')+ expect(trs[0]).to have_css('.buttons a')+ expect(trs[0]).not_to have_css('.icon-yes')+ expect(trs[0]).to have_text format_date(1.year.ago + 1.day)++ expect(trs[1]).to have_text('Mozilla Firefox (Version 12.3)')+ expect(trs[1]).to have_text('Linux')+ expect(trs[1]).not_to have_css('.icon-yes')+ expect(trs[1]).to have_css('.buttons a')+ expect(trs[1]).to have_text format_date(1.day.from_now)++ accept_confirm do+ trs[1].find('.buttons a').click+ end+ end++ expect(page).to have_text 'Successful deletion'+ page.within_test_selector('Users::AutoLoginTokens::TableComponent') do+ expect(page).to have_css('.generic-table tbody tr', count: 1)+ end++ expect { new_token.reload }.to raise_error(ActiveRecord::RecordNotFound)+ end+ end+enddiff --git a/spec/features/my/sessions_management_spec.rb b/spec/features/my/sessions_management_spec.rbindex c54722b12f5..1fcb46fc351 100644--- a/spec/features/my/sessions_management_spec.rb+++ b/spec/features/my/sessions_management_spec.rb@@ -50,23 +50,28 @@ RSpec.describe 'My account session management', js: true doit 'can list and terminate sessions' doclick_on 'Session management'- expect(page).to have_selector('.generic-table tbody tr', count: 2)- trs = page.all('.generic-table tbody tr')- expect(trs[0]).to have_selector('.icon-yes')- expect(trs[0]).to have_text('Current session')- expect(trs[0]).to have_text('unknown browser')- expect(trs[0]).to have_text('unknown operating system')- expect(trs[0]).not_to have_selector('.buttons a')- expect(trs[1]).not_to have_selector('.spot-icon_yes')- expect(trs[1]).to have_text('Mozilla Firefox (Version 12.3)')- expect(trs[1]).to have_text('Linux')- expect(trs[1]).to have_text format_time(old_session_time)- expect(trs[1]).to have_selector('.buttons a')+ page.within_test_selector('Users::Sessions::TableComponent') do+ expect(page).to have_selector('.generic-table tbody tr', count: 2)+ trs = page.all('.generic-table tbody tr')+ expect(trs[0]).to have_selector('.icon-yes')+ expect(trs[0]).to have_text('Current session')+ expect(trs[0]).to have_text('unknown browser')+ expect(trs[0]).to have_text('unknown operating system')+ expect(trs[0]).not_to have_selector('.buttons a')+ expect(trs[1]).not_to have_selector('.spot-icon_yes')+ expect(trs[1]).to have_text('Mozilla Firefox (Version 12.3)')+ expect(trs[1]).to have_text('Linux')+ expect(trs[1]).to have_text format_time(old_session_time)+ expect(trs[1]).to have_selector('.buttons a')++ trs[1].find('.buttons a').click+ end- trs[1].find('.buttons a').clickpage.driver.browser.switch_to.alert.accept- expect(page).to have_selector('.generic-table tbody tr', count: 1)+ page.within_test_selector('Users::Sessions::TableComponent') do+ expect(page).to have_selector('.generic-table tbody tr', count: 1)+ endexpect { user_session.reload }.to raise_error(ActiveRecord::RecordNotFound)end