简体   繁体   中英

web2py application configuration file

Is there a good way to store configuration settings for a web2py app?

I have written a little app that includes a script which looks up user attributes (names, email addresses, etc) from LDAP. Our corporate LDAP server requires a bind, before it allows a search for user details.

I want to share my app from a GitHub repository but not before I remove the credentials used for the bind.

Coming from a .Net background, I'm used to putting configuration like this into an app|web.config file. But this seems to be frowned on in web2py.

Whats a Pythonic or web2pyonic way of doing this?

def user_info(username):
    #todo: move these parameters to config
    ldap_host = 'example.com'
    ldap_port = 389
    ldap_base_dn = 'OU=DK,DC=example,DC=com'
    ldap_bind_dn = 'CN=<removed>,OU=DK,DC=example,DC=com'
    ldap_bind_pw = '<removed>'
    ldap_attr_uid = 'sAMAccountName'
    ldap_attr_forename = 'givenName'
    ldap_attr_surname = 'sn'
    ldap_attr_display_name = 'displayName'
    ldap_attr_department = 'department'
    ldap_attr_employee_type = 'employeeType'
    ldap_attr_email = 'mail'

    l = ldap.initialize('ldap://%s:%s' % (ldap_host, ldap_port))
    l.simple_bind_s(ldap_bind_dn, ldap_bind_pw)
    r = l.search_s(base=ldap_base_dn,
                   scope=ldap.SCOPE_SUBTREE,
                   filterstr='(%s=%s)' % (ldap_attr_uid, username),
                   attrlist=[
                       ldap_attr_forename,
                       ldap_attr_surname,
                       ldap_attr_email,
                       ldap_attr_display_name,
                       ldap_attr_employee_type,
                       ldap_attr_department])
    if r:
        dn, e = r[0]
        return {
            'dn': dn,
            'forename': e[ldap_attr_forename][0],
            'surname': e[ldap_attr_surname][0],
            'email': e[ldap_attr_email][0],
            'display_name': e[ldap_attr_display_name][0],
            'department': e[ldap_attr_department][0],
            'employee_type': e[ldap_attr_employee_type][0]}
    return None

There are different options, but one approach is just to put the settings in a module and import. In /yourapp/modules/ldap_settings.py:

ldap_host = 'example.com'
ldap_port = 389
...

And then in your function:

def user_info(username):
    from ldap_settings import *
    ...

I believe the proper web2py method is to use AppConfig module. Take a look at it in the web2py reference manual here: http://web2py.com/books/default/chapter/29/13/deployment-recipes#AppConfig-module

As Keht mentioned AppConfig is the proper way to do it. The ini file is present in private folder. You can see that default email smtp settings are already there that are used by web2py for sending user verification emails/forgot password emails.

You can a entry here for your required key-value and then create the AppConfig object and read the required key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM