def retrieve_principal(principal, allow_groups=True, legacy=True): """Retrieves principal object from a `(type, id)` tuple. Valid principal types are 'Avatar', 'User' and 'Group'. :param principal: The principal (a tuple/list) :param allow_groups: If group principals are allowed :param legacy: If legacy wrappers or new objects should be returned. """ from indico.modules.groups import GroupProxy from indico.modules.groups.legacy import LocalGroupWrapper, LDAPGroupWrapper from indico.modules.users import User type_, id_ = principal if type_ in {'Avatar', 'User'}: user = User.get(int(id_)) if not user: return None return user.as_avatar if legacy else user elif type_ == 'Group' and allow_groups: if isinstance(id_, (int, basestring)): # legacy group group = LocalGroupWrapper(id_) if unicode( id_).isdigit() else LDAPGroupWrapper(id_) return group if legacy else group.group else: # new group provider, name_or_id = id_ group = GroupProxy(name_or_id, provider) return group.as_legacy_group if legacy else group else: raise ValueError('Unexpected type: {}'.format(type_))
def as_legacy_group(self): from indico.modules.groups.legacy import LDAPGroupWrapper return LDAPGroupWrapper(self.name, self.provider)