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

memoize anonymous in request store

ulferts611462b03caf
diff --git a/app/models/user.rb b/app/models/user.rb
index a1c35b39bb4..421edc9ebfb 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -678,18 +678,21 @@ class User < Principal
# Returns the anonymous user. If the anonymous user does not exist, it is created. There can be only
# one anonymous user per database.
def self.anonymous
- anonymous_user = AnonymousUser.first
- if anonymous_user.nil?
- (anonymous_user = AnonymousUser.new.tap do |u|
- u.lastname = 'Anonymous'
- u.login = ''
- u.firstname = ''
- u.mail = ''
- u.status = 0
- end).save
- raise 'Unable to create the anonymous user.' if anonymous_user.new_record?
+ RequestStore[:anonymous_user] ||= begin
+ anonymous_user = AnonymousUser.first
+
+ if anonymous_user.nil?
+ (anonymous_user = AnonymousUser.new.tap do |u|
+ u.lastname = 'Anonymous'
+ u.login = ''
+ u.firstname = ''
+ u.mail = ''
+ u.status = 0
+ end).save
+ raise 'Unable to create the anonymous user.' if anonymous_user.new_record?
+ end
+ anonymous_user
end
- anonymous_user
end
def self.system