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

extract user creation in mailer

ulferts4cf59e7adf78
diff --git a/app/models/mail_handler.rb b/app/models/mail_handler.rb
index 91e37934878..ef9b52e3203 100644
--- a/app/models/mail_handler.rb
+++ b/app/models/mail_handler.rb
@@ -105,7 +105,7 @@ class MailHandler < ActionMailer::Base
when 'accept'
@user = User.anonymous
when 'create'
- @user, password = MailHandler.create_user_from_email(email)
+ @user, password = MailHandler::UserCreator.create_user_from_email(email)
if @user
log "[#{@user.login}] account created"
UserMailer.account_information(@user, password).deliver_later
@@ -433,52 +433,6 @@ class MailHandler < ActionMailer::Base
@full_sanitizer ||= Rails::Html::FullSanitizer.new
end
- # Returns a User from an email address and a full name
- def self.new_user_from_attributes(email_address, fullname = nil)
- user = User.new
- user.mail = email_address
- user.login = user.mail
- user.random_password!
- user.language = Setting.default_language
-
- names = fullname.blank? ? email_address.gsub(/@.*\z/, '').split('.') : fullname.split
- user.firstname = names.shift
- user.lastname = names.join(' ')
- user.lastname = '-' if user.lastname.blank?
-
- unless user.valid?
- user.login = "user#{SecureRandom.hex(6)}" if user.errors[:login].present?
- user.firstname = '-' if user.errors[:firstname].present?
- user.lastname = '-' if user.errors[:lastname].present?
- end
-
- user
- end
-
- # Creates a user account for the +email+ sender
- def self.create_user_from_email(email)
- from = email.header['from'].to_s
- addr = from
- name = nil
- if m = from.match(/\A"?(.+?)"?\s+<(.+@.+)>\z/)
- addr = m[2]
- name = m[1]
- end
- if addr.present?
- user = new_user_from_attributes(addr, name)
- password = user.password
- if user.save
- [user, password]
- else
- log "failed to create User: #{user.errors.full_messages}", :error
- nil
- end
- else
- log 'failed to create User: no FROM address found', :error
- nil
- end
- end
-
def allow_override_option(options)
if options[:allow_override].is_a?(String)
options[:allow_override].split(',').map(&:strip)
diff --git a/app/models/mail_handler/user_creator.rb b/app/models/mail_handler/user_creator.rb
new file mode 100644
index 00000000000..eb001f24152
--- /dev/null
+++ b/app/models/mail_handler/user_creator.rb
@@ -0,0 +1,100 @@
+# OpenProject is an open source project management software.
+# Copyright (C) 2022 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.
+
+class MailHandler::UserCreator
+ class << self
+ # Creates a user account for the +email+ sender
+ def create_user_from_email(email)
+ addr, name = extract_addr_and_name_from_email(email)
+
+ if addr.present?
+ user = new_user_from_attributes(addr, name)
+ password = user.password
+
+ if user.save
+ [user, password]
+ else
+ logger.error "failed to create User: #{user.errors.full_messages}"
+ nil
+ end
+ else
+ logger.error 'failed to create User: no FROM address found'
+ nil
+ end
+ end
+
+ private
+
+ # Returns a User from an email address and a full name
+ def new_user_from_attributes(email_address, fullname = nil)
+ call = Users::SetAttributesService
+ .new(user: User.system, model: User.new, contract_class: Users::CreateContract)
+ .call(**user_initialization_attributes(email_address, fullname))
+
+ user = call.result
+
+ user.random_password!
+
+ assign_fallback_attributes(user, call.errors)
+
+ user
+ end
+
+ def user_initialization_attributes(email_address, fullname = nil)
+ names = fullname.blank? ? email_address.gsub(/@.*\z/, '').split('.') : fullname.split
+ firstname = names.shift
+ lastname = names.join(' ')
+ lastname = '-' if lastname.blank?
+
+ {
+ mail: email_address,
+ login: email_address,
+ firstname:,
+ lastname:
+ }
+ end
+
+ def assign_fallback_attributes(user, errors)
+ if errors.any?
+ user.login = "user#{SecureRandom.hex(6)}" if errors[:login].present?
+ user.firstname = '-' if errors[:firstname].present?
+ user.lastname = '-' if errors[:lastname].present?
+ end
+ end
+
+ def extract_addr_and_name_from_email(email)
+ from = email.header['from'].to_s
+ addr = from
+ name = nil
+ if m = from.match(/\A"?(.+?)"?\s+<(.+@.+)>\z/)
+ addr = m[2]
+ name = m[1]
+ end
+
+ [addr, name]
+ end
+ end
+end
diff --git a/spec/models/mail_handler/user_creator_spec.rb b/spec/models/mail_handler/user_creator_spec.rb
new file mode 100644
index 00000000000..747ce1ee96e
--- /dev/null
+++ b/spec/models/mail_handler/user_creator_spec.rb
@@ -0,0 +1,89 @@
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2022 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'
+
+describe MailHandler::UserCreator, type: :model do
+ describe '.new_user_from_attributes' do
+ context 'with sufficient information' do
+ # [address, name] => [login, firstname, lastname]
+ {
+ ['[email protected]', nil] => %w[[email protected] jsmith -],
+ %w[[email protected] John] => %w[[email protected] John -],
+ ['[email protected]', 'John Smith'] => %w[[email protected] John Smith],
+ ['[email protected]', 'John Paul Smith'] => ['[email protected]', 'John', 'Paul Smith'],
+ ['[email protected]', 'AVeryLongFirstnameThatNoLongerExceedsTheMaximumLength Smith'] =>
+ %w[[email protected] AVeryLongFirstnameThatNoLongerExceedsTheMaximumLength Smith],
+ ['[email protected]', 'John AVeryLongLastnameThatNoLongerExceedsTheMaximumLength'] =>
+ %w[[email protected] John AVeryLongLastnameThatNoLongerExceedsTheMaximumLength]
+ }.each do |(provided_mail, provided_fullname), (expected_login, expected_firstname, expected_lastname)|
+ it 'returns a valid user' do
+ user = described_class.send(:new_user_from_attributes, provided_mail, provided_fullname)
+
+ expect(user)
+ .to be_valid
+ expect(user.mail)
+ .to eq provided_mail
+ expect(user.login)
+ .to eq expected_login
+ expect(user.firstname)
+ .to eq expected_firstname
+ expect(user.lastname)
+ .to eq expected_lastname
+ end
+ end
+ end
+
+ context 'with min password length',
+ with_legacy_settings: { password_min_length: 15 } do
+ it 'respects minimum password length' do
+ user = described_class.send(:new_user_from_attributes, '[email protected]')
+
+ expect(user)
+ .to be_valid
+
+ expect(user.password.length)
+ .to be 15
+ end
+ end
+
+ context 'when the attributes are invalid',
+ with_legacy_settings: { password_min_length: 15 } do
+ it 'respects minimum password length' do
+ user = described_class.send(:new_user_from_attributes, 'foo&[email protected]')
+
+ expect(user)
+ .to be_valid
+
+ expect(user.login)
+ .to match /^user[a-f0-9]+$/
+
+ expect(user.mail)
+ .to eq 'foo&[email protected]'
+ end
+ end
+ end
+end
diff --git a/spec/models/mail_handler_spec.rb b/spec/models/mail_handler_spec.rb
index 6cce261b9f3..188bb281df9 100644
--- a/spec/models/mail_handler_spec.rb
+++ b/spec/models/mail_handler_spec.rb
@@ -39,6 +39,7 @@ describe MailHandler, type: :model do
# there is a default work package priority to save any work packages
priority_low
anno_user
+ User.system
allow(UserMailer)
.to receive(:incoming_email_error)
@@ -1520,66 +1521,6 @@ describe MailHandler, type: :model do
end
end
- describe '.new_user_from_attributes' do
- context 'with sufficient information' do
- # [address, name] => [login, firstname, lastname]
- {
- ['[email protected]', nil] => %w[[email protected] jsmith -],
- %w[[email protected] John] => %w[[email protected] John -],
- ['[email protected]', 'John Smith'] => %w[[email protected] John Smith],
- ['[email protected]', 'John Paul Smith'] => ['[email protected]', 'John', 'Paul Smith'],
- ['[email protected]', 'AVeryLongFirstnameThatNoLongerExceedsTheMaximumLength Smith'] =>
- %w[[email protected] AVeryLongFirstnameThatNoLongerExceedsTheMaximumLength Smith],
- ['[email protected]', 'John AVeryLongLastnameThatNoLongerExceedsTheMaximumLength'] =>
- %w[[email protected] John AVeryLongLastnameThatNoLongerExceedsTheMaximumLength]
- }.each do |(provided_mail, provided_fullname), (expected_login, expected_firstname, expected_lastname)|
- it 'returns a valid user' do
- user = described_class.new_user_from_attributes(provided_mail, provided_fullname)
-
- expect(user)
- .to be_valid
- expect(user.mail)
- .to eq provided_mail
- expect(user.login)
- .to eq expected_login
- expect(user.firstname)
- .to eq expected_firstname
- expect(user.lastname)
- .to eq expected_lastname
- end
- end
- end
-
- context 'with min password length',
- with_legacy_settings: { password_min_length: 15 } do
- it 'respects minimum password length' do
- user = described_class.new_user_from_attributes('[email protected]')
-
- expect(user)
- .to be_valid
-
- expect(user.password.length)
- .to be 15
- end
- end
-
- context 'when the attributes are invalid',
- with_legacy_settings: { password_min_length: 15 } do
- it 'respects minimum password length' do
- user = described_class.new_user_from_attributes('foo&[email protected]')
-
- expect(user)
- .to be_valid
-
- expect(user.login)
- .to match /^user[a-f0-9]+$/
-
- expect(user.mail)
- .to eq 'foo&[email protected]'
- end
- end
- end
-
private
def read_email(filename)