summaryrefslogtreecommitdiff
path: root/app/models/group_deploy_key.rb
blob: 160ac28b33b30549c5589821948d899f0b86862c (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
# frozen_string_literal: true

class GroupDeployKey < Key
  self.table_name = 'group_deploy_keys'

  has_many :group_deploy_keys_groups, inverse_of: :group_deploy_key, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
  has_many :groups, through: :group_deploy_keys_groups

  validates :user, presence: true

  def type
    'DeployKey'
  end

  def group_deploy_keys_group_for(group)
    group_deploy_keys_groups.find_by(group: group)
  end

  def can_be_edited_for?(user, group)
    Ability.allowed?(user, :update_group_deploy_key, self) ||
      Ability.allowed?(
        user,
        :update_group_deploy_key_for_group,
        group_deploy_keys_group_for(group)
      )
  end

  def group_deploy_keys_groups_for_user(user)
    group_deploy_keys_groups.select do |group_deploy_keys_group|
      Ability.allowed?(user, :read_group, group_deploy_keys_group.group)
    end
  end
end