Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

modify.py 3.8KB

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