Class: ProjectTeam

Inherits:
Object
  • Object
show all
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


4
5
6
# File 'app/models/project_team.rb', line 4

def initialize(project)
  @project = project
end

Instance Attribute Details

#projectObject

Returns the value of attribute project


2
3
4
# File 'app/models/project_team.rb', line 2

def project
  @project
end

Instance Method Details

#<<(args) ⇒ Object

Shortcut to add users

Use:

@team << [@user, :master]
@team << [@users, :master]

14
15
16
17
18
19
20
21
22
# File 'app/models/project_team.rb', line 14

def <<(args)
  users, access, current_user = *args

  if users.respond_to?(:each)
    add_users(users, access, current_user)
  else
    add_user(users, access, current_user)
  end
end

#add_user(user, access, current_user = nil) ⇒ Object


55
56
57
# File 'app/models/project_team.rb', line 55

def add_user(user, access, current_user = nil)
  add_users([user], access, current_user)
end

#add_users(users, access, current_user = nil) ⇒ Object


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

def add_users(users, access, current_user = nil)
  ProjectMember.add_users_into_projects(
    [project.id],
    users,
    access,
    current_user
  )
end

#developer?(user) ⇒ Boolean

Returns:

  • (Boolean)

126
127
128
# File 'app/models/project_team.rb', line 126

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

#developersObject


80
81
82
# File 'app/models/project_team.rb', line 80

def developers
  @developers ||= fetch_members(:developers)
end

#find(user_id) ⇒ Object


24
25
26
27
28
29
30
31
32
# File 'app/models/project_team.rb', line 24

def find(user_id)
  user = project.users.find_by(id: user_id)

  if group
    user ||= group.users.find_by(id: user_id)
  end

  user
end

#find_member(user_id) ⇒ Object


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

def find_member(user_id)
  member = project.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.group_members.find_by(user_id: user_id)
  end

  member
end

#guest?(user) ⇒ Boolean

Returns:

  • (Boolean)

118
119
120
# File 'app/models/project_team.rb', line 118

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

#guestsObject


72
73
74
# File 'app/models/project_team.rb', line 72

def guests
  @guests ||= fetch_members(:guests)
end

#human_max_access(user_id) ⇒ Object


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

def human_max_access(user_id)
  Gitlab::Access.options_with_owner.key(max_member_access(user_id))
end

#import(source_project, current_user = nil) ⇒ Object


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/models/project_team.rb', line 88

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

#master?(user) ⇒ Boolean

Returns:

  • (Boolean)

130
131
132
# File 'app/models/project_team.rb', line 130

def master?(user)
  max_member_access(user.id) == Gitlab::Access::MASTER
end

#mastersObject


84
85
86
# File 'app/models/project_team.rb', line 84

def masters
  @masters ||= fetch_members(:masters)
end

#max_invited_level(user_id) ⇒ Object


171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'app/models/project_team.rb', line 171

def max_invited_level(user_id)
  project.project_group_links.map do |group_link|
    invited_group = group_link.group
    access = invited_group.group_members.find_by(user_id: user_id).try(:access_field)

    # If group member has higher access level we should restrict it
    # to max allowed access level
    if access && access > group_link.group_access
      access = group_link.group_access
    end

    access
  end.compact.max
end

#max_member_access(user_id) ⇒ Object

This method assumes project and group members are eager loaded for optimal performance.


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'app/models/project_team.rb', line 144

def max_member_access(user_id)
  access = []

  project.project_members.each do |member|
    if member.user_id == user_id
      access << member.access_field if member.access_field
      break
    end
  end

  if group
    group.group_members.each do |member|
      if member.user_id == user_id
        access << member.access_field if member.access_field
        break
      end
    end
  end

  if project.invited_groups.any? && project.allowed_to_share_with_group?
    access << max_invited_level(user_id)
  end

  access.compact.max
end

#member?(user_id) ⇒ Boolean

Returns:

  • (Boolean)

134
135
136
# File 'app/models/project_team.rb', line 134

def member?(user_id)
  !!find_member(user_id)
end

#membersObject


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

def members
  @members ||= fetch_members
end

#reporter?(user) ⇒ Boolean

Returns:

  • (Boolean)

122
123
124
# File 'app/models/project_team.rb', line 122

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

#reportersObject


76
77
78
# File 'app/models/project_team.rb', line 76

def reporters
  @reporters ||= fetch_members(:reporters)
end

#truncateObject

Remove all users from project team


60
61
62
# File 'app/models/project_team.rb', line 60

def truncate
  ProjectMember.truncate_team(project)
end

#usersObject


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

def users
  members
end