Class: SBS2003Contacts

Inherits:
Object
  • Object
show all
Defined in:
lib/sbs2003_contacts.rb

Constant Summary collapse

VERSION =
'0.2.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user = ADMIN, pass = PASSWD, base = BASEDN, server = SERVER, port = PORT) ⇒ SBS2003Contacts

Returns a new instance of SBS2003Contacts.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sbs2003_contacts.rb', line 17

def initialize(user = ADMIN, pass = PASSWD, base = BASEDN, server = SERVER, port = PORT)
  @ldap = Net::LDAP.new :host => server, 
            :port => port,
            :base => base,
            :auth => {
              :method => :simple,
              :username => user,
              :password => pass
            }
  # @ldap.bind ? true : false
end

Instance Attribute Details

#ldapObject

Returns the value of attribute ldap.



15
16
17
# File 'lib/sbs2003_contacts.rb', line 15

def ldap
  @ldap
end

Instance Method Details

#add_email_to_list(list_email, user_email) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/sbs2003_contacts.rb', line 127

def add_email_to_list(list_email, user_email)
  # 0. Assumes User already exists!
  # 1. Check to see if user is already a member?
  # 2. Add user_dn to list#member:
  # 3. Add list_dn to user#memberOf:

  list_dn = find_list_dn_by_email(list_email)
  user_dn = find_user_dn_by_email(user_email)
  
  if list_lists_member_emails(list_email).include?([user_dn, user_email])
    raise AlreadyListMemberException, "User e-mail already a member: user_email"
  else
    @ldap.add_attribute list_dn, :member, user_dn
    @ldap.add_attribute user_dn, :memberOf, list_dn
  end
end

#find_list_dn_by_email(list_email) ⇒ Object

def add_new_user

# TODO, need to reverse engineer all of AD's magical madness first
#       since M$ went way off the fraking reservation and changed stadard LDAP schemas

end



100
101
102
103
104
105
106
107
108
# File 'lib/sbs2003_contacts.rb', line 100

def find_list_dn_by_email(list_email)
  result = ""
  filter = set_filter( "mail", list_email)
  @ldap.search( :filter => filter ) do |entry|
    result = entry.dn
  end
  raise ListEmailNotFoundException, "List e-mail not found: #{list_email}" if result.empty?
  result
end

#find_user_dn_by_email(user_email) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/sbs2003_contacts.rb', line 47

def find_user_dn_by_email(user_email)
  results = ''
  filter = set_filter( "mail", user_email)
  @ldap.search( :filter => filter ) do |entry|
    results = entry.dn
  end
  raise UserEmailNotFoundException, "No DN found for e-mail: #{user_email}" if results.empty?
  results
end

#find_user_dn_by_name(user_name) ⇒ Object

User management



37
38
39
40
41
42
43
44
45
# File 'lib/sbs2003_contacts.rb', line 37

def find_user_dn_by_name(user_name)
  results = ''
  filter = set_filter( "cn", user_name)
  @ldap.search( :filter => filter ) do |entry|
    results = entry.dn
  end
  raise UserNameNotFoundException, "No DN found for user: #{user_name}" if results.empty?
  results
end

#find_user_email_by_cn(user_cn) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sbs2003_contacts.rb', line 57

def find_user_email_by_cn(user_cn)
  result = ''
  filter = set_filter("cn", user_cn)
  attrs = [ "cn", "mail" ]
  @ldap.search( :filter => filter, :attributes => attrs ) do |entry|
    entry.each do |attr,values|
      if attr.to_s == "mail" then
        values.each do |value|
          result = value.downcase
        end
      end
    end
  end
end

#list_lists_member_emails(list_email) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/sbs2003_contacts.rb', line 110

def list_lists_member_emails(list_email)
  results = []
  filter = set_filter( "mail", list_email)
  attrs = [ "mail", "member", "cn", "objectClass"]
  @ldap.search( :filter => filter, :attributes => attrs ) do |entry|
    entry.each do |attr, values|
      if attr.to_s == "member" then
        values.each do |value|
          user_cn = value.dup.split(",").first.split("=").last
          results.push( [ value.dup, self.find_user_email_by_cn(user_cn) ] )
        end
      end
    end
  end
  results.sort
end

#list_user_lists(user_name) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sbs2003_contacts.rb', line 79

def list_user_lists(user_name)
  results = []
  filter = set_filter("cn", user_name)
  attrs = [ "memberOf", "mail", "cn"]
  @ldap.search( :filter => filter, :attributes => attrs ) do |entry|
    entry.each do |attr, values|
      if attr.to_s == "memberof" then
        values.each do |value|
          results.push(value.dup)
        end
      end
    end
  end
  results.sort
end

#remove_email_from_list(list_email, user_email) ⇒ Object



144
145
146
147
148
149
150
151
152
153
# File 'lib/sbs2003_contacts.rb', line 144

def remove_email_from_list(list_email, user_email)
  list_dn = find_list_dn_by_email(list_email)
  user_dn = find_user_dn_by_email(user_email)

  list_ops = [ [ :delete, :member, user_dn ] ]
  user_ops = [ [ :delete, :memberof, list_dn ] ]
  
  @ldap.modify :dn => list_dn, :operations => list_ops
  @ldap.modify :dn => user_dn, :operations => user_ops
end

#set_filter(attribute, search_term) ⇒ Object

meta



31
32
33
# File 'lib/sbs2003_contacts.rb', line 31

def set_filter(attribute, search_term)
  Net::LDAP::Filter.eq(attribute, search_term)
end

#update_user_email(user_name, new_email) ⇒ Object



72
73
74
75
76
77
# File 'lib/sbs2003_contacts.rb', line 72

def update_user_email(user_name, new_email)
  target_dn = find_user_dn_by_name(user_name)
  ldap.replace_attribute target_dn, :mail, new_email
  ldap.replace_attribute target_dn, :proxyAddresses, "SMTP:#{new_email}"
  ldap.replace_attribute target_dn, :targetAddress, "SMTP:#{new_email}"
end