Class: ActiveDirectory::Group

Inherits:
Base
  • Object
show all
Includes:
Member
Defined in:
lib/active_directory/group.rb

Constant Summary

Constants inherited from Base

Base::NIL_FILTER

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Member

#join, #member_of?, #unjoin

Methods inherited from Base

#==, #changed?, create, #destroy, error, exists?, find, find_all, find_first, #initialize, make_filter_from_hash, method_missing, #method_missing, #move, #new_record?, parse_finder_spec, #save, setup, #update_attribute, #update_attributes

Constructor Details

This class inherits a constructor from ActiveDirectory::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ActiveDirectory::Base

Class Method Details

.filterObject

:nodoc:



28
29
30
# File 'lib/active_directory/group.rb', line 28

def self.filter # :nodoc:
	Net::LDAP::Filter.eq(:objectClass,'group')
end

.required_attributesObject

:nodoc:



32
33
34
# File 'lib/active_directory/group.rb', line 32

def self.required_attributes # :nodoc:
	{ :objectClass => [ 'top', 'group' ] }
end

Instance Method Details

#add(new_member) ⇒ Object

Add the passed User or Group object to this Group. Returns true if the User or Group is already a member of the group, or if the operation to add them succeeds.



59
60
61
62
63
64
65
66
67
68
# File 'lib/active_directory/group.rb', line 59

def add(new_member)
	return false unless new_member.is_a?(User) || new_member.is_a?(Group)
	if @@ldap.modify(:dn => distinguishedName, :operations => [
		[ :add, :member, new_member.distinguishedName ]
	])
		return true
	else
		return has_member?(new_member)
	end
end

#groupsObject

Returns an array of Group objects that this Group belongs to.



157
158
159
160
# File 'lib/active_directory/group.rb', line 157

def groups
	return [] if memberOf.nil?
	@groups ||= memberOf.collect { |group_dn| Group.find_by_distinguishedName(group_dn) }
end

#has_member?(user) ⇒ Boolean

Returns true if the passed User or Group object belongs to this group. For performance reasons, the check is handled by the User or Group object passed.

Returns:

  • (Boolean)


50
51
52
# File 'lib/active_directory/group.rb', line 50

def has_member?(user)
	user.member_of?(self)
end

#has_members?Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
92
# File 'lib/active_directory/group.rb', line 86

def has_members?
	begin
		return (@entry.member.nil? || @entry.member.empty?) ? false : true
	rescue NoMethodError
		return false
	end
end

#member_groups(recursive = false) ⇒ Object

Returns an array of all Group objects that belong to this group.

If the recursive argument is passed as false, then only Groups that belong explicitly to this Group are returned.

If the recursive argument is passed as true, then all Groups that belong to this Group, or any of its subgroups, are returned.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/active_directory/group.rb', line 135

def member_groups(recursive = false)
	return [] unless has_members?
	if recursive
		if @member_groups_r.nil?
			@member_groups_r = []
			@entry.member.each do |member_dn|
				subgroup = Group.find_by_distinguishedName(member_dn)
				if subgroup
					@member_groups_r << subgroup
					@member_groups_r = @member_groups_r.concat(subgroup.member_groups(true))
				end
			end
		end
		return @member_groups_r
	else
		@member_groups_non_r ||= @entry.member.collect { |dn| Group.find_by_distinguishedName(dn) }.delete_if { |g| g.nil? }
	end
end

#member_users(recursive = false) ⇒ Object

Returns an array of all User objects that belong to this group.

If the recursive argument is passed as false, then only Users who belong explicitly to this Group are returned.

If the recursive argument is passed as true, then all Users who belong to this Group, or any of its subgroups, are returned.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/active_directory/group.rb', line 103

def member_users(recursive = false)
	return [] unless has_members?
	if recursive
		if @member_users_r.nil?
			@member_users_r = []
			@entry.member.each do |member_dn|
				subuser = User.find_by_distinguishedName(member_dn)
				if subuser
					@member_users_r << subuser
				else
					subgroup = Group.find_by_distinguishedName(member_dn)
					if subgroup
						@member_users_r = @member_users_r.concat(subgroup.member_users(true))
					end
				end
			end
		end
		return @member_users_r
	else
		@member_users_non_r ||= @entry.member.collect { |dn| User.find_by_distinguishedName(dn) }.delete_if { |u| u.nil? }
	end
end

#reloadObject

:nodoc:



36
37
38
39
40
41
42
43
# File 'lib/active_directory/group.rb', line 36

def reload # :nodoc:
	@member_users_non_r  = nil
	@member_users_r      = nil
	@member_groups_non_r = nil
	@member_groups_r     = nil
	@groups              = nil
	super
end

#remove(member) ⇒ Object

Remove a User or Group from this Group. Returns true if the User or Group does not belong to this Group, or if the oepration to remove them succeeds.



75
76
77
78
79
80
81
82
83
84
# File 'lib/active_directory/group.rb', line 75

def remove(member)
	return false unless member.is_a?(User) || member.is_a?(Group)
	if @@ldap.modify(:dn => distinguishedName, :operations => [
		[ :delete, :member, member.distinguishedName ]
	])
		return true
	else
		return !has_member?(member)
	end
end