Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
add.py73 linesDownload Raw Back to operation
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 .. import SEQUENCE_TYPES
27from ..protocol.rfc4511 import AddRequest, LDAPDN, AttributeList, Attribute, AttributeDescription, ResultCode, Vals
28from ..protocol.convert import referrals_to_list, attributes_to_dict, validate_attribute_value, prepare_for_sending
29
30
31def add_operation(dn,
32                  attributes,
33                  auto_encode,
34                  schema=None,
35                  validator=None,
36                  check_names=False):
37    # AddRequest ::= [APPLICATION 8] SEQUENCE {
38    #     entry           LDAPDN,
39    #     attributes      AttributeList }
40    #
41    # attributes is a dictionary in the form 'attribute': ['val1', 'val2', 'valN']
42    attribute_list = AttributeList()
43    for pos, attribute in enumerate(attributes):
44        attribute_list[pos] = Attribute()
45        attribute_list[pos]['type'] = AttributeDescription(attribute)
46        vals = Vals()  # changed from ValsAtLeast1() for allowing empty member value in groups
47        if isinstance(attributes[attribute], SEQUENCE_TYPES):
48            for index, value in enumerate(attributes[attribute]):
49                vals.setComponentByPosition(index, prepare_for_sending(validate_attribute_value(schema, attribute, value, auto_encode, validator, check_names)))
50        else:
51            vals.setComponentByPosition(0, prepare_for_sending(validate_attribute_value(schema, attribute, attributes[attribute], auto_encode, validator, check_names)))
52
53        attribute_list[pos]['vals'] = vals
54
55    request = AddRequest()
56    request['entry'] = LDAPDN(dn)
57    request['attributes'] = attribute_list
58
59    return request
60
61
62def add_request_to_dict(request):
63    return {'entry': str(request['entry']),
64            'attributes': attributes_to_dict(request['attributes'])}
65
66
67def add_response_to_dict(response):
68    return {'result': int(response['resultCode']),
69            'description': ResultCode().getNamedValues().getName(response['resultCode']),
70            'dn': str(response['matchedDN']),
71            'message': str(response['diagnosticMessage']),
72            'referrals': referrals_to_list(response['referral'])}
73 
codekingpro/portable-devtools · Team Ai