main_43.py 1.1 KB

12345678910111213141516171819202122232425
  1. def anonymize(cls, user, ldap_attrs, **kwargs):
  2. # type: (User, Dict[AnyStr, Any], **Any) -> Dict[AnyStr, AnyStr]
  3. """
  4. Change values of function arguments to anonymize/pseudonymize user if
  5. UCRV asm/attributes/<staff/student>/anonymize is true. Will return
  6. unchanged function arguments otherwise.
  7. :param User user: user object
  8. :param dict ldap_attrs: dictionary with the users LDAP attributes
  9. :return: dictionary with [modified] function arguments
  10. :rtype: dict
  11. :raises NotImplementedError: if cls.ucr_anonymize_key_base is unset
  12. """
  13. ucr = get_ucr()
  14. if ucr.is_true(cls.ucr_anonymize_key_base):
  15. for k, v in cls.anonymize_mapping().items():
  16. if v and v.startswith('%'):
  17. attr = v[1:].strip()
  18. try:
  19. v = ldap_attrs[attr][0]
  20. except KeyError:
  21. raise ValueError('Attribute {!r} not found in LDAP object of {}.'.format(attr, user))
  22. except IndexError:
  23. raise ValueError('Attribute {!r} empty in LDAP object of {}.'.format(attr, user))
  24. kwargs[k] = v
  25. return kwargs