Sha256: 4e315e9503ba07cb5cd35adb57395a0f5cae225232730fc7b2695c350e28549a

Contents?: true

Size: 1.75 KB

Versions: 57

Compression:

Stored size: 1.75 KB

Contents

module Model
  module Cmor
    module Rbac
      module UserConcern
        extend ActiveSupport::Concern

        included do
          # associations
          has_many :user_roles, class_name: 'Cmor::Rbac::UserRole', inverse_of: :user, dependent: :destroy
          has_many :roles, class_name: 'Cmor::Rbac::Role', through: :user_roles
          has_many :role_permissions, class_name: 'Cmor::Rbac::RolePermission', through: :roles
          has_many :permissions, through: :role_permissions, class_name: 'Cmor::Rbac::Permission'

          has_many :enabled_roles, -> { enabled }, class_name: 'Cmor::Rbac::Role', through: :user_roles # , source: :user
          has_many :enabled_role_permissions, class_name: 'Cmor::Rbac::RolePermission', through: :enabled_roles, source: :user_roles
          
          has_many :enabled_permissions, through: :enabled_role_permissions, class_name: 'Cmor::Rbac::Permission', source: :permission
        end

        def allowed_to?(permission_identifier)
          enabled_permissions.map(&:identifier).map(&:to_sym).include?(permission_identifier.to_sym)
        end

        def has_role?(role_identifier)
          enabled_roles.map(&:identifier).map(&:to_sym).include?(role_identifier.to_sym)
        end

        def add_role(role_identifier)
          role = ::Cmor::Rbac::Role.where(identifier: role_identifier).first
          if role
            roles << role
          else
            false
          end
        end

        def roles_count
          roles.count
        end

        def enabled_roles_count
          enabled_roles.count
        end

        def permissions_count
          permissions.count
        end

        def enabled_permissions_count
          enabled_permissions.count
        end
      end
    end
  end
end

Version data entries

57 entries across 57 versions & 1 rubygems

Version Path
cmor_rbac-0.0.17.pre app/models/concerns/model/cmor/rbac/user_concern.rb
cmor_rbac-0.0.16.pre app/models/concerns/model/cmor/rbac/user_concern.rb
cmor_rbac-0.0.15.pre app/models/concerns/model/cmor/rbac/user_concern.rb
cmor_rbac-0.0.14.pre app/models/concerns/model/cmor/rbac/user_concern.rb
cmor_rbac-0.0.13.pre app/models/concerns/model/cmor/rbac/user_concern.rb
cmor_rbac-0.0.12.pre app/models/concerns/model/cmor/rbac/user_concern.rb
cmor_rbac-0.0.11.pre app/models/concerns/model/cmor/rbac/user_concern.rb
cmor_rbac-0.0.10.pre app/models/concerns/model/cmor/rbac/user_concern.rb
cmor_rbac-0.0.9.pre app/models/concerns/model/cmor/rbac/user_concern.rb
cmor_rbac-0.0.8.pre app/models/concerns/model/cmor/rbac/user_concern.rb
cmor_rbac-0.0.7.pre app/models/concerns/model/cmor/rbac/user_concern.rb
cmor_rbac-0.0.6.pre app/models/concerns/model/cmor/rbac/user_concern.rb
cmor_rbac-0.0.5.pre app/models/concerns/model/cmor/rbac/user_concern.rb
cmor_rbac-0.0.4.pre app/models/concerns/model/cmor/rbac/user_concern.rb
cmor_rbac-0.0.3.pre app/models/concerns/model/cmor/rbac/user_concern.rb
cmor_rbac-0.0.2.pre app/models/concerns/model/cmor/rbac/user_concern.rb
cmor_rbac-0.0.1.pre app/models/concerns/model/cmor/rbac/user_concern.rb