Sha256: 05f791dff8429b616186c1e6640cf5fa1b44c5dc507fdce28d62e9eac9e04075

Contents?: true

Size: 1 KB

Versions: 3

Compression:

Stored size: 1 KB

Contents

# frozen_string_literal: true

module Hydra
  module RoleManagement
    # Module defining Controller actions for adding and managing Roles for Users
    module UserRolesBehavior
      extend ActiveSupport::Concern

      included do
        load_and_authorize_resource :role
      end

      def create
        authorize! :add_user, @role
        u = find_user
        if u
          u.roles << @role
          u.save!
          redirect_to role_management.role_path(@role)
        else
          redirect_to role_management.role_path(@role),
                      flash: { error: "Invalid user #{params[:user_key]}" }
        end
      end

      def destroy
        authorize! :remove_user, @role
        @role.users.delete(::User.find(params[:id]))
        redirect_to role_management.role_path(@role)
      end

      protected

      def find_user
        ::User.send("find_by_#{find_column}".to_sym, params[:user_key])
      end

      def find_column
        Devise.authentication_keys.first
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hydra-role-management-1.2.0 app/controllers/concerns/hydra/role_management/user_roles_behavior.rb
hydra-role-management-1.1.0 app/controllers/concerns/hydra/role_management/user_roles_behavior.rb
hydra-role-management-1.0.3 app/controllers/concerns/hydra/role_management/user_roles_behavior.rb