summaryrefslogtreecommitdiff
path: root/app/models/preloaders/user_max_access_level_in_projects_preloader.rb
blob: 3764e9dcb168138d1d72041ca03ce29d1c0f3238 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# frozen_string_literal: true

module Preloaders
  # This class preloads the max access level (role) for the user within the given projects and
  # stores the values in requests store via the ProjectTeam class.
  class UserMaxAccessLevelInProjectsPreloader
    def initialize(projects, user)
      @projects = projects
      @user = user
    end

    def execute
      # Use reselect to override the existing select to prevent
      # the error `subquery has too many columns`
      # NotificationsController passes in an Array so we need to check the type
      project_ids = @projects.is_a?(ActiveRecord::Relation) ? @projects.reselect(:id) : @projects
      access_levels = @user
        .project_authorizations
        .where(project_id: project_ids)
        .group(:project_id)
        .maximum(:access_level)

      @projects.each do |project|
        access_level = access_levels[project.id] || Gitlab::Access::NO_ACCESS
        ProjectTeam.new(project).write_member_access_for_user_id(@user.id, access_level)
      end
    end
  end
end