BigW Consortium Gitlab

project_team.rb 3.69 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 139 140 141
  def human_max_access(user_id)
    Gitlab::Access.options.key max_member_access(user_id)
  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
    access.max
164 165
  end

166 167 168
  private

  def fetch_members(level = nil)
169 170
    project_members = project.project_members
    group_members = group ? group.group_members : []
171 172 173

    if level
      project_members = project_members.send(level)
174
      group_members = group_members.send(level) if group
175 176
    end

177
    user_ids = project_members.pluck(:user_id)
178
    user_ids.push(*group_members.pluck(:user_id)) if group
179 180

    User.where(id: user_ids)
181
  end
182 183 184 185

  def group
    project.group
  end
186
end