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

Fix activation of invited users during sharing

Oliver Günther01cbb3ef1ad9
diff --git a/app/controllers/account_controller.rb b/app/controllers/account_controller.rb
index ace77f009c3..08aca931c65 100644
--- a/app/controllers/account_controller.rb
+++ b/app/controllers/account_controller.rb
@@ -203,8 +203,6 @@ class AccountController < ApplicationController
else
flash[:error] = I18n.t(:notice_activation_failed)
end
-
- redirect_to signin_path
else
if user.active?
flash[:notice] = I18n.t(:notice_account_already_activated)
@@ -212,8 +210,9 @@ class AccountController < ApplicationController
flash[:error] = I18n.t(:notice_activation_failed)
end
- redirect_to home_url
end
+
+ redirect_to signin_path(back_url: params[:back_url])
end
def activate_by_invite_token(token)
@@ -224,6 +223,7 @@ class AccountController < ApplicationController
def activate_invited(token)
session[:invitation_token] = token.value
+ session[:back_url] = params[:back_url]
user = token.user
if user.ldap_auth_source
@@ -524,7 +524,7 @@ class AccountController < ApplicationController
def invalid_token_and_redirect
flash[:error] = I18n.t(:notice_account_invalid_token)
- redirect_to home_url
+ redirect_to signin_path
end
def apply_csp_appends
diff --git a/app/controllers/concerns/accounts/redirect_after_login.rb b/app/controllers/concerns/accounts/redirect_after_login.rb
index fd76342bcf7..3ff528d1e96 100644
--- a/app/controllers/concerns/accounts/redirect_after_login.rb
+++ b/app/controllers/concerns/accounts/redirect_after_login.rb
@@ -42,21 +42,19 @@ module Accounts::RedirectAfterLogin
end
end
- # * * *
-
def default_redirect
- if url = OpenProject::Configuration.after_login_default_redirect_url
- redirect_to url
+ if (url = OpenProject::Configuration.after_login_default_redirect_url)
+ redirect_back_or_default url
else
redirect_back_or_default my_page_path
end
end
def first_login_redirect
- if url = OpenProject::Configuration.after_first_login_redirect_url
- redirect_to url
+ if (url = OpenProject::Configuration.after_first_login_redirect_url)
+ redirect_back_or_default url
else
- redirect_to home_url(first_time_user: true)
+ redirect_back_or_default home_url(first_time_user: true)
end
end
end
diff --git a/app/helpers/mail_notification_helper.rb b/app/helpers/mail_notification_helper.rb
index 1b7f1cdd299..7fcf9f9b4ff 100644
--- a/app/helpers/mail_notification_helper.rb
+++ b/app/helpers/mail_notification_helper.rb
@@ -39,10 +39,6 @@ module MailNotificationHelper
notifications_center_url(['details', id, 'activity'])
end
- def shared_work_package_path(id)
- work_package_url(id)
- end
-
def type_color(type, default_fallback)
color_id = selected_color(type)
if color_id
diff --git a/app/mailers/sharing_mailer.rb b/app/mailers/sharing_mailer.rb
index 7546b7b50ba..dbbcc4f8d52 100644
--- a/app/mailers/sharing_mailer.rb
+++ b/app/mailers/sharing_mailer.rb
@@ -1,17 +1,21 @@
# frozen_string_literal: true
class SharingMailer < ApplicationMailer
+ include MailNotificationHelper
helper :mail_notification
def shared_work_package(sharer, membership, group = nil)
@sharer = sharer
@shared_with_user = membership.principal
+ @invitation_token = @shared_with_user.invited? ? @shared_with_user.invitation_token : nil
@group = group
@work_package = membership.entity
role = membership.roles.first
@role_rights = derive_role_rights(role)
@allowed_work_package_actions = derive_allowed_work_package_actions(role)
+ @url = optionally_activated_url(work_package_url(@work_package.id), @invitation_token)
+ @notification_url = optionally_activated_url(notifications_path(@work_package.id), @invitation_token)
set_open_project_headers(@work_package)
message_id(membership, sharer)
@@ -25,6 +29,15 @@ class SharingMailer < ApplicationMailer
private
+ def optionally_activated_url(back_url, invitation_token)
+ return back_url unless invitation_token
+
+ url_for(controller: '/account',
+ action: :activate,
+ token: invitation_token.value,
+ back_url:)
+ end
+
def derive_role_rights(role)
case role.builtin
when Role::BUILTIN_WORK_PACKAGE_EDITOR
@@ -38,16 +51,16 @@ class SharingMailer < ApplicationMailer
def derive_allowed_work_package_actions(role)
allowed_actions = case role.builtin
- when Role::BUILTIN_WORK_PACKAGE_EDITOR
- [I18n.t('work_package.sharing.permissions.view'),
- I18n.t('work_package.sharing.permissions.comment'),
- I18n.t('work_package.sharing.permissions.edit')]
- when Role::BUILTIN_WORK_PACKAGE_COMMENTER
- [I18n.t('work_package.sharing.permissions.view'),
- I18n.t('work_package.sharing.permissions.comment')]
- when Role::BUILTIN_WORK_PACKAGE_VIEWER
- [I18n.t('work_package.sharing.permissions.view')]
- end
+ when Role::BUILTIN_WORK_PACKAGE_EDITOR
+ [I18n.t('work_package.sharing.permissions.view'),
+ I18n.t('work_package.sharing.permissions.comment'),
+ I18n.t('work_package.sharing.permissions.edit')]
+ when Role::BUILTIN_WORK_PACKAGE_COMMENTER
+ [I18n.t('work_package.sharing.permissions.view'),
+ I18n.t('work_package.sharing.permissions.comment')]
+ when Role::BUILTIN_WORK_PACKAGE_VIEWER
+ [I18n.t('work_package.sharing.permissions.view')]
+ end
allowed_actions.map(&:downcase)
end
diff --git a/app/models/token/invitation.rb b/app/models/token/invitation.rb
index ffe7eab75b6..58fc6349914 100644
--- a/app/models/token/invitation.rb
+++ b/app/models/token/invitation.rb
@@ -31,9 +31,9 @@ module Token
include ExpirableToken
##
- # Invitation tokens are valid for one day.
+ # Invitation tokens are valid for a configurable amount of days
def self.validity_time
- (Setting.invitation_expiration_days || 1).days
+ Setting.invitation_expiration_days.days
end
##
diff --git a/app/models/user.rb b/app/models/user.rb
index 2742dfc7c22..e56970f30cd 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -56,6 +56,9 @@ class User < Principal
has_one :rss_token, class_name: '::Token::RSS', dependent: :destroy
has_one :api_token, class_name: '::Token::API', dependent: :destroy
+ # The user might have one invitation token
+ has_one :invitation_token, class_name: '::Token::Invitation', dependent: :destroy
+
# everytime a user subscribes to a calendar, a new ical_token is generated
# unlike on other token types, all previously generated ical_tokens are kept
# in order to keep all previously generated ical urls valid and usable
diff --git a/app/views/mailer/_notification_row.html.erb b/app/views/mailer/_notification_row.html.erb
index bcb9e6912f0..b8f4789e8aa 100644
--- a/app/views/mailer/_notification_row.html.erb
+++ b/app/views/mailer/_notification_row.html.erb
@@ -1,5 +1,5 @@
<a style="text-decoration: none;display: block;"
- href="<%= notifications_path(work_package.id) %>"
+ href="<%= local_assigns[:notification_url] || notifications_path(work_package.id) %>"
target="_blank">
<%= render layout: 'mailer/border_table' do %>
<tr>
diff --git a/app/views/sharing_mailer/shared_work_package.html.erb b/app/views/sharing_mailer/shared_work_package.html.erb
index d29a6556309..7a5939c5f61 100644
--- a/app/views/sharing_mailer/shared_work_package.html.erb
+++ b/app/views/sharing_mailer/shared_work_package.html.erb
@@ -20,7 +20,7 @@
locals: {
user: @shared_with_user,
summary:,
- button_href: shared_work_package_path(@work_package.id),
+ button_href: @url,
button_text: I18n.t(:'mail.sharing.work_packages.open_work_package'),
} %>
@@ -29,7 +29,8 @@
work_package: @work_package,
unique_reasons: [:shared],
show_count: false,
- open_in_browser_path: shared_work_package_path(@work_package.id)
+ notification_url: @notification_url,
+ open_in_browser_path: @url
} do %>
<table <%= placeholder_table_styles(width:'100%',style: 'width:100%;') %>>
<tr>
diff --git a/app/views/sharing_mailer/shared_work_package.text.erb b/app/views/sharing_mailer/shared_work_package.text.erb
index 82288e31d9f..63bc3f4c197 100644
--- a/app/views/sharing_mailer/shared_work_package.text.erb
+++ b/app/views/sharing_mailer/shared_work_package.text.erb
@@ -21,4 +21,7 @@
<%= I18n.t('mail.work_packages.reason.shared') %>:
<%= t('mail.sharing.work_packages.allowed_actions', allowed_actions: @allowed_work_package_actions.to_sentence).html_safe %>
+<%= t('mail.sharing.work_packages.open_work_package') %>
+<%= @url %>
+
<%= "-" * 100 %>
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 989b7a385b4..9ebc07e0cf6 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -2338,7 +2338,7 @@ en:
sharing:
work_packages:
allowed_actions: "You may %{allowed_actions} this work package. This can change depending on your project role and permissions."
- create_account: "To access this work package you will need to create an %{instance} account. "
+ create_account: "To access this work package you will need to create and activate an %{instance} account. "
open_work_package: "Open work package"
subject: "You have been shared work package #%{id}"
enterprise_text: "Share work packages with users who are not members of the project."
diff --git a/spec/features/work_packages/share/share_account_activation_spec.rb b/spec/features/work_packages/share/share_account_activation_spec.rb
new file mode 100644
index 00000000000..07469814218
--- /dev/null
+++ b/spec/features/work_packages/share/share_account_activation_spec.rb
@@ -0,0 +1,110 @@
+# frozen_string_literal: true
+
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-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 'Work package sharing invited users',
+ :js,
+ with_ee: %i[work_package_sharing],
+ with_flag: { work_package_sharing: true } do
+ shared_let(:view_work_package_role) { create(:view_work_package_role) }
+ shared_let(:editor) { create(:admin, firstname: 'Mr.', lastname: 'Sharer') }
+
+ shared_let(:sharer_role) do
+ create(:project_role,
+ permissions: %i(view_work_packages
+ view_shared_work_packages
+ share_work_packages))
+ end
+
+ shared_let(:project) do
+ create(:project,
+ members: { editor => [sharer_role] })
+ end
+
+ shared_let(:work_package) do
+ create(:work_package, project:)
+ end
+ let(:work_package_page) { Pages::FullWorkPackage.new(work_package) }
+ let(:share_modal) { Components::WorkPackages::ShareModal.new(work_package) }
+
+ it 'allows to invite and activate the account' do
+ login_with(editor.login, 'adminADMIN!')
+ expect(page).to have_current_path '/my/page'
+
+ work_package_page.visit!
+ click_button 'Share'
+
+ # Invite a user that does not exist yet
+ share_modal.invite_user('[email protected]', 'View')
+ # New user is shown in the list of shares
+ share_modal.expect_shared_count_of(1)
+
+ perform_enqueued_jobs
+
+ expect(ActionMailer::Base.deliveries.size).to eq(1)
+
+ link = ActionMailer::Base.deliveries.first.text_part.body.encoded.scan(/http:\/\/.+$/).first.chomp
+ token = Token::Invitation.last
+ user = token.user
+ expect(token).to be_present
+ expect(user).to be_invited
+ expect(user.mail).to eq '[email protected]'
+ expect(link).to include token.value
+
+ # Can log in and register the first time
+ visit signout_path
+ visit link
+
+ expect(page).to have_text 'Create a new account'
+ password = SecureRandom.hex(16)
+
+ fill_in 'Password', with: password
+ fill_in 'Confirmation', with: password
+
+ click_button 'Create'
+
+ expect(page).to have_text 'Welcome, your account has been activated. You are logged in now.'
+ expect(page).to have_current_path project_work_package_path(project, work_package.id, 'activity')
+
+ expect(page).to have_text work_package.subject
+ expect(user.reload).to be_active
+
+ # Can log in with the link the second time
+ visit signout_path
+ visit link
+
+ expect(page).to have_text 'Sign in'
+ login_with '[email protected]', password, visit_signin_path: false
+
+ expect(page).to have_current_path project_work_package_path(project, work_package.id, 'activity')
+ expect(page).to have_text work_package.subject
+ end
+end