# Path: amivapi/auth/auth.py
# class AmivTokenAuth(BasicAuth):
#     """Amiv authentication and authorization base class.
# 
#     Subclass and overwrite functions if you don't want default behaviour.
#     """
# 
#     def authorized(self, allowed_roles, resource, method):
#         """Authorize Request.
# 
#         This is the method Eve will call if the endpoint is not public.
# 
#         We use this by setting `g.auth_required` to inform auth hook to abort
#         later if user can't be identified.
# 
#         Do NOT overwrite this when subclassing `AmivTokenAuth`.
#         """
#         g.auth_required = True
#         return True
# 
#     def has_resource_write_permission(self, user_id):
#         """Check if the user is alllowed to write to the resource.
# 
#         Implement this function for your resource.
#         Default behaviour: No user has write permission.
# 
#         Args:
#             user_id (str): The if of the user
# 
#         Returns:
#             bool: True if user has permission to write, False otherwise.
#         """
#         return False
# 
#     def has_item_write_permission(self, user_id, item):
#         """Check if the user is allowed to modify the item.
# 
#         Implement this function for your resource.
#         Default behaviour: No user has write permission.
# 
#         Args:
#             user (str): The id of the user that wants to access the item
#             item (dict): The item the user wants to change or delete.
#                 Attention! If they are any ObjectIds in here, Eve will not have
#                 converted them yet, so be sure to cast them to str if you want
#                 to compare them to e.g. g.current_user
# 
#         Returns:
#             bool: True if user has permission to change the item, False if not.
#         """
#         return False
# 
#     def create_user_lookup_filter(self, user_id):
#         """Create a filter for item lookup in GET, PATCH and DELETE.
# 
#         Implement this function for your resource.
#         Default behaviour: No lookup filter.
# 
#         Args:
#             user_id (str): The id of the user
# 
#         Returns:
#             dict: The filter, will be combined with other filters in the hook.
#                 Return empty dict if no filters should be applied.
#                 Return None if no lookup should be possible for the user.
#         """
#         return {}
# 
# Path: amivapi/utils.py
# def on_post_hook(func):
#     """Wrapper for an Eve `on_post_METHOD_resource` hook.
# 
#     The hook receives only a flask response object, which is difficult to
#     manipulate.
#     This wrapper extracts the data as dict and set the data again after the
#     wrapped function has manipulated it.
#     The function is only called for successful requests, otherwise there
#     is no payload.
# 
#     If we are in passthrough mode, e.g. for sending files, modifying the
#     payload is not possible and the function is not called.
# 
#     The wrapped function can look like this:
# 
#         my_hook(payload):
#             ...
# 
#     or, for hooks that don't specify the resource:
# 
#         my_hook(payload):
#             ...
#     """
#     @wraps(func)
#     def wrapped(*args):
#         """This is the hook eve will see."""
#         response = args[-1]
#         if (response.status_code in range(200, 300) and
#                 not response.direct_passthrough):
#             payload = json.loads(response.get_data(as_text=True))
#             func(*args, payload)
#             response.set_data(json.dumps(payload))
#     return wrapped


from bson import ObjectId
from flask import current_app, g
from amivapi.auth import AmivTokenAuth
from amivapi.utils import on_post_hook
    def create_user_lookup_filter(self, user_id):
        """Create a filter for item lookup.

        Not a member: Can see only himself
        A Member: Can see everyone

        Note: Users will only see complete info for themselves.
        But excluding other fields will be done in a hook later.

        Args:
            user_id (str): Id of the user. No public methods -> wont be None

        Returns:
            dict: The filter, will be combined with other filters in the hook.
                Return None if no filters should be applied.
        """
        # Find out if not member
        collection = current_app.data.driver.db['users']
        # set projection to only return membership
        result = collection.find_one({'_id': ObjectId(user_id)},
                                     {'membership': 1})

        if result['membership'] == "none":
            # Can't see others
            return {'_id': user_id}
        else:
            # Can see everyone (fields will be filtered later)
            return {}



# Path: amivapi/auth/auth.py
# class AmivTokenAuth(BasicAuth):
#     """Amiv authentication and authorization base class.
# 
#     Subclass and overwrite functions if you don't want default behaviour.
#     """
# 
#     def authorized(self, allowed_roles, resource, method):
#         """Authorize Request.
# 
#         This is the method Eve will call if the endpoint is not public.
# 
#         We use this by setting `g.auth_required` to inform auth hook to abort
#         later if user can't be identified.
# 
#         Do NOT overwrite this when subclassing `AmivTokenAuth`.
#         """
#         g.auth_required = True
#         return True
# 
#     def has_resource_write_permission(self, user_id):
#         """Check if the user is alllowed to write to the resource.
# 
#         Implement this function for your resource.
#         Default behaviour: No user has write permission.
# 
#         Args:
#             user_id (str): The if of the user
# 
#         Returns:
#             bool: True if user has permission to write, False otherwise.
#         """
#         return False
# 
#     def has_item_write_permission(self, user_id, item):
#         """Check if the user is allowed to modify the item.
# 
#         Implement this function for your resource.
#         Default behaviour: No user has write permission.
# 
#         Args:
#             user (str): The id of the user that wants to access the item
#             item (dict): The item the user wants to change or delete.
#                 Attention! If they are any ObjectIds in here, Eve will not have
#                 converted them yet, so be sure to cast them to str if you want
#                 to compare them to e.g. g.current_user
# 
#         Returns:
#             bool: True if user has permission to change the item, False if not.
#         """
#         return False
# 
#     def create_user_lookup_filter(self, user_id):
#         """Create a filter for item lookup in GET, PATCH and DELETE.
# 
#         Implement this function for your resource.
#         Default behaviour: No lookup filter.
# 
#         Args:
#             user_id (str): The id of the user
# 
#         Returns:
#             dict: The filter, will be combined with other filters in the hook.
#                 Return empty dict if no filters should be applied.
#                 Return None if no lookup should be possible for the user.
#         """
#         return {}
# 
# Path: amivapi/utils.py
# def on_post_hook(func):
#     """Wrapper for an Eve `on_post_METHOD_resource` hook.
# 
#     The hook receives only a flask response object, which is difficult to
#     manipulate.
#     This wrapper extracts the data as dict and set the data again after the
#     wrapped function has manipulated it.
#     The function is only called for successful requests, otherwise there
#     is no payload.
# 
#     If we are in passthrough mode, e.g. for sending files, modifying the
#     payload is not possible and the function is not called.
# 
#     The wrapped function can look like this:
# 
#         my_hook(payload):
#             ...
# 
#     or, for hooks that don't specify the resource:
# 
#         my_hook(payload):
#             ...
#     """
#     @wraps(func)
#     def wrapped(*args):
#         """This is the hook eve will see."""
#         response = args[-1]
#         if (response.status_code in range(200, 300) and
#                 not response.direct_passthrough):
#             payload = json.loads(response.get_data(as_text=True))
#             func(*args, payload)
#             response.set_data(json.dumps(payload))
#     return wrapped


# Path: amivapi/users/security.py
from bson import ObjectId
from flask import current_app, g
from amivapi.auth import AmivTokenAuth
from amivapi.utils import on_post_hook

    def create_user_lookup_filter(self, user_id):
        """Create a filter for item lookup.

        Not a member: Can see only himself
        A Member: Can see everyone

        Note: Users will only see complete info for themselves.
        But excluding other fields will be done in a hook later.

        Args:
            user_id (str): Id of the user. No public methods -> wont be None

        Returns:
            dict: The filter, will be combined with other filters in the hook.
                Return None if no filters should be applied.
        """
        # Find out if not member
        collection = current_app.data.driver.db['users']
        # set projection to only return membership
        result = collection.find_one({'_id': ObjectId(user_id)},
                                     {'membership': 1})

        if result['membership'] == "none":
            # Can't see others
            return {'_id': user_id}
        else:
            # Can see everyone (fields will be filtered later)
            return {}



@on_post_hook