Sha256: 06897033a7016663fb91fd56e5e497245b014fb5c8e976a0aa3fe9e6440bbf58

Contents?: true

Size: 1.43 KB

Versions: 15

Compression:

Stored size: 1.43 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])
        yield
      ensure
        User.current = nil
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
foreman-tasks-0.10.9 app/lib/actions/middleware/keep_current_user.rb
foreman-tasks-0.11.0 app/lib/actions/middleware/keep_current_user.rb
foreman-tasks-0.10.8 app/lib/actions/middleware/keep_current_user.rb
foreman-tasks-0.10.7 app/lib/actions/middleware/keep_current_user.rb
foreman-tasks-0.10.6 app/lib/actions/middleware/keep_current_user.rb
foreman-tasks-0.10.4 app/lib/actions/middleware/keep_current_user.rb
foreman-tasks-0.9.6 app/lib/actions/middleware/keep_current_user.rb
foreman-tasks-0.10.3 app/lib/actions/middleware/keep_current_user.rb
foreman-tasks-0.10.2 app/lib/actions/middleware/keep_current_user.rb
foreman-tasks-0.10.1 app/lib/actions/middleware/keep_current_user.rb
foreman-tasks-0.9.5 app/lib/actions/middleware/keep_current_user.rb
foreman-tasks-0.10.0 app/lib/actions/middleware/keep_current_user.rb
foreman-tasks-0.9.4 app/lib/actions/middleware/keep_current_user.rb
foreman-tasks-0.9.3 app/lib/actions/middleware/keep_current_user.rb
foreman-tasks-0.9.2 app/lib/actions/middleware/keep_current_user.rb