Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
modify.py97 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, MODIFY_ADD, MODIFY_DELETE, MODIFY_REPLACE, MODIFY_INCREMENT
27from ..protocol.rfc4511 import ModifyRequest, LDAPDN, Changes, Change, Operation, PartialAttribute, AttributeDescription, Vals, ResultCode
28from ..operation.bind import referrals_to_list
29from ..protocol.convert import changes_to_list, validate_attribute_value, prepare_for_sending
30
31# ModifyRequest ::= [APPLICATION 6] SEQUENCE {
32#    object          LDAPDN,
33#    changes         SEQUENCE OF change SEQUENCE {
34#        operation       ENUMERATED {
35#            add     (0),
36#            delete  (1),
37#            replace (2),
38#            ...  },
39#    modification    PartialAttribute } }
40
41change_table = {MODIFY_ADD: 0,  # accepts actual values too
42                MODIFY_DELETE: 1,
43                MODIFY_REPLACE: 2,
44                MODIFY_INCREMENT: 3,
45                0: 0,
46                1: 1,
47                2: 2,
48                3: 3}
49
50
51def modify_operation(dn,
52                     changes,
53                     auto_encode,
54                     schema=None,
55                     validator=None,
56                     check_names=False):
57    # changes is a dictionary in the form {'attribute': [(operation, [val1, ...]), ...], ...}
58    # operation is 0 (add), 1 (delete), 2 (replace), 3 (increment)
59    # increment as per RFC4525
60
61    change_list = Changes()
62    pos = 0
63    for attribute in changes:
64        for change_operation in changes[attribute]:
65            partial_attribute = PartialAttribute()
66            partial_attribute['type'] = AttributeDescription(attribute)
67            partial_attribute['vals'] = Vals()
68            if isinstance(change_operation[1], SEQUENCE_TYPES):
69                for index, value in enumerate(change_operation[1]):
70                    partial_attribute['vals'].setComponentByPosition(index, prepare_for_sending(validate_attribute_value(schema, attribute, value, auto_encode, validator, check_names=check_names)))
71            else:
72                partial_attribute['vals'].setComponentByPosition(0, prepare_for_sending(validate_attribute_value(schema, attribute, change_operation[1], auto_encode, validator, check_names=check_names)))
73            change = Change()
74            change['operation'] = Operation(change_table[change_operation[0]])
75            change['modification'] = partial_attribute
76
77            change_list[pos] = change
78            pos += 1
79
80    request = ModifyRequest()
81    request['object'] = LDAPDN(dn)
82    request['changes'] = change_list
83    return request
84
85
86def modify_request_to_dict(request):
87    return {'entry': str(request['object']),
88            'changes': changes_to_list(request['changes'])}
89
90
91def modify_response_to_dict(response):
92    return {'result': int(response['resultCode']),
93            'description': ResultCode().getNamedValues().getName(response['resultCode']),
94            'message': str(response['diagnosticMessage']),
95            'dn': str(response['matchedDN']),
96            'referrals': referrals_to_list(response['referral'])}
97 
codekingpro/portable-devtools · Team Ai