Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2011
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # Copyright 2011, Ante Karamatic
  5. # Init, Zagreb, Croatia
  6.  
  7. # All rights reserved
  8.  
  9. # Permission to use, copy, modify, and distribute this software and its
  10. # documentation for any purpose and without fee is hereby granted,
  11. # provided that the above copyright notice appear in all copies and that
  12. # both that copyright notice and this permission notice appear in
  13. # supporting documentation, and that the name of Ante Karamatic
  14. # not be used in advertising or publicity pertaining to distribution
  15. # of the software without specific, written prior permission.
  16. # ANTE KARAMATIC DISCLAIMS ALL WARRANTIES WITH REGARD TO
  17. # THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  18. # FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE CENTRUM BE LIABLE
  19. # FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  20. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  21. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  22. # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  23.  
  24. from lxml import etree
  25. from StringIO import StringIO
  26. from suds.client import Client
  27. import base64
  28. import vars
  29.  
  30. client = Client(vars.url)
  31.  
  32. # FIXME: naci pametniji nacin za generiranje XML-a
  33.  
  34. # AOSIsearch - returns attributes and values for a specific user
  35. #
  36. # Returns result as a dictionary
  37. # FIXME: malo to pametnije izvesti (error code i objasnjenje, i sl)
  38. def AOSIsearch(searchQ='(objectclass=*)', attributeQ=""):
  39. result = client.service.ldapSearch(vars.username, vars.passwordb64, vars.ldap_tree, searchQ, attributeQ)
  40. if result.code == 0:
  41. content = base64.b64decode(result.result)
  42. feed = StringIO(content)
  43. tree = etree.parse(feed)
  44. values = tree.xpath('/ldap/entry/attribute[@ldapname="' + attributeQ + '"]/value')
  45. attributes = attributeQ.split(",")
  46. result = {}
  47. for field in attributes:
  48. values_listed = ""
  49. values = tree.xpath('/ldap/entry/attribute[@ldapname="' + field + '"]/value')
  50. i = 0
  51. while i < len(values):
  52. if i == 0:
  53. values_listed = values_listed + values[i].text
  54. else:
  55. values_listed = values_listed + ',' + values[i].text
  56. i = i + 1
  57. print values_listed
  58. result[field] = values_listed
  59. else:
  60. result = {}
  61. return result
  62.  
  63. # AOSImodify - modify one user's attribute
  64. #
  65. # Modifies multiple attributes at the time
  66. # It takes dictionary as an input
  67. # {'attribue': 'new value'}
  68. def AOSImodify(uidQ, dictionaryQ):
  69. ldap_xml = '<ldap><entry dn="uid=' + uidQ + ',' + vars.ldap_tree + '">'
  70. ldap_xml_footer = '</entry></ldap>'
  71. for attribute, value in dictionaryQ.iteritems():
  72. ldap_xml = ldap_xml + '<attribute ldapname="' + attribute + '"><value>' + value.decode("UTF-8") + '</value></attribute>'
  73. ldap_xml = ldap_xml + ldap_xml_footer
  74. result = client.service.ldapModifyAttribute(vars.username, vars.passwordb64, vars.ldap_tree, ldap_xml)
  75. return result.code
  76.  
  77. # AOSImultimodify - modify users from dictionary
  78. #
  79. # Dictionary in the form of:
  80. # {'uid': {'attribute1': 'value1,value2', 'attribute2': 'value'}, 'uid2': {'attribute1': 'value'},...}
  81. def AOSImultimodify(dictionaryQ):
  82. ldap_xml = '<ldap>'
  83. for uid, attributes in dictionaryQ.iteritems():
  84. ldap_xml = ldap_xml + '<entry dn="uid=' + uid + ',' + vars.ldap_tree + '">'
  85. for attribute, value in attributes.iteritems():
  86. ldap_xml = ldap_xml + '<attribute ldapname="' + attribute + '">'
  87. values = value.split(",")
  88. for value in values:
  89. ldap_xml = ldap_xml + '<value>' + value.decode("UTF-8") + '</value>'
  90. ldap_xml = ldap_xml + '</attribute>'
  91. ldap_xml = ldap_xml + '</entry>'
  92. ldap_xml = ldap_xml + '</ldap>'
  93. print ldap_xml
  94. result = client.service.ldapModifyAttribute(vars.username, vars.passwordb64, vars.ldap_tree, ldap_xml)
  95. return result.code
  96.  
  97. # AOSInewusers - create new users from dictionary
  98. #
  99. # Dictionary in the form of:
  100. # {'uid': {'attribute1': 'value1,value2', 'attribute2': 'value'}, 'uid2': {'attribute1': 'value'},...}
  101. def AOSInewusers(dictionaryQ):
  102. ldap_xml = '<ldap>'
  103. for uid, attributes in dictionaryQ.iteritems():
  104. ldap_xml = ldap_xml + '<entry dn="uid=' + uid + ',' + vars.ldap_tree + '">'
  105. for attribute, value in attributes.iteritems():
  106. ldap_xml = ldap_xml + '<attribute ldapname="' + attribute + '">'
  107. values = value.split(",")
  108. for value in values:
  109. ldap_xml = ldap_xml + '<value>' + value.decode("UTF-8") + '</value>'
  110. ldap_xml = ldap_xml + '</attribute>'
  111. ldap_xml = ldap_xml + '</entry>'
  112. ldap_xml = ldap_xml + '</ldap>'
  113. result = client.service.ldapAddUserLE(vars.username, vars.passwordb64, vars.ldap_tree, ldap_xml)
  114. return result.code
  115.  
  116. # AOSIdeleteusers - deletes users based on a list of uids
  117. #
  118. # Returns dictionary or error codes for each uid
  119. def AOSIdeleteusers(listQ):
  120. results = {}
  121. for uid in listQ:
  122. ldap_xml = '<ldap><entry dn="uid=' + uid + ',' + vars.ldap_tree + '"></entry></ldap>'
  123. result = client.service.ldapDeleteUser(vars.username, vars.passwordb64, vars.ldap_tree, ldap_xml)
  124. results[uid] = result.code
  125. print results
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement