def check_resource_project(context, resource, project_safe):
    """Check if the resource's project matches the context's project

    For non-admin users, if project_safe is set and the resource's project
    does not match the context's project, none is returned.
    Otherwise return the resource unmodified.

    :param context: context of the call
    :param resource: resource to check
    :param project_safe: boolean indicating if project restriction should be
                         checked.
    """

    if resource is None:
        return resource

    if project_safe and not context.is_admin:
        if context.project_id != resource.project:
            return None

    return resource