Class: Entitlements::Models::Group

Inherits:
Object
  • Object
show all
Includes:
Contracts::Core
Defined in:
lib/entitlements.rb,
lib/entitlements/models/group.rb

Defined Under Namespace

Classes: NoMembers, NoMetadata

Constant Summary collapse

C =
::Contracts

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Contracts::Core

common, extended, included

Constructor Details

#initialize(dn:, members:, description: nil, metadata: nil) ⇒ Group

Returns a new instance of Group.



30
31
32
33
34
35
# File 'lib/entitlements/models/group.rb', line 30

def initialize(dn:, members:, description: nil, metadata: nil)
  @dn = dn
  set_members(members)
  @description = description == [] ? "" : description
   = 
end

Instance Attribute Details

#dnObject (readonly)

Returns the value of attribute dn.



12
13
14
# File 'lib/entitlements/models/group.rb', line 12

def dn
  @dn
end

Instance Method Details

#add_member(person) ⇒ Object



218
219
220
221
222
223
224
# File 'lib/entitlements/models/group.rb', line 218

def add_member(person)
  @members.add(person)

  # Clear these so they will be recomputed
  @member_strings = nil
  @member_strings_insensitive = nil
end

#cnObject



132
133
134
135
# File 'lib/entitlements/models/group.rb', line 132

def cn
  return Regexp.last_match(1) if dn =~ /\Acn=(.+?),/
  raise "Could not determine CN from group DN #{dn.inspect}!"
end

#copy_of(dn) ⇒ Object



43
44
45
# File 'lib/entitlements/models/group.rb', line 43

def copy_of(dn)
  self.class.new(dn: dn, members: members.dup, description: description, metadata: .dup)
end

#descriptionObject



58
59
60
61
# File 'lib/entitlements/models/group.rb', line 58

def description
  return cn if @description.nil? || @description.empty?
  @description
end

#equals?(other_group) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/entitlements/models/group.rb', line 154

def equals?(other_group)
  unless other_group.is_a?(Entitlements::Models::Group)
    return false
  end

  unless dn == other_group.dn
    return false
  end

  unless description == other_group.description
    return false
  end

  unless member_strings == other_group.member_strings
    return false
  end

  true
end

#member?(person) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/entitlements/models/group.rb', line 122

def member?(person)
  member_strings_insensitive.member?(any_to_uid(person).downcase)
end

#member_stringsObject



97
98
99
100
101
102
103
104
105
# File 'lib/entitlements/models/group.rb', line 97

def member_strings
  @member_strings ||= begin
    result = Set.new(@members.map { |member| member.is_a?(Entitlements::Models::Person) ? member.uid : member })
    if result.empty? && !no_members_ok?
      raise NoMembers, "The group #{dn} has no members!"
    end
    result
  end
end

#member_strings_insensitiveObject

Retrieve the members as a string of DNs, case-insensitive.

Takes no arguments.

Returns Set.



112
113
114
# File 'lib/entitlements/models/group.rb', line 112

def member_strings_insensitive
  @member_strings_insensitive ||= Set.new(member_strings.map(&:downcase))
end

#members(people_obj: nil) ⇒ Object

Raises:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/entitlements/models/group.rb', line 71

def members(people_obj: nil)
  result = Set.new(
    @members.map do |member|
      if member.is_a?(Entitlements::Models::Person)
        member
      elsif people_obj.nil?
        nil
      elsif people_obj.read.key?(member)
        people_obj.read(member)
      else
        nil
      end
    end.compact
  )

  return result if result.any? || no_members_ok?
  raise NoMembers, "The group #{dn} has no members!"
end

#metadataObject

Raises:



143
144
145
146
# File 'lib/entitlements/models/group.rb', line 143

def 
  return  if 
  raise , "Group #{dn} was not constructed with metadata!"
end

#metadata_fetch_if_exists(key) ⇒ Object



183
184
185
186
# File 'lib/entitlements/models/group.rb', line 183

def (key)
  return unless .is_a?(Hash)
  [key]
end

#no_members_ok?Boolean

Returns:

  • (Boolean)


195
196
197
# File 'lib/entitlements/models/group.rb', line 195

def no_members_ok?
  ![false, "false"].include?(("no_members_ok"))
end

#remove_member(person) ⇒ Object



233
234
235
236
237
238
239
240
# File 'lib/entitlements/models/group.rb', line 233

def remove_member(person)
  person_uid = any_to_uid(person).downcase
  @members.delete_if { |member| any_to_uid(member).downcase == person_uid }

  # Clear these so they will be recomputed
  @member_strings = nil
  @member_strings_insensitive = nil
end

#set_members(members) ⇒ Object



206
207
208
209
210
# File 'lib/entitlements/models/group.rb', line 206

def set_members(members)
  @members = members
  @member_strings = nil
  @member_strings_insensitive = nil
end

#update_case(person) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/entitlements/models/group.rb', line 248

def update_case(person)
  person_uid = any_to_uid(person)
  downcased_dn = person_uid.downcase

  the_member = @members.find { |member| any_to_uid(member).downcase == downcased_dn }
  return false unless the_member
  return false if any_to_uid(the_member) == person_uid

  remove_member(person_uid)
  add_member(person_uid)
  true
end