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

Code maintenance/61471 Migration Utils Permission adder crashes when supplied with a permission that no longer exists (#17936)

Kabiru Mwenjaa42ca8561d60
diff --git a/db/migrate/migration_utils/permission_adder.rb b/db/migrate/migration_utils/permission_adder.rb
index 7f2c48708e6..61c35cf1955 100644
--- a/db/migrate/migration_utils/permission_adder.rb
+++ b/db/migrate/migration_utils/permission_adder.rb
@@ -31,9 +31,14 @@ module Migration
module PermissionAdder
module_function
- def add(having, add)
+ def add(having, add) # rubocop:disable Metrics/AbcSize
added_permission = OpenProject::AccessControl.permission(add)
+ if added_permission.blank?
+ OpenProject.logger.warn("Permission #{add} is not a valid permission in use. Skipping...")
+ return
+ end
+
role_scope = Role
.joins(:role_permissions)
.where(role_permissions: { permission: having.to_s })
diff --git a/spec/migrations/permission_adder_spec.rb b/spec/migrations/permission_adder_spec.rb
index aa122b5914e..0d250771476 100644
--- a/spec/migrations/permission_adder_spec.rb
+++ b/spec/migrations/permission_adder_spec.rb
@@ -85,4 +85,18 @@ RSpec.describe Migration::MigrationUtils::PermissionAdder, type: :model do # rub
expect(anonymous_role.permissions).not_to include(:archive_project)
end
end
+
+ context "with a permission that does not exist" do
+ it "results in a no-op" do
+ result = nil
+ expect { result = described_class.add(:view_project, :non_existent_permission) }.not_to raise_error
+ expect(result).to be_nil
+
+ roles.each(&:reload)
+
+ expect(role.permissions).not_to include(:non_existent_permission)
+ expect(non_member_role.permissions).not_to include(:non_existent_permission)
+ expect(anonymous_role.permissions).not_to include(:non_existent_permission)
+ end
+ end
end