Module: GitHub::Ldap::Filter

Included in:
Domain, Group, VirtualGroup
Defined in:
lib/github/ldap/filter.rb

Constant Summary collapse

ALL_GROUPS_FILTER =
Net::LDAP::Filter.eq("objectClass", "groupOfNames") |
Net::LDAP::Filter.eq("objectClass", "groupOfUniqueNames") |
Net::LDAP::Filter.eq("objectClass", "posixGroup")
MEMBERSHIP_NAMES =
%w(member uniqueMember)

Instance Method Summary collapse

Instance Method Details

#all_members_by_uid(uids, uid_attr) ⇒ Object

Filter to get all the members of a group which uid is included in ‘memberUid`.

uids: is an array with all the uids to search. uid_attr: is the names of the uid attribute in the directory.

Returns a Net::LDAP::Filter



97
98
99
# File 'lib/github/ldap/filter.rb', line 97

def all_members_by_uid(uids, uid_attr)
  uids.map {|uid| Net::LDAP::Filter.eq(uid_attr, uid)}.reduce(:|)
end

#group_contains_filter(query) ⇒ Object

Filter groups that match a query cn.

query: is a string to match the cn with.

Returns a Net::LDAP::Filter.



67
68
69
# File 'lib/github/ldap/filter.rb', line 67

def group_contains_filter(query)
  Net::LDAP::Filter.contains("cn", query) & ALL_GROUPS_FILTER
end

#group_filter(group_names) ⇒ Object

Filter to get the configured groups in the ldap server. Takes the list of the group names and generate a filter for the groups with cn that match.

group_names: is an array of group CNs.

Returns a Net::LDAP::Filter.



17
18
19
# File 'lib/github/ldap/filter.rb', line 17

def group_filter(group_names)
  group_names.map {|g| Net::LDAP::Filter.eq("cn", g)}.reduce(:|)
end

#login_filter(uid, login) ⇒ Object

Filter to map a uid with a login. It escapes the login before creating the filter.

uid: the entry field to map. login: the login to map.

Returns a Net::LDAP::Filter.



58
59
60
# File 'lib/github/ldap/filter.rb', line 58

def (uid, )
  Net::LDAP::Filter.eq(uid, Net::LDAP::Filter.escape())
end

#member_filter(entry = nil) ⇒ Object

Filter to check group membership.

entry: finds groups this Net::LDAP::Entry is a member of (optional)

Returns a Net::LDAP::Filter.



26
27
28
29
30
31
32
33
34
# File 'lib/github/ldap/filter.rb', line 26

def member_filter(entry = nil)
  if entry
    MEMBERSHIP_NAMES.
      map {|n| Net::LDAP::Filter.eq(n, entry.dn) }.reduce(:|)
  else
    MEMBERSHIP_NAMES.
      map {|n| Net::LDAP::Filter.pres(n) }.        reduce(:|)
  end
end

#members_of_group(group_dn, attr = 'memberOf') ⇒ Object

Filter to get all the members of a group using the virtual attribute ‘memberOf`.

group_dn: is the group dn to look members for. attr: is the membership attribute.

Returns a Net::LDAP::Filter



77
78
79
# File 'lib/github/ldap/filter.rb', line 77

def members_of_group(group_dn, attr = 'memberOf')
  Net::LDAP::Filter.eq(attr, group_dn)
end

#posix_member_filter(entry, uid_attr) ⇒ Object

Filter to check group membership for posixGroups.

Used by Domain#membership when posix_support_enabled? is true.

entry: finds groups this Net::LDAP::Entry is a member of uid_attr: specifies the memberUid attribute to match with

Returns a Net::LDAP::Filter or nil if no entry has no UID set.



44
45
46
47
48
49
# File 'lib/github/ldap/filter.rb', line 44

def posix_member_filter(entry, uid_attr)
  if !entry[uid_attr].empty?
    entry[uid_attr].map { |uid| Net::LDAP::Filter.eq("memberUid", uid) }.
                    reduce(:|)
  end
end

#subgroups_of_group(group_dn, attr = 'memberOf') ⇒ Object

Filter to get all the members of a group that are groups using the virtual attribute ‘memberOf`.

group_dn: is the group dn to look members for. attr: is the membership attribute.

Returns a Net::LDAP::Filter



87
88
89
# File 'lib/github/ldap/filter.rb', line 87

def subgroups_of_group(group_dn, attr = 'memberOf')
  Net::LDAP::Filter.eq(attr, group_dn) & ALL_GROUPS_FILTER
end