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.

add.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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
  25. from ..protocol.rfc4511 import AddRequest, LDAPDN, AttributeList, Attribute, AttributeDescription, ResultCode, Vals
  26. from ..protocol.convert import referrals_to_list, attributes_to_dict, validate_attribute_value, prepare_for_sending
  27. def add_operation(dn,
  28. attributes,
  29. auto_encode,
  30. schema=None,
  31. validator=None,
  32. check_names=False):
  33. # AddRequest ::= [APPLICATION 8] SEQUENCE {
  34. # entry LDAPDN,
  35. # attributes AttributeList }
  36. #
  37. # attributes is a dictionary in the form 'attribute': ['val1', 'val2', 'valN']
  38. attribute_list = AttributeList()
  39. for pos, attribute in enumerate(attributes):
  40. attribute_list[pos] = Attribute()
  41. attribute_list[pos]['type'] = AttributeDescription(attribute)
  42. vals = Vals() # changed from ValsAtLeast1() for allowing empty member value in groups
  43. if isinstance(attributes[attribute], SEQUENCE_TYPES):
  44. for index, value in enumerate(attributes[attribute]):
  45. vals.setComponentByPosition(index, prepare_for_sending(validate_attribute_value(schema, attribute, value, auto_encode, validator, check_names)))
  46. else:
  47. vals.setComponentByPosition(0, prepare_for_sending(validate_attribute_value(schema, attribute, attributes[attribute], auto_encode, validator, check_names)))
  48. attribute_list[pos]['vals'] = vals
  49. request = AddRequest()
  50. request['entry'] = LDAPDN(dn)
  51. request['attributes'] = attribute_list
  52. return request
  53. def add_request_to_dict(request):
  54. return {'entry': str(request['entry']),
  55. 'attributes': attributes_to_dict(request['attributes'])}
  56. def add_response_to_dict(response):
  57. return {'result': int(response['resultCode']),
  58. 'description': ResultCode().getNamedValues().getName(response['resultCode']),
  59. 'dn': str(response['matchedDN']),
  60. 'message': str(response['diagnosticMessage']),
  61. 'referrals': referrals_to_list(response['referral'])}