aboutsummaryrefslogtreecommitdiffstats
path: root/app/policies/application_policy.rb
blob: 4a2d760fb044e5aba4ed200d5135eb2285f629e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class ApplicationPolicy
  attr_reader :user, :record

  def initialize(user_context, record)
    @user = user_context.user
    @referential = user_context.context[:referential]
    @record = record
  end

  attr_accessor :referential
  def referential
    @referential ||= record_referential
  end

  def record_referential
    record.referential if record.respond_to?(:referential)
  end

  def index?
    false
  end

  def show?
    scope.where(:id => record.id).exists?
  end

  def create?
    false
  end

  def new?
    create?
  end

  def update?
    false
  end

  def edit?
    update?
  end

  def destroy?
    false
  end

  def scope
    Pundit.policy_scope!(user, record.class)
  end

  def organisation_match?
    user.organisation == organisation
  end

  def organisation
    # When sending permission to react UI, we don't have access to record object for edit & destroy.. actions
    organisation = record.is_a?(Symbol) ? nil : record.try(:organisation)
    organisation or referential.try :organisation
  end

  class Scope
    attr_reader :user, :scope

    def initialize(user, scope)
      @user = user
      @scope = scope
    end

    def resolve
      scope
    end
  end
end