Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

291 Zeilen
12 KiB

  1. """
  2. """
  3. # Created on 2014.01.06
  4. #
  5. # Author: Giovanni Cannata
  6. #
  7. # Copyright 2014 - 2020 Giovanni Cannata
  8. #
  9. # This file is part of ldap3.
  10. #
  11. # ldap3 is free software: you can redistribute it and/or modify
  12. # it under the terms of the GNU Lesser General Public License as published
  13. # by the Free Software Foundation, either version 3 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # ldap3 is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU Lesser General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU Lesser General Public License
  22. # along with ldap3 in the COPYING and COPYING.LESSER files.
  23. # If not, see <http://www.gnu.org/licenses/>.
  24. from os import linesep
  25. from .. import MODIFY_ADD, MODIFY_REPLACE, MODIFY_DELETE, SEQUENCE_TYPES
  26. from ..core.exceptions import LDAPCursorError
  27. from ..utils.repr import to_stdout_encoding
  28. from . import STATUS_PENDING_CHANGES, STATUS_VIRTUAL, STATUS_READY_FOR_DELETION, STATUS_READY_FOR_MOVING, STATUS_READY_FOR_RENAMING
  29. from ..utils.log import log, log_enabled, ERROR, BASIC, PROTOCOL, EXTENDED
  30. # noinspection PyUnresolvedReferences
  31. class Attribute(object):
  32. """Attribute/values object, it includes the search result (after post_query transformation) of each attribute in an entry
  33. Attribute object is read only
  34. - values: contain the processed attribute values
  35. - raw_values': contain the unprocessed attribute values
  36. """
  37. def __init__(self, attr_def, entry, cursor):
  38. self.key = attr_def.key
  39. self.definition = attr_def
  40. self.values = []
  41. self.raw_values = []
  42. self.response = None
  43. self.entry = entry
  44. self.cursor = cursor
  45. other_names = [name for name in attr_def.oid_info.name if self.key.lower() != name.lower()] if attr_def.oid_info else None
  46. self.other_names = set(other_names) if other_names else None # self.other_names is None if there are no short names, else is a set of secondary names
  47. def __repr__(self):
  48. if len(self.values) == 1:
  49. r = to_stdout_encoding(self.key) + ': ' + to_stdout_encoding(self.values[0])
  50. elif len(self.values) > 1:
  51. r = to_stdout_encoding(self.key) + ': ' + to_stdout_encoding(self.values[0])
  52. filler = ' ' * (len(self.key) + 6)
  53. for value in self.values[1:]:
  54. r += linesep + filler + to_stdout_encoding(value)
  55. else:
  56. r = to_stdout_encoding(self.key) + ': ' + to_stdout_encoding('<no value>')
  57. return r
  58. def __str__(self):
  59. if len(self.values) == 1:
  60. return to_stdout_encoding(self.values[0])
  61. else:
  62. return to_stdout_encoding(self.values)
  63. def __len__(self):
  64. return len(self.values)
  65. def __iter__(self):
  66. return self.values.__iter__()
  67. def __getitem__(self, item):
  68. return self.values[item]
  69. def __getstate__(self):
  70. cpy = dict(self.__dict__)
  71. cpy['cursor'] = None
  72. return cpy
  73. def __eq__(self, other):
  74. try:
  75. if self.value == other:
  76. return True
  77. except Exception:
  78. return False
  79. def __ne__(self, other):
  80. return not self == other
  81. @property
  82. def value(self):
  83. """
  84. :return: The single value or a list of values of the attribute.
  85. """
  86. if not self.values:
  87. return None
  88. return self.values[0] if len(self.values) == 1 else self.values
  89. class OperationalAttribute(Attribute):
  90. """Operational attribute/values object. Include the search result of an
  91. operational attribute in an entry
  92. OperationalAttribute object is read only
  93. - values: contains the processed attribute values
  94. - raw_values: contains the unprocessed attribute values
  95. It may not have an AttrDef
  96. """
  97. def __repr__(self):
  98. if len(self.values) == 1:
  99. r = to_stdout_encoding(self.key) + ' [OPERATIONAL]: ' + to_stdout_encoding(self.values[0])
  100. elif len(self.values) > 1:
  101. r = to_stdout_encoding(self.key) + ' [OPERATIONAL]: ' + to_stdout_encoding(self.values[0])
  102. filler = ' ' * (len(self.key) + 6)
  103. for value in sorted(self.values[1:]):
  104. r += linesep + filler + to_stdout_encoding(value)
  105. else:
  106. r = ''
  107. return r
  108. class WritableAttribute(Attribute):
  109. def __repr__(self):
  110. filler = ' ' * (len(self.key) + 6)
  111. if len(self.values) == 1:
  112. r = to_stdout_encoding(self.key) + ': ' + to_stdout_encoding(self.values[0])
  113. elif len(self.values) > 1:
  114. r = to_stdout_encoding(self.key) + ': ' + to_stdout_encoding(self.values[0])
  115. for value in self.values[1:]:
  116. r += linesep + filler + to_stdout_encoding(value)
  117. else:
  118. r = to_stdout_encoding(self.key) + to_stdout_encoding(': <Virtual>')
  119. if self.definition.name in self.entry._changes:
  120. r += linesep + filler + 'CHANGES: ' + str(self.entry._changes[self.definition.name])
  121. return r
  122. def __iadd__(self, other):
  123. self.add(other)
  124. return Ellipsis # hack to avoid calling set() in entry __setattr__
  125. def __isub__(self, other):
  126. self.delete(other)
  127. return Ellipsis # hack to avoid calling set_value in entry __setattr__
  128. def _update_changes(self, changes, remove_old=False):
  129. # checks for friendly key in AttrDef and uses the real attribute name
  130. if self.definition and self.definition.name:
  131. key = self.definition.name
  132. else:
  133. key = self.key
  134. if key not in self.entry._changes or remove_old: # remove old changes (for removing attribute)
  135. self.entry._changes[key] = []
  136. self.entry._changes[key].append(changes)
  137. if log_enabled(PROTOCOL):
  138. log(PROTOCOL, 'updated changes <%r> for <%s> attribute in <%s> entry', changes, self.key, self.entry.entry_dn)
  139. self.entry._state.set_status(STATUS_PENDING_CHANGES)
  140. def add(self, values):
  141. if log_enabled(PROTOCOL):
  142. log(PROTOCOL, 'adding %r to <%s> attribute in <%s> entry', values, self.key, self.entry.entry_dn)
  143. # new value for attribute to commit with a MODIFY_ADD
  144. if self.entry._state._initial_status == STATUS_VIRTUAL:
  145. error_message = 'cannot perform a modify operation in a new entry'
  146. if log_enabled(ERROR):
  147. log(ERROR, '%s for <%s>', error_message, self)
  148. raise LDAPCursorError(error_message)
  149. if self.entry.entry_status in [STATUS_READY_FOR_DELETION, STATUS_READY_FOR_MOVING, STATUS_READY_FOR_RENAMING]:
  150. error_message = self.entry.entry_status + ' - cannot add attributes'
  151. if log_enabled(ERROR):
  152. log(ERROR, '%s for <%s>', error_message, self)
  153. raise LDAPCursorError(error_message)
  154. if values is None:
  155. error_message = 'value to add cannot be None'
  156. if log_enabled(ERROR):
  157. log(ERROR, '%s for <%s>', error_message, self)
  158. raise LDAPCursorError(error_message)
  159. if values is not None:
  160. validated = self.definition.validate(values) # returns True, False or a value to substitute to the actual values
  161. if validated is False:
  162. error_message = 'value \'%s\' non valid for attribute \'%s\'' % (values, self.key)
  163. if log_enabled(ERROR):
  164. log(ERROR, '%s for <%s>', error_message, self)
  165. raise LDAPCursorError(error_message)
  166. elif validated is not True: # a valid LDAP value equivalent to the actual values
  167. values = validated
  168. self._update_changes((MODIFY_ADD, values if isinstance(values, SEQUENCE_TYPES) else [values]))
  169. def set(self, values):
  170. # new value for attribute to commit with a MODIFY_REPLACE, old values are deleted
  171. if log_enabled(PROTOCOL):
  172. log(PROTOCOL, 'setting %r to <%s> attribute in <%s> entry', values, self.key, self.entry.entry_dn)
  173. if self.entry.entry_status in [STATUS_READY_FOR_DELETION, STATUS_READY_FOR_MOVING, STATUS_READY_FOR_RENAMING]:
  174. error_message = self.entry.entry_status + ' - cannot set attributes'
  175. if log_enabled(ERROR):
  176. log(ERROR, '%s for <%s>', error_message, self)
  177. raise LDAPCursorError(error_message)
  178. if values is None:
  179. error_message = 'new value cannot be None'
  180. if log_enabled(ERROR):
  181. log(ERROR, '%s for <%s>', error_message, self)
  182. raise LDAPCursorError(error_message)
  183. validated = self.definition.validate(values) # returns True, False or a value to substitute to the actual values
  184. if validated is False:
  185. error_message = 'value \'%s\' non valid for attribute \'%s\'' % (values, self.key)
  186. if log_enabled(ERROR):
  187. log(ERROR, '%s for <%s>', error_message, self)
  188. raise LDAPCursorError(error_message)
  189. elif validated is not True: # a valid LDAP value equivalent to the actual values
  190. values = validated
  191. self._update_changes((MODIFY_REPLACE, values if isinstance(values, SEQUENCE_TYPES) else [values]), remove_old=True)
  192. def delete(self, values):
  193. # value for attribute to delete in commit with a MODIFY_DELETE
  194. if log_enabled(PROTOCOL):
  195. log(PROTOCOL, 'deleting %r from <%s> attribute in <%s> entry', values, self.key, self.entry.entry_dn)
  196. if self.entry._state._initial_status == STATUS_VIRTUAL:
  197. error_message = 'cannot delete an attribute value in a new entry'
  198. if log_enabled(ERROR):
  199. log(ERROR, '%s for <%s>', error_message, self)
  200. raise LDAPCursorError(error_message)
  201. if self.entry.entry_status in [STATUS_READY_FOR_DELETION, STATUS_READY_FOR_MOVING, STATUS_READY_FOR_RENAMING]:
  202. error_message = self.entry.entry_status + ' - cannot delete attributes'
  203. if log_enabled(ERROR):
  204. log(ERROR, '%s for <%s>', error_message, self)
  205. raise LDAPCursorError(error_message)
  206. if values is None:
  207. error_message = 'value to delete cannot be None'
  208. if log_enabled(ERROR):
  209. log(ERROR, '%s for <%s>', error_message, self)
  210. raise LDAPCursorError(error_message)
  211. if not isinstance(values, SEQUENCE_TYPES):
  212. values = [values]
  213. for single_value in values:
  214. if single_value not in self.values:
  215. error_message = 'value \'%s\' not present in \'%s\'' % (single_value, ', '.join(self.values))
  216. if log_enabled(ERROR):
  217. log(ERROR, '%s for <%s>', error_message, self)
  218. raise LDAPCursorError(error_message)
  219. self._update_changes((MODIFY_DELETE, values))
  220. def remove(self):
  221. if log_enabled(PROTOCOL):
  222. log(PROTOCOL, 'removing <%s> attribute in <%s> entry', self.key, self.entry.entry_dn)
  223. if self.entry._state._initial_status == STATUS_VIRTUAL:
  224. error_message = 'cannot remove an attribute in a new entry'
  225. if log_enabled(ERROR):
  226. log(ERROR, '%s for <%s>', error_message, self)
  227. raise LDAPCursorError(error_message)
  228. if self.entry.entry_status in [STATUS_READY_FOR_DELETION, STATUS_READY_FOR_MOVING, STATUS_READY_FOR_RENAMING]:
  229. error_message = self.entry.entry_status + ' - cannot remove attributes'
  230. if log_enabled(ERROR):
  231. log(ERROR, '%s for <%s>', error_message, self)
  232. raise LDAPCursorError(error_message)
  233. self._update_changes((MODIFY_REPLACE, []), True)
  234. def discard(self):
  235. if log_enabled(PROTOCOL):
  236. log(PROTOCOL, 'discarding <%s> attribute in <%s> entry', self.key, self.entry.entry_dn)
  237. del self.entry._changes[self.key]
  238. if not self.entry._changes:
  239. self.entry._state.set_status(self.entry._state._initial_status)
  240. @property
  241. def virtual(self):
  242. return False if len(self.values) else True
  243. @property
  244. def changes(self):
  245. if self.key in self.entry._changes:
  246. return self.entry._changes[self.key]
  247. return None