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
[39807] Use SetAttributesService in invite
Oliver Güntheraa651df116ac
diff --git a/app/controllers/concerns/user_invitation.rb b/app/controllers/concerns/user_invitation.rbindex d38debad979..75da3f9c67a 100644--- a/app/controllers/concerns/user_invitation.rb+++ b/app/controllers/concerns/user_invitation.rb@@ -55,13 +55,15 @@ module UserInvitation# on the returned user will yield `false`. Check for validation errors# in that case.def invite_new_user(email:, login: nil, first_name: nil, last_name: nil)- user = User.new mail: email,- login: login,- firstname: first_name,- lastname: last_name,- status: Principal.statuses[:invited]+ attributes = {+ mail: email,+ login: login,+ firstname: first_name,+ lastname: last_name,+ status: Principal.statuses[:invited]+ }- assign_user_attributes(user)+ user = user_from_attributes(attributes)yield user if block_given?@@ -73,13 +75,11 @@ module UserInvitation# derives login and first name## The default login is the email address.- def assign_user_attributes(user)- placeholder = placeholder_name(user.mail)-- user.login = user.login.presence || user.mail- user.firstname = user.firstname.presence || placeholder.first- user.lastname = user.lastname.presence || placeholder.last- user.language = user.language.presence || Setting.default_language+ def user_from_attributes(attributes)+ ::Users::SetAttributesService+ .new(user: User.system, model: User.new, contract_class: ::Users::CreateContract)+ .call(attributes)+ .resultend##@@ -107,25 +107,6 @@ module UserInvitationUserPassword.where(user_id: user_id).destroy_allend- ##- # Creates a placeholder name for the user based on their email address.- # For the unlikely case that the local or domain part of the email address- # are longer than 30 characters they will be trimmed to 27 characters and an- # ellipsis will be appended.- def placeholder_name(email)- first, last = email.split('@').map { |name| trim_name(name) }-- [first, '@' + last]- end-- def trim_name(name)- if name.size > 30- name[0..26] + '...'- else- name- end- end-### Invites the given user. An email will be sent to their email address# containing the token necessary for the user to register.diff --git a/app/services/users/set_attributes_service.rb b/app/services/users/set_attributes_service.rbindex 6636adb2667..d207401981d 100644--- a/app/services/users/set_attributes_service.rb+++ b/app/services/users/set_attributes_service.rb@@ -40,10 +40,10 @@ module Userssuper(params)end- def set_default_attributes(_params)+ def set_default_attributes(params)# Assign values other than mail to new_user when invitedif model.invited? && model.valid_attribute?(:mail)- ::UserInvitation.assign_user_attributes model+ assign_invited_attributes!(params)endinitialize_notification_settings unless model.notification_settings.any?@@ -56,5 +56,35 @@ module Usersdef initialize_notification_settingsmodel.notification_settings.build(involved: true, mentioned: true, watched: true)end++ # rubocop:disable Metrics/AbcSize+ def assign_invited_attributes!(params)+ placeholder = placeholder_name(params[:mail])++ model.login = model.login.presence || params[:mail]+ model.firstname = model.firstname.presence || placeholder.first+ model.lastname = model.lastname.presence || placeholder.last+ model.language = model.language.presence || Setting.default_language+ end+ # rubocop:enable Metrics/AbcSize++ ##+ # Creates a placeholder name for the user based on their email address.+ # For the unlikely case that the local or domain part of the email address+ # are longer than 30 characters they will be trimmed to 27 characters and an+ # ellipsis will be appended.+ def placeholder_name(email)+ first, last = email.split('@').map { |name| trim_name(name) }++ [first, "@#{last}"]+ end++ def trim_name(name)+ if name.size > 30+ "#{name[0..26]}..."+ else+ name+ end+ endendenddiff --git a/spec/controllers/concerns/user_invitation_spec.rb b/spec/controllers/concerns/user_invitation_spec.rbindex e2dca4ea590..c3272608ec7 100644--- a/spec/controllers/concerns/user_invitation_spec.rb+++ b/spec/controllers/concerns/user_invitation_spec.rb@@ -29,24 +29,6 @@require 'spec_helper'describe UserInvitation do- describe '.placeholder_name' do- it 'given an email it uses the local part as first and the domain as the last name' do- email = '[email protected]'- first, last = UserInvitation.placeholder_name email-- expect(first).to eq 'xxxhunterxxx'- expect(last).to eq '@openproject.com'- end-- it 'trims names if they are too long (> 30 characters)' do- email = 'hallowurstsalatgetraenkebuechse@veryopensuchproject.openproject.com'- first, last = UserInvitation.placeholder_name email-- expect(first).to eq 'hallowurstsalatgetraenkebue...'- expect(last).to eq '@veryopensuchproject.openpro...'- end- end-describe '.reinvite_user' dolet(:user) { FactoryBot.create :invited_user }let!(:token) { FactoryBot.create :invitation_token, user: user }diff --git a/spec/services/users/set_attributes_service_spec.rb b/spec/services/users/set_attributes_service_spec.rbindex fd03347762d..93ae95e4557 100644--- a/spec/services/users/set_attributes_service_spec.rb+++ b/spec/services/users/set_attributes_service_spec.rb@@ -143,4 +143,22 @@ describe Users::SetAttributesService, type: :model do.to eql(contract_errors)endend++ describe '.placeholder_name' do+ it 'given an email it uses the local part as first and the domain as the last name' do+ email = '[email protected]'+ first, last = instance.send(:placeholder_name, email)++ expect(first).to eq 'xxxhunterxxx'+ expect(last).to eq '@openproject.com'+ end++ it 'trims names if they are too long (> 30 characters)' do+ email = 'hallowurstsalatgetraenkebuechse@veryopensuchproject.openproject.com'+ first, last = instance.send(:placeholder_name, email)++ expect(first).to eq 'hallowurstsalatgetraenkebue...'+ expect(last).to eq '@veryopensuchproject.openpro...'+ end+ endend