BigW Consortium Gitlab

project_team.rb 4.4 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: current_user)
19
    else
20
      add_user(users, access, current_user: current_user)
21
    end
22 23
  end

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
  def add_guest(user, current_user: nil)
    self << [user, :guest, current_user]
  end

  def add_reporter(user, current_user: nil)
    self << [user, :reporter, current_user]
  end

  def add_developer(user, current_user: nil)
    self << [user, :developer, current_user]
  end

  def add_master(user, current_user: nil)
    self << [user, :master, current_user]
  end

40
  def find_member(user_id)
41
    member = project.members.find_by(user_id: user_id)
42 43 44

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

49
    member
50 51
  end

52
  def add_users(users, access_level, current_user: nil, expires_at: nil)
53
    ProjectMember.add_users_to_projects(
54
      [project.id],
55
      users,
56
      access_level,
57 58
      current_user: current_user,
      expires_at: expires_at
59 60 61
    )
  end

62 63 64 65 66 67 68 69
  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
    )
70 71
  end

72 73
  # Remove all users from project team
  def truncate
74
    ProjectMember.truncate_team(project)
75 76 77
  end

  def members
78
    @members ||= fetch_members
79
  end
80
  alias_method :users, :members
81 82

  def guests
83
    @guests ||= fetch_members(Gitlab::Access::GUEST)
84 85 86
  end

  def reporters
87
    @reporters ||= fetch_members(Gitlab::Access::REPORTER)
88 89 90
  end

  def developers
91
    @developers ||= fetch_members(Gitlab::Access::DEVELOPER)
92 93 94
  end

  def masters
95
    @masters ||= fetch_members(Gitlab::Access::MASTER)
96
  end
97

98
  def import(source_project, current_user = nil)
99 100
    target_project = project

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

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

109 110 111 112
    source_members.map! do |member|
      new_member = member.dup
      new_member.id = nil
      new_member.source = target_project
113
      new_member.created_by = current_user
114
      new_member
115 116
    end

117
    ProjectMember.transaction do
118 119
      source_members.each do |member|
        member.save
120 121 122 123 124 125 126
      end
    end

    true
  rescue
    false
  end
127

128
  def guest?(user)
129
    max_member_access(user.id) == Gitlab::Access::GUEST
130 131 132
  end

  def reporter?(user)
133
    max_member_access(user.id) == Gitlab::Access::REPORTER
134 135 136
  end

  def developer?(user)
137
    max_member_access(user.id) == Gitlab::Access::DEVELOPER
138 139 140
  end

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

144 145 146 147 148 149
  # Checks if `user` is authorized for this project, with at least the
  # `min_access_level` (if given).
  def member?(user, min_access_level = Gitlab::Access::GUEST)
    return false unless user

    user.authorized_project?(project, min_access_level)
150 151
  end

152 153 154 155
  def human_max_access(user_id)
    Gitlab::Access.options_with_owner.key(max_member_access(user_id))
  end

156 157
  # Determine the maximum access level for a group of users in bulk.
  #
Stan Hu committed
158
  # Returns a Hash mapping user ID -> maximum access level.
159 160 161
  def max_member_access_for_user_ids(user_ids)
    user_ids = user_ids.uniq
    key = "max_member_access:#{project.id}"
162 163 164 165 166 167 168

    access = {}

    if RequestStore.active?
      RequestStore.store[key] ||= {}
      access = RequestStore.store[key]
    end
169

170 171
    # Lookup only the IDs we need
    user_ids = user_ids - access.keys
172 173 174

    return access if user_ids.empty?

175 176 177 178
    users_access = project.project_authorizations.
      where(user: user_ids).
      group(:user_id).
      maximum(:access_level)
179

180
    access.merge!(users_access)
181 182 183 184
    access
  end

  def max_member_access(user_id)
185
    max_member_access_for_user_ids([user_id])[user_id] || Gitlab::Access::NO_ACCESS
186 187
  end

188
  private
189

190
  def fetch_members(level = nil)
191 192
    members = project.authorized_users
    members = members.where(project_authorizations: { access_level: level }) if level
193

194
    members
195
  end
196 197 198 199

  def group
    project.group
  end
200
end