def classify_user(user):
    if hasattr(user, 'ldap_user'):
        return {'name': user.username, 'source': 'ldap'}
    elif hasattr(user, 'mailbox'):
        return {'name': user.username, 'source': 'mail'}
    else:
        return {'name': user.username, 'source': 'system'}


def user_from_request(request):
    return classify_user(request.user)


def may_own_domain(user):
    if not user.is_authenticated or not user.is_active:
        return False
    u = classify_user(user)
    return u["source"] == "system" or u["source"] == "ldap"
