Module: Concerns::User::Permission

Extended by:
ActiveSupport::Concern
Defined in:
app/models/faalis/concerns/user/permission.rb

Overview

This concern contains all the permission related methods to use within Faalis::User

Instance Method Summary (collapse)

Instance Method Details

- (Boolean) can_not?(action, obj)

Returns:

  • (Boolean)


19
20
21
# File 'app/models/faalis/concerns/user/permission.rb', line 19

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

- (Boolean) has_ownership?(record)

Returns:

  • (Boolean)


25
26
27
# File 'app/models/faalis/concerns/user/permission.rb', line 25

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

- (Boolean) has_permission?(action, obj) Also known as: can?

Returns:

  • (Boolean)


12
13
14
15
16
17
# File 'app/models/faalis/concerns/user/permission.rb', line 12

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

- (Boolean) member_of?(group)

Returns:

  • (Boolean)


7
8
9
10
# File 'app/models/faalis/concerns/user/permission.rb', line 7

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

- (Boolean) owned?(record)

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/faalis/concerns/user/permission.rb', line 29

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

- (Object) permissions



43
44
45
# File 'app/models/faalis/concerns/user/permission.rb', line 43

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