Class: ActiveDirectory::User

Inherits:
DirectoryObject show all
Defined in:
lib/active_directory/user.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DirectoryObject

#create, #delete, #initialize, #rename, #update

Constructor Details

This class inherits a constructor from ActiveDirectory::DirectoryObject

Class Method Details

.add(dn, attrs = {}) ⇒ Object



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

def self.add(dn, attrs = {})
  #todo
end

.fieldsObject



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/active_directory/user.rb', line 66

def self.fields
  {
    mail: "email",
    title: "title",
    sn: "last_name",
    givenName: "first_name",
    department: "department",
    employeeType: "employee_type",
    sAMAccountName: "username",
    l: "location",
    dn: "dn"
  }
end

.find_by_email(email, treebase = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/active_directory/user.rb', line 56

def self.find_by_email(email, treebase=nil)
  filter = "(&(objectCategory=person)(objectClass=user)(mail=#{email}))"
  results = Client.search(filter, self.fields.keys, treebase).first
  if results
    self.new(results)
  else
    false
  end
end

.find_by_username(username, treebase = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/active_directory/user.rb', line 46

def self.find_by_username(username, treebase=nil)
  filter = "(&(objectCategory=person)(objectClass=user)(samaccountname=#{username}))"
  results = Client.search(filter, self.fields.keys, treebase).first
  if results
    self.new(results)
  else
    false
  end
end

Instance Method Details

#add_to_group(group_dn) ⇒ Object



24
25
26
# File 'lib/active_directory/user.rb', line 24

def add_to_group(group_dn)
  Client.add_attribute(group_dn, :member, @dn)
end

#change_password(old_password, new_password) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/active_directory/user.rb', line 4

def change_password(old_password, new_password)
  result = Client.modify_unicode_pwd(
    @dn,
    Utils::microsoft_encode_password(old_password),
    Utils::microsoft_encode_password(new_password),
  )
end

#groupsObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/active_directory/user.rb', line 32

def groups
  groups = []
  filter = "(&(objectClass=group)(member:1.2.840.113556.1.4.1941:=#{@dn}))"
  results = Client.search(filter, Group.fields.keys)
  if results
    results.each do |r|
      groups << Group.new(r)
    end
    groups
  else
    false
  end
end

#password(new_password) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/active_directory/user.rb', line 12

def password(new_password)
  result = Client.update_attribute(
    @dn,
    'unicodePwd',
    Utils::microsoft_encode_password(new_password)
  )
end

#remove_from_group(group_dn) ⇒ Object



20
21
22
# File 'lib/active_directory/user.rb', line 20

def remove_from_group(group_dn)
   Client.modify(group_dn.to_s, [[:delete, :member, @dn]])
end