diff options
author | James Edwards-Jones <jedwardsjones@gitlab.com> | 2019-04-24 17:50:00 +0700 |
---|---|---|
committer | James Edwards-Jones <jedwardsjones@gitlab.com> | 2019-05-02 12:28:26 +0700 |
commit | 5faa98f481e0f5e0ccb1758c34a104f523ab21d2 (patch) | |
tree | 3119efc05e2ecc91cc0d91217c0869b69f656ea6 /lib/gitlab | |
parent | 0f863c68bb8bc5054a22e0c553a933c83bea4df6 (diff) | |
download | gitlab-ce-5faa98f481e0f5e0ccb1758c34a104f523ab21d2.tar.gz |
Session stored globally per requestjej/session-stored-globaly
- This can be accessed with Session.current and is restored after.
- Data can be stored under a key with NamespacedSessionStore
Diffstat (limited to 'lib/gitlab')
-rw-r--r-- | lib/gitlab/namespaced_session_store.rb | 22 | ||||
-rw-r--r-- | lib/gitlab/session.rb | 27 |
2 files changed, 49 insertions, 0 deletions
diff --git a/lib/gitlab/namespaced_session_store.rb b/lib/gitlab/namespaced_session_store.rb new file mode 100644 index 00000000000..34520078bfb --- /dev/null +++ b/lib/gitlab/namespaced_session_store.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module Gitlab + class NamespacedSessionStore + delegate :[], :[]=, to: :store + + def initialize(key) + @key = key + end + + def initiated? + !Session.current.nil? + end + + def store + return unless Session.current + + Session.current[@key] ||= {} + Session.current[@key] + end + end +end diff --git a/lib/gitlab/session.rb b/lib/gitlab/session.rb new file mode 100644 index 00000000000..7487ba04a6d --- /dev/null +++ b/lib/gitlab/session.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +module Gitlab + class Session + STORE_KEY = :session_storage + + class << self + def with_session(session) + old = self.current + self.current = session + yield + ensure + self.current = old + end + + def current + Thread.current[STORE_KEY] + end + + protected + + def current=(value) + Thread.current[STORE_KEY] = value + end + end + end +end |