# Path: amivapi/utils.py
# @contextmanager
# def admin_permissions():
#     """Switch to a context with admin rights and restore state afterwards.
# 
#     Use as context:
#     >> with admin_rights():
#     >>     do_something()
#     """
#     old_admin = g.get('resource_admin')
#     g.resource_admin = True
# 
#     yield
# 
#     if old_admin is not None:  # None means it wasn't set before..
#         g.resource_admin = old_admin


from eve.methods.patch import patch_internal
from eve.methods.post import post_internal
from flask import current_app
from nethz.ldap import AuthenticatedLdap
from amivapi.utils import admin_permissions
        u"male" if int(data['swissEduPersonGender']) == 1 else u"female"

    # See file docstring for explanation of `deparmentNumber` field
    # In some rare cases, the departmentNumber field is either empty
    # or missing -> normalize to empty string
    department_info = next(iter(
        data.get('description') or data.get('departmentNumber') or []
    ), '')
    department_map = current_app.config['LDAP_DEPARTMENT_MAP'].items()
    department = (dept for phrase, dept in department_map
                  if phrase in department_info)
    res['department'] = next(department, None)  # None if no match

    # Membership: One of our departments and VSETH member
    is_member = ((res['department'] is not None) and
                 ('VSETH Mitglied' in data['ou']))
    res['membership'] = u"regular" if is_member else u"none"

    # For members, send newsletter to True by default
    if is_member:
        res['send_newsletter'] = True

    return res


def _create_or_update_user(ldap_data):
    """Try to find user in database. Update if it exists, create otherwise."""
    query = {'nethz': ldap_data['nethz']}
    db_data = current_app.data.driver.db['users'].find_one(query)


# Path: amivapi/utils.py
# @contextmanager
# def admin_permissions():
#     """Switch to a context with admin rights and restore state afterwards.
# 
#     Use as context:
#     >> with admin_rights():
#     >>     do_something()
#     """
#     old_admin = g.get('resource_admin')
#     g.resource_admin = True
# 
#     yield
# 
#     if old_admin is not None:  # None means it wasn't set before..
#         g.resource_admin = old_admin


# Path: amivapi/ldap.py
from eve.methods.patch import patch_internal
from eve.methods.post import post_internal
from flask import current_app
from nethz.ldap import AuthenticatedLdap
from amivapi.utils import admin_permissions

        u"male" if int(data['swissEduPersonGender']) == 1 else u"female"

    # See file docstring for explanation of `deparmentNumber` field
    # In some rare cases, the departmentNumber field is either empty
    # or missing -> normalize to empty string
    department_info = next(iter(
        data.get('description') or data.get('departmentNumber') or []
    ), '')
    department_map = current_app.config['LDAP_DEPARTMENT_MAP'].items()
    department = (dept for phrase, dept in department_map
                  if phrase in department_info)
    res['department'] = next(department, None)  # None if no match

    # Membership: One of our departments and VSETH member
    is_member = ((res['department'] is not None) and
                 ('VSETH Mitglied' in data['ou']))
    res['membership'] = u"regular" if is_member else u"none"

    # For members, send newsletter to True by default
    if is_member:
        res['send_newsletter'] = True

    return res


def _create_or_update_user(ldap_data):
    """Try to find user in database. Update if it exists, create otherwise."""
    query = {'nethz': ldap_data['nethz']}
    db_data = current_app.data.driver.db['users'].find_one(query)


    with admin_permissions():