BigW Consortium Gitlab

project_team.rb 5.28 KB
Newer Older
1
class ProjectTeam
2 3 4 5
  attr_accessor :project

  def initialize(project)
    @project = project
6 7 8 9 10 11 12 13
  end

  # Shortcut to add users
  #
  # Use:
  #   @team << [@user, :master]
  #   @team << [@users, :master]
  #
14
  def <<(args)
15
    users, access, current_user = *args
16 17

    if users.respond_to?(:each)
18
      add_users(users, access, current_user)
19
    else
20
      add_user(users, access, current_user)
21
    end
22 23
  end

24
  def find(user_id)
skv committed
25
    user = project.users.find_by(id: user_id)
26 27

    if group
skv committed
28
      user ||= group.users.find_by(id: user_id)
29 30 31
    end

    user
32 33
  end

34 35
  def find_member(user_id)
    member = project.project_members.find_by(user_id: user_id)
36 37 38

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

43
    member
44 45
  end

46
  def add_users(users, access, current_user = nil)
47
    ProjectMember.add_users_into_projects(
48
      [project.id],
49 50 51
      users,
      access,
      current_user
52 53 54
    )
  end

55 56 57 58
  def add_user(user, access, current_user = nil)
    add_users([user], access, current_user)
  end

59 60
  # Remove all users from project team
  def truncate
61
    ProjectMember.truncate_team(project)
62 63
  end

64 65 66 67
  def users
    members
  end

68
  def members
69
    @members ||= fetch_members
70 71 72
  end

  def guests
73
    @guests ||= fetch_members(:guests)
74 75 76
  end

  def reporters
77
    @reporters ||= fetch_members(:reporters)
78 79 80
  end

  def developers
81
    @developers ||= fetch_members(:developers)
82 83 84
  end

  def masters
85
    @masters ||= fetch_members(:masters)
86
  end
87

88
  def import(source_project, current_user = nil)
89 90
    target_project = project

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

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

99 100 101 102
    source_members.map! do |member|
      new_member = member.dup
      new_member.id = nil
      new_member.source = target_project
103
      new_member.created_by = current_user
104
      new_member
105 106
    end

107
    ProjectMember.transaction do
108 109
      source_members.each do |member|
        member.save
110 111 112 113 114 115 116
      end
    end

    true
  rescue
    false
  end
117

118
  def guest?(user)
119
    max_member_access(user.id) == Gitlab::Access::GUEST
120 121 122
  end

  def reporter?(user)
123
    max_member_access(user.id) == Gitlab::Access::REPORTER
124 125 126
  end

  def developer?(user)
127
    max_member_access(user.id) == Gitlab::Access::DEVELOPER
128 129 130
  end

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

134
  def member?(user_id)
135
    !!find_member(user_id)
136 137
  end

138
  def human_max_access(user_id)
139
    Gitlab::Access.options_with_owner.key(max_member_access(user_id))
140 141
  end

142 143
  # This method assumes project and group members are eager loaded for optimal
  # performance.
144
  def max_member_access(user_id)
145
    access = []
146 147 148 149 150 151 152

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

    if group
155 156 157 158 159 160
      group.group_members.each do |member|
        if member.user_id == user_id
          access << member.access_field if member.access_field
          break
        end
      end
161 162
    end

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    if project.invited_groups.any? && project.allowed_to_share_with_group?
      access << max_invited_level(user_id)
    end

    access.compact.max
  end


  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
184 185
  end

186 187 188
  private

  def fetch_members(level = nil)
189 190
    project_members = project.project_members
    group_members = group ? group.group_members : []
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    invited_members = []

    if project.invited_groups.any? && project.allowed_to_share_with_group?
      project.project_group_links.each do |group_link|
        invited_group = group_link.group
        im = invited_group.group_members

        if level
          int_level = GroupMember.access_level_roles[level.to_s.singularize.titleize]

          # Skip group members if we ask for masters
          # but max group access is developers
          next if int_level > group_link.group_access

          # If we ask for developers and max
          # group access is developers we need to provide
          # both group master, developers as devs
          if int_level == group_link.group_access
            im.where("access_level >= ?)", group_link.group_access)
          else
            im.send(level)
          end
        end

        invited_members << im
      end

      invited_members = invited_members.flatten.compact
    end
220 221 222

    if level
      project_members = project_members.send(level)
223
      group_members = group_members.send(level) if group
224 225
    end

226
    user_ids = project_members.pluck(:user_id)
227
    user_ids.push(*invited_members.map(&:user_id)) if invited_members.any?
228
    user_ids.push(*group_members.pluck(:user_id)) if group
229 230

    User.where(id: user_ids)
231
  end
232 233 234 235

  def group
    project.group
  end
236
end