Exemplo n.º 1
0
def rest_settings_authentication(request):
    # Get the settings
    if request.method == 'GET':
        # load the settings file
        config = ConfigParser.ConfigParser()
        config.read(settings.SETTINGS_PATH)

        # retrieve the settings
        ldap_helper = LdapHelper()

        # build json reply
        # if the ldap password has a value
        if ldap_helper.bind_password != '':
            # write "saved" instead of the password
            displayed_bind_password = "******"

        json_reply = '{"authMethod":"' + ldap_helper.auth_method + '","ldap":{"protocol": "' + ldap_helper.protocol + '","host": "' + ldap_helper.host + '","port": "' + ldap_helper.port + '","baseDn": "' + ldap_helper.base_dn + '","attribute": "' + ldap_helper.attribute + '","scope": "' + ldap_helper.scope + '","filter": "' + ldap_helper.filter + '","bindDn": "' + ldap_helper.bind_dn + '","bindPassword": "******"}}'
        # json_reply = '{"authMethod":"' + auth_method + '","ldap":{"host": "' + ldap_host +'","baseDn": "' + ldap_base_dn +'","bindDn": "' + ldap_bind_dn +'","bindPassword": "******"}}'
        # json_reply = '{"authMethod":"ldap","ldap":{"url": "ldap://10.0.1.24:389/","baseDn": "CN=Users,DC=contoso,DC=com","bindDn": "CN=john,CN=Users,DC=contoso,DC=com","bindPassword": "******"}}'
        return HttpResponse(json_reply)
    # Set the settings
    if request.method == 'PUT':
        auth_settings = json.loads(request.raw_post_data)

        # load the config file
        config = ConfigParser.ConfigParser()
        config.read(settings.SETTINGS_PATH)

        # save the settings
        # retrieve the settings
        ldap_helper = LdapHelper()
        ldap_helper.auth_method = auth_settings['authMethod']
        ldap_helper.protocol = auth_settings['ldap']['protocol']
        ldap_helper.host = auth_settings['ldap']['host']
        ldap_helper.port = auth_settings['ldap']['port']
        ldap_helper.base_dn = auth_settings['ldap']['baseDn']
        ldap_helper.attribute = auth_settings['ldap']['attribute']
        ldap_helper.scope = auth_settings['ldap']['scope']
        ldap_helper.filter = auth_settings['ldap']['filter']
        ldap_helper.bind_dn = auth_settings['ldap']['bindDn']
        if auth_settings['ldap']['bindPassword'] != "saved":
            ldap_helper.bind_password = auth_settings['ldap']['bindPassword']

        ldap_helper.save()

        # Load and save all the repositories to update the ldap config
        repositories = Repository.retrieve_all()
        for repository in repositories:
            repository.load()
            repository.save()

        return HttpResponse("Settings successfully saved.")
Exemplo n.º 2
0
def rest_settings_authentication(request):
    # Get the settings
    if request.method == 'GET':
        # load the settings file
        config = ConfigParser.ConfigParser()
        config.read(settings.SETTINGS_PATH)
        
        # retrieve the settings
        ldap_helper = LdapHelper()     
        
        # build json reply
        json_reply = '{"authMethod":"' + ldap_helper.auth_method + '","ldap":{"protocol": "' + ldap_helper.protocol +'","host": "' + ldap_helper.host +'","port": "' + ldap_helper.port +'","baseDn": "' + ldap_helper.base_dn +'","attribute": "' + ldap_helper.attribute +'","scope": "' + ldap_helper.scope +'","filter": "' + ldap_helper.filter +'","bindDn": "' + ldap_helper.bind_dn +'","bindPassword": "******"}}'
        # json_reply = '{"authMethod":"' + auth_method + '","ldap":{"host": "' + ldap_host +'","baseDn": "' + ldap_base_dn +'","bindDn": "' + ldap_bind_dn +'","bindPassword": "******"}}'
        # json_reply = '{"authMethod":"ldap","ldap":{"url": "ldap://10.0.1.24:389/","baseDn": "CN=Users,DC=contoso,DC=com","bindDn": "CN=john,CN=Users,DC=contoso,DC=com","bindPassword": "******"}}'
        return HttpResponse(json_reply)
    # Set the settings
    if request.method == 'PUT':
        auth_settings = json.loads(request.raw_post_data)
        
        # load the config file
        config = ConfigParser.ConfigParser()
        config.read(settings.SETTINGS_PATH)
        
        # save the settings
        # retrieve the settings
        ldap_helper = LdapHelper()  
        ldap_helper.auth_method = auth_settings['authMethod']
        ldap_helper.protocol = auth_settings['ldap']['protocol']
        ldap_helper.host = auth_settings['ldap']['host']
        ldap_helper.port = auth_settings['ldap']['port']
        ldap_helper.base_dn = auth_settings['ldap']['baseDn']
        ldap_helper.attribute = auth_settings['ldap']['attribute']
        ldap_helper.scope = auth_settings['ldap']['scope']
        ldap_helper.filter = auth_settings['ldap']['filter']
        ldap_helper.bind_dn = auth_settings['ldap']['bindDn']
        ldap_helper.bind_password = auth_settings['ldap']['bindPassword']
        
        ldap_helper.save()
        
        
        return HttpResponse("Settings successfully saved.")