Class: AuthorizedProjectUpdate::FindRecordsDueForRefreshService

Inherits:
Object
  • Object
show all
Defined in:
app/services/authorized_project_update/find_records_due_for_refresh_service.rb

Overview

Service for finding the authorized_projects records of a user that needs addition or removal.

Usage:

user = User.find_by(username: 'alice')
service = AuthorizedProjectUpdate::FindRecordsDueForRefreshService.new(some_user)
service.execute

Instance Method Summary collapse

Constructor Details

#initialize(user, source: nil, incorrect_auth_found_callback: nil, missing_auth_found_callback: nil) ⇒ FindRecordsDueForRefreshService

Returns a new instance of FindRecordsDueForRefreshService.


12
13
14
15
16
17
# File 'app/services/authorized_project_update/find_records_due_for_refresh_service.rb', line 12

def initialize(user, source: nil, incorrect_auth_found_callback: nil, missing_auth_found_callback: nil)
  @user = user
  @source = source
  @incorrect_auth_found_callback = incorrect_auth_found_callback
  @missing_auth_found_callback = missing_auth_found_callback
end

Instance Method Details

#current_authorizationsObject


77
78
79
# File 'app/services/authorized_project_update/find_records_due_for_refresh_service.rb', line 77

def current_authorizations
  @current_authorizations ||= user.project_authorizations.select(:project_id, :access_level)
end

#current_authorizations_per_projectObject


73
74
75
# File 'app/services/authorized_project_update/find_records_due_for_refresh_service.rb', line 73

def current_authorizations_per_project
  current_authorizations.index_by(&:project_id)
end

#executeObject


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/services/authorized_project_update/find_records_due_for_refresh_service.rb', line 19

def execute
  current = current_authorizations_per_project
  fresh = fresh_access_levels_per_project

  # Projects that have more than one authorizations associated with
  # the user needs to be deleted.
  # The correct authorization is added to the ``add`` array in the
  # next stage.
  remove = projects_with_duplicates
  current.except!(*projects_with_duplicates)

  remove |= current.each_with_object([]) do |(project_id, row), array|
    # rows not in the new list or with a different access level should be
    # removed.
    if !fresh[project_id] || fresh[project_id] != row.access_level
      if incorrect_auth_found_callback
        incorrect_auth_found_callback.call(project_id, row.access_level)
      end

      array << row.project_id
    end
  end

  add = fresh.each_with_object([]) do |(project_id, level), array|
    # rows not in the old list or with a different access level should be
    # added.
    if !current[project_id] || current[project_id].access_level != level
      if missing_auth_found_callback
        missing_auth_found_callback.call(project_id, level)
      end

      array << {
        user_id: user.id,
        project_id: project_id,
        access_level: level
      }
    end
  end

  [remove, add]
end

#fresh_access_levels_per_projectObject


67
68
69
70
71
# File 'app/services/authorized_project_update/find_records_due_for_refresh_service.rb', line 67

def fresh_access_levels_per_project
  fresh_authorizations.each_with_object({}) do |row, hash|
    hash[row.project_id] = row.access_level
  end
end

#fresh_authorizationsObject


81
82
83
# File 'app/services/authorized_project_update/find_records_due_for_refresh_service.rb', line 81

def fresh_authorizations
  Gitlab::ProjectAuthorizations.new(user).calculate
end

#needs_refresh?Boolean

Returns:

  • (Boolean)

61
62
63
64
65
# File 'app/services/authorized_project_update/find_records_due_for_refresh_service.rb', line 61

def needs_refresh?
  remove, add = execute

  remove.present? || add.present?
end