Sha256: 93fbbd96762a9858f896d79ca85205ef9a420f354bb349f9895bb38ee16c2e4a

Contents?: true

Size: 831 Bytes

Versions: 2

Compression:

Stored size: 831 Bytes

Contents

module RightOn
  module RoleModel
    def self.included(base)
      base.module_eval 'has_and_belongs_to_many :roles, :class_name => "RightOn::Role"'
      Role.module_eval "has_and_belongs_to_many :#{base.table_name}"
    end

    def roles_allowed_to_assign
      Role.accessible_to(self)
    end

    def rights
      @rights ||=
        Right
          .select('distinct rights.*')
          .joins(:roles)
          .where('rights_roles.role_id IN (?)', role_ids)
    end

    def has_access_to?(client_type)
      has_right?(client_type.right)
    end

    def has_right?(right_or_string)
      right = right_or_string.is_a?(Right) ? right_or_string : Right.find_by_name(right_or_string)
      rights.include?(right)
    end

    def has_privileges_of?(other_user)
      (other_user.rights - rights).empty?
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
right_on-0.2.0 lib/right_on/role_model.rb
right_on-0.1.0 lib/right_on/role_model.rb