Skip to content

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

Basic session management table

Oliver Günther5756195947e4
diff --git a/app/components/users/sessions/row_component.rb b/app/components/users/sessions/row_component.rb
new file mode 100644
index 00000000000..f56f6b4410c
--- /dev/null
+++ b/app/components/users/sessions/row_component.rb
@@ -0,0 +1,103 @@
+# 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 Sessions
+ class RowComponent < ::RowComponent
+ property :firstname, :lastname
+ delegate :current_session, to: :table
+
+ def session
+ model
+ end
+
+ def session_data
+ @session_data ||= session.data.with_indifferent_access
+ end
+
+ def current?
+ @current ||= session.current?(current_session)
+ end
+
+ def is_current # rubocop:disable Naming/PredicateName
+ if current?
+ helpers.op_icon 'icon-yes'
+ end
+ end
+
+ def name
+ browser_name = session_data[:browser] || I18n.t('users.sessions.unknown_browser')
+ browser_version = session_data[:browser_version]
+ os_name = session_data[:platform] || I18n.t('users.sessions.unknown_os')
+
+ I18n.t('users.sessions.session_name',
+ browser_name:,
+ browser_version: browser_version ? "(#{browser_version})" : '',
+ os_name:)
+ end
+
+ def browser
+ name = session_data[:browser] || 'unknown browser'
+ version = session_data[:browser_version]
+ "#{name} #{version ? "(Version #{version})" : ''}"
+ end
+
+ def platform
+ session_data[:platform] || 'unknown platform'
+ end
+
+ def updated_at
+ if current?
+ I18n.t('users.sessions.current')
+ else
+ helpers.format_time session_data[:updated_at]
+ end
+ end
+
+ def button_links
+ [delete_link].compact
+ end
+
+ def delete_link
+ return if current?
+
+ link_to(
+ helpers.op_icon('icon icon-delete'),
+ { controller: '/my/sessions', action: 'destroy', id: session },
+ 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
+end
diff --git a/app/components/users/sessions/table_component.rb b/app/components/users/sessions/table_component.rb
new file mode 100644
index 00000000000..9dd1d091c4a
--- /dev/null
+++ b/app/components/users/sessions/table_component.rb
@@ -0,0 +1,51 @@
+# 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 Sessions
+ class TableComponent < ::TableComponent
+ columns :is_current, :name, :updated_at
+ sortable_columns :updated_at
+ options :current_session
+
+ def initial_sort
+ %i[updated_at desc]
+ end
+
+ def headers
+ [
+ [:is_current, { caption: I18n.t('users.sessions.current') }],
+ [:name, { caption: I18n.t(:label_name) }],
+ [:updated_at, { caption: I18n.t('attributes.updated_at') }]
+ ]
+ end
+ end
+ end
+end
diff --git a/app/controllers/my/sessions_controller.rb b/app/controllers/my/sessions_controller.rb
new file mode 100644
index 00000000000..a76b07b0ba5
--- /dev/null
+++ b/app/controllers/my/sessions_controller.rb
@@ -0,0 +1,35 @@
+module My
+ class SessionsController < ::ApplicationController
+ before_action :require_login
+ self._model_object = ::Sessions::UserSession
+
+ before_action :find_model_object, only: %i(show destroy)
+ before_action :prevent_current_session_deletion, only: %i(destroy)
+
+ layout 'my'
+ menu_item :sessions
+
+ def index
+ @sessions = ::Sessions::UserSession
+ .for_user(current_user)
+ .order(updated_at: :desc)
+ end
+
+ def show; end
+
+ def destroy
+ @session.delete
+
+ flash[:notice] = I18n.t(:notice_successful_delete)
+ redirect_to action: :index
+ end
+
+ private
+
+ def prevent_current_session_deletion
+ if @session.current?(session)
+ render_400 message: I18n.t('users.sessions.may_not_delete_current')
+ end
+ end
+ end
+end
diff --git a/app/models/sessions/user_session.rb b/app/models/sessions/user_session.rb
index 5c8cae3b443..a09ca5217d5 100644
--- a/app/models/sessions/user_session.rb
+++ b/app/models/sessions/user_session.rb
@@ -50,6 +50,10 @@ module Sessions
true
end
+ def current?(session_object)
+ session_object.id.private_id == session_id
+ end
+
def data
SqlBypass.deserialize(super)
end
diff --git a/app/services/users/login_service.rb b/app/services/users/login_service.rb
index d0a7f76c9dd..26d6ba609b6 100644
--- a/app/services/users/login_service.rb
+++ b/app/services/users/login_service.rb
@@ -28,13 +28,14 @@
module Users
class LoginService
- attr_accessor :controller, :request
+ attr_accessor :controller, :request, :browser
delegate :session, to: :controller
def initialize(controller:, request:)
self.controller = controller
self.request = request
+ self.browser = controller.send(:browser)
end
def call(user)
@@ -53,6 +54,7 @@ module Users
::Sessions::InitializeSessionService.call(user, session)
session.merge!(retained_values) if retained_values
+ session.merge!(session_identification)
user.log_successful_login
@@ -63,6 +65,14 @@ module Users
private
+ def session_identification
+ {
+ platform: browser.platform&.name,
+ browser: browser.name,
+ browser_version: browser.version
+ }
+ end
+
def after_login_hook(user)
context = { user:, request:, session: }
diff --git a/app/views/my/sessions/index.html.erb b/app/views/my/sessions/index.html.erb
new file mode 100644
index 00000000000..14fd61e528f
--- /dev/null
+++ b/app/views/my/sessions/index.html.erb
@@ -0,0 +1,35 @@
+<%#-- 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.
+
+++#%>
+<% html_title(t('users.sessions.title')) -%>
+
+<%= toolbar title: t('users.sessions.title') -%>
+
+<p><%= t('users.sessions.instructions')%></p>
+
+<%= render ::Users::Sessions::TableComponent.new(rows: @sessions, current_session: session) %>
diff --git a/config/initializers/menus.rb b/config/initializers/menus.rb
index 48829f17100..9b576e84aba 100644
--- a/config/initializers/menus.rb
+++ b/config/initializers/menus.rb
@@ -137,6 +137,10 @@ Redmine::MenuManager.map :my_menu do |menu|
{ controller: '/my', action: 'settings' },
caption: :label_setting_plural,
icon: 'settings2'
+ menu.push :sessions,
+ { controller: '/my/sessions', action: :index },
+ caption: :'users.sessions.title',
+ icon: 'installation-services'
menu.push :password,
{ controller: '/my', action: 'password' },
caption: :button_change_password,
diff --git a/config/locales/en.yml b/config/locales/en.yml
index c47e4cabedf..a21911501b4 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -309,6 +309,14 @@ en:
no_results_content_text: Add a news item
users:
+ sessions:
+ session_name: "%{browser_name} %{browser_version} on %{os_name}"
+ unknown_browser: 'unknown browser'
+ unknown_os: 'unknown operating system'
+ current: 'Current session'
+ title: 'Session management'
+ instructions: 'This is a list of devices that have logged into your account. Revoke any sessions that you do not recognize or you have no longer access to.'
+ may_not_delete_current: 'You cannot delete your current session.'
groups:
member_in_these_groups: 'This user is currently a member of the following groups:'
no_results_title_text: This user is currently not a member in any group.
diff --git a/config/routes.rb b/config/routes.rb
index bc117107683..35fb61baa14 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -538,6 +538,8 @@ OpenProject::Application.routes.draw do
scope 'my' do
get '/deletion_info' => 'users#deletion_info', as: 'delete_my_account_info'
post '/oauth/revoke_application/:application_id' => 'oauth/grants#revoke_application', as: 'revoke_my_oauth_application'
+
+ resources :sessions, controller: 'my/sessions', as: 'my_sessions', only: %i[index show destroy]
end
scope controller: 'my' do
diff --git a/frontend/src/global_styles/content/_buttons.sass b/frontend/src/global_styles/content/_buttons.sass
index 479f32e6dc2..ef488d05fc9 100644
--- a/frontend/src/global_styles/content/_buttons.sass
+++ b/frontend/src/global_styles/content/_buttons.sass
@@ -175,7 +175,7 @@ html.-browser-windows.-browser-chrome
&:hover
text-decoration: underline
- &:disabled, &:disabled:hover
+ &[disabled], &[disabled]:hover
cursor: not-allowed
background: none
color: var(--gray-dark)
diff --git a/spec/features/my/sessions_management_spec.rb b/spec/features/my/sessions_management_spec.rb
new file mode 100644
index 00000000000..baf89fd94b5
--- /dev/null
+++ b/spec/features/my/sessions_management_spec.rb
@@ -0,0 +1,72 @@
+#-- 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 session management', js: true do
+ include Redmine::I18n
+ let(:user) { create(:user) }
+
+ let(:old_session_time) { 5.days.ago }
+ let(:session_data) do
+ { browser: 'Mozilla Firefox', browser_version: '12.3', platform: 'Linux', updated_at: old_session_time }
+ end
+
+ let!(:plain_session) { create(:user_session, user:, data: session_data) }
+ let!(:user_session) { Sessions::UserSession.find_by(session_id: plain_session.session_id) }
+
+ before do
+ login_as(user)
+ # Session is inserted with now() by default, and doesn't take the data attribute
+ Sessions::UserSession.where(id: user_session.id).update_all(updated_at: 5.days.ago)
+ visit my_account_path
+ end
+
+ it 'can list and terminate sessions' do
+ click_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 on 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 (12.3) on 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
+ page.driver.browser.switch_to.alert.accept
+
+ expect(page).to have_selector('.generic-table tbody tr', count: 1)
+ expect(page).not_to have_text('Unknown browser on unknown platform')
+
+ expect { user_session.reload }.to raise_error(ActiveRecord::RecordNotFound)
+ end
+end
diff --git a/spec/services/users/login_service_spec.rb b/spec/services/users/login_service_spec.rb
index 13e4b7b7649..3dcbb09d18d 100644
--- a/spec/services/users/login_service_spec.rb
+++ b/spec/services/users/login_service_spec.rb
@@ -30,9 +30,15 @@ require 'spec_helper'
RSpec.describe Users::LoginService, type: :model do
let(:input_user) { build_stubbed(:user) }
- let(:controller) { double('ApplicationController') }
let(:request) { {} }
let(:session) { {} }
+ let(:browser) do
+ instance_double(Browser::Safari,
+ name: 'Safari',
+ version: '13.2',
+ platform: instance_double(Browser::Platform::Linux, name: 'Linux'))
+ end
+ let(:controller) { double(ApplicationController, browser:) } # rubocop:disable RSpec/VerifiedDoubles
let(:flash) { ActionDispatch::Flash::FlashHash.new }
let(:instance) { described_class.new(controller:, request:) }
@@ -89,7 +95,7 @@ RSpec.describe Users::LoginService, type: :model do
end
it 'retains present flash values' do
- flash[:notice] = 'bar'
+ flash[:notice] = 'bar' # rubocop:disable Rails/I18nLocaleTexts
subject