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