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

Update the Projects::CreateContract to set writable custom fields based on permissions

Dombi Attila1611630ef4eb
diff --git a/app/contracts/projects/create_contract.rb b/app/contracts/projects/create_contract.rb
index bc922099e22..1c65de38364 100644
--- a/app/contracts/projects/create_contract.rb
+++ b/app/contracts/projects/create_contract.rb
@@ -33,6 +33,14 @@ module Projects
# so allowing writing here would be useless.
allow_writable_timestamps :created_at
+ def writable_attributes
+ if allowed_to_write_custom_fields?
+ super
+ else
+ without_custom_fields(super)
+ end
+ end
+
protected
def collect_available_custom_field_attributes
@@ -41,6 +49,12 @@ module Projects
private
+ def allowed_to_write_custom_fields?
+ user.admin? || user.allowed_globally?(:add_project) || user.allowed_in_any_project?(:edit_project_attributes)
+ end
+
+ def without_custom_fields(changes) = changes.grep_v(/^custom_field_/)
+
def validate_user_allowed_to_manage
unless user.allowed_globally?(:add_project) ||
(model.parent && user.allowed_in_project?(:add_subprojects, model.parent))
diff --git a/spec/contracts/projects/create_contract_spec.rb b/spec/contracts/projects/create_contract_spec.rb
index 6db5535303d..3012f0a9dd7 100644
--- a/spec/contracts/projects/create_contract_spec.rb
+++ b/spec/contracts/projects/create_contract_spec.rb
@@ -57,8 +57,19 @@ RSpec.describe Projects::CreateContract do
end
describe "writing read-only attributes" do
- shared_examples "can not write" do |attribute, value|
- it "can not write #{attribute}", :aggregate_failures do
+ shared_examples "can write" do
+ let(:value) { 1 }
+ it "can write", :aggregate_failures do
+ expect(contract.writable_attributes).to include(attribute.to_s)
+
+ project.send(:"#{attribute}=", value)
+ expect(validated_contract.errors[attribute]).to be_empty
+ end
+ end
+
+ shared_examples "can not write" do
+ let(:value) { 1 }
+ it "can not write", :aggregate_failures do
expect(contract.writable_attributes).not_to include(attribute.to_s)
project.send(:"#{attribute}=", value)
@@ -67,34 +78,105 @@ RSpec.describe Projects::CreateContract do
end
end
+ describe "project attributes" do
+ let(:global_permissions) { [] }
+ let(:current_user) { create(:user) }
+ let!(:role) { create(:existing_project_role, permissions: project_permissions) }
+ let!(:other_project) do
+ create(:project,
+ members: { current_user => role })
+ end
+ let!(:mapping) { create(:project_custom_field_project_mapping, project: other_project) }
+ let!(:custom_field) { mapping.project_custom_field }
+ let!(:non_member_custom_field) do
+ create(:project_custom_field_project_mapping).project_custom_field
+ end
+
+ before { User.current = current_user }
+
+ context "with view_project_attributes permission" do
+ let(:project_permissions) { %i(view_project_attributes) }
+
+ it_behaves_like "can not write" do
+ let(:attribute) { custom_field.attribute_name }
+ end
+ end
+
+ context "with edit_project_attributes permission" do
+ let(:project_permissions) { %i(view_project_attributes edit_project_attributes) }
+
+ it_behaves_like "can write" do
+ let(:attribute) { custom_field.attribute_name }
+ end
+
+ it_behaves_like "can not write" do
+ let(:attribute) { non_member_custom_field.attribute_name }
+ end
+ end
+
+ context "with add_project permission" do
+ let(:global_permissions) { %i(add_project) }
+
+ it_behaves_like "can write" do
+ let(:attribute) { custom_field.attribute_name }
+ end
+
+ it_behaves_like "can write" do
+ let(:attribute) { non_member_custom_field.attribute_name }
+ end
+ end
+ end
+
context "when enabled for admin", with_settings: { apiv3_write_readonly_attributes: true } do
let(:current_user) { build_stubbed(:admin) }
- it_behaves_like "can not write", :updated_at, 1.day.ago
-
- it "can write created_at", :aggregate_failures do
- expect(contract.writable_attributes).to include("created_at")
+ it_behaves_like "can write" do
+ let(:attribute) { :created_at }
+ let(:value) { 10.days.ago }
+ end
- project.created_at = 10.days.ago
- expect(validated_contract.errors[:created_at]).to be_empty
+ it_behaves_like "can not write" do
+ let(:attribute) { :updated_at }
+ let(:value) { 1.day.ago }
end
end
context "when disabled for admin", with_settings: { apiv3_write_readonly_attributes: false } do
let(:current_user) { build_stubbed(:admin) }
- it_behaves_like "can not write", :created_at, 1.day.ago
- it_behaves_like "can not write", :updated_at, 1.day.ago
+ it_behaves_like "can not write" do
+ let(:attribute) { :created_at }
+ let(:value) { 1.day.ago }
+ end
+
+ it_behaves_like "can not write" do
+ let(:attribute) { :updated_at }
+ let(:value) { 1.day.ago }
+ end
end
context "when enabled for regular user", with_settings: { apiv3_write_readonly_attributes: true } do
- it_behaves_like "can not write", :created_at, 1.day.ago
- it_behaves_like "can not write", :updated_at, 1.day.ago
+ it_behaves_like "can not write" do
+ let(:attribute) { :created_at }
+ let(:value) { 1.day.ago }
+ end
+
+ it_behaves_like "can not write" do
+ let(:attribute) { :updated_at }
+ let(:value) { 1.day.ago }
+ end
end
context "when disabled for regular user", with_settings: { apiv3_write_readonly_attributes: false } do
- it_behaves_like "can not write", :created_at, 1.day.ago
- it_behaves_like "can not write", :updated_at, 1.day.ago
+ it_behaves_like "can not write" do
+ let(:attribute) { :created_at }
+ let(:value) { 1.day.ago }
+ end
+
+ it_behaves_like "can not write" do
+ let(:attribute) { :updated_at }
+ let(:value) { 1.day.ago }
+ end
end
end
end