Sha256: 6dc43fb24eb4b85f05cb3f10fca33026622e5b62ef5682f94071da702c15d844

Contents?: true

Size: 1.47 KB

Versions: 5

Compression:

Stored size: 1.47 KB

Contents

#
# Copyright 2014 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public
# License as published by the Free Software Foundation; either version
# 2 of the License (GPLv2) or (at your option) any later version.
# There is NO WARRANTY for this software, express or implied,
# including the implied warranties of MERCHANTABILITY,
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
# have received a copy of GPLv2 along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.

module Actions
  module Middleware
    class KeepCurrentUser < Dynflow::Middleware
      def delay(*args)
        pass(*args).tap { store_current_user }
      end

      def plan(*args)
        with_current_user do
          pass(*args).tap { store_current_user }
        end
      end

      def run(*args)
        restore_curent_user { pass(*args) }
      end

      def finalize
        restore_curent_user { pass }
      end

      private

      def with_current_user
        if User.current || action.input[:current_user_id].nil?
          yield
        else
          restore_curent_user { yield }
        end
      end

      def store_current_user
        action.input[:current_user_id] = User.current.try(:id)
      end

      def restore_curent_user
        User.current = User.unscoped.find(action.input[:current_user_id]) if action.input[:current_user_id].present?
        yield
      ensure
        User.current = nil
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
foreman-tasks-0.11.3 app/lib/actions/middleware/keep_current_user.rb
foreman-tasks-0.11.2 app/lib/actions/middleware/keep_current_user.rb
foreman-tasks-0.12.1 app/lib/actions/middleware/keep_current_user.rb
foreman-tasks-0.12.0 app/lib/actions/middleware/keep_current_user.rb
foreman-tasks-0.11.1 app/lib/actions/middleware/keep_current_user.rb