codekingpro/portable-devtools
114k
1"""
2"""
3
4# Created on 2013.05.31
5#
6# Author: Giovanni Cannata
7#
8# Copyright 2013 - 2020 Giovanni Cannata
9#
10# This file is part of ldap3.
11#
12# ldap3 is free software: you can redistribute it and/or modify
13# it under the terms of the GNU Lesser General Public License as published
14# by the Free Software Foundation, either version 3 of the License, or
15# (at your option) any later version.
16#
17# ldap3 is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU Lesser General Public License for more details.
21#
22# You should have received a copy of the GNU Lesser General Public License
23# along with ldap3 in the COPYING and COPYING.LESSER files.
24# If not, see <http://www.gnu.org/licenses/>.
25
26from ..protocol.convert import validate_attribute_value, prepare_for_sending
27from ..protocol.rfc4511 import CompareRequest, AttributeValueAssertion, AttributeDescription, LDAPDN, AssertionValue, ResultCode
28from ..operation.search import ava_to_dict
29from ..operation.bind import referrals_to_list
30
31
32def compare_operation(dn,
33 attribute,
34 value,
35 auto_encode,
36 schema=None,
37 validator=None,
38 check_names=False):
39 # CompareRequest ::= [APPLICATION 14] SEQUENCE {
40 # entry LDAPDN,
41 # ava AttributeValueAssertion }
42 ava = AttributeValueAssertion()
43 ava['attributeDesc'] = AttributeDescription(attribute)
44 ava['assertionValue'] = AssertionValue(prepare_for_sending(validate_attribute_value(schema, attribute, value, auto_encode, validator, check_names=check_names)))
45
46 request = CompareRequest()
47 request['entry'] = LDAPDN(dn)
48 request['ava'] = ava
49
50 return request
51
52
53def compare_request_to_dict(request):
54 ava = ava_to_dict(request['ava'])
55 return {'entry': str(request['entry']),
56 'attribute': ava['attribute'],
57 'value': ava['value']}
58
59
60def compare_response_to_dict(response):
61 return {'result': int(response['resultCode']),
62 'description': ResultCode().getNamedValues().getName(response['resultCode']),
63 'dn': str(response['matchedDN']), 'message': str(response['diagnosticMessage']),
64 'referrals': referrals_to_list(response['referral'])}
65 