Sha256: 51d265e3045bc0c169a1dcc1894bda8f92c49010db6ea952d92fe6e3c07fe636

Contents?: true

Size: 906 Bytes

Versions: 3

Compression:

Stored size: 906 Bytes

Contents

module Hydra
  module RoleManagement
    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=>"Unable to find the 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.0.0 app/controllers/concerns/hydra/role_management/user_roles_behavior.rb
hydra-role-management-0.2.2 app/controllers/concerns/hydra/role_management/user_roles_behavior.rb
hydra-role-management-0.2.1 app/controllers/concerns/hydra/role_management/user_roles_behavior.rb