project_member.rb 3.53 KB
Newer Older
1
2
3
4
5
class ProjectMember < Member
  SOURCE_TYPE = 'Project'

  include Gitlab::ShellAdapter

6
7
  belongs_to :project, class_name: 'Project', foreign_key: 'source_id'

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

  scope :in_project, ->(project) { where(source_id: project.id) }
  scope :in_projects, ->(projects) { where(source_id: projects.pluck(:id)) }
  scope :with_user, ->(user) { where(user_id: user.id) }

Long Nguyen's avatar
Long Nguyen committed
17
  before_destroy :delete_member_todos
18

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  class << self

    # Add users to project teams with passed access option
    #
    # access can be an integer representing a access code
    # or symbol like :master representing role
    #
    # Ex.
    #   add_users_into_projects(
    #     project_ids,
    #     user_ids,
    #     ProjectMember::MASTER
    #   )
    #
    #   add_users_into_projects(
    #     project_ids,
    #     user_ids,
    #     :master
    #   )
    #
39
    def add_users_into_projects(project_ids, user_ids, access, current_user = nil)
40
41
42
43
44
45
46
47
      access_level = if roles_hash.has_key?(access)
                       roles_hash[access]
                     elsif roles_hash.values.include?(access.to_i)
                       access
                     else
                       raise "Non valid access"
                     end

48
      users = user_ids.map { |user_id| Member.user_for_id(user_id) }
49

50
51
      ProjectMember.transaction do
        project_ids.each do |project_id|
52
53
54
          project = Project.find(project_id)

          users.each do |user|
55
            Member.add_user(project.project_members, user, access_level, current_user)
56
57
58
59
60
61
62
63
64
65
66
67
          end
        end
      end

      true
    rescue
      false
    end

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

69
70
71
72
73
74
75
76
77
78
        members.each do |member|
          member.destroy
        end
      end

      true
    rescue
      false
    end

79
    def truncate_team(project)
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
      truncate_teams [project.id]
    end

    def roles_hash
      Gitlab::Access.sym_options
    end

    def access_roles
      Gitlab::Access.options
    end
  end

  def access_field
    access_level
  end

Douwe Maan's avatar
Douwe Maan committed
96
97
98
99
  def project
    source
  end

100
101
102
103
  def owner?
    project.owner == user
  end

Douwe Maan's avatar
Douwe Maan committed
104
105
  private

Long Nguyen's avatar
Long Nguyen committed
106
107
108
109
  def delete_member_todos
    user.todos.where(project_id: source_id).destroy_all if user
  end

110
111
112
113
114
115
  def send_request_access
    notification_service.request_access_project_member(self)

    super
  end

Douwe Maan's avatar
Douwe Maan committed
116
117
118
119
120
121
  def send_invite
    notification_service.invite_project_member(self, @raw_invite_token)

    super
  end

122
  def post_create_hook
123
124
    unless owner?
      event_service.join_project(self.project, self.user)
125
      notification_service.new_project_member(self)
126
    end
127

Douwe Maan's avatar
Douwe Maan committed
128
    super
129
130
131
  end

  def post_update_hook
Douwe Maan's avatar
Douwe Maan committed
132
    if access_level_changed?
133
      notification_service.update_project_member(self)
Douwe Maan's avatar
Douwe Maan committed
134
135
136
    end

    super
137
138
139
  end

  def post_destroy_hook
140
    event_service.leave_project(self.project, self.user)
Douwe Maan's avatar
Douwe Maan committed
141
142

    super
143
144
  end

145
146
147
148
149
150
151
152
153
154
155
156
  def after_accept_request_access
    notification_service.accept_project_request_access(self)

    super
  end

  def after_decline_request_access
    notification_service.decline_project_request_access(self)

    super
  end

Douwe Maan's avatar
Douwe Maan committed
157
158
159
160
161
162
  def after_accept_invite
    notification_service.accept_project_invite(self)

    super
  end

Douwe Maan's avatar
Douwe Maan committed
163
164
165
166
167
168
  def after_decline_invite
    notification_service.decline_project_invite(self)

    super
  end

169
170
171
  def event_service
    EventCreateService.new
  end
172
end