Sha256: ea30f0e294561a48d86e7377a3806fda4b1f6858e3727cf0e279aa1cf97efb9f

Contents?: true

Size: 1.1 KB

Versions: 2

Compression:

Stored size: 1.1 KB

Contents

module Faalis
  # This concern contains all the permission related methods
  # to use within `Faalis::User`
  module Concerns::User::Permission
    extend ActiveSupport::Concern

    def member_of?(group)
      @group_ids ||= self.groups.all.map(&:id)
      @group_ids.include? group.id
    end

    def has_permission? action, obj
      perm = self.groups.includes(:permissions)
        .where(faalis_permissions: { model: obj, permission_type: action })
        .count
      perm == 1
    end

    def can_not? action, obj
      !has_permission? action, obj
    end

    alias_method :can?, :has_permission?

    def has_ownership?(record)
      has_permission? :ownership, record.name
    end

    def owned?(record)
      if has_permission? :ownership, record.name
        if record.respond_to? :user
          return true if record.user == self
          return false
        end

        if record.respond_to? :__owned_by
          return true if record.__owned_by == self
        end
      end
      false
    end

    def permissions
      groups.includes(:permissions).map(&:permissions).flatten.uniq
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
faalis-2.2.1 app/models/faalis/concerns/user/permission.rb
faalis-2.2.0 app/models/faalis/concerns/user/permission.rb