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

Fix a bug in Device model that never allowed updating the default device

Klaus Zanders11315017da31
diff --git a/modules/two_factor_authentication/app/models/two_factor_authentication/device.rb b/modules/two_factor_authentication/app/models/two_factor_authentication/device.rb
index b18065afef9..9618588635c 100644
--- a/modules/two_factor_authentication/app/models/two_factor_authentication/device.rb
+++ b/modules/two_factor_authentication/app/models/two_factor_authentication/device.rb
@@ -25,6 +25,14 @@ module TwoFactorAuthentication
self.class.has_default? user
end
+ def has_other_default?
+ if persisted?
+ Device.where.not(id:).exists?(active: true, default: true, user:)
+ else
+ has_default?
+ end
+ end
+
##
# Make the device active, and set it as default if no other device exists
def confirm_registration_and_save
@@ -101,7 +109,7 @@ module TwoFactorAuthentication
end
def cannot_set_default_if_exists
- if default && has_default?
+ if default && has_other_default?
errors.add :default, :default_already_exists
end