[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/se3-logonpy/usr/share/se3/logonpy/ -> se3LDAP.py (source)

   1  # -*- coding: utf-8 -*-
   2  import sys, ldap
   3  import ldap.modlist as modlist
   4  
   5  class se3LDAP:
   6  
   7      scope = ldap.SCOPE_SUBTREE
   8  
   9  
  10      def __init__ (self, host, port, adminRdn, adminPw, baseDn, peopleRdn, computersRdn, \
  11                    groupsRdn, parcsRdn):
  12          """
  13              Connect to se3 LDAP
  14          """
  15          try:
  16              self.__ldap = ldap.initialize ("ldap://%s:%s" % (host, port))
  17              self.__ldap.simple_bind("%s,%s" % (adminRdn, baseDn), "%s" % adminPw)
  18              self.__baseDn = baseDn
  19              self.__peopleRdn = peopleRdn
  20              self.__computersRdn = computersRdn
  21              self.__parcsRdn = parcsRdn
  22              self.__groupsRdn = groupsRdn
  23  
  24          except ldap.LDAPError:
  25              print "Can't connect to ldap://%s:%s" % (host, port)
  26              sys.exit (1)
  27  
  28  
  29      def __del__ (self):
  30          """
  31              Close LDAP connexion
  32          """
  33          self.__ldap.unbind ()
  34  
  35  
  36      def getUserGroups (self, user):
  37          """
  38              Return a list of all user's groups
  39          """
  40          return self.__search (self.__groupsRdn, "(&(memberUid=%s)(objectClass=posixGroup))" % user, "cn") 
  41  
  42  
  43      def getComputerParcs (self, computer):
  44          """
  45              Return a list of all computer's parcs
  46          """
  47          return self.__search (self.__parcsRdn, "(&(member=cn=%s,%s,%s)(objectClass=groupOfNames))" % (computer, self.__computersRdn, self.__baseDn), "cn")
  48  
  49  
  50      def getSe3Master (self):
  51          """
  52              Return master server
  53          """
  54          try:
  55              return self.__search (self.__computersRdn, "l=maitre", "cn")[0]
  56  
  57          except IndexError:
  58              print "No master server !?!?"
  59  
  60  
  61      def getNtUserProfileStatus (self, user):
  62          """
  63              Return NT user profile status (lock, unlock, del)
  64              If status is deletion, set it to unlock
  65          """
  66          try:
  67              return self.__search (self.__peopleRdn, "uid=%s" % user, "l")[0]
  68  
  69          except IndexError:
  70              return "unlock"
  71  
  72  
  73      def getParcPrinters (self, parc):
  74          """
  75              Return a list of all parc's printers
  76          """
  77          printers = []
  78          dnList = self.__search (self.__parcsRdn, "(&(cn=%s)(objectClass=groupOfNames))" \
  79                                  % parc, "member")
  80          defaultPrinterDnList = \
  81          self.__search (self.__parcsRdn, "(&(cn=%s)(objectClass=groupOfNames))" \
  82                                  % parc, "owner")
  83          if len ((defaultPrinterDnList)):
  84              defaultPrinter = ldap.explode_dn (defaultPrinterDnList[0], 1)[0]
  85          else:
  86              defaultPrinter = ""
  87  
  88          for dn in dnList:
  89              rdnList = ldap.explode_dn (dn, 1)
  90              if rdnList[1] == "Printers":
  91                  if rdnList[0] == defaultPrinter:
  92                      printers.insert (0, defaultPrinter)
  93                  else:
  94                      printers.append (rdnList[0])
  95   
  96          return printers
  97  
  98  
  99      def setNtUserProfileStatus (self, user, status):
 100          """
 101              Unlock NT user profile status
 102          """
 103          ldif = []
 104          try:
 105              ldif.append ((ldap.MOD_REPLACE, "l", status))
 106              self.__ldap.modify_s ("uid=%s,%s,%s" % \
 107                                   (user, self.__peopleRdn, self.__baseDn), ldif)
 108  
 109          except ldap.INVALID_DN_SYNTAX:
 110              print "Invalide LDAP admin: %s,%s,%s" \
 111                     % (user, self.__peopleRdn, self.__baseDn)
 112          except ldap.STRONG_AUTH_REQUIRED:
 113              print "Can't modify profile status, bind as anonym"
 114          except ldap.LDAPError, error:
 115              print "Error while modifying %s profile status" %user
 116  
 117  
 118      def __search (self, dn, filter, attr):
 119          """
 120              Return a list based on filter and attr
 121          """
 122          resultList = []
 123          try:
 124              searchResult = self.__ldap.search_s (dn + "," + self.__baseDn, self.scope, \
 125                                                   filter, [attr])
 126              for topEntries in searchResult:
 127                  for subEntries in topEntries[1][attr]:
 128                      resultList.append (subEntries)
 129  
 130              return resultList
 131  
 132          except: return resultList


Generated: Tue Mar 17 22:47:18 2015 Cross-referenced by PHPXref 0.7.1