Sha256: 20e5c1e322e9ea7ed772f88085a3a647650eb6961e23ff7301be0b2f998d608e

Contents?: true

Size: 1.11 KB

Versions: 11

Compression:

Stored size: 1.11 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.class.to_s
    end

    def owned?(record)
      if has_permission? :ownership, record.class.to_s
        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

11 entries across 11 versions & 1 rubygems

Version Path
faalis-2.1.1 app/models/faalis/concerns/user/permission.rb
faalis-2.1.0 app/models/faalis/concerns/user/permission.rb
faalis-2.0.8 app/models/faalis/concerns/user/permission.rb
faalis-2.0.7 app/models/faalis/concerns/user/permission.rb
faalis-2.0.5 app/models/faalis/concerns/user/permission.rb
faalis-2.0.4 app/models/faalis/concerns/user/permission.rb
faalis-2.0.3 app/models/faalis/concerns/user/permission.rb
faalis-2.0.1 app/models/faalis/concerns/user/permission.rb
faalis-2.0.0 app/models/faalis/concerns/user/permission.rb
faalis-2.0.0.rc6 app/models/faalis/concerns/user/permission.rb
faalis-2.0.0.rc5 app/models/faalis/concerns/user/permission.rb