Class: AdDir::Group

Inherits:
Entry
  • Object
show all
Defined in:
lib/ad_dir/group.rb

Overview

AdDir::Group models a 'Group' entry in an Active Directory.

For basic CRUD operations see Entry. In additon to these Group offers methods to list and managed User relationships.

List users belonging to a group

  • members list of members' DNs
   mygrp = AdDir::Group.find('lpadmin')
   mygrp.members   # => ["CN=John Doe",OU=people,ou....", "CN=Betty...", ...]

  • users => Array of User objects
   mygrp.users   # => [#<AdDir::User dn: "CN=John Doe",..." ...>, <#AdDir::User dn: ..]

  • users_usernames lists the username of each member
   mygrp.users_usernames   # => ["jdoe", "bblue", "shhinter"]

Modify User Relationship

Note: Contrary to modifications of 'normal' attributes modifications of user relationships are instantly saved!

Add User

  jdoe = AdDir::User.find('jdoe')
  mygrp.add_user(jdoe)

Removing a user

  jdoe = AdDir::User.find('jdoe')
  mygrp.remove_user(jdoe)

Constant Summary collapse

OBJECTCATEGORY =

Used to efficiently filter Group entries in ActiveDirectory.

'group'.freeze

Constants inherited from Entry

Entry::FIND_METHOD_REGEXP

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Entry

#[], #[]=, _select_dn, all, #attribute_for_inspect, #attribute_present?, #attributes, category_filter, #changed?, #changes, connection, #connection, create, #destroy, from_ldap_entry, #get_value, #initialize, #inspect, #modify, #new_entry?, parent_name, primary_key, primary_key=, #raw_attributes, #reload!, #save, select_dn, sibling_klass, #string_inspect, tree_base, tree_base=, where

Methods included from DerivedAttributes

#created_at, #derived_attribute_names, #objectguid_decoded, #objectsid_decoded, #updated_at

Constructor Details

This class inherits a constructor from AdDir::Entry

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class AdDir::Entry

Class Method Details

.user_klassObject

Get the correct User class. When querying and managing users subclasses of this class have to get the correct User model.

  module B
    class User < AdDir::User
    end

    class Group < AdDir::Group
    end
  end

  g = B::Group.user_klass  #=> B::User

If there is no class B::User any group related methods will fail.

If you want to override this method simply set the class instance variable @user_klass to your custom group class:

  module B
    class Group < AdDir::Group
      @user_klass = C::User
    end
  end  #

  B::Group.user_klass  # => C::User


90
91
92
93
# File 'lib/ad_dir/group.rb', line 90

def self.user_klass
  return @user_klass if defined? @user_klass
  @user_klass = sibling_klass('User')
end

Instance Method Details

#add_user(user) ⇒ Object

Adds a user to the group


144
145
146
147
148
149
150
# File 'lib/ad_dir/group.rb', line 144

def add_user(user)
  unless members.include?(user.dn)
    self[:member] += [user.dn]
    save
  end
  users
end

#membersObject

Note:

If the group is a primary group :member is empty (and mutually, the primary group is not present in the :memberof attribute of a User object).

Returns the DNs of all user.


128
129
130
131
# File 'lib/ad_dir/group.rb', line 128

def members
  return @ldap_entry[:member] if attribute_present?(:member)
  @ldap_entry[:member] = []
end

#members_usernamesObject Also known as: users_usernames

Return an array of the members' usernames.


135
136
137
138
# File 'lib/ad_dir/group.rb', line 135

def members_usernames  # users.map { |u| u.username }.sort

  users.map(&:username).sort
end

#nameObject

The name of the group (i.e. the samaccountname)


97
98
99
# File 'lib/ad_dir/group.rb', line 97

def name
  samaccountname
end

#primary_group?Boolean

Returns true if the group is a primary group.

Returns:

  • (Boolean)

118
119
120
# File 'lib/ad_dir/group.rb', line 118

def primary_group?
  !primary_user.nil?
end

#primary_userObject

Find the 'primary user' of the group If this is a normal group 'nil' is returned.


109
110
111
112
113
114
# File 'lib/ad_dir/group.rb', line 109

def primary_user  # @primary_user ||= AdDir::User.find_by_primarygroupid(

  @primary_user ||= self.class.user_klass.find_by_primarygroupid(
    objectsid_decoded.split('-').last
  )
end

#remove_user(user) ⇒ Object

Remove a user from the group


154
155
156
157
158
159
160
# File 'lib/ad_dir/group.rb', line 154

def remove_user(user)
  if members.include?(user.dn)
    self[:member] -= [user.dn]
    save
  end
  users
end

#usersObject

Return all users being member of this group.


102
103
104
105
# File 'lib/ad_dir/group.rb', line 102

def users  # members.map { |dn| User.select_dn(dn) }

  members.map { |dn| self.class.user_klass.select_dn(dn) }
end