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
Fix: 36299 -- Allow to modify login when global permission :manage_users present (#9026)
Wieland Lindenthalc4c2149d9835
diff --git a/app/contracts/users/base_contract.rb b/app/contracts/users/base_contract.rbindex 8733bdabcf8..bbabb54df7e 100644--- a/app/contracts/users/base_contract.rb+++ b/app/contracts/users/base_contract.rb@@ -30,7 +30,8 @@module Usersclass BaseContract < ::ModelContract- attribute :login+ attribute :login,+ writeable: ->(*) { user.allowed_to_globally?(:manage_user) && model.id != user.id }attribute :firstnameattribute :lastnameattribute :namediff --git a/app/contracts/users/create_contract.rb b/app/contracts/users/create_contract.rbindex f4bcb80a6a3..ce7e95a2ae8 100644--- a/app/contracts/users/create_contract.rb+++ b/app/contracts/users/create_contract.rb@@ -54,7 +54,8 @@ module Usersend##- # Users can only be created by Admins+ # Users can only be created by Admins or users with+ # the global right to :manage_userdef user_allowed_to_addunless user.allowed_to_globally?(:manage_user)errors.add :base, :error_unauthorizeddiff --git a/app/models/permitted_params.rb b/app/models/permitted_params.rbindex 45ea1839031..6ff7bfda1bd 100644--- a/app/models/permitted_params.rb+++ b/app/models/permitted_params.rb@@ -207,8 +207,9 @@ class PermittedParamsif current_user.admin?additional_params << :force_password_change if change_password_allowedadditional_params << :admin- additional_params << :loginend++ additional_params << :login if Users::BaseContract.new(User.new, current_user).writable?(:login)user additional_paramsenddiff --git a/spec/contracts/users/create_contract_spec.rb b/spec/contracts/users/create_contract_spec.rbindex e330ba70c35..c66e9d59cc9 100644--- a/spec/contracts/users/create_contract_spec.rb+++ b/spec/contracts/users/create_contract_spec.rb@@ -30,81 +30,47 @@require 'spec_helper'require 'contracts/shared/model_contract_shared_context'+require_relative 'shared_contract_examples'describe Users::CreateContract doinclude_context 'ModelContract shared context'- let(:user) { FactoryBot.build_stubbed(:user) }- let(:contract) { described_class.new(user, current_user) }-- context 'when admin' do- let(:current_user) { FactoryBot.build_stubbed(:admin) }-- it_behaves_like 'contract is valid'-- describe 'requires a password set when active' do- before do- user.password = nil- user.activate- end-- it_behaves_like 'contract is invalid', password: :blank-- context 'when password is set' do- before do- user.password = user.password_confirmation = 'password!password!'- end-- it_behaves_like 'contract is valid'- end+ it_behaves_like 'user contract' do+ let(:user) { User.new(attributes) }+ let(:contract) { described_class.new(user, current_user) }+ let(:attributes) do+ {+ firstname: user_firstname,+ lastname: user_lastname,+ login: user_login,+ mail: user_mail,+ password: user_password,+ password_confirmation: user_password_confirmation+ }end- end- context 'when global user' do- shared_let(:current_user) { FactoryBot.create :user, global_permission: :manage_user }-- describe 'can invite user' do- before do- user.password = user.password_confirmation = nil- user.mail = '[email protected]'- user.invite- end+ context 'when admin' do+ let(:current_user) { FactoryBot.build_stubbed(:admin) }it_behaves_like 'contract is valid'- end- describe 'cannot set the password' do- before do- user.password = user.password_confirmation = 'password!password!'- end-- it_behaves_like 'contract is invalid', password: :error_readonly- end-- describe 'can set the auth_source' do- let!(:auth_source) { FactoryBot.create :auth_source }+ describe 'requires a password set when active' do+ before do+ user.password = nil+ user.password_confirmation = nil+ user.activate+ end- before do- user.password = user.password_confirmation = nil- user.auth_source = auth_source- end+ it_behaves_like 'contract is invalid', password: :blank- it_behaves_like 'contract is valid'- end+ context 'when password is set' do+ before do+ user.password = user.password_confirmation = 'password!password!'+ end- describe 'cannot set the identity url' do- before do- user.identity_url = 'saml:123412foo'+ it_behaves_like 'contract is valid'+ endend-- it_behaves_like 'contract is invalid', identity_url: :error_readonly-endend-- context 'when unauthorized user' do- let(:current_user) { FactoryBot.build_stubbed(:user) }-- it_behaves_like 'contract user is unauthorized'- endenddiff --git a/spec/contracts/users/shared_contract_examples.rb b/spec/contracts/users/shared_contract_examples.rbnew file mode 100644index 00000000000..9fa96851988--- /dev/null+++ b/spec/contracts/users/shared_contract_examples.rb@@ -0,0 +1,85 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 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 docs/COPYRIGHT.rdoc for more details.+#++++require 'spec_helper'++shared_examples_for 'user contract' do+ let(:user_firstname) { 'Bob' }+ let(:user_lastname) { 'Bobbit' }+ let(:user_login) { 'bob' }+ let(:user_mail) { '[email protected]' }+ let(:user_password) { 'adminADMIN!' }+ let(:user_password_confirmation) { 'adminADMIN!' }++ it_behaves_like 'contract is valid for active admins and invalid for regular users'++ context 'when admin' do+ let(:current_user) { FactoryBot.build_stubbed :admin }++ it_behaves_like 'contract is valid'+ end++ context 'when global user' do+ let(:current_user) { FactoryBot.create :user, global_permission: :manage_user }++ describe 'cannot set the password' do+ before do+ user.password = user.password_confirmation = 'password!password!'+ end++ it_behaves_like 'contract is invalid', password: :error_readonly+ end++ describe 'can set the auth_source' do+ let!(:auth_source) { FactoryBot.create :auth_source }++ before do+ user.password = user.password_confirmation = nil+ user.auth_source = auth_source+ end++ it_behaves_like 'contract is valid'+ end++ describe 'cannot set the identity url' do+ before do+ user.identity_url = 'saml:123412foo'+ end++ it_behaves_like 'contract is invalid', identity_url: :error_readonly+ end+ end++ context 'when unauthorized user' do+ let(:current_user) { FactoryBot.build_stubbed(:user) }++ it_behaves_like 'contract user is unauthorized'+ end+enddiff --git a/spec/contracts/users/update_contract_spec.rb b/spec/contracts/users/update_contract_spec.rbnew file mode 100644index 00000000000..ecdb0549283--- /dev/null+++ b/spec/contracts/users/update_contract_spec.rb@@ -0,0 +1,61 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 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 docs/COPYRIGHT.rdoc for more details.+#++++require 'spec_helper'+require 'contracts/shared/model_contract_shared_context'+require_relative 'shared_contract_examples'++describe Users::UpdateContract do+ include_context 'ModelContract shared context'++ it_behaves_like 'user contract' do+ let(:user) { FactoryBot.build_stubbed(:user, attributes) }+ let(:contract) { described_class.new(user, current_user) }+ let(:attributes) do+ {+ firstname: user_firstname,+ lastname: user_lastname,+ login: user_login,+ mail: user_mail+ }+ end++ describe "validations" do+ describe "#user_allowed_to_update" do+ context "updated user is current user" do+ # That scenario is the only that is not covered by the shared examples+ let(:current_user) { user }++ it_behaves_like 'contract is valid'+ end+ end+ end+ end+enddiff --git a/spec/models/permitted_params_spec.rb b/spec/models/permitted_params_spec.rbindex e3054ef52a7..5aa10bd6bc1 100644--- a/spec/models/permitted_params_spec.rb+++ b/spec/models/permitted_params_spec.rb@@ -29,8 +29,8 @@require 'spec_helper'describe PermittedParams, type: :model do- let(:user) { FactoryBot.build(:user) }- let(:admin) { FactoryBot.build(:admin) }+ let(:user) { FactoryBot.build_stubbed(:user) }+ let(:admin) { FactoryBot.build_stubbed(:admin) }shared_context 'prepare params comparison' dolet(:params_key) { defined?(hash_key) ? hash_key : attribute }@@ -519,13 +519,22 @@ describe PermittedParams, type: :model dodescribe :user_create_as_admin dolet(:attribute) { :user_create_as_admin }+ let(:default_permissions) { %w[custom_fields firstname lastname language mail mail_notification auth_source_id] }context 'non-admin' dolet(:hash) { Hash[all_permissions.zip(all_permissions)] }it 'permits default permissions' do- expect(subject.keys)- .to match_array(%w[custom_fields firstname lastname language mail mail_notification auth_source_id])+ expect(subject.keys).to match_array(default_permissions)+ end+ end++ context 'non-admin with global :manage_user permission' do+ let(:user) { FactoryBot.create(:user, global_permission: :manage_user) }+ let(:hash) { Hash[all_permissions.zip(all_permissions)] }++ it 'permits default permissions and "login"' do+ expect(subject.keys).to match_array(default_permissions + ['login'])endenddiff --git a/spec/requests/api/v3/user/create_user_resource_spec.rb b/spec/requests/api/v3/user/create_user_resource_spec.rbindex 535e24fad6a..3b439187a54 100644--- a/spec/requests/api/v3/user/create_user_resource_spec.rb+++ b/spec/requests/api/v3/user/create_user_resource_spec.rb@@ -56,7 +56,7 @@ describe ::API::V3::Users::UsersAPI, type: :request doenddescribe 'admin user' do- let(:current_user) { FactoryBot.build(:admin) }+ let(:current_user) { FactoryBot.create(:admin) }it_behaves_like 'create user request flow'