Read about our upcoming Code of Conduct on this issue

This instance was upgraded to Heptapod 0.28.0 today

project_member.rb 3.17 KB
Newer Older
1
2
# frozen_string_literal: true

3
class ProjectMember < Member
4
  SOURCE_TYPE = 'Project'
5

6
  belongs_to :project, foreign_key: 'source_id'
7

8
9
  # Make sure project member points only to project as it source
  default_value_for :source_type, SOURCE_TYPE
Douwe Maan's avatar
Douwe Maan committed
10
  validates :source_type, format: { with: /\AProject\z/ }
11
  validates :access_level, inclusion: { in: Gitlab::Access.values }
12
13
14
  default_scope { where(source_type: SOURCE_TYPE) }

  scope :in_project, ->(project) { where(source_id: project.id) }
15
16
17
18
  scope :in_namespaces, ->(groups) do
    joins('INNER JOIN projects ON projects.id = members.source_id')
      .where('projects.namespace_id in (?)', groups.select(:id))
  end
19
20

  class << self
21
    # Add users to projects with passed access option
22
23
    #
    # access can be an integer representing a access code
24
    # or symbol like :maintainer representing role
25
26
    #
    # Ex.
27
    #   add_users_to_projects(
28
29
    #     project_ids,
    #     user_ids,
30
    #     ProjectMember::MAINTAINER
31
32
    #   )
    #
33
    #   add_users_to_projects(
34
35
    #     project_ids,
    #     user_ids,
36
    #     :maintainer
37
38
    #   )
    #
39
40
    def add_users_to_projects(project_ids, users, access_level, current_user: nil, expires_at: nil)
      self.transaction do
41
        project_ids.each do |project_id|
42
43
          project = Project.find(project_id)

44
          add_users(
45
46
47
48
49
50
            project,
            users,
            access_level,
            current_user: current_user,
            expires_at: expires_at
          )
51
52
53
54
55
56
57
        end
      end
    end

    def truncate_teams(project_ids)
      ProjectMember.transaction do
        members = ProjectMember.where(source_id: project_ids)
58

59
60
61
62
63
64
65
66
67
68
        members.each do |member|
          member.destroy
        end
      end

      true
    rescue
      false
    end

69
    def truncate_team(project)
70
71
72
      truncate_teams [project.id]
    end

Rémy Coutable's avatar
Rémy Coutable committed
73
    def access_level_roles
74
75
      Gitlab::Access.options
    end
76
77
78
79
80
81

    private

    def can_update_member?(current_user, member)
      super || (member.owner? && member.new_record?)
    end
82
83
  end

Douwe Maan's avatar
Douwe Maan committed
84
85
86
87
  def project
    source
  end

88
89
90
91
  def owner?
    project.owner == user
  end

92
93
  def notifiable_options
    { project: project }
94
95
  end

Douwe Maan's avatar
Douwe Maan committed
96
97
  private

Rémy Coutable's avatar
Rémy Coutable committed
98
  def send_invite
99
    run_after_commit_or_now { notification_service.invite_project_member(self, @raw_invite_token) }
100
101
102
103

    super
  end

104
  def post_create_hook
105
106
    unless owner?
      event_service.join_project(self.project, self.user)
107
      run_after_commit_or_now { notification_service.new_project_member(self) }
108
    end
109

Douwe Maan's avatar
Douwe Maan committed
110
    super
111
112
113
  end

  def post_update_hook
Jasper Maes's avatar
Jasper Maes committed
114
    if saved_change_to_access_level?
115
      run_after_commit { notification_service.update_project_member(self) }
Douwe Maan's avatar
Douwe Maan committed
116
117
118
    end

    super
119
120
121
  end

  def post_destroy_hook
122
123
124
125
126
    if expired?
      event_service.expired_leave_project(self.project, self.user)
    else
      event_service.leave_project(self.project, self.user)
    end
Douwe Maan's avatar
Douwe Maan committed
127
128

    super
129
130
  end

Douwe Maan's avatar
Douwe Maan committed
131
132
133
134
135
136
  def after_accept_invite
    notification_service.accept_project_invite(self)

    super
  end

Douwe Maan's avatar
Douwe Maan committed
137
138
139
140
141
142
  def after_decline_invite
    notification_service.decline_project_invite(self)

    super
  end

143
  # rubocop: disable CodeReuse/ServiceClass
144
145
146
  def event_service
    EventCreateService.new
  end
147
  # rubocop: enable CodeReuse/ServiceClass
148
end