Class: Entitlements::Backend::BaseProvider

Inherits:
Object
  • Object
show all
Includes:
Contracts::Core
Defined in:
lib/entitlements/backend/base_provider.rb

Constant Summary collapse

C =
::Contracts

Instance Method Summary collapse

Methods included from Contracts::Core

common, extended, included

Instance Method Details

#diff(group, ignored_users = Set.new) ⇒ Object



19
20
21
22
# File 'lib/entitlements/backend/base_provider.rb', line 19

def diff(group, ignored_users = Set.new)
  existing_group = read(group.cn.downcase)
  return diff_existing_updated(existing_group, group, ignored_users)
end

#diff_existing_updated(existing_group, group, ignored_users = Set.new) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/entitlements/backend/base_provider.rb', line 31

def diff_existing_updated(existing_group, group, ignored_users = Set.new)
  # The comparison needs to be done case-insensitive because some backends (e.g. GitHub organizations or teams)
  # may report members with different capitalization than is used in Entitlements. Keep track of correct capitalization
  # of member names here so they can be applied later. Note that `group` (from Entitlements) overrides `existing_group`
  # (from the backend).
  member_with_correct_capitalization = existing_group.member_strings.map { |ms| [ms.downcase, ms] }.to_h
  member_with_correct_capitalization.merge! group.member_strings.map { |ms| [ms.downcase, ms] }.to_h

  existing_members = existing_group.member_strings.map { |u| u.downcase }
  Entitlements::Util::Util.remove_uids(existing_members, ignored_users)

  proposed_members = group.member_strings.map { |u| u.downcase }
  Entitlements::Util::Util.remove_uids(proposed_members, ignored_users)

  added_members = proposed_members - existing_members
  removed_members = existing_members - proposed_members

  {
    added: Set.new(added_members.map { |ms| member_with_correct_capitalization[ms] }),
    removed: Set.new(removed_members.map { |ms| member_with_correct_capitalization[ms] })
  }
end