group_member.rb 1.42 KB
Newer Older
1
class GroupMember < Member
Douwe Maan's avatar
Douwe Maan committed
2
  SOURCE_TYPE = 'Namespace'.freeze
3

4
  belongs_to :group, foreign_key: 'source_id'
5

6
7
  # Make sure group member points only to group as it source
  default_value_for :source_type, SOURCE_TYPE
8
  validates_format_of :source_type, with: /\ANamespace\z/
9
10
11
12
13
14
  default_scope { where(source_type: SOURCE_TYPE) }

  def self.access_level_roles
    Gitlab::Access.options_with_owner
  end

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  def self.access_levels
    Gitlab::Access.sym_options_with_owner
  end

  def self.add_users_to_group(group, users, access_level, current_user: nil, expires_at: nil)
    self.transaction do
      add_users_to_source(
        group,
        users,
        access_level,
        current_user: current_user,
        expires_at: expires_at
      )
    end
  end

31
32
33
34
35
36
37
38
  def group
    source
  end

  def access_field
    access_level
  end

39
40
41
42
43
  # Because source_type is `Namespace`...
  def real_source_type
    'Group'
  end

Douwe Maan's avatar
Douwe Maan committed
44
45
  private

Douwe Maan's avatar
Douwe Maan committed
46
47
48
49
50
51
  def send_invite
    notification_service.invite_group_member(self, @raw_invite_token)

    super
  end

52
  def post_create_hook
53
    notification_service.new_group_member(self)
Douwe Maan's avatar
Douwe Maan committed
54
55

    super
56
57
  end

Douwe Maan's avatar
Douwe Maan committed
58
  def post_update_hook
59
60
61
    if access_level_changed?
      notification_service.update_group_member(self)
    end
62

Douwe Maan's avatar
Douwe Maan committed
63
    super
64
  end
Douwe Maan's avatar
Douwe Maan committed
65
66
67
68
69
70

  def after_accept_invite
    notification_service.accept_group_invite(self)

    super
  end
Douwe Maan's avatar
Douwe Maan committed
71
72
73
74
75
76

  def after_decline_invite
    notification_service.decline_group_invite(self)

    super
  end
77
end