aboutsummaryrefslogtreecommitdiff
path: root/app/models/agency.rb
blob: 7ffe489a1e4a9d8463518c6c52da25383491e340 (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
# SPDX-FileCopyrightText: 2020 IN COMMON Collective <collective@incommon.cc>
#
# SPDX-License-Identifier: AGPL-3.0-or-later

class Agency < ApplicationRecord
  include Bitfields

  belongs_to :agent
  belongs_to :user

  bitfield :roles, :observer, :editor, :maintainer, :leader

  class << self
    # Grant role in agent to user
    def grant(agent, user, role)
      r = find_or_create_by(agent: agent, user: user)
      r&.public_send("#{role}=", true) && r&.save
    end

    # Revoke role in agent from user
    def revoke(agent, user, role)
      r = find_by(agent: agent, user: user)
      r&.public_send("#{role}=", false) && r&.save
    end
  end

  # Grant role to current user in current agent
  def grant(role)
    self.class.grant(agent, user, role)
  end

  # Revoke role from current user in current agent
  def revoke(role)
    self.class.revoke(agent, user, role)
  end
end