Sha256: 59776fe48b562260e3bf3ae56d08d1504e687a96e8352f8909495d2f2c875b48

Contents?: true

Size: 1.38 KB

Versions: 6

Compression:

Stored size: 1.38 KB

Contents

module Sufia
  module DepositorsControllerBehavior
    extend ActiveSupport::Concern

    included do
      before_action :authenticate_user!
      before_action :validate_users, only: :create
    end

    # Overriding the default behavior from Hydra::Core::ContorllerBehavior
    def deny_access(exception)
      if current_user && current_user.persisted?
        redirect_to root_path, alert: exception.message
      else
        session['user_return_to'.freeze] = request.url
        redirect_to new_user_session_path, alert: exception.message
      end
    end

    def create
      grantor = authorize_and_return_grantor
      grantee = ::User.from_url_component(params[:grantee_id])
      if grantor.can_receive_deposits_from.include?(grantee)
        head :ok
      else
        grantor.can_receive_deposits_from << grantee
        render json: { name: grantee.name, delete_path: sufia.user_depositor_path(grantor.user_key, grantee.user_key) }
      end
    end

    def destroy
      grantor = authorize_and_return_grantor
      grantor.can_receive_deposits_from.delete(::User.from_url_component(params[:id]))
      head :ok
    end

    def validate_users
      head :ok if params[:user_id] == params[:grantee_id]
    end

    private

      def authorize_and_return_grantor
        grantor = ::User.from_url_component(params[:user_id])
        authorize! :edit, grantor
        grantor
      end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
sufia-6.7.0 app/controllers/concerns/sufia/depositors_controller_behavior.rb
sufia-6.6.1 app/controllers/concerns/sufia/depositors_controller_behavior.rb
sufia-6.6.0 app/controllers/concerns/sufia/depositors_controller_behavior.rb
sufia-6.5.0 app/controllers/concerns/sufia/depositors_controller_behavior.rb
sufia-6.4.0 app/controllers/concerns/sufia/depositors_controller_behavior.rb
sufia-6.3.0 app/controllers/concerns/sufia/depositors_controller_behavior.rb