'Configure User Fields - for registration & user administration
pe_ids: an optional list of pe_ids for the Org Filter
i.e. org_admin coming from admin.py/user()'

def configure_user_fields(self, pe_ids=None):

    from s3validators import IS_ONE_OF
    T = current.T
    db = current.db
    s3db = current.s3db
    request = current.request
    messages = self.messages
    cmessages = current.messages
    settings = self.settings
    deployment_settings = current.deployment_settings
    if deployment_settings.get_ui_multiselect_widget():
        from s3widgets import S3MultiSelectWidget
        multiselect_widget = True
    else:
        multiselect_widget = False
    utable = self.settings.table_user
    utable.password.label = T('Password')
    first_name = utable.first_name
    first_name.label = T('First    Name')
    first_name.requires = (IS_NOT_EMPTY(error_message=messages.is_empty),)
    last_name = utable.last_name
    last_name.label = T('Last    Name')
    if deployment_settings.get_L10n_mandatory_lastname():
        last_name.notnull = True
        last_name.requires = IS_NOT_EMPTY(error_message=messages.is_empty)
    userfield = settings.login_userfield
    if (userfield != 'email'):
        utable[userfield].requires = IS_NOT_IN_DB(db, ('%s.%s' % (utable._tablename, userfield)))
    email = utable.email
    email.label = T('Email')
    email.requires = [IS_EMAIL(error_message=messages.invalid_email), IS_LOWER(), IS_NOT_IN_DB(db, ('%s.email' % utable._tablename), error_message=messages.duplicate_email)]
    language = utable.language
    language.label = T('Language')
    language.comment = DIV(_class='tooltip', _title=('%s|%s' % (T('Language'), T('The    language    you    wish    the    site    to    be    displayed    in.'))))
    languages = current.deployment_settings.get_L10n_languages()
    language.represent = (lambda opt: languages.get(opt, cmessages.UNKNOWN_OPT))
    language.default = T.accepted_language
    if multiselect_widget:
        language.widget = S3MultiSelectWidget(multiple=False)
    utc_offset = utable.utc_offset
    utc_offset.label = messages.label_utc_offset
    utc_offset.comment = DIV(_class='tooltip', _title=('%s|%s' % (messages.label_utc_offset, messages.help_utc_offset)))
    try:
        from s3validators import IS_UTC_OFFSET
        utc_offset.requires = IS_EMPTY_OR(IS_UTC_OFFSET())
    except:
        pass
    utable.registration_key.label = messages.label_registration_key
    if self.s3_has_role('ADMIN'):
        show_org = deployment_settings.get_auth_admin_sees_organisation()
    else:
        show_org = deployment_settings.get_auth_registration_requests_organisation()
    if show_org:
        if pe_ids:
            filterby = 'pe_id'
            filter_opts = pe_ids
        else:
            filterby = None
            filter_opts = None
        organisation_id = utable.organisation_id
        organisation_id.label = messages.label_organisation_id
        organisation_id.readable = organisation_id.writable = True
        organisation_id.default = deployment_settings.get_auth_registration_organisation_id_default()
        org_represent = s3db.org_organisation_represent
        organisation_id.represent = org_represent
        requires = IS_ONE_OF(db, 'org_organisation.id', org_represent, filterby=filterby, filter_opts=filter_opts, orderby='org_organisation.name', sort=True)
        if deployment_settings.get_auth_registration_organisation_required():
            organisation_id.requires = requires
        else:
            organisation_id.requires = IS_EMPTY_OR(requires)
        from s3layouts import S3PopupLink
        org_crud_strings = s3db.crud_strings['org_organisation']
        organisation_id.comment = S3PopupLink(c='org', f='organisation', label=org_crud_strings.label_create, title=org_crud_strings.title_list)
        if multiselect_widget:
            organisation_id.widget = S3MultiSelectWidget(multiple=False)
    if deployment_settings.get_auth_registration_requests_organisation_group():
        org_group_id = utable.org_group_id
        org_group_id.label = messages.label_org_group_id
        org_group_id.readable = org_group_id.writable = True
        org_group_represent = s3db.org_group_represent
        org_group_id.represent = org_group_represent
        requires = IS_ONE_OF(db, 'org_group.id', org_group_represent, orderby='org_group.name', sort=True)
        if deployment_settings.get_auth_registration_organisation_group_required():
            org_group_id.requires = requires
        else:
            org_group_id.requires = IS_EMPTY_OR(requires)
        if multiselect_widget:
            org_group_id.widget = S3MultiSelectWidget(multiple=False)
    if deployment_settings.get_auth_registration_requests_site():
        site_id = request.get_vars.get('site_id', None)
        field = utable.site_id
        field.label = deployment_settings.get_org_site_label()
        site_represent = s3db.org_site_represent
        field.represent = site_represent
        if site_id:
            field.default = site_id
            field.readable = True
        else:
            field.readable = field.writable = True
            site_required = deployment_settings.get_auth_registration_site_required()
            if show_org:
                from s3validators import IS_ONE_OF_EMPTY
                requires = IS_ONE_OF_EMPTY(db, 'org_site.site_id', site_represent, orderby='org_site.name', sort=True)
                if site_required:
                    site_optional = ''
                else:
                    site_optional = ",\n    'optional':    true"
                current.response.s3.jquery_ready.append(("\n$.filterOptionsS3({\n    'trigger':'organisation_id',\n    'target':'site_id',\n    'lookupField':'site_id',\n    'lookupResource':'site',\n    'lookupURL':S3.Ap.concat('/org/sites_for_org.json/')%s\n})" % site_optional))
            else:
                requires = IS_ONE_OF(db, 'org_site.site_id', site_represent, orderby='org_site.name', sort=True)
            field.comment = DIV(_class='tooltip', _title=('%s|%s' % (T('Facility'), T('Select    the    default    site.'))))
            if site_required:
                field.requires = requires
            else:
                field.requires = IS_EMPTY_OR(requires)
    if ('profile' in request.args):
        return
    link_user_to_opts = deployment_settings.get_auth_registration_link_user_to()
    if link_user_to_opts:
        link_user_to = utable.link_user_to
        link_user_to_default = deployment_settings.get_auth_registration_link_user_to_default()
        req_vars = request.vars
        for type in ['staff', 'volunteer', 'member']:
            if (('link_user_to_%s' % type) in req_vars):
                link_user_to_default.append(type)
        if link_user_to_default:
            link_user_to.default = link_user_to_default
        else:
            link_user_to.readable = link_user_to.writable = True
            link_user_to.label = T('Register    As')
            link_user_to.requires = IS_IN_SET(link_user_to_opts, multiple=True)
            link_user_to.represent = (lambda ids: ((ids and ',    '.join([str(link_user_to_opts[id]) for id in ids])) or cmessages['NONE']))
            link_user_to.widget = SQLFORM.widgets.checkboxes.widget
            link_user_to.comment = DIV(_class='tooltip', _title=('%s|%s' % (link_user_to.label, T('Will    create    and    link    your    user    account    to    the    following    records'))))
