Sha256: 7feb1a46a64d3de56beed3caa2f2824d3475fa935cc863acbdbe61815fe71735

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

require 'active_support'

module Rbacan
    module Permittable
        extend ActiveSupport::Concern

        included do

            has_many :user_roles, class_name: Rbacan.user_role_class, dependent: :destroy
            has_many :roles, class_name: Rbacan.role_class, through: :user_roles

            def assign_role(role_name)
                assigned_role = Rbacan::Role.find_by_name(role_name)
                self.user_roles.find_or_create_by(role_id: assigned_role.id)
                self.save if self.persisted?
            end

            def remove_role(role_name)
                removed_role = Rbacan::Role.find_by_name(role_name)
                Rbacan::UserRole.where(user_id: self.id, role_id: removed_role.id).destroy_all
            end

            def can?(permission)
                @user_roles = self.roles
                user_permission = Rbacan::Permission.find_by_name(permission)
                if user_permission && @user_roles.joins(:role_permissions).where(role_permissions: {permission_id: user_permission.id}).count > 0
                    return true
                else
                    return false
                end
            end
        end
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rbacan-0.1.2 lib/rbacan/permittable.rb