Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

modlist.py 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. """
  2. ldap.modlist - create add/modify modlist's
  3. See https://www.python-ldap.org/ for details.
  4. """
  5. from ldap import __version__
  6. import ldap
  7. def addModlist(entry,ignore_attr_types=None):
  8. """Build modify list for call of method LDAPObject.add()"""
  9. ignore_attr_types = {v.lower() for v in ignore_attr_types or []}
  10. modlist = []
  11. for attrtype, value in entry.items():
  12. if attrtype.lower() in ignore_attr_types:
  13. # This attribute type is ignored
  14. continue
  15. # Eliminate empty attr value strings in list
  16. attrvaluelist = [item for item in value if item is not None]
  17. if attrvaluelist:
  18. modlist.append((attrtype, value))
  19. return modlist # addModlist()
  20. def modifyModlist(
  21. old_entry,new_entry,ignore_attr_types=None,ignore_oldexistent=0,case_ignore_attr_types=None
  22. ):
  23. """
  24. Build differential modify list for calling LDAPObject.modify()/modify_s()
  25. old_entry
  26. Dictionary holding the old entry
  27. new_entry
  28. Dictionary holding what the new entry should be
  29. ignore_attr_types
  30. List of attribute type names to be ignored completely
  31. ignore_oldexistent
  32. If non-zero attribute type names which are in old_entry
  33. but are not found in new_entry at all are not deleted.
  34. This is handy for situations where your application
  35. sets attribute value to '' for deleting an attribute.
  36. In most cases leave zero.
  37. case_ignore_attr_types
  38. List of attribute type names for which comparison will be made
  39. case-insensitive
  40. """
  41. ignore_attr_types = {v.lower() for v in ignore_attr_types or []}
  42. case_ignore_attr_types = {v.lower() for v in case_ignore_attr_types or []}
  43. modlist = []
  44. attrtype_lower_map = {}
  45. for a in old_entry.keys():
  46. attrtype_lower_map[a.lower()]=a
  47. for attrtype, value in new_entry.items():
  48. attrtype_lower = attrtype.lower()
  49. if attrtype_lower in ignore_attr_types:
  50. # This attribute type is ignored
  51. continue
  52. # Filter away null-strings
  53. new_value = [item for item in value if item is not None]
  54. if attrtype_lower in attrtype_lower_map:
  55. old_value = old_entry.get(attrtype_lower_map[attrtype_lower],[])
  56. old_value = [item for item in old_value if item is not None]
  57. del attrtype_lower_map[attrtype_lower]
  58. else:
  59. old_value = []
  60. if not old_value and new_value:
  61. # Add a new attribute to entry
  62. modlist.append((ldap.MOD_ADD,attrtype,new_value))
  63. elif old_value and new_value:
  64. # Replace existing attribute
  65. replace_attr_value = len(old_value)!=len(new_value)
  66. if not replace_attr_value:
  67. if attrtype_lower in case_ignore_attr_types:
  68. old_value_set = {v.lower() for v in old_value}
  69. new_value_set = {v.lower() for v in new_value}
  70. else:
  71. old_value_set = set(old_value)
  72. new_value_set = set(new_value)
  73. replace_attr_value = new_value_set != old_value_set
  74. if replace_attr_value:
  75. modlist.append((ldap.MOD_DELETE,attrtype,None))
  76. modlist.append((ldap.MOD_ADD,attrtype,new_value))
  77. elif old_value and not new_value:
  78. # Completely delete an existing attribute
  79. modlist.append((ldap.MOD_DELETE,attrtype,None))
  80. if not ignore_oldexistent:
  81. # Remove all attributes of old_entry which are not present
  82. # in new_entry at all
  83. for a, val in attrtype_lower_map.items():
  84. if a in ignore_attr_types:
  85. # This attribute type is ignored
  86. continue
  87. attrtype = val
  88. modlist.append((ldap.MOD_DELETE,attrtype,None))
  89. return modlist # modifyModlist()