Class: ProjectTeam

Inherits:
Object
  • Object
show all
Includes:
BulkMemberAccessLoad
Defined in:
app/models/project_team.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project) ⇒ ProjectTeam

Returns a new instance of ProjectTeam.


8
9
10
# File 'app/models/project_team.rb', line 8

def initialize(project)
  @project = project
end

Instance Attribute Details

#projectObject

Returns the value of attribute project


6
7
8
# File 'app/models/project_team.rb', line 6

def project
  @project
end

Instance Method Details

#add_developer(user, current_user: nil) ⇒ Object


20
21
22
# File 'app/models/project_team.rb', line 20

def add_developer(user, current_user: nil)
  add_user(user, :developer, current_user: current_user)
end

#add_guest(user, current_user: nil) ⇒ Object


12
13
14
# File 'app/models/project_team.rb', line 12

def add_guest(user, current_user: nil)
  add_user(user, :guest, current_user: current_user)
end

#add_maintainer(user, current_user: nil) ⇒ Object


24
25
26
# File 'app/models/project_team.rb', line 24

def add_maintainer(user, current_user: nil)
  add_user(user, :maintainer, current_user: current_user)
end

#add_reporter(user, current_user: nil) ⇒ Object


16
17
18
# File 'app/models/project_team.rb', line 16

def add_reporter(user, current_user: nil)
  add_user(user, :reporter, current_user: current_user)
end

#add_role(user, role, current_user: nil) ⇒ Object


28
29
30
# File 'app/models/project_team.rb', line 28

def add_role(user, role, current_user: nil)
  public_send(:"add_#{role}", user, current_user: current_user) # rubocop:disable GitlabSecurity/PublicSend
end

#add_user(user, access_level, current_user: nil, expires_at: nil) ⇒ Object


54
55
56
57
58
59
60
61
62
# File 'app/models/project_team.rb', line 54

def add_user(user, access_level, current_user: nil, expires_at: nil)
  ProjectMember.add_user(
    project,
    user,
    access_level,
    current_user: current_user,
    expires_at: expires_at
  )
end

#add_users(users, access_level, current_user: nil, expires_at: nil) ⇒ Object


44
45
46
47
48
49
50
51
52
# File 'app/models/project_team.rb', line 44

def add_users(users, access_level, current_user: nil, expires_at: nil)
  ProjectMember.add_users(
    project,
    users,
    access_level,
    current_user: current_user,
    expires_at: expires_at
  )
end

#contribution_check_for_user_ids(user_ids) ⇒ Object


181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'app/models/project_team.rb', line 181

def contribution_check_for_user_ids(user_ids)
  user_ids = user_ids.uniq
  key = "contribution_check_for_users:#{project.id}"

  Gitlab::SafeRequestStore[key] ||= {}
  contributors = Gitlab::SafeRequestStore[key] || {}

  user_ids -= contributors.keys

  return contributors if user_ids.empty?

  resource_contributors = project.merge_requests
                                 .merged
                                 .where(author_id: user_ids, target_branch: project.default_branch.to_s)
                                 .pluck(:author_id)
                                 .product([true]).to_h

  contributors.merge!(resource_contributors)

  missing_resource_ids = user_ids - resource_contributors.keys

  missing_resource_ids.each do |resource_id|
    contributors[resource_id] = false
  end

  contributors
end

#contributor?(user_id) ⇒ Boolean

Returns:

  • (Boolean)

209
210
211
212
213
# File 'app/models/project_team.rb', line 209

def contributor?(user_id)
  return false if max_member_access(user_id) >= Gitlab::Access::GUEST

  contribution_check_for_user_ids([user_id])[user_id]
end

#developer?(user) ⇒ Boolean

Returns:

  • (Boolean)

145
146
147
# File 'app/models/project_team.rb', line 145

def developer?(user)
  max_member_access(user.id) == Gitlab::Access::DEVELOPER
end

#developersObject


90
91
92
# File 'app/models/project_team.rb', line 90

def developers
  @developers ||= fetch_members(Gitlab::Access::DEVELOPER)
end

#find_member(user_id) ⇒ Object


32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/project_team.rb', line 32

def find_member(user_id)
  member = project.members.find_by(user_id: user_id)

  # If user is not in project members
  # we should check for group membership
  if group && !member
    member = group.members.find_by(user_id: user_id)
  end

  member
end

#guest?(user) ⇒ Boolean

Returns:

  • (Boolean)

137
138
139
# File 'app/models/project_team.rb', line 137

def guest?(user)
  max_member_access(user.id) == Gitlab::Access::GUEST
end

#guestsObject


82
83
84
# File 'app/models/project_team.rb', line 82

def guests
  @guests ||= fetch_members(Gitlab::Access::GUEST)
end

#human_max_access(user_id) ⇒ Object


161
162
163
# File 'app/models/project_team.rb', line 161

def human_max_access(user_id)
  Gitlab::Access.human_access(max_member_access(user_id))
end

#import(source_project, current_user = nil) ⇒ Object


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/models/project_team.rb', line 107

def import(source_project, current_user = nil)
  target_project = project

  source_members = source_project.project_members.to_a
  target_user_ids = target_project.project_members.pluck(:user_id)

  source_members.reject! do |member|
    # Skip if user already present in team
    !member.invite? && target_user_ids.include?(member.user_id)
  end

  source_members.map! do |member|
    new_member = member.dup
    new_member.id = nil
    new_member.source = target_project
    new_member.created_by = current_user
    new_member
  end

  ProjectMember.transaction do
    source_members.each do |member|
      member.save
    end
  end

  true
rescue
  false
end

#maintainer?(user) ⇒ Boolean

Returns:

  • (Boolean)

149
150
151
# File 'app/models/project_team.rb', line 149

def maintainer?(user)
  max_member_access(user.id) == Gitlab::Access::MAINTAINER
end

#maintainersObject


94
95
96
# File 'app/models/project_team.rb', line 94

def maintainers
  @maintainers ||= fetch_members(Gitlab::Access::MAINTAINER)
end

#max_member_access(user_id) ⇒ Object


177
178
179
# File 'app/models/project_team.rb', line 177

def max_member_access(user_id)
  max_member_access_for_user_ids([user_id])[user_id]
end

#max_member_access_for_user_ids(user_ids) ⇒ Object

Determine the maximum access level for a group of users in bulk.

Returns a Hash mapping user ID -> maximum access level.


168
169
170
171
172
173
174
175
# File 'app/models/project_team.rb', line 168

def max_member_access_for_user_ids(user_ids)
  max_member_access_for_resource_ids(User, user_ids, project.id) do |user_ids|
    project.project_authorizations
           .where(user: user_ids)
           .group(:user_id)
           .maximum(:access_level)
  end
end

#member?(user, min_access_level = Gitlab::Access::GUEST) ⇒ Boolean

Checks if `user` is authorized for this project, with at least the `min_access_level` (if given).

Returns:

  • (Boolean)

155
156
157
158
159
# File 'app/models/project_team.rb', line 155

def member?(user, min_access_level = Gitlab::Access::GUEST)
  return false unless user

  max_member_access(user.id) >= min_access_level
end

#membersObject Also known as: users


69
70
71
# File 'app/models/project_team.rb', line 69

def members
  @members ||= fetch_members
end

#members_in_project_and_ancestorsObject

`members` method uses project_authorizations table which is updated asynchronously, on project move it still contains old members who may not have access to the new location, so we filter out only members of project or project's group


78
79
80
# File 'app/models/project_team.rb', line 78

def members_in_project_and_ancestors
  members.where(id: member_user_ids)
end

#ownersObject


98
99
100
101
102
103
104
105
# File 'app/models/project_team.rb', line 98

def owners
  @owners ||=
    if group
      group.owners
    else
      [project.owner]
    end
end

#reporter?(user) ⇒ Boolean

Returns:

  • (Boolean)

141
142
143
# File 'app/models/project_team.rb', line 141

def reporter?(user)
  max_member_access(user.id) == Gitlab::Access::REPORTER
end

#reportersObject


86
87
88
# File 'app/models/project_team.rb', line 86

def reporters
  @reporters ||= fetch_members(Gitlab::Access::REPORTER)
end

#truncateObject

Remove all users from project team


65
66
67
# File 'app/models/project_team.rb', line 65

def truncate
  ProjectMember.truncate_team(project)
end