+ self.baseDN = "ou=people,dc=senslab,dc=info"
+ self.conn = ldap_co()
+
+ #def connect (self):
+ #self.ldapserv=ldap.open("192.168.0.251")
+
+ #def authenticate(self):
+ #self.l = ldap.initialize("ldaps://192.168.0.251:636/")
+ #login = 'cn=admin,dc=senslab,dc=info'
+
+ #passwd='sfa-ldap'
+ ## Bind/authenticate with a user with apropriate rights to add objects
+ #self.l = simple_bind_s(login,passwd)
+
+ def ldapAdd(self, recordix = None) :
+ attrs = {'cn': ['Bruce Wayne'], 'objectClass': ['top', 'inetOrgPerson', 'posixAccount', 'systemQuotas', 'ldapPublicKey'], 'loginShell': '/senslab/users/.ssh/welcome.sh', 'sshPublicKey': '', 'quota': '/dev/sda3:2000000:2500000:0:0', 'gidNumber': '2000', 'sn': 'Wayne', 'homeDirectory': '/senslab/users/batman', 'mail': 'bw@gotham.com', 'givenName': 'Bruce', 'uid': 'batman','description' :'SFA USER FROM OUTSIDE SENSLAB'}
+ result = self.conn.connect()
+ if(result['bool']):
+ # The dn of our new entry/object
+ dn = self.baseDN
+ print >>sys.stderr, "\r\n \r\n \t LDAP.PY \t\t ldapAdd attrs %s " %(attrs)
+ # A dict to help build the "body" of the object
+ #attrs = {}
+ #attrs['objectclass'] = ['top','inetOrgPerson','posixAccount', 'systemQuotas','ldapPuclicKey']
+ #attrs['cn'] = str(record['first_name'])+' ' + str(record['last_name'])
+ #attrs['sn'] = str(record['last_name'])
+ #attrs['givenName'] = str(record['first_name'])
+ #attrs['gidNumber'] = '2000'
+ #loginslab =str(record['first_name'])+ str(record['last_name'])
+ #loginslab= loginslab.lower()
+ ##loginslab = loginslab[0:12]
+ #attrs['uid']= loginslab
+ #attrs['mail'] = record['mail']
+ #attrs['quota'] = '/dev/sda3:2000000:2500000:0:0'
+ #attrs['homeDirectory'] = '/senslab/users/' + loginslab
+ #attrs['loginShell'] = '/senslab/users/.ssh/welcome.sh'
+ #attrs['sshPublicKey'] = ''
+ #attrs['description'] = 'SFA USER FROM OUTSIDE SENSLAB'
+ category ="ou=people, dc=senslab, dc=info"
+ try:
+ ldif = modlist.addModlist(attrs)
+ print " \r\n \r\n LDAPTEST.PY add attrs %s \r\n ldif %s " %(attrs,ldif)
+ self.conn.ldapserv.add_s('%s,%s' %(dn, category),ldif)
+ except ldap.LDAPError, e:
+ return {'bool' : False, 'message' : e }
+
+ self.close()
+ return {'bool': True}
+ else:
+ return result
+ return
+
+
+ def ldapModify(self, record_filter, new_fileds):
+ person = self.ldapSearch(record_filter)
+ if person:
+ result = self.conn.connect()
+ if(result['bool']):
+ req_ldap = self.parse_record(record_filter)
+
+ #TODO Handle OR filtering in the ldap query when
+ #dealing with a list of records instead of doing a for loop in GetPersons
+ def parse_record(self, record=None):
+
+ req_ldapdict = {}
+ if record :
+ if 'first_name' in record and 'last_name' in record:
+ req_ldapdict['cn'] = str(record['first_name'])+" "+str(record['last_name'])
+ if 'email' in record :
+ req_ldapdict['mail'] = record['email']
+ if 'hrn' in record :
+ splited_hrn = record['hrn'].split(".")
+ if splited_hrn[0] != self.authname :
+ print >>sys.stderr,"i know nothing about",record['hrn'], " my authname is ", self.authname, " not ", splited_hrn[0]
+ login=splited_hrn[1]
+ if login == 'avakian':
+ login = 'savakian'
+ req_ldapdict['uid'] = login
+
+ req_ldap=''
+ print >>sys.stderr, "\r\n \r\n \t LDAP.PY \t\t parse_record record %s req_ldapdict %s" %(record,req_ldapdict)
+ for k in req_ldapdict:
+ req_ldap += '('+str(k)+'='+str(req_ldapdict[k])+')'
+ if len(req_ldapdict.keys()) >1 :
+ req_ldap = req_ldap[:0]+"(&"+req_ldap[0:]
+ size = len(req_ldap)
+ req_ldap= req_ldap[:(size-1)] +')'+ req_ldap[(size-1):]
+ else:
+ req_ldap = "(cn*)"
+
+ return req_ldap