# Path: core/config.py

# 
# Path: core/validation/validation.py
# def validate_json_list(collection, list_in):
#     
#     for json_in in list_in:    
#         # Validate json schema
#         validator(schema[collection], format_checker=jsonschema.FormatChecker()).validate(json_in)
# 
# def sanitize_objectify_json(json_in):
#     """Sanitize string and convert _ids to ObjectId in JSON"""
#     return recursive_replace(json_in, _replace_function_sanitize_objectify_json)
# 
# def update_password_salt_user_json(json_in):
#     json_in.update(get_password_salt(json_in['password']))
# 
# Path: core/api/crud.py
# def get(collection, criteria_projection_order):
# def remove(collection, criterias = []):
# def add(collection, documents_list):
# def update(collection, document):


import ldap, sys, logging
from core.config import conf_auth_ldap
from core.validation.validation import validate_json_list, sanitize_objectify_json, update_password_salt_user_json
from core.api.crud import db
def _user_exists_in_db(username):
    """Check if username already exists in db"""
    cursor = db['user'].find({"username": username}).limit(1)
    return cursor.count() > 0

def _migrate_user_to_db(ldap_result_dict, password, group):
    """Migrate LDAP user informations to database. Password is not inserted"""
    
    user_dict = {
                 'name' : ldap_result_dict.get('givenName', [''])[0], 
                 'surname' : ldap_result_dict.get('sn', [''])[0],
                 'username' : ldap_result_dict.get('uid', [''])[0],
                 'email' : ldap_result_dict.get('mail',[''])[0],
                 'phone' : ldap_result_dict.get('phone',[''])[0],
                 'mobile' : ldap_result_dict.get('mobile',[''])[0],
                 'city' : ldap_result_dict.get('city',[''])[0],
                 'group' : group,
                 'password' : '',
                 'salary' : [],
                 'salt' : '',
                 'status': 'active'
                 }
    
    # Copy user_dict with a fake password to permit correct validation
    user_dict_copy = user_dict.copy()
    user_dict_copy['password'] = 'fakepwd'
    validate_json_list('user', [ user_dict_copy ])
    
    sanified_user_dict = sanitize_objectify_json(user_dict)
    

# Path: core/config.py

# 
# Path: core/validation/validation.py
# def validate_json_list(collection, list_in):
#     
#     for json_in in list_in:    
#         # Validate json schema
#         validator(schema[collection], format_checker=jsonschema.FormatChecker()).validate(json_in)
# 
# def sanitize_objectify_json(json_in):
#     """Sanitize string and convert _ids to ObjectId in JSON"""
#     return recursive_replace(json_in, _replace_function_sanitize_objectify_json)
# 
# def update_password_salt_user_json(json_in):
#     json_in.update(get_password_salt(json_in['password']))
# 
# Path: core/api/crud.py
# def get(collection, criteria_projection_order):
# def remove(collection, criterias = []):
# def add(collection, documents_list):
# def update(collection, document):


# Path: core/auth/auth_ldap.py
import ldap, sys, logging
from core.config import conf_auth_ldap
from core.validation.validation import validate_json_list, sanitize_objectify_json, update_password_salt_user_json
from core.api.crud import db

def _user_exists_in_db(username):
    """Check if username already exists in db"""
    cursor = db['user'].find({"username": username}).limit(1)
    return cursor.count() > 0

def _migrate_user_to_db(ldap_result_dict, password, group):
    """Migrate LDAP user informations to database. Password is not inserted"""
    
    user_dict = {
                 'name' : ldap_result_dict.get('givenName', [''])[0], 
                 'surname' : ldap_result_dict.get('sn', [''])[0],
                 'username' : ldap_result_dict.get('uid', [''])[0],
                 'email' : ldap_result_dict.get('mail',[''])[0],
                 'phone' : ldap_result_dict.get('phone',[''])[0],
                 'mobile' : ldap_result_dict.get('mobile',[''])[0],
                 'city' : ldap_result_dict.get('city',[''])[0],
                 'group' : group,
                 'password' : '',
                 'salary' : [],
                 'salt' : '',
                 'status': 'active'
                 }
    
    # Copy user_dict with a fake password to permit correct validation
    user_dict_copy = user_dict.copy()
    user_dict_copy['password'] = 'fakepwd'
    validate_json_list('user', [ user_dict_copy ])
    
    sanified_user_dict = sanitize_objectify_json(user_dict)
    

    return db['user'].insert(user_dict)