lib/cratus/group.rb in cratus-0.3.9 vs lib/cratus/group.rb in cratus-0.4.0
- old
+ new
@@ -21,10 +21,11 @@
def member_groups
all_members[:groups]
end
+ # Recursively determine group memberships of a group
def member_of
memrof_attr = Cratus.config.group_memberof_attribute
# TODO: make this work with more things...
unless @raw_ldap_data
@@ -46,20 +47,53 @@
all_the_groups.concat(group.member_of) # recursion!
end
all_the_groups.uniq(&:name)
end
+ # Returns the LDAP dn for a Group
+ def dn
+ @raw_ldap_data[:dn].last
+ end
+
# LDAP description attribute
def description
@raw_ldap_data[Cratus.config.group_description_attribute].last
end
+ # Add a User to the group
+ def add_user(user)
+ raise 'InvalidUser' unless user.respond_to?(:dn)
+ direct_members = @raw_ldap_data[Cratus.config.group_member_attribute]
+ return true if direct_members.include?(user.dn)
+
+ direct_members << user.dn
+ Cratus::LDAP.replace_attribute(
+ dn,
+ Cratus.config.group_member_attribute,
+ direct_members.uniq
+ )
+ end
+
+ # Remove a User from the group
+ def remove_user(user)
+ raise 'InvalidUser' unless user.respond_to?(:dn)
+ direct_members = @raw_ldap_data[Cratus.config.group_member_attribute]
+ return true unless direct_members.include?(user.dn)
+
+ direct_members.delete(user.dn)
+ Cratus::LDAP.replace_attribute(
+ dn,
+ Cratus.config.group_member_attribute,
+ direct_members.uniq
+ )
+ end
+
# All the LDAP Groups
def self.all
filter = "(#{ldap_dn_attribute}=*)"
Cratus::LDAP.search(filter, basedn: ldap_search_base, attrs: ldap_dn_attribute).map do |entry|
- new(entry[ldap_dn_attribute].last)
+ new(entry[ldap_dn_attribute.to_sym].last)
end
end
def self.ldap_dn_attribute
Cratus.config.group_dn_attribute.to_s
@@ -80,9 +114,11 @@
def self.ldap_search_base
Cratus.config.group_basedn.to_s
end
+ # Compare based on the group's name
+ # TODO: possibly change to dn
def <=>(other)
@name <=> other.name
end
private